Browse by Domains

Class in C++

Table of contents

Introduction

Class is the foundation for object-oriented programming. It is a user-defined data type that works as a blueprint and allows its instances to be created which are known as an object. Class in C++ is the combination of data members and member function, which implements the encapsulation and data hiding concepts.

The concept of class in C++ is extracted from the Stimula 65 programming language therefore C++ is also called the C language with Stimula 65.

Importance

Before learning a concept one must know why we are learning it and what the importance of the concept is. Class is the most important concept of object-oriented programming as it provides features like Data Security, Reusability. 

Prerequisite

Flow for understanding the concept of class:

Structure in C language -> Structure in C++ -> Class in C++

Class is the extension of the structure therefore it is necessary to understand the concept of the structure first.

Structure in C language

Structure is a user define complex data type which contains different types of variable, data types tells about the amount of memory required by a variable also the type of value that can be stored in the variable, the structure can store primitive as well as derived data type in one place that’s why it is called as the user defined data type. Structure is also known as a heterogamous collection of data types as it can contain different data types.

For creating a structure struct keyword is used.

Syntax of Structure in C Programming Language

struct <Tag Name>	// struct is the keyword
{
Data / variable 1;	// n number of variables/Data of different data type can be declared
Data / Variable 2;
.
.
Data / Variable n;
} Structure Variable;
<Tag Name > Structure Variable;		// Declaration of structure variable

Description:  In the above syntax struct is a keyword that is used to create a structure and tag name is the name of structure you can give by following the variable naming conventions. Inside the structure block, various data type variables can be declared and the structure block is to be terminated with the semicolon. 

As the structure is a user-defined data type various data type variable can be declared for structure data type known as structure variable in two ways – 

a) Declaring it after the end of the structure block and prior to the semicolon. 

b) Declaring by following the tag name given to the structure variable after the structure block ends.

Example of Structure in C Programming Language

struct book			//Creating structure
{
int book_id;			//Data or variable
char[20] book_name;
float book_price;
} b1;				// Structure variable declaration
book b2;			// Structure variable declaration.

Description : In the above example a structure is created named as book, inside the structure book block three variables of different data types are declared book_id as integer variable , book_name as character array and book_price as float variable. 

Two structure variables b1 and b2 are declared of the type book. There are two possible ways to declare the structure variables which are shown in the example. Here the structure variables are declared in the following two ways.

  1. Structure variable b1 is declared just after the end of the structure book block and prior to the terminating semicolon.
  2. Structure variable b2 is declared by writing the structure variable name just after the structure name i.e. book.

How to access the data inside the structure

To access the data of a structure dot operator is used ( . ) along with the structure variable in the following manner:

struct book			//Creating structure
{
int book_id;			//Data or variable
char[20] book_name;
float book_price;
} b1;				// Structure variable declaration
book b2,b3;			// Structure variable
void main()
{
Statements……
b1.book_id =121;			// accessing structure data 
b1.book_name=”Classes in C++”;
b1.book_price = 545.00;
b2.book_id =788;
b2.book_name=”Object in C++”;
b2.book_price = 752.25;
printf(“Enter the Book Id”);		//Input from user
scanf(“%d”,&b3.book_id);
printf(“Enter the Book Name”);
scanf(“%C”,b3.book_name);
printf(“Enter the Book Price”);
scanf(“%f”,&b3.book_price);
statements….
}

In the above example a user-defined data type – structure is created with the name book using keyword struct, inside the block of structure three variables of different data types are declared with the name book_id,book_name, and book_price.

Three structure variables b1,b2, and b3 are declared. These three structure variables are used to access the data of the structure block.

Inside the main function structure variables b1, b2 and b3 are used along with the dot (.) operator to access the data elements.

b1.book_id =121 this statement is used to input the value 121 in the integer variable book_id. Similarly, the values are inputted in book_name and book_id.

scanf(“%d”,&b3.book_id) this statement is used to take input from user similarly structure variable can be used to take input from the user by using the scan function.

Structure in C++ 

