In the previous part of this Object-Oriented Programming series, we discussed the basics of object-oriented programming concepts and their advantages and disadvantages. Similarly, we explored the concept of classes and objects, member functions, constructors and destructors. We also wrote some example codes in C++.

In this part of the series, we will delve deeper into object-oriented programming concepts.

Object as Function Argument and Return Type

Objects can also be passed as arguments to a function, which in turn can also return an object.

#include <iostream>
using namespace std;

class Complex{
    public:
    int r , i;
    
    void Input(){
        cout<<"Enter Real part ";
        cin>>r;
        cout<<"Enter imaginary part ";
        cin>>i;
    }
    void Display(){
        cout<<r<<"+i"<<i<<endl; 
    }
};
Complex Add(Complex a, Complex b){
    Complex t ;
    t.r = a.r+b.r;
    t.i = a.i+b.i;
    
    return t;
}

int main() {
    // Write C++ code here
    Complex c1,c2,c3;
    c1.Input();
    c2.Input();
    
    c3 = Add(c1,c2);
    c3.Display();

    return 0;
}
Passing an Object as an argument and return type
Enter Real part 10
Enter imaginary part 5
Enter Real part 20
Enter imaginary part 10
30+i15
Output of the above code

Array of Objects

An array of objects can be created in the same way as other variables. Private and protected members of a class can not be accessed through array members;

Syntax:

   class-name array-name[array-size];

We can access the members of an array using the following syntax:

   array-name[index].datamember;

   array-name[index].function();

Pointer of objects and Member Access

Like creating pointers of other data types, we can also create pointers for classes. The pointer will hold the address of an object of class. The general form of the class's pointer declaration is

   class-name *Pointer-name;

Members of the pointer can be accessed using the arrow operator (->)

   Pointer-name->member;

   (*Pointer-name).member;

Dynamic Memory Allocation for Objects and Objects Array

We can allocate dynamic memory for objects and object arrays like dynamic memory allocation for other data types.

Syntax:

           class-name *pointer-name;         //declaring a pointer

          pointer-name  = new class-name;    //for a single object

           pointer-name = new class-name[size];       //for an array of objects

For the de-allocation of memory

     delete[] pointer-name;                                  //De-allocating memory

Static data members and static function

Static Data Members

A Static data member is shared by or common to all the objects of that class. It is defined inside a class as a static.

Static Member Function

A member static function is a function which can only access static data members and can be invoked or called using the name or object of that class.

#include <iostream>
using namespace std;

class Counter{
    public:
    static int count;
    
    Counter(){
       count++;
    }
    static void Display(){
        cout<<"value of count is "<<count<<endl; 
    }
};
int Counter::count = 0;
int main() {
    // Write C++ code here
    Counter c1;
    Counter::Display();
    
    Counter c2;
    Counter::Display();
    
    Counter c3;
    Counter::Display();

    return 0;
}
Demonstration of a Static Member Function
value of count is 1
value of count is 2
value of count is 3
Output of the above code

Constant Member Functions and Constant Objects

Constant member functions are those functions which can only use the data member of a class but cannot modify it. The constant member function is declared using the const keyword.

Both class member functions declaration and definition have const keyword on them. Once the function is declared as const, it cannot modify data members. If they try to alter data compiler will generate an error.

Example code

#include <iostream>
#include <cmath>
using namespace std;

class Coordinate{
    int x, y;
    
    public:
    void Input(){
        cout<<"Enter the value of x and y coordinates ";
        cin>>x>>y;
    }
    
    void Display() const {
        float sum;
        cout<<"\nCoordinate X :"<<x;
        cout<<"\nCoordinate Y :"<<y;
        sum = sqrt(x*x+y*y);
        cout<<"\nMagnitude :"<<sum;
        
        //x++ ; Error : Cannot change the value of variable inside the constant member function
    }
};

int main() {
    // Write C++ code here
    Coordinate c1;
    c1.Input();
    c1.Display();

    return 0;
}
Demonstration of a Constant Member Function
Constant member function can only invoke other constant member function of class. Display cannot call input function in the above case.
Enter the value of x and y coordinates 5 9
Coordinate X :5
Coordinate Y :9
Magnitude :10.2956
Output of the above code

Constant Objects

Like constant member data and function, constant objects cannot be modified. Constant objects can only have constant functions as they are guaranteed not to modify the output.

Example Code

#include <iostream>
#include <cmath>
using namespace std;

class Coordinate{
    int x, y;
    
    public:
    Coordinate(int x , int y){
        this->x = x;
        this->y = y;
    }
    void Input(){
        cout<<"Enter the value of x and y coordinates ";
        cin>>x>>y;
    }
    
    void show_Coordinate() const{
        cout<<"\nCoordinate X :"<<x;
        cout<<"\nCoordinate Y :"<<y;
    }
    
    void Display() {
        cout<<"\nCoordinate X :"<<x;
        cout<<"\nCoordinate Y :"<<y;
    }
};

int main() {
    // Write C++ code here
    Coordinate c1(5,9);
    c1.Input();
    c1.Display();
    c1.show_Coordinate();
    
    const Coordinate c2(5,10);
    //c2.Input(); Invalid as input modifies the value of member data
    //c2.Display(); Invalid as constant object can only invoke the constant member function of class
    c2.show_Coordinate();

    return 0;
}
Demonstration of a Constant Object
ℹ️
In C++, the "this" pointer is a special pointer that refers to the current instance of a class within its member functions.

Mutable Keyword

There might be situations where constant objects must only change the particular data members. In such cases, the data member is declared as mutable keywords.

Example Code

#include <iostream>
#include <cmath>
using namespace std;

class Coordinate{
    int x, y;
    mutable int z;
    public:
    Coordinate(int x , int y,int z){
        this->x = x;
        this->y = y;
        this->z = z;
    }
    void Input(){
        cout<<"Enter the value of x, y and z coordinates ";
        cin>>x>>y;
    }
    
    void Change_Z(int z) const{
        this->z = z;
    }
    
    void show_Coordinate() const{
        cout<<"\nCoordinate X :"<<x;
        cout<<"\nCoordinate Y :"<<y;
        cout<<"\nCoordinate Z :"<<z;
    }
    
    void Display() {
        cout<<"\nCoordinate X :"<<x;
        cout<<"\nCoordinate Y :"<<y;
        cout<<"\nCoordinate Y :"<<y;
    }
};

int main() {
    // Write C++ code here
    
    const Coordinate c2(5,10,15);
    c2.Change_Z(20);
    c2.show_Coordinate();

    return 0;
}
Mutable
Coordinate X :5
Coordinate Y :10
Coordinate Z :20
Output of the above code
Thank you for reading this article. Please leave a comment below if you liked it.