Objects and Classes in C++ with Examples

Objects and Classes in C++ with Examples

Here, you will learn all about classes and objects with different examples.


What is class and object?

  • Class is a description of an object.
  • Object is an instance of a class.
  • Instance member variables (are attributes, data members, fields, properties).
  • Instance member functions (methods, procedures, actions, operations, services).

C++ fully supports object-oriented programming including the six pillars of object-oriented development. Classes are one of them. In OOP, classes are the user-defined data types having member functions and member variables.

The concepts of objects and classes are linked with each other and form the foundation of the object-oriented paradigm. An object is described as an instance of the class.

An object is a real-world component in object-oriented circumstances that may have a physical or conceptual presence.

Objects can be modeled according to the need of the application. An object can be a customer, a car, etc or an intangible conceptual existence like a project, a process, etc.


For example:- Each and every Car has its own color, registration number, wheels and model number. In class, these values can be different for different objects. We always create object of the class.


What is difference between Structure and Class in C++

The only difference between structure and class in C++ is that, the members of structures are by default public and the members of classes are by default private.

The instance created by using 'struct' keyword is called variable of that structure while the instance created of a class is called object of that class.


Syntax of Class declaration

The syntax of declaring Class in C++ is as follows:
class className
{

  // class body

};

Here, class is a keyword which is used in declaring any class in C++. All classes must be end with a semicolon (;) like structure. All data members and member functions related to the class are declared inside the body of the class so that we can achieve encapsulation.


For Example:
class Book
{
  int Bookid;
  char title[20];
  float price;
};

In the above example class Book has only 3 member functions. Any class may have data members or member functions or both.


Access specifiers in C++

There are three access specifiers in C++. They are:
  • private
  • public
  • protected

Private Access Specifier

The private access specifier is the default access specifier in C++ class. Member variables declared within private access specifier can only be accessed by its member function declared within the class. It cannot be accessed outside the class.

So, private access specifier is used for implementing encapsulation (data hiding). When we want to protect the important data from the user then we use private access specifier.


Public Access Specifier

Public access specifier allows the access of data members from inside the class and also from outside the class. Each and every data member and member function declared with public access specifier can be accessed from anywhere in the program.

Generally, member functions are declared with public access specifier so that the user can access it and make some operations on the objects.


Protected Access Specifier

Protected access specifier and function overriding are used in the case of inheritance when user wants to access data of different classes in the inheritance.


Syntax of Object declaration or How to declare Objects?

The syntax of declaring object is as follows:

className objectName;

Here, className is the name of the class whose object is to be created and objectName is the name of the object that we want to create.


For Example:- Here is an example of class and objects that are used in the program.

#include<iostream>
using namespace std;

class Complex
{
public:
   int real, imaginary;
   
   void setData(int num1, int num2)
   {
     real = num1;
     imaginary = num2;
   }
   
   void showData()
   {
     cout << "real no. = " << real << "\n" << "imaginary no. = " << imaginary;
   }
};
 
int main()
{
  Complex C1;
  C1.setData(3,5);
  C1.showData();
}
Output:
real no. = 3
imaginary no. = 5

Function Call by Passing Object and returning object

#include<iostream>
using namespace std;

class Complex
{
private:
  int real, imaginary;
  
public:
  void setData(int num1, int num2)
  {
    real = num1;
    imaginary = num2;
  }
  
  void showData()
  {
    cout << "real no. = " << real << "\n" << "imaginary no. = " << imaginary;
  }
  
  Complex add(Complex C)
  {
    Complex temp;
    
    temp.real = real+C.real;
    temp.imaginary = imaginary + C.imaginary;
    
    return temp;
  }
};

int main()
{
  Complex c1, c2, c3;
  
  c1.setData(3,5);
  c2.setData(5,6);
  
  c3 = c1.add(c2);
  c3.showData();
}
Output:
real no. = 8
imaginary no. = 11

Reference >> HeelGeeks, Guru99, W3Schools


<< Previous                                                                                      Next >>
Previous
Next Post »