× Back Class definition Object of a class Accessing class member Defining member function Static variables in class Arrays within a class Array of objects Friend function Friend class Constructor Constructor overloading Destructors Dynamic initialization of objects Pointer to object "this" pointer
Next Topic → ← Previous Topic

Classes and Objects

Class

Class declaration syntax

                       
class class_name
{
    private:
        data_type1 variable_name;
        data_type2 variable_name;
    public:
        data_type1 variable_name;
        data_type2 variable_name;
};
                       
                   

Example ↓

                                           
class item
{
    int num; // these will be private by default, as we have not specified.
    float cost;
    public:
        void getdata(int a, int b);
        void putdata(int);
};
                       
                   

Class v/s Structure

Creating object of a class

                   
name_of_class    object/instance of the class 
      ↓              ↓ 
    item        i1, i2, i3, i4;
                   
               

Accessing class member

Syntax ↓

                   
object.function_name(argument);
object.data_member_name = 10;
                   
               

Example ↓

                   
object        arguments
↑              ↑ 
i1.getData(100, 105.5);
    ↓ 
name of function
                   
               

Defining member function

Outside the class definition

  • Here we use scope resolution operator.

Syntax ↓

                                         
return_type class_name :: function_name (argument1, argument2, ...)
{
    // function body
}
                       
                   

Example ↓

                                              
void item :: getData(int a, float b)
{
    number = a;
    cost = b;
}

void item :: printData(void)
{
    cout << "number " << number << endl;
    cout << "cost " << number << endl;
}
                       
                   

Inside the class definition

                       
class item
{
    int number;
    float cost;
    
    public:
        void getData(int a, float b);
        void putData(void)
        {
            cout << number << endl;
            cout << cost << endl
        }
}
                       
                   
                    
#include <iostream>
using namespace std;
class item
{
    int number;
    float cost;

public:
    void getData(int, float);
    void showData(void) // definition inside the function
    {
        cout << number << endl;
        cout << cost << endl;
    }
};
void item ::getData(int a, float b) // definition outside the function
{
    number = a;
    cost = b;
}
int main()
{
    item x, y;
    x.getData(100, 105.15);
    x.showData();
    y.getData(180, 195.15);
    y.showData();
    return 0;
}
                    
                

Static variables in a class

                   
#include <iostream>
using namespace std;
class Prac
{
public:
    static int var;
};
int main()
{
    Prac obj1, obj2;
    obj1.var = 2;
    obj2.var = 3;
    cout << obj1.var << " " << obj2.var;
    return 0;
}
                   
               

Output ↓

                   
Undefined symbols for architecture x86_64:
"Prac::var", referenced from:
    _main in test-f1185c.o
                   
               
                   
#include <iostream>
using namespace std;
class Prac
{
public:
    static int var;
};
int Prac::var = 1;
int main()
{
    Prac obj1, obj2;
    // initially the value of "var" is 1
    // now we will try to increment it using one object
    obj1.var++;
    cout << obj1.var << " " << obj2.var << endl;
    // above we are trying to check the value of "var" from both the object
    // and it is giving 2 which means it is being shared.
    return 0;
}
                   
               

Arrays within a class

                   
#include <iostream>
using namespace std;
class student
{
    int roll_no;
    int marks[3];

public:
    void getdata();
    void tot_marks();
};
void student ::getdata()
{
    cout << "Enter roll no : ";
    cin >> roll_no;
    for (int i = 0; i < 3; i++)
    {
        cout << "Enter marks in subject " << i + 1 << ": ";
        cin >> marks[i];
    }
}
void student ::tot_marks()
{
    int total = 0;
    for (int i = 0; i < 3; i++)
    {
        total += marks[i];
    }
    cout << "Total Marks : " << total << endl;
}
int main()
{
    student stu;
    stu.getdata();
    stu.tot_marks();
    return 0;
}
                   
               

Array of objects

