Thursday, 19 February 2026

Classes and Objects in C++

1. What is a Class?
A class is a user-defined data type that contains:
  • Variables (data members)
  • Functions (member functions)

In C++, classes are first declared and are normally put in a separate header file. Then the member functions for each class are defined. Finally, the user code is written to create instances of classes (objects) and to perform the required tasks. A class’s attributes and behaviors are implemented using data members (instance variables) and member functions, respectively.

Information hiding is implemented by declaring members (data or functions) with one of the three categories of accessibility: private, public, and protected. Any function (member or nonmember) can access a public member. Only member functions of the class can access a private member. When members are protected, they can be accessed by member functions of the base class and its derived classes but not by nonmember functions. A derived class inherits all members from the base class.

Function oriented programming Object oriented programming (OOP)
User-defined types                             Classes
Variables                                     Objects
Structure members                             Instance variables
Functions                                     Methods
Function call                             Message passing
class user_de ned_name {
private :
data_type members
implementation operations
list of friend functions
list of friend functions
public :
data_type members
implementation operations
protected :
data_type operations
implementation operations
};
class user_de ned_name variable1,variable2..variable n;
(a) Private: In private section, a member data can only be accessed by the member function and friends of this class. The member functions and friends of this class can always read or write private data members. The private data member is not accessible to the outside world (out of the class).
(b) Protected: The members which are declared in the protected section, can only be accessed by the member functions and friends of this class. Also, these functions can be accessed by the member functions and friends derived from this class. It is not accessible to the outside world.
(c) Public: The members which are declared in the public section, can be accessed by any function in the outside world (out of the class). The public implementation operations are also called as member functions or methods, or interfaces to out of the class. Any function can send messages to an object of this class through these interface functions.
The public data members can always read and write outside this class. A member function can be inline, which means the member function can be defi ned within the body of the class constant. The keyword inline is used for short functions and effi cient storage like the register keyword.

OBJECT ORIENTED LANGUAGES

It is well known that Object Oriented Programming provides major advantages in the creation and maintenance of software. These include shorter development time and a high degree of code sharing and flexibility. These advantages make object oriented programming an important technology for building complex software systems now and in the future. A number of languages are claimed to be object oriented. The following are certain well-known Object Oriented Programming languages:

• smalltalk
• Common Lisp Object System (CLOS)
• Object Pascal
• Object C
• C++
• Java
• C#
Example 1.
A program to assign values to the data members of a class such as day, month, year and display the contents of the class on the screen.
// class 1.cpp
#include <iostream>
using namespace std;
int main()
{
class date {
public :
int day,month,year;
};
class date today;
today.day = 10;
today.month = 5;
today.year = 2007;
cout << “ Today’s date is = ” << today.day << “/”;
cout << today.month << “/” << today.year << endl;
return 0;
}
Output of the above program
Today’s date is = 10/5/2007

While the keyword public is not used to defi ne the members of a class, the C++ compiler assumes, by default, that all its members are private. The data members are not accessible outside the class. For example, the following program demonstrates the accessibility of the members.
// class 2.cpp
#include <iostream>
using namespace std;
int main()
{
class date { // by default, members are private
int day,month,year;
};
class date today;
today.day = 10;
today.month = 5;
today.year = 2007;
cout << “ Today’s date is = ” << today.day << “/”;
cout << today.month << “/” << today.year << endl;
}
The following error message will be displayed during the compilation time.
date.day is a private
date.month is a private
date.year is a private

Example 2.
A program to demonstrate how to defi ne both data member and member function of a class within the scope of class defi nition.
//class with data and member function
#include <iostream>
using namespace std;
class date {
private :
int day,month,year;
public :
void getdata( int d,int m,int y)
{
day = d;
month = m;
year = y;
}
void display (void)
{
cout << “ Today’s date is = ” << day << “/”;
cout << month << “/” << year << endl;
}
}; // end of class de nition
int main()
{
date today;
int d1,m1,y1;
d1 = 10;
m1 = 5;
y1 = 2007;
today.getdata(d1,m1,y1);
today.display();
return 0;
}
Output of the above program
Today’s date is = 10/5/2007

Example 3.
A program to read the data variables of a class by the member function and display the contents of the class objects on the screen.
//class with data and member function
#include <iostream>
using namespace std;
class date {
private :
int day,month,year;
public :
void getdata()
{
cout << “ enter the date ( dd-mm-year) ” << endl;
cin >> day >> month >> year;
}
void display ()
{
cout << “ Today’s date is = ” << day << “/”;
cout << month << “/” << year << endl;
}
}; // end of class de nition
int main()
{
date today;
today.getdata();
today.display();
return 0;
}
Output of the above program
enter the date ( dd-mm-year)
12 5 2007
Today’s date is = 12/5/2007

