You are on page 1of 56

# 1

 CHAPTER NO 13
BASICS OF OBJECT ORIENTED PROGRAMMING
01- Write a program that declares a class with one integer data member and two member
function in () and out () and output data in data to input member.

#include<iostream>
#include<conio.h>
using namespace std;
class A
{
private:
int n;
public:
void in()
{
cout<<"Enter the number=";
cin>>n;
}
void out()
{
cout<<"The value of n="<<n;
}

};
main()
{
A ob;
ob.in();
ob.out();
getch();
}
02-write a class marks with three data member to store three marks. Write three member
function in () to input marks, sum () to calculate and return the sum and avg () to calculate
and return the average marks.

#include<iostream>
2

#include<conio.h>
using namespace std;
class marks
{
private:
float a,b,c;
float sum;
float avg;
public:
void in()
{

## cout<<"Enter the one number=";

cin>>a;
cout<<"Enter the 2nd number=";
cin>>b;
cout<<"Enter the 3rd numner=";
cin>>c;
}
float add()
{
sum=a+b+c;
cout<<"Result is="<<sum;
}
float average()
{
avg=sum/3;
cout<<"\n average is="<<avg;
}
};
main()
{
marks ob;
ob.in();
ob.add();
ob.average();
getch();
}
3

## Example: 2 number add:

#include<iostream>
#include<conio.h>
using namespace std;
class marks
{
private:
int a,b,sum;
public:
void dec()
{

## cout<<"Enter the one number=";

cin>>a;
cout<<"Enter the 2nd number=";
cin>>b;
}
void add()
{
sum=a+b;
cout<<"Result is="<<sum;
}
};
main()
{
marks ob;
ob.dec();
ob.add();
getch();
}
03-write a class circle with one data member radius. Write three member function get radius
() to set radius value with parameter value, area () to display radius and circum () to
Calculate and display circumference of circle.

#include<iostream>
#include<conio.h>
using namespace std;
4

class circle
{
private:
float r,a,c;

public:
float radius()
{
cout<<"The Radius is=";
cin>>r;
}
float area()
{
a=3.14*r*r;
cout<<"\n area is="<<a;
}
float circum()
{
c=2*3.14*r;
cout<<"\n circum is="<<c;
}
};
main()
{
circle Result;
Result.radius();
Result.area();
Result.circum();
getch();
}
04-write a class book with three data member BookiD, pages and Price.it also contains the
following member functions. The get () function is used to input value. The show () function is
used to display value. The () function is used to set the value of data members using
parameters. The get price () function is used to return the value of price.

#include<iostream>
#include<conio.h>
using namespace std;
5

class Book
{
private:
int bookid,pages,price;
char title;
public:
void booksid()
{
cout<<"Enter the bookid=";
cin>>bookid;
}
void page()
{
cout<<"Enter the page=";
cin>>pages;
}
void prices()
{
cout<<"Enter the price=";
cin>>price;
}
void titles()
{
cout<<"Enter the title=";
cin>>title;
}
void out()
{
cout<<"book id"<<bookid<<endl;
cout<<"pages is"<<pages<<endl;
cout<<"price is"<<price<<endl;
cout<<"title is"<<title<<endl;
}
};
main()
{
Book ob;
ob.booksid();
6

ob.page();
ob.prices();
ob.titles();
ob.out();
getch();
}
05-write a class Results that contains roll no, name and marks of three subjects. The marks
are store in an array of integers. The class also contains the following member functions. The
input () function is used the value of data members. The show () function to displays the
value of data members. The total () function returns the total marks of a student. The avg ()
function return the average marks of a student.
The program should create an object of the class and call the member function.

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Result
{
private:
int rolno,marks;
string name;
public:
void input()
{
cout<<"Enter the roll number=";
cin>>rolno;
cout<<"Enter the name=";
cin>>name;
for(int i=0;i<5;i++)
{
cout<<"Enter the marks=";
cin>>marks[i];
}
}
void show()
{
cout<<"\nEnter the roll number=";
7

cout<<rolno;
cout<<"\nEnter the name=";
cout<<name;
for(int i=0;i<5;i++)
{
cout<<"\nEnter the marks=";
cout<<marks[i]<<endl;
}
}
int total()
{ int a=0;
for(int i=0;i<5;i++)
a=a+marks[i];
cout<<"\nTotal Marks Are= "<<a;
return 0;

}
float average()
{ int a=0;
for(int i=0;i<5;i++)
a=a+marks[i];
cout<<"\nAverage is ="<<a/5;
return 0;
}
};
main()
{
Result ob;
ob.input();
ob.show();
ob.total();
ob.average();
getch();
}
06-write a class Array that contain an array of integers to store five values.it also contains the
following member functions. The fill () function is used to fill the array with value from the
user. The display () function is used to display the value of array. The max () and min ()
8