Program to demonstrate array of objects ↓

                                  
#include <iostream>
using namespace std;
class employee
{
    int id;
    int salary;

public:
    void getdata(void);
    void putdata(void);
};
int main()
{
    employee e[10]; // array of objects
    for (int i = 0; i < 10; i++)
    {
        e[i].getdata();
    }
    for (int i = 0; i < 10; i++)
    {
        e[i].putdata();
    }
    return 0;
}

void employee ::getdata()
{
    cout << "Enter id : ";
    cin >> id;
    cout << "Enter salary : ";
    cin >> salary;
}
void employee ::putdata()
{
    cout << "\Id is : " << id;
    cout << "\nSalary is : " << salary;
}
                   
               

Friend Function

Syntax:

                                         
class abc
{
    // private data 
    public:
        friend void xyz(abc);
};
                       
                   

Program to demonstrate friend function ↓

                       
#include <iostream>
using namespace std;
class sample
{
    int a, b;
    public:
        void setValue()
        {
            a = 20;
            b = 30;
        }
        friend float mean(sample);
};
float mean(sample s)
{
    return (float)((s.a + s.b)/2);
}
int main()
{
    sample x;
    x.setValue();
    cout << "mean = " << mean(x);
    return 0;
}
                       
                   

Friend Class

Syntax ↓

                   
friend class class_name; // declared inside the base class
                   
               

A simple program which use friend class concept ↓

                   
#include <iostream>
using namespace std;
class secret
{
private:
    int secretNumber;

public:
    int localNumber;
    secret()
    {
        secretNumber = 999;
        localNumber = 100;
    }
    friend class another_secret;
};
class another_secret
{
public:
    void showSecret(secret one)
    {
        cout << "The secretNumber is = " << one.secretNumber << endl;
        cout << "The localNumber is = " << one.localNumber << endl;
    }
};
int main()
{
    secret first;
    another_secret second;
    second.showSecret(first);
    return 0;
}
                   
               

Constructor

Example ↓

                              
class integer
{
    int m, n;
    public:
        integer(void); // default constructor declared
        ... 
        ... 
};
integer :: integer(void) // constructor definition
{
    cout << "This is default constructor"; 
    m = 0;
    n = 0;
}
                   
               

Parameterized constructor

                       
class interger
{
    int m, n;
    public:
        integer(int x, int y); // parameterized constructor declaration
};
integer :: integer(int x, int y)
{
    m = x;
    n = y;
}
                       
                   

Object declaration in parameterized constructor: we cannot do this → integer int1;

  • Two type of calls, explicit and implicit.
  • We must pass the intial values as arguments to the constructor function when an object is declared this can be done in two ways.
    1. By calling the constructor explicitly.
                                             
      integer int1 = integer(100, 10);
                                             
                                         
    2. By calling the constructor implicitly.
                                              
      integer int1(100, 10);
                                              
                                          

Inline parameterized constructor

  • An inline parameterized constructor is a constructor that is declared and defined within the class definition and takes one or more parameters.
  • The keyword "inline" is used to suggest the compiler to insert the code of the constructor inline at the point of call, rather than creating a separate function call. This can improve the performance of the program by reducing the overhead of function calls.
                       
class integer
{
    int m, n;
    public:
        inline integer(int x, int y) // inline keyword is not neccessary as all the constructor are by default inline.
        {
            m = x;
            n = y;
        }
}
                       
                   
                    
#include<iostream>
using namespace std;
class Rect
{
    int length, breadth;
    public:
        Rect(int x, int y)
        {
            length = x; breadth = y;
        }
        void putdata()
        {
            cout << "Length = " << length << "Breadth = " << breadth;
        }
}
int main()
{
    Rect r1(100, 10);
    r1.putdata();
    return 0;
}
                    
                

Constructor overloading

