Skip to main content

C++ (Object and Class)

The major purpose of C++ programming is to introduce the concept of object orientation to the C programming language.

Object Oriented Programming is a paradigm that provides many concepts such as inheritance, data binding, polymorphism etc.

The programming paradigm where everything is represented as an object is known as truly object-oriented programming language. Smalltalk is considered as the first truly object-oriented programming language.


OOPs (Object Oriented Programming System)

Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

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 behaviour. Here, state means data and behaviour 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 employee class using e1 as the reference variable.

  1. Employee e1;  //creating an object of Employee      

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

C++ Class

In C++, class 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 Employee   
  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 Employee {  
  4.    public:  
  5.       int id;//data member (also instance variable)      
  6.       string name;//data member(also instance variable)      
  7. };  
  8. int main() {  
  9.     Employee e1; //creating an object of Employee   
  10.     e1.id = 201;    
  11.     e1.name = "Anurag Rana";   
  12.     cout<<e1.id<<endl;  
  13.     cout<<e1.name<<endl;  
  14.     return 0;  
  15. }  

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 Employee {  
  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.     Employee e1; //creating an object of Employee   
  19.     Employee e2; //creating an object of Employee 
  20.     e11.insert(1101, "Anurag");    
  21.     e2.insert(1102, "Pankaj");    
  22.     e1.display();    
  23.     e2.display();  
  24.     return 0;  
  25. }  

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. }  





Anurag Rana

Comments

Popular posts from this blog

JAVA Scrollbar, MenuItem and Menu, PopupMenu

ava AWT Scrollbar The  object  of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a  GUI  component allows us to see invisible number of rows and columns. AWT Scrollbar class declaration public   class  Scrollbar  extends  Component  implements  Adjustable, Accessible   Java AWT Scrollbar Example import  java.awt.*;   class  ScrollbarExample{   ScrollbarExample(){               Frame f=  new  Frame( "Scrollbar Example" );               Scrollbar s= new  Scrollbar();               s.setBounds( 100 , 100 ,  50 , 100 );               f.add(s);   ...

Difference between net platform and dot net framework...

Difference between net platform and dot net framework... .net platform supports programming languages that are .net compatible. It is the platform using which we can build and develop the applications. .net framework is the engine inside the .net platform which actually compiles and produces the executable code. .net framework contains CLR(Common Language Runtime) and FCL(Framework Class Library) using which it produces the platform independent codes. What is the .NET Framework? The Microsoft .NET Framework is a platform for building, deploying, and running Web Services and applications. It provides a highly productive, standards-based, multi-language environment for integrating existing investments with next-generation applications and services as well as the agility to solve the challenges of deployment and operation of Internet-scale applications. The .NET Framework consists of three main parts: the common language runtime, a hierarchical set of unified class librari...

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...