function show the maximum and minimum value in the array. All member function should be
defined outside the class.

#include<iostream>
#include<conio.h>
using namespace std;
class Array
{
private:

int a;
public:
void in();
void out();
void max();
void min();
};
void Array::in()
{
cout<<"Enter the numbers=";
for(int i=0;i<5;i++)
{
cin>>a[i];
}
}
void Array::out()
{
cout<<"you Enter value=";
for(int i=0;i<5;i++)
{
cout<<a[i];
}
}
void Array::max()
{
int m=a;
for(int i=0;i<5;i++)
if(m<a[i])
9

m=a[i];
cout<<"\nmiximum="<<m;
}
void Array::min()
{
int m=a;
for(int i=0;i<5;i++)
if(m>a[i])
m=a[i];
cout<<"\nminimum="<<m;
}
main()
{
Array ob;
ob.in();
ob.out();
ob.max();
ob.min();
getch();
}
07-write a class that displays a simple massage on the screen whenever an object of that class
is created.

#include<iostream>
#include<conio.h>
using namespace std;
class Hello
{
private:
int n;
public:
Hello()
{
cout<<"object created="<<endl;
}
};
main()
{
10

Hello ob1,ob2,ob3;
}
08- Example-Destructors/Destroys.

#include<iostream>
#include<conio.h>
using namespace std;
class Test
{
private:
int a;
public:
Test()
{
cout<<"objected created...."<<endl;

}
~Test()
{
cout<<"object destroyed..."<<endl;
}
};
main()
{
Test a,b;
}
08-write a class that contains two integer data member which are initialized to 100 when an
object is created.it has member function avg that displays the average of data members.

#include<iostream>
#include<conio.h>
using namespace std;
class number
{
private:
int a,b;
public:
void out()
11

{
cout<<"Enter the 1 number=";
cin>>a;
cout<<"Enter the 2 number=";
cin>>b;
}
number()
{
a=b=100;
cout<<"\n the number is="<<a<<","<<b;
}

};
main()
{
number ob;
}
09-write a class that has marks and grade as data members. A constructor with two
parameters initializes data members with the given values and member function show
displays the value of data member. Create two objects and displays the value.

#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
int marks;
char grade;
public:
student(int m,char g)
{
marks=m;
grade=g;
}
void out()
{
cout<<"marks="<<marks<<endl;
12

cout<<"grade="<<grade<<endl;
}
};
main()
{
student ob1(300,'A'),ob2(700,'B');
ob1.out();
ob2.out();
getch();
}
11-write a class that has num and ch as data members. A constructor with no parameter
initializes num to 0 and ch to ‘x’.A constructor with two parameter initializes data members
with the given values and member function show displays the value of data members.

#include<iostream>
#include<conio.h>
using namespace std;
class over
{
private:
int num;
char ch;
public:
over()
{
num=0;
ch='x';
}
over(int n, char c)
{
num=n;
ch=c;
}
void show()
{
cout<<"num="<<num<<endl;
cout<<"ch="<<ch<<endl;
}
13

};
main()
{
over first,second(100,'p');
cout<<"------------------------"<<endl;
cout<<"First content is"<<endl;
first.show();
cout<<"------------------------"<<endl;
cout<<"second content is"<<endl;
second.show();
cout<<"-------------------------"<<endl;
getch();
}
12-write a class Book that has attributes for pages, price and titles.it has two functions to
input the values and displays the values. Create three objects of the class and inputs values.

