Module 1 Chapter 2
Module 1 Chapter 2
Objects and classes are used to wrap related functions and data in one place in C++.
Suppose we need to store the length, breadth, and height of a rectangular room and calculate its
area and volume.
To handle this task, we can create three variables, say, length, breadth, and height, along with the
functions calculate_area() and calculate_volume().
However, in C++, rather than creating separate variables and functions, we can also wrap the related
data and functions in a single place (by creating objects).
Create a Class
A class is defined in C++ using the keyword class followed by the name of the class.
The body of the class is defined inside curly brackets and terminated by a semicolon at the end.
class ClassName
{
// some data
// some functions
};
For example,
class Room
{
public:
double length;
double breadth;
double height;
double calculate_area()
{
return length * breadth;
}
double calculate_volume()
{
return length * breadth * height;
}
};
ClassName object_name;
We can create objects of Room class (defined in the above example) as follows:
// sample function
void sample_function()
{
// create objects
Room room1, room2;
}
int main()
{
// create objects
Room room3, room4;
}
Here, two objects room1 and room2 of the Room class are created in sample_function().
Similarly, the objects room3 and room4 are created in main().
As we can see, we can create objects of a class in any function of the program.
We can also create objects of a class within the class itself or in other classes.
Also, we can create as many objects as we want from a single class.
We can access the data members and member functions of a class by using a . (dot) operator.
For example,
room2.calculate_area();
This will call the calculate_area() function inside the Room class for object room2.
Similarly, the data members can be accessed as:
room1.length = 5.5;
In this case, it initializes the length variable of room1 to 5.5.
Example
#include <iostream>
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
};
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Here in above program getdata() and showdata() are the member function defined inside the class.
#include <iostream.h>
#include <conio.h>
class GFG {
private:
int private_variable;
protected:
int protected_variable;
public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}
// Driver code
int main()
{
clrscr();
GFG g;
F fri;
fri.display(g);
getch();
return 0;
}
// C++ program to create a global function as a friend function of some class
#include <iostream.h>
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
// driver code
int main()
{
base object1;
friendFunction(object1);
return 0;
}
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
// driver code
int main()
{
base object1;
anotherClass object2;
object2.memberFunction(object1);
return 0;
}