Programming

Concept of Classes and Objects in C++

Gautam Gautam Donate Nov 27, 2019 · 4 mins read
Concept of Classes and Objects in C++

The classes and objects are the most important feature of C++ that leads to Object Oriented programming. A class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class.

The variables inside class definition are called as data members and the functions are called member functions.

The class in C++ is used in the following manner -

Class Declaration and Definition -

class <classname>
{
   //start of class body
   Access Specifiers:
   data Members;
}; //end of class with a semicolon (;)

void main()
{
   <classname> ob;
   ob.<functionname>(); //to invoke/call any functions. 
}

Example - 

class area_rectangle
{
    int length;       //Data Member
    int breadth;      //Data Member
    Public:   //Access Specifier
    void getdata();   //Member Function
    void display();   //Member Function
};

In this example, we are declaring a class named area_rectangle, Next, we are declaring variables named length and breadth. 

Please note, the members of class are private by default, to make it accessible in main class or outside the class, you must declare its Access Specifier to Public

Defining Functions and Accessing class Members

Next, we have declared two member functions named void getdata() and void display(). Now we will define these function. There are two ways for defining these functions. you can define these function either inside the class itself or outside the class using Scope Resolution Operator.

Let's first see how to define the function inside the class itself -

class area_rectangle
{
	int length;
	int breadth;
	public:
		void getdata()  //declaring the function
		{
			//defining the function 
			cout<<"\n Enter the length and breadth of Rectangle";
			cin>>length;
		}
		void display()  //declaring the function 
		{
			//defining the function 
			cout<<"\n Area of Rectangle is: "<<length * breadth;
		
		}
};

Now, to access this in main() we need to create an object of this class, the syntax is given below -

Class Objects -

<classname> <reference variable name>

Example - 

area_rectangle ob;

ob.<functionname>();   //use of Dot Operator

For the above program, the main() can be written as -

void main ()
{
	area_rectangle ob;   //creating the object for class - area_rectangle
	ob.getdata();        //calling function - getdata();
	ob.display();       //calling function - display();
	getch();
}

The complete program will look like this -

#include<conio.h>
#include<iostream.h>

class area_rectangle
{
	int length;
	int breadth;
	public:
		void getdata()  //declaring the function
		{
			//defining the function 
			cout<<"\n Enter the length and breadth of Rectangle";
			cin>>length;
		}
		void display()  //declaring the function 
		{
			//defining the function 
			cout<<"\n Area of Rectangle is: "<<length * breadth;
		
		}
};
void main ()
{
	area_rectangle ob;   //creating the object for class - area_rectangle
	ob.getdata();        //calling function - getdata();
	ob.display();       //calling function - display();
	getch();
}

Using Scope Resolution Operator -

Now, we will do the same program using Scope Resolution Operator -

Syntax for using Scope Resolution while defining a function -

<return data-type> <classname> :: <functionname>

Example - 

#include<conio.h>
#include<iostream.h>

class area_rectangle
{
	int length;
	int breadth;
	public:
		void getdata();  //declaring the function
		void display();  //declaring the function 
		
};

void area_rectangle :: getdata()  //defining the function
{
	cout<<"\n Enter the length and breadth: ";
	cin>>length>>breadth;
}

void area_rectangle :: display()  //defining the function
{
	cout<<"\n Area of rectangle: "<<length * breadth;
}

void main ()
{
	area_rectangle ob;
	ob.getdata();
	ob.display();
	getch();
}

While using Scope Resolution Operator, don't forget to give the same data type while defining the function as that of the function given at the time of declaration. For example,

void getdata() has data type as void, and at the time of defining the function - void area_rectangle :: getdata()

While doing the class program, you must remember these five things -

  • Class Declaration and Defining Data Members or Member Function with proper access specifiers
  • use of Data  Members and Defining the Member function either outside the class(using Scope Resolution Operator) or inside the class itself.
  • Object creation inside the main, in order to access/invoke or call the function of the class using DOT(.) operator.

Tags

Join Newsletter
Get the latest news right in your inbox. We never spam!
Gautam
Written by Gautam Sharma Profile
Developer, Writer, Emotional, loves to help, blogger

Connect with Gautam