Home > OOP > Objects in C++

Generally, an object can be a variable, data structure, a function or a method.
In object-oriented programming, an object is a particular instance of a class. This particular instance of a class can be a combination of variables, data structures or functions.
You can make an object by bundling together the data (data members) of a class and the functions (member functions) of a class that operates on its data members.
Objects provides us the access to data members and member functions of the class. One class can have one or number of objects.They are created using a special member function of a class called the constructor.

Constructor: –

It is a special member function of the class which has no return type (not even void) and the same name as of class. Always define the constructor of the class under public specifier. We can have any number of constructors in our class with different definitions. This is called constructor overloading it is same as function overloading.
E.g.
For a class named test, we will have a constructor as:

class test {
public:
	test() {
		// this is a constructor.
	}
};

Note: – If we do not write any constructor in our class than compiler adds a default constructor at compile time to our program.
But if we write a single constructor in our class than it is necessary to write default copy constructor (constructor with no arguments) in our class. Otherwise, we will have an error.

Syntax to create an object is:

Class-Name object-Name;

For a sample class


class object_Test_class
{
     private:
          int no1;
          float no2;
     public:
          void function_A()
          {
               no1 = 10;
          }
          float function_B()
          {
               no2 = 23.5;
               return no2;
          }
};

How to create an object of the class?

We can create an object as:


object_Test_class obj1;

an object

Create an array of objects

Just like simple variables, you can create an array of objects for a class.
E.g.
For above-written class, we can create an array of objects as:

object_Test_class  obj[50]; // this will create an array of 50 objects for class object_Test_class.

Different objects of the same class can be made depending on the definition of the constructor with which they are being created.
E.g.
If we have a constructor for above class as:

object_Test_class()
{
no1=50;
no2=100;
}

For the above constructor, you can create an object using this below line of code.

object_Test_class obj2;will have data members no1 and no2 of obj2 will have 50 and 100 as their 
values respectively.


Accessing Member functions of class

Objects provide access to member functions of the class.

Dot (.) operator is used to access functions of class.

obj1.function_A();
obj1.function_B();

E.g.

#include "stdafx.h"
#include "iostream"

using namespace std;

class object_Test_class
{
     private:
          int no1;
          float no2;
     public:
          void function_A()
          {
               no1 = 10;
          }
          float function_B()
          {
               no2 = 23.5;
               return no2;
          }
};

void main()
{
     object_Test_class obj1;
     cout << "Accessing Member function of a class with its object. \n";
     cout << "Value of no2 is : "<< obj1.function_B() << endl;
}

Output: –
accessing functions with objects
We can make objects of a class inside another class. This is called Association.

You can destroy objects of the class as well. They are destroyed (i.e. memory used by the object is freed) with a special member function called Destructor.

Destructor: –

It is also a special member function of the class with the same name as the class and no return type. Always define destructor under public specifier. Its definition is same as of a constructor but a tilde sign (~) precedes its name.
E.g.
For a class named test, we will have a destructor as:

class test 
{
     public:
          ~ test () 
          {
              // this is a destructor.
          }
};

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.