Example 4.
A program to illustrate the use of the simple arithmetic operations such as addition, subtraction, multiplication and division using a member function. These methods are defi ned within the scope of a class defi nition.
// member functions are de ned within the class de nition
#include <iostream>
using namespace std;
class sample {
private :
int x,y;
public :
void getinfo(){
cout << “ enter any two numbers ? ” << endl;
cin >> x >> y ;
}
void display(){
cout << “ x = ” << x << endl;
cout << “ y = ” << y << endl;
cout << “ sum = ” << sum() << endl;
cout << “ dif = ” << diff() << endl;
cout << “ mul = ” << mult() << endl;
cout << “ div = ” << div() << endl;
}
int sum(){
return(x+y);
}
int diff(){
return(x-y);
}
int mult(){
return(x*y);
}
oat div(){
return( ( oat)x/( oat)y);
}
}; // end of class de nition
int main()
{
sample obj1;
obj1.getinfo();
obj1.display();
obj1.sum();
obj1.diff();
obj1.mult();
obj1.div();
return 0;
}
Output of the above program 
enter any two numbers ?
1 2
x = 1
y = 2
sum = 3
dif = -1
mul = 2
div = 0.5

Example 5.
A program to illustrate the use of the simple arithmetic operations such as addition, subtraction, multiplication and division using a member function. These are defi ned out of the scope of a class defi nition.
//methods are de ned out of the class de nition
#include <iostream>
using namespace std;
class sample {
private :
int x;
int y;
public :
void getinfo();
void display();
int sum();
int diff();
int mult();
oat div();
}; // end of class de nition
void sample :: getinfo()
{
cout << “ enter any two number ? ” << endl;
cin >> x >> y ;
}
void sample :: display()
{
cout << “ x = ” << x << endl;
cout << “ y = ” << y << endl;
cout << “ sum = ” << sum() << endl;
cout << “ dif = ” << diff() << endl;
cout << “ mul = ” << mult() << endl;
cout << “ div = ” << div() << endl;
}
int sample :: sum()
{
return(x+y);
}
int sample :: diff()
{
return(x-y);
}
int sample :: mult()
{
return(x*y);
}
oat sample :: div()
{
return(( oat)x/ ( oat)y);
}
int main()
{
sample obj1;
obj1.getinfo();
obj1.display();
obj1.sum();
obj1.diff();
obj1.mult();
obj1.div();
return 0;
}
Output of the above program
enter any two number?
1 3
x = 1
y = 3
sum = 4
dif = -2
mul = 3
div = 0.333333

Example 6.
A program to fi nd the area of a circle whose radius is given as input using an OOP technique.
// nding the area of a circle
#include <iostream>
#include <cmath>
using namespace std;
const oat pi = 3.14159;
class circle {
private :
oat radius,area;
public :
void get_radius();
void nd_area();
void display_area();
}; // end of class de nition
void circle ::get_radius()
{
cout << “enter radius of a circle \n”;
cin >> radius;
}
void circle :: nd_area()
{
area = pi * radius * radius;
}
void circle :: display_area()
{
cout << endl;
cout << “ radius = ” << radius << ‘\n’;
cout << “ Area of a circle = ” << area << ‘\n’;
}
int main()
{
circle obj;
obj.get_radius();
obj. nd_area();
obj.display_area();
return 0;
}
Output of the above program
enter radius of a circle
10
radius = 10
Area of a circle = 314.159

Example 7.
A program to fi nd the sum of the following series using an OOP technique.
sum = 1 + 3 + 5 + 7 + ... n
//summing of series
// sum = 1 + 3 + 5 ... n
#include <iostream>
using namespace std;
class abc {
private:
int n;
public:
int sum(int n);
};
{
int temp = 0;
for (int i = 1; i <= n; i += 2)
temp += i;
return (temp);
}
int main()
{
int max;
abc obj;
cout << “enter a value for n ”;
cin >> max;
int total = obj.sum(max);
cout <<“Summing of series \n”;
cout <<“ 1 + 3 + 5 + ... ” << max << “ = ” << total;
cout << endl;
return 0;
}
Output of the above program
enter a value for n
9
Summing of series
1 + 3 + 5 + ... 9 = 25