C++ structures has following two major add-ons to structure in C language 

  1. C++ structure block can contain function also along with the data or variables.
  2. It uses the concept of access specifiers – there are three access specifiers in C++ 
  1. Private : Data is accessed privately 
  2. Public : Data is accesses publically
  3. Protected : Data access has limit 

Access specifiers allows the data or function inside the structure to be accessed with particular access rights. By default access speifier in C++ structure is private and that in C language is public 

Syntax of Structure in C++

struct <Tag Name>	// struct is the keyword
{
Data / variable 1;	// n number of variables/Data of different data type can be declared
Data / Variable 2;
.
.
Data / Variable n;
Function 1;	//n number of function can be declared and defined
Function 2;
.
.
Function n;

} Structure Variable;
<Tag Name > Structure Variable		// Declaration of structure variable
void display()
{
cout

}
} b1;				// Structure variable declaration
book b2;			// Structure variable

Description: In the above syntax struct is used to create a structure that comprises both data/variable and functions.

Example:

struct book			//Creating structure
{
int book_id;			//Data or variable
char[20] book_name;
float book_price;
void display()
{
cout<<book_id;
cout<<book_name;
cout<<book_price;


}
} b1;				// Structure variable declaration
book b2;			// Structure variable
void main()
{
Statements……
b1.book_id =121;			// accessing structure data 
b1.book_name=”Classes in C++”;
b1.book_price = 545.00;
b2.book_id =785;
b2.book_name=”object in C++”;
b2.book_price 750.50;
b1.display();
b2.display();
}

Output:
121
Classes in C++
545.00
785
Object in C++
750.50

In the above example, a structure book is created which consists of three variables book_id,book_name, and book_price along with the function display() this function is used to display the data elements of the structure.

Two structure variables b1 and b2 are declared to access the data and functionality of the structure book.

Inside the main function, the data are inputted using the structure variable and dot operator like b2.book_id=785 similarly all the inputs are done.

Inside the main function, the display() function which is declared and defined inside the structure is used to display the values of variables i.e book_id,book_name, and book_price. 

The structure-function is accessed using the structure variable and function name with the help of dot (.) operator – b1.display(); and b2.display().

The values are given to the variables of structure and for displaying them a function display() is used.

Implementation of class in C++

Keyword class is used to create class and inside the block of the class, data and function is to be placed. Data inside the class can be termed as Data Members and functions can be termed as Member Functions.

To add on the security features access specifiers are used inside the class. 

Access specifier describes the access rights of the data member and member function of the class.

There are three type of access specifiers allowed in C++.

  1. Public : Data Members and Member Functions can be accessed from anywhere in the program from outside the class.
  2. Private : Data Members and Member Functions cannot be accessed from outside the class.
  3. Protected : Data Members and Member Functions can be accessed from outside the class with a limited access from the inherited class.

Syntax of the class

class <Class Name>
{
Access Specifier :
	Data1 / Variable1 ;
Data2/Variable2 ;
.
.
.
Data n/Variable n ;

Access Specifier :

	Function1 / Method 1 ;
	Function 2/ Method 2 ;
};

Example:

class student			//Creating a class student using keyword class
{

Private :			//Data members declared with private access specifier
	int roll_no;
char[20] student_name;
	float marks;
public :				//Member functions declared with public access specifier
	
	void setinfo()		//Member function definition
		{

		cout<<”Enter the Student Roll Number”;
		cin>>roll_no;
	
cout<<”Enter the Student Name”;
		cin>>student_name;

cout<<”Enter the Student marks”;
		cin>>marks;

}

	void showinfo()

		{

		cout<<”The Student Roll Number is “<<roll_no;
		
cout<<”The Student Name is”<<name;
		
cout<<”The  Student Marks is”<<marks;
		
}



};

Explanation

In the above example a class student is created by using the keyword class, inside the class block the data members roll_no, student_name and marks are declared in private access mode i.e these data members can be accessed privately only by the members of the same class student. 