#include<iostream>
#include<conio.h>
using namespace std;
class book
{
private:
int pages,price;
char title;
public:
void in()
{
cout<<"Enter the pages=";
cin>>pages;
cout<<"Enter the title=";
cin>>title;
cout<<"Enter the prices=";
cin>>price;
}
void show()
{
14

cout<<"\npages is="<<pages<<endl;
cout<<"title is="<<title<<endl;
cout<<"price="<<price<<endl;

};
main()
{
book b1;
b1.in();
book b2(b1);
book b3=b1;
cout<<"-------------------------------"<<endl;
cout<<"\nThe content is b1"<<endl;
b1.show();
cout<<"-------------------------------"<<endl;
cout<<"\nThe content is b2"<<endl;
b2.show();
cout<<"-------------------------------"<<endl;
cout<<"\nThe content is b3"<<endl;
b3.show();
cout<<"-------------------------------"<<endl;
getch();
}
13-write a class Travel that has the attributes of kilometers and hours’ constructor with no
parameter initializes both data members to 0.A member function get () inputs the values and
function show () displays the values.it has a member function add () that takes an object of
type Travel to add the kilo meter and hours of calling object and the parameter.

#include<iostream>
#include<conio.h>
using namespace std;
class Travel
{
private:
int km,hr;
public:
15

Travel()
{
km=hr=0;
}
void get()
{
cout<<"Enter the kilometer=";
cin>>km;
cout<<"Enter the hours=";
cin>>hr;
}
void show()
{
cout<<"kilometer is="<<km<<endl;
cout<<"hours is="<<hr<<endl;
}
void add(Travel p)
{
Travel t;
t.km=km+p.km;
t.hr=hr+p.hr;
cout<<"\nTotal traveling="<<t.km;
cout<<"\nTotal hours="<<t.hr;

}
};
main()
{
Travel my,your;
my.get();
my.show();
your.get();
your.show();
cout<<"Total travel show as fellows"<<endl;
my.add(your);
getch();
}
16

14-write a class Travel that has the attributes of kilometers and hours’ constructor with no
parameter initializes both data members to 0.A member function get () inputs the values and
function show () displays the values.it has a member function add () that takes an object of
type Travel to add the kilo meter and hours of calling object and the parameter and returns
an object with added values.

#include<iostream>
#include<conio.h>
using namespace std;
class Travel
{
private:
int km,hr;
public:
Travel()
{
km=hr=0;
}
void get()
{
cout<<"Enter the kilometer=";
cin>>km;
cout<<"Enter the hours=";
cin>>hr;
}
void show()
{
cout<<"kilometer is="<<km<<endl;
cout<<"hours is="<<hr<<endl;
}
Travel add(Travel p)
{
Travel t;
t.km=km+p.km;
t.hr=hr+p.hr;
return t;
}
};
17

main()
{
Travel my,your,r;
my.get();
my.show();
your.get();
your.show();
r=my.add(your);
cout<<"Total trvel show as fellows"<<endl;
r.show();
getch();
}
15-write a program that counts the number of objects created of a particular class.
//Static data member function

#include<iostream>
#include<conio.h>
using namespace std;
class A
{
private:
static int n;
public:
A()
{
n++;
}
void show()
{
cout<<"\ncreated object"<<n;
}
};
int A::n=0;
main()
{
A ob1,ob2;
ob1.show();
A ob3;
18

ob1.show();
getch();
}
13.12-EXAMPLE NO 1: Static Function:

#include<iostream>
#include<conio.h>
using namespace std;
class Test
{
private:
static int n;
public:
static void show()
{
cout<<"N is"<<n<<endl;
}
};
int Test::n=10;
main()
{
Test::show();
getch();
}
13.19-Write a program that counts the number of objects created of particular
class. The program must be able to display the result even if no object is created
so far.

#include<iostream>
#include<conio.h>
using namespace std;
class Good
19

{
private:
static int n;
public:
Good()
{
n++;
}
static void show()
{
cout<<"created object"<<n<<endl;
cout<<"object is crated so far"<<endl;
}
};
int Good::n=0;
main()
{
Good::show();
Good x,y;
x.show();
Good z;
z.show();
getch();
}
13.16-write a program that create three objects of class student. Each object of
class must be assigned a unique roll number. (Hint: use static data member for
unique roll number.)

#include<iostream>
#include<conio.h>
using namespace std;
class student
{
20

private:
static int r;
int rollno,marks;
char name;
public:
student()
{
r++;
rollno=r;
}
void input()
{
cout<<"Enter the student name=";
cin>>name;
cout<<"Enter the student marks=";
cin>>marks;
}
void output()
{
cout<<"Roll no is="<<rollno<<endl;
cout<<"Name is="<<name<<endl;
cout<<"Marks is="<<marks<<endl;
}
};
int student::r=0;
main()
{
student s1,s2,s3;
s1.input();
s2.input();
s3.input();
s1.output();
s2.output();
s3.output();
getch();
}
17- A) CGPA program (write program on CGPA in array).
21

