C++ Classes and Objects

C++ programming language was earlier known as C with classes. C++ is an extension of C with support for object oriented programming paradigm. Classes and objects forms the backbone of object oriented programming. The class is one of the defining ideas of object-oriented programming.

  • In OOP, we try to model real world entities as classes which contains some specific data and functions.
  • Classes provides ability to define your own data types that better correspond to the problem being solved.
  • Classes are extension of structures, like structure a class contains data members but can contains member functions.

C++ Classes

A class specifies the format of an object by encapsulating the data and functions for manipulating this data in a single entity. The data and functions within a class are called members of the class. A class is like a blueprint from which we can creates instances called objects.

A class is a user defined data type, which specifies the content of the objects and what operations can be performed on such an object.

How to define a class in C++

In C++, a class is defined using keyword class followed by the name of name of class and body of the class. The body of the class defines the data members and member functions. The body of the class is enclosed by a pair of curly braces and terminated by a semicolon at the end.

class class_Name {
   // data members
   // member functions
};
For Example :
class Rectangle {
    private:
        int length;
    public: 
        int width;   
     
 void setData(int L, int W) {
    length = L;
    width = W;
 }
  
        int getArea() {
    return length * width;  
 } 

        int getPerimeter() {
    return 2*(length + width);  
 }
};

Above class Rectangle has two data members, length and width and three member functions setData, getArea and getPerimeter. We will discuss about the keywords private and public later.


C++ Objects

A class is a user defined data type and on object is an instance of a class. We can think of object as a variable of class. Class definition does not allocate any memory, when we create objects of a class then actually memory gets allocated.

Syntax to Define Objects in C++ The syntax to define object in C++ is same as the syntax of declaring any basic data type of declaring a structure variable.
Class_Name Object_Name;
For Example :
Rectangle rectangel_one;
Object rectagle_one will have its copy of data member and member function as defined in class definition.

Accessing the Data and Member Function of Objects in C++

The public data members and member functions can be accessed by inserting a dot (.) operator between object name and member name. The syntax is same as accessing the members of a structure variable.

For Example :
rectangle_one.getArea();
rectangle_one.width();
We can only access public members of class from outside of class, private members can only be accessed from with class.

C++ Classes and Object Example Program
#include <iostream>
using namespace std;

class Rectangle {
   private:
     int length;
   public: 
     int width;   
     
     void setData(int L, int W) {
         length = L;
      width = W;
  }
  
     int getArea() {
      return length * width;  
  } 

     int getPerimeter() {
         return 2*(length + width);  
  }
};

int main() {
 int L, W;
 Rectangle rectange_one;
 
 cout << "Enter length and width of rectangle\n";
 cin >> L >> W;
 
 // calling member functions of class 
 rectange_one.setData(L, W);
 cout << "Area of Rectangle : " << rectange_one.getArea();
 cout << "\nPerimiter of Rectangle : " << rectange_one.getPerimeter(); 
 
 // Accessing public data member of class
 cout << "\nWidth : " << rectange_one.width;
 // We cannot access private member "length" from here
    return 0;
}
Output
Enter length and width of rectangle
4 6
Area of Rectangle : 24
Perimiter of Rectangle : 20
Width : 6