OOP based programming using constructor, destructor, abstraction and encapsulation














































OOP based programming using constructor, destructor, abstraction and encapsulation



This article is based on C++11

C++ Classes/Objects C++ is an object-oriented programming language.

A class is a user-defined data type that we can use in our program, and it works as an object

constructor, or a "blueprint" for creating objects.

Class Methods

Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

  • Inside class definition
  • Outside class definition

In the following example, we define a function inside the class, and we name it "myMethod".

Note: You access methods just like you access attributes; by creating an object of the class and by using the dot syntax (.):

Constructors

A constructor in C++ is a special method that is automatically called when an object of a class is created.

To create a constructor, use the same name as the class, followed by parentheses ():

A default constructor, also called no-argument constructor, can initialize data members to constant values. 

class_name()
{
     //body
}

A parameterized constructor can initialize data members to values passed as arguments.

Syntax:

class_name(params)
{
     //body
}
This constructor is a one argument constructor whose argument is an object of the same class as the constructor.

Constructor Overloading

Constructors can have same name differing in number and types of arugments being passed. All other function overloading rule applies to constructor overloading.

In program below, constructor Box() has two different versions.

 

Destructors

The most common use of destructors is to deallocate memory that was allocated for the object by the constructor.

  • Destructors are called automatically when an object is destroyed.
  • It has the same name as the class in which it resides but is preceded by a tilde and is syntactically similar to a function.

  • Data Abstraction is a programming technique that depends on the seperation of the interface and implementation details of the program.
  • Data Abstraction can be achieved in two ways:

    • Abstraction using classes
    • Abstraction in header files.
    Data Abstraction in C++
    Here is example of program that uses class/objects, all 3 types of constructors and destructor, abstraction and encapsulation mentioned above.

  • #include<iostream>
    using namespace std;
    class Box
    {
    private:
        double width;
        double height;
        double depth;
        float volume;
    public:
        Box()
        {
            width = 10;
            height = 10;
            depth = 10;
        }
        Box(double w,double h, double d)
        {
            width = w;
            height = h;
            depth = d;
        }
        void calculateVolume()
        {
            volume = width * height * depth;
        }
    
        void displayVolume()
        {
            cout<<"The volume of object is : " << volume << " m^3" <<endl;
        }
        ~Box(){
            cout<<"Destructor called."<<endl;
        }
    };
    
    int main()
    {
        Box cube;                  
        cube.calculateVolume();
        cube.displayVolume();
    
        Box box(2,3,5);           
        box.calculateVolume();
        box.displayVolume();
    
        Box same_box(box);         
        box.calculateVolume();
        box.displayVolume();
        return 0;
    }


Comments