Program to demonstrate the use of constructor overloading

                   
#include <iostream>
using namespace std;
class complex
{
    float x, y;

public:
    complex() {} // constructor 1 (default)
    complex(float a) { x = y = a; } // constructor 2 (parameterized)
    complex(float real, float imag) // constructor 3 (parameterized)
    {
        x = real;
        y = imag;
    }
    friend complex sum(complex, complex);
    friend void show(complex);
};
complex sum(complex c1, complex c2)
{
    complex c3;
    c3.x = c1.x + c2.x;
    c3.y = c1.y + c2.y;
    return c3;
}
void show(complex c)
{
    cout << c.x << " + j " << c.y;
}
int main()
{
    complex A(2.7, 3.5);
    complex B(1.6);
    complex C;

    C = sum(A, B);
    cout << "A = ";
    show(A);
    cout << "\nB = ";
    show(B);
    cout << "\nC = ";
    show(C);

    // Another way to give initial values (second method)
    complex P, Q, R;
    P = complex(2.5, 3.9);
    Q = complex(1.6, 2.5);
    R = sum(P, Q);
    cout << "\nP = ";
    show(P);
    cout << "\nQ = ";
    show(Q);
    cout << "\nR = ";
    show(R);
    return 0;
}
                   
               

Output ↓

                           
A = 2.7 + j 3.5
B = 1.6 + j 1.6
C = 4.3 + j 5.1
P = 2.5 + j 3.9
Q = 1.6 + j 2.5
R = 4.1 + j 6.4
                   
               

Destructors

Simple program to demonstrate destructor functionality ↓

                   
#include <iostream>
using namespace std;
class Scope
{
public:
    Scope()
    {
        cout << "The object is in the scope\n";
    }
    ~Scope()
    {
        cout << "The object goes out of the scope\n";
    }
};
int main()
{
    Scope A;
    {
        Scope B;
    }
    return 0;
}
                   
               

Output ↓

                   
The object is in the scope
The object is in the scope
The object goes out of the scope
The object goes out of the scope
                   
               

Dynamic initialization of objects

                        
#include <iostream>
using namespace std;
class Fixed_deposit
{
    long int P_amount; // Principal amount
    int Years;         //  Period of investment
    float Rate;        // interest rate
    float R_value;     // Return value of amount
public:
    Fixed_deposit() {}
    Fixed_deposit(long int p, int y, float r = 0.12);
    Fixed_deposit(long int p, int y, int r);
    void display(void);
};

Fixed_deposit ::Fixed_deposit(long int p, int y, float r)
{
    P_amount = p;
    Years = y;
    Rate = r;
    R_value = P_amount;
    for (int i = 1; i <= y; i++)
        R_value = R_value * (1.0 + r);
}
Fixed_deposit::Fixed_deposit(long int p, int y, int r)
{
    P_amount = p;
    Years = y;
    Rate = r;
    R_value = P_amount;
    for (int i = 1; i <= y; i++)
        R_value = R_value * (1.0 + float(r) / 100);
}
void Fixed_deposit ::display(void)
{
    cout << endl;
    cout << "Principal Amount = " << P_amount << endl
        << "Return Value     = " << R_value << endl;
}
int main()
{
    Fixed_deposit FD1, FD2, FD3; // deposits created
    long int p;                  // principal amount
    int y;                       // investment period, years
    float r;                     // interest rate, decimal form
    int R;                       // interest rate, percent form
    cout << "Enter amount, period, interest rate (in percent) : ";
    cin >> p >> y >> R;
    FD1 = Fixed_deposit(p, y, R);
    cout << "Enter amount, period, interest rate (in decimal) : ";
    cin >> p >> y >> r;
    FD2 = Fixed_deposit(p, y, r);
    cout << "Enter amount and period : ";
    cin >> p >> y;
    FD3 = Fixed_deposit(p, y);
    cout << endl
        << "Deposit 1";
    FD1.display();
    cout << endl
        << "Deposit 2";
    FD2.display();
    cout << endl
        << "Deposit 3";
    FD3.display();
    return 0;
}
                        
                    
                       
#include <iostream>
using namespace std;

void displayPer(int);

class Marks
{
    float percentage;

public:
    Marks() {}
    Marks(int, int);
    Marks(int, int, int);
    Marks(int, int, int, int);
    Marks(int, int, int, int, int);
};