Example 8.
A program to fi nd the factorial of a given number using an OOP technique.
// nding factorial of a number
#include <iostream>
using namespace std;
class abc {
private:
int n;
public:
long int fact (int n);
};
long int abc :: fact(int n)
{
long int temp = 1;
for (int i = 1; i <= n; ++i)
temp *= i;
return (temp);
}
int main()
{
abc obj;
int max;
cout << “ enter a number \n”;
cin >> max;
long int total = obj.fact(max);
cout << “Factorial ” << max << “! = ” << total;
cout << endl;
return 0;
}
Output of the above program
enter a number
5
Factorial 5! = 120

Example 9.
A program to solve a quadratic equation using an OOP technique.
//solution of quadratic equation using OOP
#include <iostream>
#include <cmath>
using namespace std;
class equation {
private :
oat a,b,c;
public :
void getinfo( oat a, oat b, oat c);
void display();
void equal( oat a, oat b);
void imag();
void real( oat a, oat b, oat det);
}; // end of class de nition
void equation ::getinfo( oat aa, oat bb, oat cc)
{
a = aa;
b = bb;
c = cc;
}
void equation :: display()
{
cout << endl;
cout << “ a = ” << a << ‘\t’;
cout << “ b = ” << b << ‘\t’;
cout << “ c = ” << c << endl;
}
void equation :: equal( oat a, oat b)
{
oat x;
x = -b/(2*a);
cout << “ roots are equal = ” << x <<endl;
}
void equation :: imag()
{
cout << “ roots are imaginary \n”;
}
void equation :: real( oat a, oat b, oat det)
{
oat x1,x2,temp;
temp = sqrt(det);
x1 = (-b+temp)/(2*a);
x2 = (-b-temp)/(2*a);
cout << “ roots are real \n”;
cout << “ x1 = ” << x1 << endl;
cout << “ x2 = ” << x2 << endl;
}
int main()
{
class equation equ;
oat a,b,c;
cout << “ enter three numbers \n”;
cin >> a >> b >> c;
equ.getinfo(a,b,c);
equ.display();
if ( a == 0) {
oat temp;
temp = -c/b;
cout << “ linear roots = ” << temp << endl;
}
else {
oat det;
det = b*b-4*a*c;
if (det == 0)
equ.equal(a,b);
else if ( det < 0)
equ.imag();
else
equ.real(a,b,det);
}
return 0;
} // end of main program
Output of the above program
enter three numbers
0 1 2
a = 0 b = 1 c = 2
linear roots = -2
enter three numbers
2 4 2
a = 2 b = 4 c = 2
roots are equal = -1