#include<iostream>
#include<conio.h>
using namespace std;
class CGPA
{
private:
float gp;
public:
void in();
float avg();
};
void CGPA::in()
{
for(int i=0;i<3;i++)
{
cout<<"Enter the 3 students CGPA=";
cin>>gp[i];
}
}
float CGPA::avg()
{
int sum=0;
for(int i=0;i<3;i++)
sum=sum+gp[i];
cout<<"Total avg is="<<sum/3<<endl;
return 0;
}
main()
{
CGPA ob;
ob.in();
ob.avg();
getch();
}
17- B) CGPA program (write program on CGPA in array).

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

## using namespace std;

class student
{
private:
float CGPA;
public:
void in();
void out();
float sum();
float avg();
};
void student::in()
{
cout<<"\nEnter the 3 student CGPA="<<endl;
for(int i=0 ; i<3 ; i++)
{
cin>>CGPA[i];
}
}
void student::out()
{
cout<<"\nEnter the 3 student CGPA="<<endl;
for(int i=0 ; i<3 ; i++)
{
cout<<CGPA[i];
}
}
float student::sum()
{
int sum=0;
for(int i=0 ; i<3 ; i++)
sum=sum+CGPA[i];
cout<<"\nsum is="<<sum<<endl;
return 0;
}
float student::avg()
{
int sum=0;
23

## for(int i=0 ; i<3 ; i++)

sum=sum+CGPA[i];
cout<<"\navrege is="<<sum/3;
return 0;
}
main()
{
student ob;
ob.in();
ob.out();
ob.sum();
ob.avg();
getch();
}
Friend Functions: EXAMPLE NO 1.

#include<iostream>
#include<conio.h>
using namespace std;
class B;
class A
{
private:
int a;
public:
A()
{
a=10;
}
friend void add(A,B);
};
class B
24

{
private:
int b;
public:
B()
{
b=20;
}
friend void add(A,B);
};
void add(A x,B y)
{
int t;
t=x.a+y.b;
cout<<"The value A object="<<x.a<<endl;
cout<<"The value B object="<<y.b<<endl;
cout<<"Sum of both value object="<<t<<endl;
}
int main()
{
A ob1;
B ob2;
add(ob1,ob2);
getch();
}
Friend classes: EXAMPLE NO 2.
#include<iostream>
#include<conio.h>
using namespace std;
25

class A
{
private:
int a,b;
public:
A()
{
a=10;
b=20;
}
friend class B;
};
class B
{
public:
void showA(A ob1)
{
cout<<"the value of a="<<ob1.a<<endl;
}
void showB(A ob1)
{
cout<<"the value of b="<<ob1.b<<endl;
}
};
main()
{
A x;
B y;
y.showA(x);
y.showB(x);
getch();
}
26

EXERCISE.13:
13.1-Write a class Player that contains attributes for the players name, average and team.
Write three functions to input, change and display these attributes. Also write a constructor
that asks for input to initialize all the attributes.

#include<iostream>
#include<conio.h>
using namespace std;
class player
{
private:
char team;
char Name;
float avg;
public:
player()

{
cout<<"Enter the player name=";
cin>>Name;
cout<<"Enter the player team name=";
cin>>team;
cout<<"Enter the player avg=";
cin>>avg;
}
void change()
{
cout<<"Enter the player name=";
cin>>Name;
cout<<"Enter the player team name=";
cin>>team;
cout<<"Enter the player avg=";
cin>>avg;
}
void show()
{
cout<<"player name="<<Name<<endl;
27

## cout<<"Player Team name="<<team<<endl;

cout<<"player avg="<<avg<<endl;
}};
main()
{
player ob;
ob.show();
cout<<endl;
ob.change();
cout<<"show...................."<<endl;
ob.show();
getch();
}