Two member functions setinfo() and showinfo() are defined inside the class using public access mode i.e these functions can be used from the outside of the class also. The function setinfo is defined to take input for the data members and function showinfo is defined to display the values of data members of the class student. Lastly, the block of  the class is terminated with the semicolon.

The function of a class can also be defined outside the class using the scope resolution operator (: 🙂 but it must be declared inside the class.

Example of class where function is defined outside the class.

class student
{

Private :
	int roll_no;
char[20] student_name;
	float marks;
public :
	void setinfo();			//Declaration of the functions
	void showinfo(); 

	
};
void  student : : setinfo()				//Definition of the function outside the class.
		{

		cout<<”Enter the Student Roll Number”;
		cin>>roll_no;
	
cout<<”Enter the Student Name”;
		cin>>student_name;

cout<<”Enter the Student marks”;
		cin>>marks;

}

void student : : showinfo()

		{

		cout<<”The Student Roll Number is “<<roll_no;
		
cout<<”The Student Name is”<<name;
		
cout<<”The  Student Marks is”<<marks;
		
}


Explanation

Class student is created which comprise of three data members with private access specifier ie. These data members cannot be accessed publically, setinfo() and getinfo() are two member functions with public access specifier. The block of class is to be terminated by a semicolon. Here the function is defined outside the class for the scope resolution operator (::) is used.

How to access the elements of the class

The class members are accessed by an object and dot operator.

Object

Object is the variable of type class , it is an instance of the class  the memory is allocated to the class elements only when the object is created.

One can think of class  as a original copy and the object as the xerox of the copy .

With the help of the object of the class its data members and member function can be accesed.

Syntax to create an object

Class name <object name> ;

Example

class student			//class is created using keyword class and with name student
{
private :			// Private access specifier id implemented for data member
Char name[20];		//Declatation of data
int  roll_no;
public:				// Public access specifier id implemented for member function
	void getdata()		// Function is created and defined for inputting the data
{
cout<<”Enter the name of the student”;
cin>>name;
cout<<”Enter the roll number of the student”;
cin>>roll_no;
} 
	void showdata()		//Function is created and defined for displaying the data.
{
cout<<”The name of the student is”<<name;
cout<<”The roll number of the student is”<<roll_no;
}
};				//class end here

void main()			// main function is created and defined
{
student obj1;			//object of class student is created as obj1
student obj2;			//object of class student is created as obj2
clrscr();
obj1.getdata();	//getdata() function of class student is accessed using object (obj1) 
obj2.getdata();			// dot operator is used along with object to access data and function
obj1.showdata();
obj2.showdata();
getch();
}

Input :

Input using obj1

Enter the name of the student 

Suresh

Enter the roll number of the student

1221

Input using obj2

Enter the name of the student 

Akash

Enter the roll number of the student

1425

Output :

Output using obj1

The name of the student is Suresh

The roll number of the student is 1221

Output using obj2

The name of the student is  Akash

The roll number of the student is 1425

Explanation

In the above example a class student is created having the data members name and roll_no declared in private access mode and the member functions getdata() and showdata() defined inside the class in public access mode.

The function getdata() is used to take input for the data members and  function showdata() is used to display the values of data members.

Inside the main function two objects obj1 and obj2 are declared for the class student i.e they are the instances of the class student and can be used to access the members of the class.

The data members of the class are accessed using the object of the class student along with the dot (.) operator.

obj1.getdata() is used to input the data and obj1.showdata() is used t display the values of the data members which are inputted using the object obj1.Similarly obj2 is used.

Using obj1.getdata() the input values are Suresh 1221 hence obj1.showdata() will display the values Suresh and 1221. Similarly obj2 can be used.

Object can be compared with a variable and structure variable.

int number ;

Here int is an integer data type and number is a variable of type class.

book math;

Here book is a structure and math is a structure variable ie variable of type structure.

student bca ;

Here student is a class and bca is the variable of type student , it is an object.

Features of the class in C++

Avatar photo
Great Learning Team
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

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

Great Learning Free Online Courses
Scroll to Top