Example 10.
A program to perform simple complex number arithmetic operations using an OOP technique.
// complex number operations using OOP
#include <iostream>
#include <cstdio>
using namespace std;
class complex {
private :
oat areal;
oat aimag;
oat breal;
oat bimag;
public :
void getinfo( oat a, oat, oat c, oat d);
void display ();
void menu();
void add ( oat areal, oat aimag, oat breal , oat bimag);
void sub ( oat areal, oat aimag, oat breal , oat bimag);
void mul ( oat areal, oat aimag, oat breal , oat bimag);
void div ( oat areal, oat aimag, oat breal , oat bimag);
};
void complex::getinfo( oat x, oat y, oat z , oat w)
{
areal = x;
aimag = y;
breal = z;
bimag = w;
}
void complex :: display()
{
cout << “ rst complex number \n”;
cout << areal;
if (aimag < 0)
cout << “-i” << (-1)*aimag << endl;
else
cout << “+i”<< aimag << endl;
cout << “ second complex number \n”;
cout << breal;
if (bimag < 0)
cout << “-i” << (-1)*bimag << endl;
else
cout << “+i”<< bimag << endl;
}
void complex :: menu(void)
{
cout << “ complex number operations \n”;
cout << “ menu () \n”;
cout << “ a -> addition \n”;
cout << “ s -> subtraction \n”;
cout << “ m -> multiplication \n”;
cout << “ d -> multiplication \n”;
cout << “ q -> quit \n”;
cout << “ option, please ? \n”;
}
void complex :: add( oat areal, oat aimag, oat breal, oat bimag)
{
oat creal,cimag;
creal = areal+breal;
cimag = aimag+bimag;
cout << “ Addition of two complex numbers \n”;
cout << creal;
if (cimag < 0)
cout << “-i” << (-1)*cimag << endl;
else
cout << “+i”<< cimag << endl;
}
void complex :: sub( oat areal, oat aimag, oat breal, oat bimag)
{
oat creal,cimag;
creal = areal-breal;
cimag = aimag-bimag;
cout << “ Subtraction of two complex numbers \n”;
cout << creal;
if (cimag < 0)
cout << “-i” << (-1)*cimag << endl;
else
cout << “+i”<< cimag << endl;
}
void complex :: mul( oat areal, oat aimag, oat breal, oat bimag)
{
oat creal,cimag;
creal = (areal*breal)-(aimag*bimag);
cimag = (areal*bimag)+(aimag*breal);
cout << “ Multiplication of two complex numbers \n”;
cout << creal;
if (cimag < 0)
cout << “-i” << (-1)*cimag << endl;
else
cout << “+i”<< cimag << endl;
}
void complex :: div( oat areal, oat aimag, oat breal, oat bimag)
{
oat creal,cimag;
oat temp;
temp = (breal*breal)+(bimag*bimag);
creal = ((areal*breal)+(aimag*bimag))/temp;
cimag = ((breal*aimag)-(areal*bimag))/temp;
cout << “ Division of two complex numbers \n”;
cout << creal;
if (cimag < 0)
cout << “-i” << (-1)*cimag << endl;
else
cout << “+i”<< cimag << endl;
}
int main()
{
complex comp;
oat x,y,z,w;
char ch;
cout << “enter a rst complex number \n”;
cin >> x >> y;
cout << “ enter a second complex number \n”;
cin >> z >> w;
comp.getinfo(x,y,z,w);
comp.display();
comp.menu();
while (( ch = getchar()) != ‘q’) {
switch (ch) {
case ‘a’ :
comp.add(x,y,z,w);
break;
case ‘s’ :
comp.sub (x,y,z,w);
break;
case ‘m’ :
comp.mul (x,y,z,w);
break;
case ‘d’ :
comp.div (x,y,z,w);
break;
} // end of switch
}
return 0;
} // end of main program
Output of the above program
enter a rst complex number
1 1
enter a second complex number
2 2
rst complex number
1+i1
second complex number
2+i2
complex number operations
menu ()
a -> addition
s -> subtraction
m -> multiplication
d -> multiplication
q -> quit
option, please?
a
Addition of two complex numbers
3+i3
s
Subtraction of two complex numbers
-1-i1
m
Multiplication of two complex numbers
0+i4
d
Division of two complex numbers
0.5+i0a
q

Example 11.
A program to read a set of characters from the keyboard and store it in the character array and display its contents onto the video screen using an OOP technique.
#include <iostream>
using namespace std;
class abc {
public:
char a[200];
void getdata();
void display();
};
void abc :: getdata()
{
char ch;
int i = 0;
cout <<“enter a line of text and terminate with @\n”;
while ((ch = cin.get()) != ‘@’) {
a[i++] = ch;
}
a[i++] = ‘\0’;
}
void abc :: display()
{
cout << “contents of a character array \n”;
for (int i = 0; a[i] != ‘\0’; ++i)
cout.put(a[i]);
}
int main()
{
abc obj;
obj.getdata();
obj.display();
return 0;
}
Output of the above program
enter a line of text and terminate with @
this is
a test
program
by R
@
contents of a character array
this is
a test
program
by R

Example 12.
A program to read a set of characters from the keyboard and store it in the character array and fi nd out the number of characters that are stored in the array; display its contents onto the video screen using an OOP technique.
//counting number of characters
#include <iostream>
using namespace std;
char a[200];
class abc {
public:
void getdata();
void display();
int count(char a[]);
};
void abc :: getdata()
{
char ch;
int i = 0;
cout <<“enter a line of text and terminate with @\n”;
while ((ch = cin.get()) != ‘@’) {
a[i++] = ch;
}
a[i++] = ‘\0’;
}
void abc :: display()
{
cout << “contents of a character array \n”;
for (int i = 0; a[i] != ‘\0’; ++i)
cout.put(a[i]);
}
int abc :: count(char a[])
{
int i = 0;
while (a[i] != ‘\0’)
++i;
return (i-1);
}
int main()
{
abc obj;
int total_ch;
obj.getdata();
obj.display();
total_ch = obj.count(a);
cout <<“\n Number of characters = ” << total_ch;
return 0;
}
Output of the above program
enter a line of text and terminate with @
this is
a test
@
contents of a character array
this is
a test
Number of characters = 15

0 comments

Post a Comment