13.2-Define a class for a bank account that includes the following data members.
 Name of the depositor
 Account Number
 Type of account
 Balance amount in the account
The class also contains the following member functions:
 A constructor to assign initial value.
 Deposit function to deposit some amount.it should accept the amount as parameter.
 Withdraw function to withdraw an amount after checking the balance.it should accept
the amount as parameter.
 Display function to display name and balance.

#include<iostream>
#include<conio.h>
using namespace std;
class Bank
{
private:
char dn;
char an;
char ta;
float blnc,amnt,widrw;
public:
28

## Bank() //constructor for input

{
cout<<"Enter the depositor name =";
cin>>dn;
cout<<"Enter the account name =";
cin>>an;
cout<<"Enter the depositor type of account =";
cin>>ta;
cout<<"Enter the balance ammount =";
cin>>blnc;
}
void dpst(float a)
{

amnt=blnc+a;
}
void widw(float b)
{

if(b>amnt)
cout<<"your amount is less";
else
widrw=amnt-b;

}
void show()
{
cout<<"your name="<<dn<<endl;
cout<<"your balance after deposit and withdraw="<<widrw<<endl;

};
main()
{
Bank ob;
float dm,wm; //create object
cout<<"Total deposit amount="<<endl;
29

cin>>dm;
ob.dpst(dm);
cout<<"Total withdraw ammount="<<endl;
cin>>wm;
ob.widw(wm);
ob.show();
getch();
}
13.3-Create two classes DM and DB to store the value of distances.DM stores distances in
meters and centimeters and DB in feet and inchs.Write a program that can read values for
the class objects and add one object of DM with another object of DB.
Hint: Use friend functions.

#include<iostream>
#include<conio.h>
using namespace std;
class DB;
class DM
{
private:
float meters,centimeter;
public:
void input()
{
cout<<"Enter the meters=";
cin>>meters;
cout<<"Enter the centimeter=";
cin>>centimeter;
}
friend void add(DM,DB);
};
class DB
{
private:
float feet,inch;
public:
void input()
{
30

## cout<<"Enter the feet=";

cin>>feet;
cout<<"Enter the inch=";
cin>>inch;
}
friend void add(DM,DB);
};
void add(DM mt,DB ft)
{
float m,f;
m=mt.meters+mt.centimeter*0.01;
f=ft.feet+ft.inch/12;
cout<<"The total distance in meters="<<m<<endl;
cout<<"The total distance in feet="<<f<<endl;
}
int main()
{
DM a;
a.input();
DB b;
b.input();
add(a,b);
getch();
}
13.4-Write a class Run that contains the following data members:
 The name of the runner.
 The distance covered by a runner.
The class has the following member functions:
 Get function to input runner name and distance.
 Show function to display runner name and distance.
The user should be able to show the name of the runner who has covered the longest
distance at any point of time. Hint: use static data members:

#include<iostream>
#include<conio.h>
using namespace std;
class Run
31

{
private:
char name;
float distance;
public:
void input()
{
cout<<"Enetr the runnr name:";
cin>>name;
cout<<"Enetr the runner distance:";
cin>>distance;
}
void output()
{
cout<<"Runner Name is="<<name<<endl;
cout<<"Runner Distance is="<<distance<<endl;
}
};
main()
{
Run ob;
ob.input();
ob.output();
getch();
}
13.5-Write a class Car that contains the following attributes:
 The name of car.
 The direction of car (E, W, N, S).
 The positions of car (from imaginary zero point)
The class has the following member functions:
 A constructor to initialize the attributes.
 Turn function to change the direction of car to one step right side (e.g. if the
direction is to E, it should be changed to S and so on.)
 Overload the Turn function to change the direction to any side directly.it should
accept the direction as parameter.
 Move function to change the position of car away from zero point.it should accept
the distance as parameter.
32

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Car
{
private:
string name;
string dir;
int pos;
public:
Car(char n[], char d[], int p)
{
name=n;
dir=d;
pos=p;
}
void turn()
{
if(dir=="N")
dir="E";
else
if(dir=="E")
dir="S";
else
if(dir=="S")
dir="W";
else
dir="N";
}
void turn(char d)
{
dir=d;
}
void move(int p)
{
pos=p;
33

}
void show()
{
cout<<"Name is="<<name<<endl;
cout<<"Direction ="<<dir<<endl;
cout<<"position is="<<pos<<endl;
}
};
int main()
{
Car a("City","E",0);
a.turn();
a.turn('E');
a.move(500);
a.show();
getch();
}
34

CHAPTER NO 14
14.1-write a program that overloads increment operators to work with user-defined objects.

#include<iostream>
#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
void show()
{
cout<<"value is="<<n<<endl;
}
void operator ++()
{
n=n+1;
}

};
main()
{
count ob;
ob.show();
++ ob;
ob.show();
getch();
}
14.2-write a program that overloads increment operator to work with user – defined objects.
The overloaded function should return an object after increment the data member.