Marks::Marks(int m1, int m2)
{
    percentage = (m1 + m2) / 2;
    displayPer(percentage);
}

Marks::Marks(int m1, int m2, int m3)
{
    percentage = (m1 + m2 + m3) / 3;
    displayPer(percentage);
}

Marks::Marks(int m1, int m2, int m3, int m4)
{
    percentage = (m1 + m2 + m3 + m4) / 4;
    displayPer(percentage);
}

Marks::Marks(int m1, int m2, int m3, int m4, int m5)
{
    percentage = (m1 + m2 + m3 + m4 + m5) / 5;
    displayPer(percentage);
}

void enterMarks(int);

int m[5] = {0}, m1, m2, m3, m4, m5;

int main()
{
    int sub;
    Marks result;
    cout << "----- Percentage Calculator------" << endl;
    cout << "How many subjects do you have : ";
    cin >> sub;
    switch (sub)
    {
    case 2:
        enterMarks(sub);
        result = Marks(m1, m2);
        break;
    case 3:
        enterMarks(sub);
        result = Marks(m1, m2, m3);
        break;
    case 4:
        enterMarks(sub);
        result = Marks(m1, m2, m3, m4);
        break;
    case 5:
        enterMarks(sub);
        result = Marks(m1, m2, m3, m4, m5);
        break;
    default:
        cout << "Enter subject between 1 and 6";
    }
    return 0;
}

void enterMarks(int sub)
{
    for (int i = 1; i <= sub; i++)
    {
        cout << "Enter the marks in subject " << i << " : ";
        cin >> m[i - 1];
    }
    m1 = m[0];
    m2 = m[1];
    m3 = m[2];
    m4 = m[3];
    m5 = m[4];
}

void displayPer(int val)
{
    cout << " you got " << val << "%\n";
}
                    
                

Pointer to object

Syntax ↓

                   
class_name object_name;
class_name *ptr = &object_name;
                   
               

Example ↓

                   
#include <iostream>
using namespace std;
class Rectangle
{
    int length;
    int breadth;

public:
    Rectangle(int l, int b)
    {
        length = l;
        breadth = b;
    }
    int getArea()
    {
        return 2 * length * breadth;
    }
};
int main()
{
    // creating an object of Rectangle
    Rectangle var1(10, 30);

    // creating a pointer for the object using class name as data type
    Rectangle *ptr = &var1;

    // calling the member function using -> symbol
    int area = ptr->getArea();
    cout << "Area of rectangle is = " << area << endl;
    return 0;
}
                   
               

Output ↓

                   
Area of rectangle is = 600
                   
               

"this" pointer

Example code ↓

                   
#include <iostream>
using namespace std;
class Test
{
private:
    int x;

public:
    void setX(int x)
    {
        // 'this' pointer is used to retrieve the object's x
        // here 'x' is in argument and also a data member
        // using 'this' pointer we can point to data member
        this->x = x;
    }
    void print() { cout << "x = " << x << endl; }
};
int main()
{
    Test obj;
    int x = 20;
    obj.setX(x);
    obj.print();
    return 0;
}
                   
               

Dynamic Objects

Syntax ↓

                   
ClassName *ptr_obj; // pointer to object
ptr_obj = new ClassName; // Dynamic object creation
delete ptr_obj; // Delete object dynamically
                   
               

Program to implement dynamic objects ↓

                   
#include <iostream>
using namespace std;
class Sum
{
    int a, b;

public:
    Sum()
    {
        cout << "Constructor is called" << endl;
        a = 1;
        b = 2;
    }
    ~Sum()
    {
        cout << "Destructor is called using free()" << endl;
    }
    void show()
    {
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
    }
};
int main()
{
    Sum *ptr;
    ptr = new Sum; // dynamic object creation
    ptr->show(); // Accessing member through pointer to object
    delete ptr; // Destroying object dynamically
    return 0;
}
                   
               

References ↓