Object Oriented Programming Through C C2
Object Oriented Programming Through C C2
in
Chapter 2
Class and Object
A class is a way to bind the data and its associated functions together. It allows the data and functions
to be hidden, if necessary, from external use. A class declaration is similar syntactically to a structure.
class class_name
{
private:
Variable declaration/data members; Private members can be accessed only from within
Function declaration/ member functions; the class.
protected:
Variable declaration/data members; Protected members can be accessed by own class
Function declaration/ member functions; and its derived classes.
public:
Variable declaration/data members; Public members can be accessed from outside the
Function declaration/ member functions; class also.
};
Points to remember!
The variables declared inside the class definition are known as data members and the functions
declared inside a class are known as member functions.
Wrapping of data and function and function into a single unit (i.e. class) is known as data
encapsulation.
By default the data members and member function of a class are private.
Private data members can be accessed by the functions that are wrapped inside the class.
Header files
Class definition
Member function definition
void main function
Program 2.1 Write a program to find sum of two integers using class and object.
Solution:
#include<iostream.h>
30
1
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
class Add
{
int x, y, z;
public:
void getdata()
{
cout<<”Enter two numbers”;
cin>>x>>y;
}
void calculate(void);
void display(void);
};
void main()
{
Add a;
a.getdata();
a.calculate();
a.display();
}
Output:
Enter two numbers 5 6
11
31
2
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Here in the above example we are defining the member function getdata() inside the class
definition. And we are defining the member functions calculate() and display(), outside the class
definition using the scope resolution operator.
Here void Add :: calculate() means the scpoe of member function calculate() is inside the class
Add or we can say the function calculate() belongs to the class Add. :: is the scope resolution
operator which tells the scope of a member function.
We cannot directly call a function, we can call it using object (through . operator) of the class in
which the function is declared.
Solution:
#include<iostream.h>
class time
{
int hours, minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void sum(time, time);
void display(void);
};
void time :: sum (time t1, time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
32
3
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
void time :: display()
{
cout<<hours<<” : ”<<minutes<<endl;
}
void main()
{
time T1, T2, T3;
T1.gettime(2,45);
T2.gettime(3,30);
T3.sum(T1, T2);
T1.display();
T2.display();
cout<<"Addition of above two time is ";
T3.display();
}
Output:
2 : 45
3 : 15
Addition of above two time is 6:15
Array of object
Program 2.3 Write a program to input name and age of 5 employees and display them.
Solution:
#include<iostream.h>
class Employee
{
char name[30];
int age;
public:
void getdata(void);
void putdata(void);
};
33
4
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
void main()
{
Employee e[5];
int i;
for(i=0; i<5; i++)
{
e[i].getdata();
}
for(i=0; i<5; i++)
{
e[i].putdata();
}
}
Output:
Enter Name and Age: Rajib 25
Enter Name and Age: Sunil 27
Enter Name and Age: Ram 23
Enter Name and Age: Bibhuti 26
Enter Name and Age: Ramani 32
Rajib 25
Sunil 27
Ram 23
Bibhuti 26
Ramani 32
Constructor
A constructor is a special member function whose task is to initialize the object of a class.
Its name is same as the class name.
A constructor does not have a return type.
34
5
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
A constructor is called or invoked when the object of its associated class is created.
It is called constructor because it constructs the values of data members of the class.
A constructor cannot be virtual (shall be discussed later on).
A constructor can be overloaded.
Default Constructor
Solution:
#include<iostream.h>
class Add
{
int x, y, z;
public:
Add(); // Default Constructor
void calculate(void);
void display(void);
};
Add::Add()
{
x=6;
y=5;
}
void Add :: calculate()
{
z=x+y;
}
void Add :: display()
{
cout<<z;
}
void main()
{
Add a;
a.calculate();
35
6
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
a.display();
}
Output:
11
Note: Here in the above program when the statement Add a; will execute (i.e. object is created), the
default constructor Add () will be called automatically and value of x and y will be set to 6 and 5
respectively.
Parameterized constructor
Program 2.5 Write a program to initialize two integer variables using parameterized
constructor and add them.
Solution:
#include<iostream.h>
class Add
{
int x, y, z;
public:
Add(int, int);
void calculate(void);
void display(void);
};
Add :: Add(int a, int b)
{
x=a;
y=b;
}
void Add :: calculate()
{
z=x+y;
}
void Add :: display()
{
cout<<z;
}
void main()
{
36
7
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Copy Constructor
The constructor which takes reference to its own class as argument is known as copy constructor.
Program 2.6 Write a program to initialize two integer variables using parameterized
constructor. Copy given integers into a new object and add them.
Solution:
#include<iostream.h>
class Add
{
int x, y, z;
public:
Add()
{
}
Add(int a, int b)
{
x=a;
y=b;
37
8
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
}
Add(Add &);
void calculate(void);
void display(void);
};
Output:
Value of x and y for new object are 5 and 6
11
Note: Here in the above program when the statement Add a(5, 6); will execute (i.e. object creation), the
parameterized constructor Add (int, int) will be called automatically and value of x and y will be set to 5
and 6respectively. Now when the statement Add b(a) ; will execute, the copy constructor Add(Add& )
will be called and the content of object a will be copied into object b.
38
9
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Destructor
Solution:
#include<iostream.h>
class XYZ
{
int x;
public:
XYZ( );
~XYZ( );
void display(void);
};
XYZ::XYZ( )
{
x=9;
}
XYZ:: ~XYZ( )
{
cout<<”Object is destroyed”<<endl;
}
void XYZ::display()
{
cout<<x;
}
void main()
{
XYZ xyz;
xyz.display();
}
Output:
9
Object is destroyed.
39
10
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Inline function
In C++, we can create short functions that are not actually called, rather their code is expanded in line at
the point of each invocation. This process is similar to using a function-like macro. To cause a function
to be expanded in line rather than called, precede its definition with the inline keyword.
A function which is expanded in a line when it is called is called inline function.
It executes faster than other member function.
It can be recursive.
Its body does not contain if else, switch, loop, goto statement.
The inline keyword is preceded by function definition.
Program 2.8 Write a program to find area of a circle using inline function.
Solution:
#include<iostream.h>
inline float area(int);
void main()
{
int r;
cout<<“ Enter the Value of r: ”;
cin>>r;
cout<<” Area is: “ << area(r);
}
inline float area (int a)
{
return(3.14*a*a);
}
Output:
Enter the Value of r:
7
153.86
40
11
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Friend Function
Solution:
#include<iostream.h>
class Add
{
int x, y, z;
public:
Add(int, int);
friend int calculate(Add p);
};
Output:
11
Note: Here the function calculate () is called directly like normal function as it is declared as friend.
41
12
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Friend Classes
It is possible for one class to be a friend of another class. When this is the case, the friend class and all of
its member functions have access to the private members defined within the other class.
#include <iostream.h>
class TwoValues
{
int a;
int b;
public:
TwoValues(int i, int j)
{
a = i;
b = j;
}
friend class Min;
};
class Min
{
public:
int min(TwoValues x);
};
int Min::min(TwoValues x)
{
return x.a < x.b ? x.a : x.b;
}
int main()
{
TwoValues ob(10, 20);
Min m;
cout << m.min(ob);
return 0;
}
Output:
10
Note: In this example, class Min has access to the private variables a and b declared within the
TwoValues class.
42
13
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
The data member of a class preceded by the keyword static is known as static member.
When we precede a member variable's declaration with static, we are telling the compiler that
only one copy of that variable will exist and that all objects of the class will share that variable.
Hence static variables are called class variables.
Unlike regular data members, individual copies of a static member variable are not made for
each object. No matter how many objects of a class are created, only one copy of a static data
member exists. Thus, all objects of that class use that same variable.
All static variables are initialized to zero before the first object is created.
Normal data members are called object variable but static data members are called class
variables.
Solution:
#include<iostream.h>
class A
{
int p;
static int q;
public:
A();
void incr(void);
void display(void);
};
A :: A()
{
p=5;
}
int A:: q=10;
void A:: incr()
{
p++;
q++;
}
void A:: display()
{
cout<<p<<”\t”<<q<<endl;
}
void main()
43
14
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
{
A a1, a2, a3;
a1.incr();
a1.display();
a2.incr();
a2.display();
a3.incr();
a3.display();
}
Output:
6 11
6 12
6 13
Note: Here p is a normal variable, whose value is 5 for all 3 objects a1, a2 and a3 (For each object,
separate copy of p exists). But q is static variable or member, whose initial value is 10 and a single copy
of q exists for all the objects.
A static function can have access to only other static members (functions or variables) declared
in the same class. (Of course, global functions and data may be accessed by static member
functions.)
It is accessed by class name and not by object’s name i.e. class-name::function-name;
The function name is preceded by the keyword static.
A static member function does not have this pointer.
There cannot be a static and a non-static version of the same function.
A static member function may not be virtual.
Finally, they cannot be declared as const or volatile.
Solution:
#include<iostream.h>
class ABC
{
public:
static int add(int, int);
};
int ABC:: add(int a, int b)
{
44
15
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
return(a+b);
}
void main()
{
ABC abc;
int res;
res=ABC :: add(30, 40);
cout<<res;
}
Output:
70
45
16
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Assignment 2
46
17
Smartzworld.com jntuworldupdates.org
Smartworld.asia Specworld.in
Find Output:
1. #include<iostream.h> 3. #include<iostream.h>
int x=5; int x=5;
void main() void main()
{ {
int x=4; (5/2)? cout<<”Hi”:cout<<”Hello”;
cout<<x<<::x; }
}
4. #include<iostream.h>
2. #include<iostream.h> void main()
void main() {
{ int a, b;
int a=2, b=1; a=(b=7, b+2);
char x=1, y=0; cout<<a;
if(a, b, x, y) }
cout<<”Congratulations!!”;
}
47
18
Smartzworld.com jntuworldupdates.org