#include<iostream>
35

#include<conio.h>
using namespace std;
class count
{
private:
int n;
public:
count()
{
n=0;
}
void show()
{
cout<<"value n is="<<n<<endl;
}
count operator ++()
{
count temp;
n=n+1;
temp.n=n;
return temp;
}
};
main()
{
count x,y;
x.show();
y.show();
y=++x;
x.show();
y.show();
getch();
}
14.3-write a program that overloads postfix increment operator to work user-defined objects.

#include<iostream>
#include<conio.h>
using namespace std;
36

class count
{
private:
int a;
public:
count()
{
a=0;
}
void show()
{
cout<<"a="<<a<<endl;
}
count operator ++()
{
count temp;
a=a+1;
temp.a=a;
return temp;
}
count operator ++(int)
{
count temp;
a=a+1;
temp.a=a;
return temp;
}
};
main()
{
count x;
x.show();
++x;
x++;
x.show();
getch();
}
14.4-write a program that overloads binary addition operator +.
37

#include<iostream>
#include<conio.h>
using namespace std;
class add
{
private:
int a,b;
public:
add()
{
a=b=0;
}
void in()
{
cout<<"Enter the value a=";
cin>>a;
cout<<"Enter the value b=";
cin>>b;
}
void show()
{
cout<<"value a is="<<a<<endl;
cout<<"value b is="<<b<<endl;
}
add operator +(add p)
{
add temp;
temp.a=p.a+a;
temp.b=p.b+b;
return temp;

}
};
main()
{
add x,y,z;
x.in();
38

y.in();
z=x+y;
x.show();
y.show();
z.show();
getch();
}
14.5-Write a program that overloads arithmetic addition operator + for concatenating two
string values.

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class String
{
private:
char str;
public:
String()
{
str='\0';
}
void in()
{
cout<<"Enter the string value=";
cin>>str;
}
void show()
{
cout<<"string value="<<str<<endl;
}
String operator +(String s)
{
String temp;
strcpy(temp.str,str);
strcat(temp.str, s.str);
39

return temp;
}

};
main()
{
String s1,s2,s3;
s1.in();
s2.in();
cout<<"s1 =";
s1.show();
cout<<"s2 =";
s2.show();
cout<<"s3 =";
s3.show();
cout<<"your concatenating value is=";
s3=s1+s2;
cout<<"s3 =";
s3.show();
getch();
}
14.6-write a program that overloads the comparison operators == to work with string class.
The result of comparison must be 1 if two string are of same length and 0 otherwise.

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
class String
{
private:
char str;
public:
String()
{
str='\0';
}
40

void in()
{
cout<<"Enter the string=";
cin>>str;
}
void show()
{
cout<<str<<endl;
}
int operator==(String s)
{
if(strlen(s.str) == strlen(str))
return 1;
else
return 0;
}
};
main()
{
String s1,s2;
s1.in();
s2.in();
cout<<"s1";
s1.show();
cout<<"s2";
s2.show();
if(s1==s2)
cout<<" both string are equel"<<endl;
else
cout<<"both string are differ"<<endl;
getch();
}
14.7-write a program that overload arithmetic’s assignment operator to with use defined
objects.

#include<iostream>
#include<conio.h>
using namespace std;
41

