Skip to main content

C++: Object, Class

C++ Object and Class

Since C++ is an object-oriented language, program is designed using objects and classes in C++.


C++ Object

In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality.

Object is a runtime entity, it is created at runtime.

Object is an instance of a class. All the members of the class can be accessed through object.

Let's see an example to create object of student class using s1 as the reference variable.

  1. Student s1;  //creating an object of Student      

In this example, Student is the type and s1 is the reference variable that refers to the instance of Student class.

C++ Class

In C++, object is a group of similar objects. It is a template from which objects are created. It can have fields, methods, constructors etc.

Let's see an example of C++ class that has three fields only.

  1. class Student    
  2.  {    
  3.      public:  
  4.      int id;  //field or data member     
  5.      float salary; //field or data member  
  6.      String name;//field or data member    
  7.  }    

C++ Object and Class Example

Let's see an example of class that has two fields: id and name. It creates instance of the class, initializes the object and prints the object value.

  1. #include <iostream>  
  2. using namespace std;  
  3. class Student {  
  4.    public:  
  5.       int id;//data member (also instance variable)      
  6.       string name;//data member(also instance variable)      
  7. };  
  8. int main() {  
  9.     Student s1; //creating an object of Student   
  10.     s1.id = 201;    
  11.     s1.name = "Sonoo Jaiswal";   
  12.     cout<<s1.id<<endl;  
  13.     cout<<s1.name<<endl;  
  14.     return 0;  
  15. }  

Output:

201
Sonoo Jaiswal

C++ Class Example: Initialize and Display data through method

Let's see another example of C++ class where we are initializing and displaying object through method.

  1. #include <iostream>  
  2. using namespace std;  
  3. class Student {  
  4.    public:  
  5.        int id;//data member (also instance variable)      
  6.        string name;//data member(also instance variable)      
  7.        void insert(int i, string n)    
  8.         {    
  9.             id = i;    
  10.             name = n;    
  11.         }    
  12.        void display()    
  13.         {    
  14.             cout<<id<<"  "<<name<<endl;    
  15.         }    
  16. };  
  17. int main(void) {  
  18.     Student s1; //creating an object of Student   
  19.     Student s2; //creating an object of Student  
  20.     s1.insert(201, "Sonoo");    
  21.     s2.insert(202, "Nakul");    
  22.     s1.display();    
  23.     s2.display();  
  24.     return 0;  
  25. }  

Output:

201  Sonoo
202  Nakul

C++ Class Example: Store and Display Employee Information

Let's see another example of C++ class where we are storing and displaying employee information using method.

  1. #include <iostream>  
  2. using namespace std;  
  3. class Employee {  
  4.    public:  
  5.        int id;//data member (also instance variable)      
  6.        string name;//data member(also instance variable)  
  7.        float salary;  
  8.        void insert(int i, string n, float s)    
  9.         {    
  10.             id = i;    
  11.             name = n;    
  12.             salary = s;  
  13.         }    
  14.        void display()    
  15.         {    
  16.             cout<<id<<"  "<<name<<"  "<<salary<<endl;    
  17.         }    
  18. };  
  19. int main(void) {  
  20.     Employee e1; //creating an object of Employee   
  21.     Employee e2; //creating an object of Employee  
  22.     e1.insert(201, "Sonoo",990000);    
  23.     e2.insert(202, "Nakul", 29000);    
  24.     e1.display();    
  25.     e2.display();    
  26.     return 0;  
  27. }  

Output:

201  Sonoo  990000
202  Nakul  29000



Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database

Normalization   is a process of organizing the data in database to avoid data redundancy, insertion anomaly, update anomaly & deletion anomaly.  Anomalies in DBMS There are three types of anomalies that occur when the database is not normalized. These are – Insertion, update and deletion anomaly. Let’s take an example to understand this. Example : Suppose a manufacturing company stores the employee details in a table named employee that has four attributes: emp_id for storing employee’s id, emp_name for storing employee’s name, emp_address for storing employee’s address and emp_dept for storing the department details in which the employee works. At some point of time the table looks like this: emp_id emp_name emp_address emp_dept 101 Nikhil Kangra D001 101 Nikhil Kangra D002 123 Ashish Shimla D890 166 Rahul Pathankot D900 166 Rahul Pathankot D004 The above table is not normalized.  Update anomaly : In the above table we have two rows for employee Nikhil as he belongs ...