class Read
{
private:
int days,pages;
public:
Read()
{
days=pages=0;
}
void in()
{
cout<<"how many you read days=";
cin>>days;
cout<<"how many you read pages=";
cin>>pages;
}
void show()
{
cout<<"Days is="<<days<<"\nPages is="<<pages<<endl;
}
Read operator +=(Read r)
{
days=days+r.days;
pages=pages+r.pages;
}
};
main()
{
Read r1,r2;
r1.in();
r2.in();
cout<<"-------------------------";
cout<<"\nRreading Number1...."<<endl;
r1.show();
cout<<"-------------------------";
cout<<"\nReading Number2...."<<endl;
r2.show();
cout<<"--------------------------";
42

## cout<<"\n Adding R2 to R1 using += operator...."<<endl;

r1+=r2;
cout<<"--------------------------";
cout<<"\nTotal Reading as follows.....:"<<endl;
r1.show();
getch();
}
43

EXERCISE NO 14:
01-Write a class Time that has three data member hour, minutes and seconds. The class has
the following member functions:
 Constructor to initialize the time.
 Show function to show the time.
 Overload ++ operator to increase the time by 1 minutes.
 Overload -- operator to decrease the time by 1 minutes.

#include<iostream>
#include<conio.h>
using namespace std;
class Time
{
private:
int h,m,s;
public:
Time()
{
h=m=s=0;
}
Time(int hh,int mm,int ss)
{
h=hh;
m=mm;
s=ss;
}
void in()
{
cout<<"Enter the hour=";
cin>>h;
cout<<"Enter the min=";
cin>>m;
cout<<"Enetr the second=";
cin>>s;
}
void show()
{
cout<<"\nhour is="<<h<<endl;
cout<<"\nmin is="<<m<<endl;
cout<<"\nsecond is="<<s<<endl;
44

}
void operator ++(int)
{
if(m==59)
{
m=0;
h++;
}
else
m++;
}
void operator --(int)
{
if(m==0)
{
m=59;
h--;
}
else
m--;
}
};
main()
{
Time x;
x.in();
x.show();
x++;
x.show();
x--;
x.show();
getch();
}
02-define a class for a bank account that include the following data members:
 Name of the depositor
 Account number
 Type of account
 Balance amount in the account
The class also contains the following member functions:
 A constructor to assign initial value.
45

 Deposit function to deposit some amount.it should accept the amount as parameter.
 Withdraw functions to withdraw the amount after checking the balance.it should
accept the amount as parameter.
 Display function to display name and balance.
 Overload binary + operator that adds the balance of one account to another account.it
should accept an object as parameter and add the values of the parameter to the
calling object.

03-Write a class Array that contains an array of five integers as data member. The class
contains the following member functions:
 A constructor that initialize the array elements to -1.
 Input function to input the value of the array.
 Show functions to display the value of the array.
 Overload== operator to compare the values of two objects. The overloaded functions
returns 1 if all value of both object are same and return 0 otherwise.

#include<iostream>
#include<conio.h>
#include<string>
#include<stdio.h>
using namespace std;
class Array
{
private:
int arr;
public:
Array();
void input();
void show();
int operator==(Array a);
};
Array::Array()
{
int i;
for(i=0 ; i<5 ;i++)
arr[i]=-1;
}
void Array::input()
46

{
int i;

## for(i=0 ; i<5 ;i++)

{
cout<<"Enter the integer:";
cin>>arr[i];
}
}
void Array::show()
{
int i;
cout<<"Array values:";
for(i=0 ; i<5 ;i++)
cout<<arr[i];
}
int Array::operator==(Array a)
{
int i,eq=1;
for(i=0 ; i<5 ;i++)
if(arr[i]!=a.arr[i])
eq=0;
return eq;
}
main()
{
Array x,y;
int n;
x.input();
y.input();
if(x==y)
cout<<"Both arrays are equel.";
else
cout<<"Both arrays are diff.";
getch();
}
47

CHAPTER NO 15
01=Example number 01:
15.1.2: Example number 1.position forward and backward:

#include<iostream>
#include<conio.h>
using namespace std;
class move
{
protected:
int position;
public:
move()
{
position=0;
}
void forword()
{
position++;
}
void show()
{
cout<<"position is="<<position<<endl;
}
};
class move2:public move
{
public:
void backword()
{
position--;
}
void show()
{
cout<<"position is="<<position<<endl;
}
48

};
main()
{
move2 m;
m.show();
m.forword();
m.show();
m.backword();
m.show();
getch();
}
02=Example number 02:
Accessing constructors of parent class.

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string>
using namespace std;
class Parent
{
protected:
int n;
string c;
public:
Parent()
{
n=0;
}
Parent(int p)
{
n=p;
}
void show()
49

{
cout<<"N is="<<n<<endl;
}
};
class Child:public Parent
{
private:
string ch;
public:
Child():Parent()
{
ch='x';
}
Child(int c,char m):Parent(m)
{
ch= c;
}
void display()
{
cout<<"CH is="<<ch<<endl;
}
};
main()
{
Child ob1,ob2('@',100);
cout<<"\nob1 is as follows....:"<<endl;
ob1.show();
ob1.display();
cout<<"\nob2 is as follows....:"<<endl;
ob2.show();
ob2.display();
getch();
}
50

15.3-write a class person that has the attributes of id, name and adress.it has a
constructor to initialize, a member function to input and a member function to
display data members. Create another class student that inherits person class.it
has additional attributes of roll number and marks.it also has member function
to inputs and display its data members.

#include<iostream>
#include<conio.h>
using namespace std;
class person
{
protected:
int id;
char name;
char address;
public:
person()
{
id=0;
name='\0';
address='\0';
}
vsoid input()
{
cout<<"Enter the name=";
cin>>name;
cout<<"Enter the address=";
cin>>address;
cout<<"Enter the id=";
cin>>id;
}
void output()
51

{
cout<<"\n your personal informations is as follows="<<endl;
cout<<"Enter the name="<<name<<endl;
cout<<"Enter the address="<<address<<endl;
cout<<"Enter the id="<<id<<endl;
}
};
class student:public person
{
private:
int rollno;
float marks;
public:
student()
{

rollno=marks=0;
}
void in()
{
cout<<"Enter the rollno=";
cin>>rollno;
cout<<"Enter the marks=";
cin>>marks;
}
void out()
{
cout<<"\n your additional informations is as follows="<<endl;
cout<<"Enter the rollno="<<rollno<<endl;
cout<<"Enter the marks="<<marks<<endl;
}
};
main()
52

{
student s;
s.input();
s.in();
s.output();
s.out();
getch();
}
15.4-Example: overriding function:

#include<iostream>
#include<conio.h>
using namespace std;
class parent
{
protected:
int n;
string c;
public:
parent()
{
n=0;
}
parent(int p)
{
n=p;
}
void show()
{
cout<<"N is="<<n<<endl;
}
};
53

## class child:public parent

{
private:
string ch;
public:
child():parent()
{
ch='x';
}
child(char c,int m):parent(m)
{
ch= c;
}
void display()
{
parent::show();
cout<<"CH is="<<ch<<endl;
}
};
main()
{
child ob1('@',100);
ob1.show();
getch();
}
15.4-write a program that declares two classes. The parent class is called simple
that has two data members a and b to store two numbers.it also has four
member functions:
The add () function adds two numbers and displays the result. The sub ()
function subtracts two numbers and displays the result. The mul() function
multiplies two numbers and displays the result. The div () function divides two
numbers and displays the result. The child class is called complex that overrides
54

all four functions. Each function in the child class checks the value of data
members.it calls the corresponding member function in the parent class if the
values are greater than 0.otherwise it displays error message.
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class simple
{
protected:
int a,b;
public:
simple()
{
a=b=0;
}
void in()
{
cout<<"Enter the value a=";
cin>>a;
cout<<"Enter the value b=";
cin>>b;
}
void add()
{
cout<<"a+b="<<a+b<<endl;
}
void sub()
{
cout<<"a-b="<<a-b<<endl;
}
void mul()
{
55

cout<<"a*b="<<a*b<<endl;
}
void div()
{
cout<<"a/b="<<a/b<<endl;
}
};
class complex:public simple
{
public:
void add()
{
if(a<=0||b<=0)
cout<<"invalid values."<<endl;
else
complex::add();
}
void sub()
{
if(a<=0||b<=0)
cout<<"invalid values."<<endl;
else
complex::sub();
}
void mul()
{
if(a<=0||b<=0)
cout<<"invalid values."<<endl;
else
complex::mul();
}
void div()
{
56

if(a<=0||b<=0)
cout<<"invalid values."<<endl;
else
complex::div();
}
};
int main()
{
simple ob;
ob.add();
ob.in();
ob.add();
ob.sub();
ob.mul();
ob.div();
getch();
return 0;
}