0% found this document useful (0 votes)
238 views

17ec442 PDF

The document is a lab manual submitted by student Samir Barot (enrollment no. 170080111003, ID no. 17EC442) for the subject Object Oriented Programming. It contains 14 programs demonstrating various C++ concepts like functions, structures, function overloading, call by value/reference etc. Each program has the code, input and output sections. The lab manual is certified by the lab in-charge and head of department.

Uploaded by

Yash Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views

17ec442 PDF

The document is a lab manual submitted by student Samir Barot (enrollment no. 170080111003, ID no. 17EC442) for the subject Object Oriented Programming. It contains 14 programs demonstrating various C++ concepts like functions, structures, function overloading, call by value/reference etc. Each program has the code, input and output sections. The lab manual is certified by the lab in-charge and head of department.

Uploaded by

Yash Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 162

17EC442

Lab Manual

Object Oriented Programming (EC-204)

Submitted to

Prof. Mayur Sevak

By

Samir Barot

Enrollment No: 170080111003

ID no: 17EC442

Birla Vishvakarma Mahavidyalaya


(An Autonomous Institution)

1|Page
17EC442

CERTIFICATE

This is to certify that Mr. Samir Barot of second year (3rd semester)
Enrollment No. 170080111003 and ID no. 17EC442 has completed the
practical work in the Subject Object Oriented Programming (EC- 204),
satisfactorily in the Department of Electronics and communication at the
end of month october– 2018.

lab In-charge Head of Department

(Prof. Mayur Sevak) (Dr. Bhargav Goradiya)

2|Page
17EC442

Program 1: Write a program to print hello world


on screen.

Program:

#include<iostream>
int main()
{
cout<<”Hello world”<<endl;
getch();
return 0;
}

Output:

Hello world

3|Page
17EC442

Program 2: Write a program to accept integers,


float and string.

Program:

#include<iostream>
int main()
{
int i;
char c[50];
string s;
cout<<”enter int ,char and string”<<endl;
cin>>i>>f>>s;
cout<<”your data is :”<<i<<f<<s<<endl;
return 0;
}

Output:

Enter int, char and string.


50
Bvm
Engineering college

4|Page
17EC442

Program 3: Write a program to find average of two


numbers.

Program:

#include<iostream>
int main()
{
int a,b,c;
cout<<”enter integer a, and b”<<endl;
cin>>a>>b;
c=(a+b)/2;
cout<<”your average is “<<c<<endl;
return 0;
}

Output:

Enter integer a and b


a=10
b=20
your average is
c=15

5|Page
17EC442

Program 4: Write a program to check whether number


is even or odd.

Program:

#include<iostream>
Int main()
{
Int a;
cout<<”enter value of a”<<endl;
cin>>a;
if (a%2==0)
{
cout<<”number is even”<<endl;
}
else
{
cout<<”the number is odd”<<endl;
}
return 0;
}

Output:

Enter value of a 10
the number is even

6|Page
17EC442

Program 5: Write a program to make a simple


calculator.

Program:

#include<iostream>
Int main()
{
char operator;
double num1,num2;
cout<<”enter an operator”<<endl;
cin>>operator;
cout<<”enter two operands”<<endl;
cin>>num1>>num2;
switch(operator)
{
case ‘+’ : cout<<num1+num2;
break;
case ‘-‘ : cout<<num1-num2;
break;
case ‘*’ : cout<<num1*num2;
break;
case ‘/’: cout<<num1/num2;
break;
default :
cout<<”wrong choice”<<endl;
break;
}
return 0;
}

7|Page
17EC442

Output:

enter operator either + or – or *


enter two opearands :
3.4
8.4
3.4-8.4 = -5.0

8|Page
17EC442

Program 6: write a program that uses a structure


to store three parts of phone number
seperately.

Program:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
struct student
{
int roll_no;
string name;
int phone_number;
};

struct student p1 = {1,"James",123443882};


struct student p2, p3, p4;
p2.roll_no = 2;
p2.name = "Sam";
p2.phone_number = 123;
p3.roll_no = 3;
p3.name = "Addy";
p3.phone_number = 443;
p4.roll_no = 4;
p4.name = "George";
p4.phone_number = 882;

cout << "First Student" << endl;


cout << "roll no : " << p1.roll_no << endl;

9|Page
17EC442

cout << "name : " << p1.name << endl;


cout << "phone no : " << p1.phone_number << endl;
cout << "Second Student" << endl;
cout << "roll no : " << p2.roll_no << endl;
cout << "name : " << p2.name << endl;
cout << "phone no : " << p2.phone_number << endl;
cout << "Third Student" << endl;
cout << "roll no : " << p3.roll_no << endl;
cout << "name : " << p3.name << endl;
cout << "phone no : " << p3.phone_number << endl;
cout << "Forth Student" << endl;
cout << "roll no : " << p4.roll_no << endl;
cout << "name : " << p4.name << endl;
cout << "phone no : " << p4.phone_number << endl;
return 0;
}

Output:

First Student
roll no : 1
name : Brown
phone no : 123443882
Second Student
roll no : 2
name : Sam
phone no : 123
Third Student
roll no : 3
name : Addy
phone no : 443
Fouth Student

10 | P a g e
17EC442

Program 7: write a program in C++ to implement


concept of refrence variable.

Program:

#include <iostream>
usingnamespacestd;

intmain()
{
inta=10;

/*reference variable is alias of other variable,


It does not take space in memory*/

int&b = a;

cout << endl << "Value of a: "<< a;


cout << endl << "Value of b: "<< b << endl;

return0;
}

Output:

Value of a:10
Value of b:10

11 | P a g e
17EC442

Program 8: Write a program in C++ to find sum of


digits of a number

Program:

#include<iostream.h>
#include<conio.h>
void main()
{
int a,no,sum=0;
clrscr();
cout<<"Enter any num : ";
cin>>no;
while(no>0)
{
a=no%10;
no=no/10;
sum=sum+a;
}
cout<<"\nSum of digits: "<<sum;
getch();
return 0;
}

Output:
Enter any number : 584
Sum of digits : 17

12 | P a g e
17EC442

Program 9: Write a program for function with default


arguments to find area of circle.

Program:

#include<iostream.h>
int main()
{
float radius,area,pi;
void display(void);
display();

cout<<"\n function with default parameters";


cout<<"\n enter the radius:"
cin>>radius;

float area(float,const float pi);

area=area(radius,pi=3.14);
cout<<"area of circle="<<area;

float area(float radius,const float pi=3.14)


{
float a=pi*radius*radius;
return(a);
}
void display(void)
{
cout<<"\n This is sample program for function"<<endl;
}
return 0;
}

13 | P a g e
17EC442

Output:

This is sample program for function


Function with default parameters
Enter the radius: 4
Area of circle = 50.240002

14 | P a g e
17EC442

Program 10: Write a program to illustrate the


concept of call by value and call by
reference by swapping of two numbers.

Program:

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

int main()
{
int a,b;
cout<<"Enter the values of a and b:"<<endl;
cin>>a>>b;
void swap(int,int);
void swap1(int &,int &);
cout<<"original values are:"<<endl;
swap(a,b);
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"After call by value a and b remains unchanged"<<endl;
swap1(a,b);
cout<<"a=:"<<a<<"b=:"<<b<<endl;
cout<<"after call by reference original a and b changed"<<endl;
getch();
}
void swap(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
}

15 | P a g e
17EC442

void swap1(int &x,int &y)


{
x=x+y;
y=x-y;
x=x-y;
}
Return 0;
}

Output:

Enter the values of a and b : 3 5


Original values are
a=3 b=5
After the call by value of a and b remains unchanged
a=: 3 b=:5

16 | P a g e
17EC442

Program 11: Write a program to perform the concept


of scope resolution operator when used
in a program.

Program:

#include <iostream>

using namespace std;

char c = 'a';

int main()
{
char c = 'b';

cout << "Local variable: " << c << "\n";


cout << "Global variable: " << ::c << "\n";
return 0;
}

Output:
Local variable: b
Global variable: a

17 | P a g e
17EC442

When used in a class:

Program:

#include <iostream>
using namespace std;

class programming
{
public:
void output();
};

void programming::output()
{
cout << "Function defined outside the class.\n";
}

int main()
{
programming x;
x.output();

return 0;
}

Output:

Function defined outside the class.

18 | P a g e
17EC442

Program 12: Write a program to implement concept


of function overloading.
(To calculate area )

Program:

#include<iostream>
Using namespace std;
float area(float,float,float);
float area(int);
int main()
{
cout<<area(10,20,30);
cout<<area(30);
Return 0;
}
float area(float l,float b,float h)
{
return(l*b*h);
}
float area(int r)
{
return(3.14*r*r);
}

Output:

6000
2826

19 | P a g e
17EC442

Program 13: Write a program to implement function


overloading in order to compute
power(m,n)
1)m is int and n is double
2)m and n are int.

Program:

#include<iostream>
Using namespace std;
int power(int,int);
double power(double, int);
int main()
{cout<<power(10,2)<<endl;
cout<<power(2.5,2)<<endl;
return 0;
}
Int power(int n,int m)
{
return (m^n);
}
double power(double n,int m);
{
return (m^n);
}

Output:

10^2
2.5^2

20 | P a g e
17EC442

Program 14:Write a program to demonstrate


declaration and initialization of
class.

Program:

#include<iostream>
using namespace std;
class item
{
int number;
float cost
public;
void getdata(int a, float b);
void putdata(void)
{
cout<<"number :"<<number<,endl;
cout<<"cost :"<<cost<endl;
}
void item :: getdata(int a, float b)
{
number = a;
cost = b;
}
int main()
{
Item x;
cout<<"\nobject x "<<endl;
x.getdata(100,299.95)
x.putdata();

21 | P a g e
17EC442

Item y;
cout<<"\nobject y"<<"\n";
y.getdata(200,175.50),
y.putdata();
return 0;
}

Output:

object x
number :100
cost 299.95

object y
number :200
cost :175.5

22 | P a g e
17EC442

Program 15:Write a program to demonstrate the


implementation of class and objects.

Program:

#include<iostream>
using namespace std;
class add
{
int a,b,c;
public:
void enter(void);
void summation(void);
void display(void);
};
void add :: enter(void)
{
cout<<"enter a"<<endl;
cin>>a;
cout<<"enter b"<<endl;
cin>>b;
}
void add :: summation(void)
{
c=a+b;
}
void add :: display(void)
{
cout<<"the addition is :"<<c;
}

23 | P a g e
17EC442

int main()
{
add a;
a.enter();
a.summation();
a.display();
return 0;
}

Output:

enter a
1
enter b
2
the addition is :3

24 | P a g e
17EC442

Program 16: write a program to find smallest


number from the two number with the
help of object and class.

Program:

#include<iostream>
using namespace std;
class set
{
int m,n;
public:
void input(void);
void display(void);
int samllest(void);
};
int set :: smallest(void)
{
if(m<=n)
return(m);
else
return(n);
}
void set :: input(void)
{
cout<<"input values of m and n"<<endl;
cin>>m>>n;
}
void set :: display(void)
{

25 | P a g e
17EC442

cout<<"samllest value ="<<smallest()<<"/n";


}
int main()
{
set A;
A.input();
A.display();
return();
}

Output:

input value of m and n


25 and 18
smallest value = 18

26 | P a g e
17EC442

Program 17: write a program to perform the


operation on string class.

Program:

#include<iostream>
#include<string>
using namespace std;
class string
{
char *name;
int length;
public:
string()
{
length=0;
name = new char[length + 1];
}
string(char *s)
{
length = strlen(s);
name = new char[length + 1];
strcpy(name, s);
}
void display(void)
{
cout<<name<<"\n";
}
void join(string &a, string &b);
};

27 | P a g e
17EC442

void string :: join(string &a, string &b)


{
length = a.length + b.length;
delete name;
name = new char[length + 1];
strcpy(name, a.name);
strcat(name, b.name);
}
int main()
{
char *first = "Joseph";
string name1(first),name2("Louis"),name3("Lagrange"),s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
return 0;
}

Output:

Joseph
Louis
Lagrange
Joseph Louis
Joseph Louis Lagrange

28 | P a g e
17EC442

Program 18: write a program in C++ to find a


number is Armstrong or not.

Program:

#include<iostream>
#include<math>
class armstrong
{
double a,digit;
public:
void getdata(void);
void arm(void);
};
void armstrong :: getdata()
{
cout<<"enter your digit of number"<<engl;
cin>>digit;
cout<<"enter the number"<<endl;
cin>>a;
}
void armstrong :: arm()
{
int n,d,sum=0;
n=a;
while(n!=0)
{
d=n%10;
n=n/10;
sum=sum+pow(d,digit);

29 | P a g e
17EC442

}
if(sum==a)
{
cout<<"number is armstrong."<<endl;
}
else
{
cout<<"number is not armstrong."<<endl;
}
}
int main()
{
armstrong A;
A.getdata();
A.arm();
return 0;
}

Output:

enter your digit of number:


3
enter the number:
153
number is armstrong.

30 | P a g e
17EC442

Program 19: write a program to implement


concept of manipulator SETW.

Program:

#include<iostream>
#include<siomanip>
using namespace std;
int main()
{
int Basic = 950, Allowance = 95, Total = 1045;
cout<<setw(10)<<"Basic"<<setw(10)<<Basic<<endl<<setw(10)<<"Allow
ance"<<setw(10)<<Allowance<<endl<<setw(10)<<"Total"<<setw(10)<<T
otal<<endl;
return 0;
}

Output:

Basic 950
Allowance 95
Total 1045

31 | P a g e
17EC442

Program 20: Write a program to implement concept


of explicit type casting.

Program:

#include<iostream>
using namespace std;
int main()
{
int p=25;
float q=35.56;
cout<<”p=”<<p<<endl;
cout<<”q=”<<q<<endl;
cout<<”p=”<<float(p)<<endl;
cout<<”q=”<<int(q)<<endl;
return 0;
}

Output:

p=25
q=35.56
p=25
q=35

32 | P a g e
17EC442

Program 21: Write a program to find


multiplication, division and cubic
values using inline function.

Program:

#include<iostream>
Using namespace std;
class line
{
public:
inline float mul(float p,float q)
{
return(p*q);
}
inline float div(float p,float q)
{
return(p\q);
}
inline float cube(float p,float q)
{
return(p*p*p);
}
};
int main()
{
line A;
float v1,v2;
cout<<”Enter two values:”<<endl;
cin>>v1,v2;
cout<<”Multiplication is:”<<A.mul(v1,v2)<<endl;

33 | P a g e
17EC442

cout<<”Division is:”<<A.div(v1,v2)<<endl;
cout<<”Cube is:”<<A.cubr(v1)<<endl;
return 0;
}

Output:

Enter two values:


5
2
Multiplication is:10
Division is:2.5
Cube is:125

34 | P a g e
17EC442

Program 22: Write a program in C++ for calculating


the factorial of given number
( Funtion Recursion)

Program:

#include<iostream>
using namespace std;
double factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
int main()
{
int n;
cout<<”Enter the value of n:”<<endl;
cin>>n;
cout<<”Factorial is:”<<factorial(n);
return 0;
}

Output:

Enter the value of n:


4
Factorial is:24

35 | P a g e
17EC442

Program 23: Write a program to implement concept


of math functions.

Program:

#include<iostream>
#include<math.h>
Using namespace std;
int main()
{
cout<<”sin(100):”<<sin(100.00)<<endl;
cout<<”cos(100):<<cos(100.00)<<endl;
cout<<”tan(100):”<<tan(100.00)<<endl;
cout<<”7 to the power of 6:”<<pow(7.0,6.0)<<endl;
cout<<”log10(10):”<<log10(10.00)<<endl;
cout<<”Square root of 2:”<<sqrt(2.00)<<endl;
return 0;
}

Output:

sin(100):-0.506366
cos(100):0.862319
tan(100):-0.587214
7 to the power of 6:117649
Log10(10):1
Square root of 2:1.4142

36 | P a g e
17EC442

Program 24: Write a program to implement nesting


of member using class to find largest
value among the two numbers.

Program:

#include<iostream>
using namespace std;
class set
{
int a,b;
public:
void getdata(void);
void display(void);
int largest(void);
};
void set :: getdata(void)
{
cout<<"Input values of a and b:" << "\n";
cin >> a >>b;
}
int set :: largest(void)
{
if(a>b)
return (a);
else
return(b);
}
void set :: display(void)
{

37 | P a g e
17EC442

cout<<"Largest value ="<< largest( ) << "\n";


}

int main( )
{
set A;
A.getdata( );
A.display( );
return 0;
}

Output:

input values of a and b:


3 4
Largest value=4

38 | P a g e
17EC442

Program 25: Write a program to implement nesting


of member function using class to
find 1’s complement of given number.

Program:

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

class binary
{
string s;
public:
void read(void)
{
cout<<”enter a binary number:”;
cin>>s;
}
void chk_bin(void)
{
for(int i=0;i<s.length();i++)
{
if(s.at(i)!=’0’ && s.at(i)!=’1’)
{
cout<<”\nincorrect binary number format…the program will
quit”;
getch();
exit(0);
}

39 | P a g e
17EC442

}
}
void ones(void)
{
chk_bin();
for(int i=0;i<s.length();i++)
{
if(s.at(i)==’0’)
s.at(i)=’1’;
else
s.at(i)=’0’;
}
}
void displayones(void)
{
ones();
cout<<”\n the 1‘s compliment of the above binary number
is:”<<s;
}
};
int main()
{
binary b;
b.read();
b.displayones();
getch();
return 0;
}

40 | P a g e
17EC442

Output:

Enter a binary number:110101


the 1’s compliment of the above binary number is:001010
Enter a binary number:1101210
Incorrect binary number format… the program will quit

41 | P a g e
17EC442

Program 26: WAP to implement the process of


shopping list.

Program:

#include<iostream>
using namespace std;
const tint m=50;
class ITEMS
{
int itemcode[m];
float itemPrice[m];
int count;
public:
void CNT(void){count=0;}
void getitem(void);
void displaySum(void);
void remove(void);
void displayItems(void);
};
void ITEMS:: getitem(void)
{
cout<<”Enter item code :”;
cin>>item Code[count];
cout<<”Enter item cost :”;
cin>> itemPrice[count];
count++
}
void ITEMS:: displaySum(void)
{

42 | P a g e
17EC442

float sum=0;
for(int i=0;i<count;i++)
sum=sum+itemPrice[i];
cout<<”\nTotal value:”<<sum<<”\n”;
}
void ITEMS::remove(void)
{
int a;
cout<<”enter item code:”;
cin>>a;
for(int i=0;i<count;i++)
if(itemcode[i]==a)
itemPrice[i]=0;
}
void ITEMS:: displayItems(void)
{
cout<<”\ncode Price\n”;
for{int i=0;i<count;i++)
{
cout<<”\n”<<itemcode[i];
cout<<” “<<itemPrice[i];
}
cout<<”n”;
}
int main()
{
ITEM order;
order.CNT();
int x;
do
{

43 | P a g e
17EC442

cout<<”\nYou can do the following;”;


cout<<”enter appropriate number \n”;
cout<<”\n1 : add an item”;
cout<<”\n2 : display total value”;
cout<<”\n3 : delete an item”;
cout<<”\n4 : display all items”;
cout<<”\n5 : Quit”;
cout<<”\n\nwhat is your option?”;

cin>>x;
switch(x)
{
case 1: order.getitem(); break;
case 2: order.displaySum();break;
case 3: order.remove();break;
case 4: order.displayItems();break;
case 5:break;
default :cout<<”error in iput; try again\n”;
}
} while(x!=5);
return 0;
}

Output:

You can do the following Enter appropriate number


1 : add an item
2 : display total value
3 : delete an item
4 : display all items

44 | P a g e
17EC442

5 : Quit

What is your option?1


Enter item code :111
Enter item cost :100

You can do the following Enter appropriate number


1 : add an item
2 : display total value
3 : delete an item
4 : display all items
5 : Quit

What is your option?1


Enter item code :222
Enter item cost :200

You can do the following Enter appropriate number


1 : add an item
2 : display total value
3 : delete an item
4 : display all items
5 : Quit

What is your option?1


Enter item code :333
Enter item cost :300

You can do the following Enter appropriate number


1 : add an item
2 : display total value

45 | P a g e
17EC442

3 : delete an item
4 : display all items
5 : Quit

What is your option?2


Total value :600

You can do the following Enter appropriate number


1 : add an item
2 : display total value
3 : delete an item
4 : display all items
5 : Quit

What is your option?3


Enter item code :222

You can do the following Enter appropriate number


1 : add an item
2 : display total value
3 : delete an item
4 : display all items
5 : Quit

What is your option?4


Code Price
111 100
222 0
333 300

46 | P a g e
17EC442

Program 27: Write a program to implement the


concept of static class member.

Program:

#include <iostream>
using namespace std;
class item
{
static int count;
int num;
public:
void getdata(int a)
{
num=a;
count++;
}
void getcount(void)
{
cout<<"read :";
cout<< count <<"\n";
}
};

int item::count;
int main()
{
item a,b,c;
a.getcount();
b.getcount();

47 | P a g e
17EC442

c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"After reading the data"<<"\n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}

Output:

read :0
read :0
read :0
After reading the data
read :3
read :3
read :3

48 | P a g e
17EC442

Program 28: Write a program to implement the


concept of static member functions.

Program:

#include<iostream>
using namespace std;
class test
{
int code;
static int count;
public:
void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number: "<<code<<"\n";
}

static void showcount(void)


{
cout<<"count:"<<count<<"\n";
}
};
int test::count;
int main()
{
test t1,t2;

49 | P a g e
17EC442

t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}

Output:

count:2
count:3
object number: 1
object number: 2
object number: 3

50 | P a g e
17EC442

Program 29: Write a program to implement the


concept of arrays of objects.

Program:

#include<iostream>
using namespace std;
class employee
{
char name[30];
float age;
public:
void getdata(vid);
void putdata(void);
};
void employee::getdata(void)
{
cout<<"Enter name: ";
cin>>name;
cout<<"Enter age: ";
cin>>age;
}
void employee::putdata(void)
{
cout<<"Name: "<<name<<"\n";
cout<<"Age: "<<age<<"\n";
}
const int size=3;
int main()
{

51 | P a g e
17EC442

employee manager[size];
for(int i=0;i<size;i++)
{
cout<<"\nDetails of manager"<<i+1<<"\n";manager[i].getdata();
}
cout<<"\n";
for(int i=0;i<size;i++)
{
cout<<"\nManager"<<i+1<<"\n";
manager[i].putdata();
}
return 0;
}

Output:

Details of manager1
Enter name: manager1
Enter age: 30
Details of manager2
Enter name: manager2
Enter age: 35
Details of manager3
Enter name: manger3
Enter age: 40

Manager1
Name: manager1
Age: 30

52 | P a g e
17EC442

Manager2
Name: manager2
Age: 35

Manager3
Name: manger3
Age: 40

53 | P a g e
17EC442

Program30: Write a program to perform the addition


of two time objects using objects as
arguments.

Program:

#include<iostream>
using namespace std;
class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{ hours=h; minutes=m; }
void puttime(void)
{ cout<<hours<< "hours and ";
cout<<minutes<< "minutes"<<"\n";
}
void sum(time,time);
};
void time::sum(time t1,time t2)
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
int main()
{

54 | P a g e
17EC442

time k1,k2,k3;
k1.gettime(2,45);
k2.gettime(3,30);
k3.sum(k1,k2);
cout<<"K1="; k1.puttime();
cout<<"K2="; k2.puttime();
cout<<"K3="; k3.puttime();

return 0;
}

Output:

K1=2hours and 45minutes


K2=3hours and 30minutes
K3=6hours and 15minutes

55 | P a g e
17EC442

Program 31: Write a program to find mean value of


a given numbers using friends
functions.

Program:

#include<iostream>
using namespace std;
class sample
{
int a;
int b;
public:
void setvalue() {a=40;b=25;}
friend float mean(sample a);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main() {
sample x;
x.setvalue();
cout<<"Mean value"<<mean(x)<<"\n";
return 0;
}

Output:
Mean value = 32.5

56 | P a g e
17EC442

Program 32: Write a program to add data objects


of two different classes using friend
functions.

Program:

#include<iostream>
#include<conio.h>
using namespace std;
class matrix
{
int m[3][3];
public:
void read(void)
{
cout<<"Enter the elements of matrix: \n";
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cout<<"m["<<i<<"]["<<j<<"]=";
cin>>m[i][j];
}
}
void display(void)
{
int i,j;
for(i=0;i<3;i++)
{
cout<<"\n";

57 | P a g e
17EC442

for(j=0;j<3;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
friend matrix trans(matrix);
};
matrix trans(matrix m1)
{
matrix m2;
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m2.m[i][j]=m1.m[j][i];
return(m2);
}
int main()
{
matrix mat1,mat2;
mat1.read();
cout<<"\nYou have entered the following matrix: ";
mat1.display();
mat2=trans(mat1);
cout<<"\nTransposed matrix:";
mat2.display();
getch();
return 0;
}

58 | P a g e
17EC442

Output:

Enter the element of the matrix:


m[0][0] = 1
m[0][1] = 2
m[0][2] = 3
m[1][0] = 4
m[1][1] = 5
m[1][2] = 6
m[2][0] = 7
m[2][1] = 8
m[2][2] = 9

you entered the following mtrix:


1 2 3
4 5 6
7 8 9

Transposed matrix:
1 5 7
2 5 8
3 6 9

59 | P a g e
17EC442

Program 33: Write a program to add two complex


numbers by the concept of returning
object.

Program:

#include<iostream>
using namespace std;
class complex
{
float x, y;
public:
complex(){}
complex(float a) {x=y=a;}
complex(float real, float imag)
{x=real;y=imag;}
friend complex sum(complex, complex);
friend void show(complex);
};
complex sum(complex c1, complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void show(complex c)
{
cout<<c.x<<"+j"<<c.y<<"\n";
}

60 | P a g e
17EC442

int main()
{
complex a(2.7,3.5);
complex b(1.6);
complex c;

c=sum(a,b);
cout<<"A="; show(a);
cout<<"B="; show(b);
cout<<"C="; show(c);

return 0;
}

Output:

A = 2.7 + j3.5
B = 1.6 + j1.6
C = 4.1 + j6.4

61 | P a g e
17EC442

Program 34: Write a program to implement friend


function to two classes.

Program:
#include<iostream.h>
Using namespace std;

class ABC;
class XYZ;
{
int x;
public:
void setvalue(int i) {x=i;}
friend void max(XYZ,ABC);
};
Class ABC
{
int a;
public:
void setvalue(int i) {a=i;)
friend void max(XYZ,ABC);
};
Void max(XYZ m,ABC n)
{
if m.x>=n.a)
cout<<m.x;
else
cout<<n.a;
}
int main()

62 | P a g e
17EC442

{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz,abc);
return 0;
}

Output:

20

63 | P a g e
17EC442

Program 35: Write a program to swap private data


of two classes.

Program:

#include<iostream>
using namespace std;
class class2;
class class1
{
int value1;
public:
void indata(int a) {value1=a;}
void display(void) {cout<<value1<<"\n";}
friend void exchange(class1&, class2&);
};
class class2
{
int value2;
public:
void indata(int a) {value2=a;}
void display(void) {cout<<value2<<"\n";}
friend void exchange(class1&, class2&);
};

void exchange(class1&x, class2&y)


{
int temp=x.value1;
x.value1=y.value2;
y.value2=temp;

64 | P a g e
17EC442

}
int main()
{
class1 c1;
class2 c2;
c1.indata(100);
c2.indata(200);

cout<<"Values before exchange" <<"\n";


c1.display();
c2.display();
exchange(c1,c2);
cout<<"Values after exchange"<<"\n";
c1.display();
c2.display();
return 0;
}

Output:

Values before exchange


100
200
Values after exchange
200
100

65 | P a g e
17EC442

66 | P a g e
17EC442

Program 36: Write a program to transpose a 3x3


matrix by the concept of returning
objects.

Program:

#include<iostream>
using namespace std;
class MATRIX
{
int a[5][5];
int row,col;
public:
MATRIX(int x,int y)
{
row=x;
col=y;
}
void read();
void display();
void transpose();
};
void MATRIX::read()
{
cout<<"\n\nEnter elements of matrix:\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cin>>a[i][j];
}
}
void MATRIX::display()

67 | P a g e
17EC442

{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<a[i][j]<<" ";
cout<<"\n";
}
}
void MATRIX::transpose()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<a[j][i]<<" ";
cout<<"\n";333
}
}
int main()
{
int m,n;
cout<<"Enter order of matrix: ";
cin>>m>>n;
MATRIX obj1(m,n);
obj1.read();
cout<<"Matrix is…\n";
obj1.display();
cout<<"Transpose of matrix is…\n";
obj1.transpose();
return 0;
}

68 | P a g e
17EC442

Output:

Enter order of matrix : 3 3

Enter elements of matrix:


1 2 3
4 5 6
7 8 9

Matrix is
1 2 3
4 5 6
7 8 9

Transpose of matrix is:


1 4 7
2 5 8
3 6 9

69 | P a g e
17EC442

Program 37: Write a program in C++ to add two


numbers using dereferencing
operators.

Program:

#include<iostream>
using namespace std;
class M
{
int x;
int y;
public:
void set_xy(int a, int b)
{
x=a;
y=b;
}
friend int sum(M m);
};
int sum(M m)
{
int M::*px = &M :: x;
int M::*py = &M :: y;
M *pm = &m;
int S = m.*px + pm->*py;
return S;
}
int main()
{
M n;
void (M :: *pf) (int,int) = &M :: set_xy;

70 | P a g e
17EC442

(n.*pf) (10,20);
cout<<"Sum = "<<sum(n) <<"\n";
M *op =&n;
(op->*pf) (30,40);
cout<<"\n\n Sum ="<<sum(n)<<"\n";
return 0;
}

Output:

Sum = 30
Sum = 70

71 | P a g e
17EC442

Program 38: Write a program in C++ to implement


banking system.

Program:

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class bank
{
int ac;
static int reset;
float balance, amount, shorta;
public:
void deposit();
void withdraw();
void chkbalance();
int menu();
};
int bank::reset=0;
void bank::deposit()
{
cout<<"\nEnter Account Number:";
cin>>ac;
if(ac==1001)
{
cout<<"\nEnter Amount you want to Deposit:";
cin>>amount;
balance=balance+amount;

72 | P a g e
17EC442

cout<<"\n\n Message: You have successfully deposited


Rs."<<amount<<" into your account.\n Your Current Balance
is:"<<balance<<"\n\n";
reset++;
}
else
{
cout<<"You have entered invalid account number";
}
}
void bank::withdraw()
{
cout<<"Enter Account Number:";
cin>>ac;
if(ac==1001)
{
cout<<"Enter Amount you want to Withdraw:";
cin>>amount;
if(amount>balance)
{
shorta=amount-balance;
cout<<"\n\nYou cannot withdraw Rs."<<amount<<" as
your account balance is Rs."<<balance<<"\n\nYou are short of
Rs."<<shorta;
}
else
{
balance=balance-amount;
cout<<"You have Withdrawn Rs."<<amount<<"
Successfully\n\n Your account balance is:"<<balance<<"\n\n";
}
}
else
{

73 | P a g e
17EC442

cout<<"You have entered invalid account number";


}
}
void bank::chkbalance()
{
cout<<"Enter Account Number:";
cin>>ac;
if(reset==0)
{
balance=0;
}
if(ac==1001)
{
cout<<"Your Account balance is Rs."<<balance<<"\n\n";
}
else
{
cout<<"Account Doesn't Exist!!";
}
}
int main()
{
bank ob;
int ch;
do {
cout<<"\n\n\n1.Deposit\n2.Withdraw\n3.Balance
Enquiry\n4.Exit\n\n";
ch=ob.menu();
switch(ch)
{
case 1:ob.deposit();
break;
case 2:ob.withdraw();

74 | P a g e
17EC442

break;
case 3:ob.chkbalance();
break;
case 4: exit(1);
default:cout<<"Invalid Choice!!";
}
}while(1);
return 0;
}
int bank::menu()
{int ch;
cout<<"\nEnter your Choice:";cin>>ch;
return ch;
}

Output:

1.deposit
2.withdraw
3.balance inquiry
4.exit

enter your choice: 2


enter account number: 1001
enter amount you want to deposit: 1500
message: you have successfully deposited rs.1500 into yours
account
your account balance is: 1000

75 | P a g e
17EC442

Program 39: write a program to implement the


concept of constructor and
destructor.
Program:
#include <iostream>
#include<conio.h>
using namespace std;
class MyClass
{
public:
int x;
MyClass();
~MyClass();
};
MyClass::MyClass()
{
x = 10;
}
MyClass::~MyClass()
{
cout<< "Destructing ...\n";
}
int main()
{
MyClass ob1;
MyClass ob2;
cout <<ob1.x<< " " <<ob2.x<<"\n";
getch();
return 0;
}
Output:
10 10

76 | P a g e
17EC442

Program 40: Write a program to check given


number is prime or not.

Program:

#include<iostream>
using namespace std;
class prime
{
int n;
public:
prime()
{
cout<<"enter number"<<endl;
cin>>n;
}
void check()
{
int i,c=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
c++;
break;
}
}
if(c==0)
cout<<"prime";
else
cout<<"not prime";
}
};
int main()
{
prime p;
p.check();
return 0;7
}

Output:
enter number
11
Number is prime

77 | P a g e
17EC442

Program 41: write a program to calculate the


factorial of a given number.
Program:

#include<iostream>
using namespace std;
class fact
{
int n;
public:
fact()
{
cout<<"enter num:"<<endl;
cin>>n;
}
fact(fact &f1)
{
n=f1.n;
}
void cal()
{
int i,ans=1;
for(i=1;i<=n;i++)
{
ans=ans*i;
}
cout<<"fact is"<<ans<<endl;
}
};
int main()
{
fact f1;
fact f2(f1);
f2.cal();
return 0;
}

Output:
Enter number :4
Factorial is :24

78 | P a g e
17EC442

Program 42: Write a program to implement concept


of Overloaded constructor.
Program:

#include<iostream.h>
using namespace std;
class complex
{
float x,y;
public:
complex()
{}
complex(float a)
{
x=y=a
}
complex(float real , float imag)
{
x=real;
y=imag;
}
friend complex sum(complex , complex);
friemd void show(complex);
};
complex sum(complex c1 , complex c2)
{
complex c3;
c3.x= c1.x + c2.x;
c3.y= c3.y + c3.y;
return c3;
}
void show(complex c)
{
cout<<c.x<<”+j”<<c.y<<endl;
}
int main()
{
complex A(2.7 , 3.5);
complex B(1.6);
complex C;
C=sum(A,b);
Cout<<”A=”<<show(A)<<endl;
Cout<<”B=”<<show(B)<<endl;
Cout<<”C=”<<show(C)<<endl;
Return 0;
}

79 | P a g e
17EC442

Output:
A=2.7 + j3.5
B=1.6 + j1.6
C=4.3 + j5.1

80 | P a g e
17EC442

Program 43: Write a program to implement dynamic


initialization of constructors.

Program:

#include<iostream>
using namespace std;
class fixed_deposit
{
long int p_amount;
int years;
float rate;
float R_value;
public:
fixed_deposit()
{}
fixed_deposit(long int p,int y,float r=0.22);
fixed_deposit(long int p,int y,int r);
void display(void);
};
fixed_deposit :: fixed_deposit(long int p,int y,int r)
{
p_amount =p;
years =y;
rate =r;
R_value = p_amount;
for(int i=0;i<=y;i++)
R_valur = R_value*(1.0+float(t)/100);
}
void fixed_deposit :: display(void)
{
cout<<”principal amount=”<<P_amount<<endl;
cout<<”return value=”<<R_value<<endl;
}
int main()
{
fixed_deposit fd1,fd2,fd3;
long int p;
int y;
float r;
int R;
cout<<”enter the amount,period,rate(%)”<<endl;
cin>>p>>y>>R;
fd1= fixed_deposit(p,y,R);
cout<<”enter the amount,period,rate”<<endl;
cin>>p>>y>>r;
fd2= fixed_deposit(p,y,r);
cout<<”enter the amount,period”<<endl;
cin>>p>>y;
fd3= fixed_deposit(p,y);

81 | P a g e
17EC442

cout<<”deposite 1”<<endl;
fd1.display();
cout<<”deposite 2”<<endl;
fd2.display();
cout<<”deposite 3”<<endl;
fd3.display();
return 0;
}

Output:
Enter the amount,period,rate(%)
1000 3 18
Enter the amount,period,rate
1000 3 0.18
Enter the amount,period
1000 3
Deposite 1
Principal amount : 10000
Return value : 16430.3
Deposite 2
Principal amount : 10000
Return value : 16430.3
Deposite 3
Principal amount : 10000
Return value : 14049.3

82 | P a g e
17EC442

Program 44: Write a program to implement concept


of copy constructor.

Program:
#include<iostream>
using namespace std;
class code
{
int id;
public:
code()
{ }
code(int a)
{
id=a;
}
code(code & x)
{
cout<<id;
}
};
int main()
{
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<endl<<”id of A:”;A.display();
cout<<endl<<”id of B:”;B.display();
cout<<endl<<”id of C:”;C.display();
cout<<endl<<”id of D:”;D.display();
return 0;
}

Output:
id of A:100
id of B:100
id of C:100
id of D:100

83 | P a g e
17EC442

Program 45: Write a program to construct strings


in objects using dynamic
constructor.

Program:

#include<string>
#include<iostream>
using namespace std;
class string
{
char *name;
int length;
public:
string()
{
length=0;
name=new char[length+1];
}
string(char *s)
{
length=strlen(S);
name=new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<name<<endl;
}
void join(string &a, string &b);
};
void string::join(string &a, string &b)
{
length=a.length + b.length;
delete name;
name= new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
int main()

84 | P a g e
17EC442

{
char *first=”Joseph “;
string name1(first), name2(“Louis “), name3(“Lagrange
“), s1, s2;
s1.join(name1, name2);
s2.join(s1, name3);
name1.display();
name2.display();

name3.display();
s1.display();
s2.display();
return 0;
}

Output:

Joseph
Louis
Joseph Louis
Joseph Louis Lagrange

85 | P a g e
17EC442

Program 46: Write a program to implement concept


of unary operator overloading.

Program:
#include<iostream>
using namespace std;
class space
{
int x, y, z;
public:
void getdata(int a, int b, int c);
void display();
void operator –();
};
void space: : getdata(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space: : display()
{
cout<< x<<” “<<y<<” “<<z<<” “<<endl;
}

void space: :operator –()


{
x=-x;
y=-y;
z=-z;
}
int main()
{
space S;
S.getdata(10, -20, 30);
cout<<”S : “;
S.display();
-S;
cout<<”S : “;
S.display();

return 0;
}

86 | P a g e
17EC442

Output:
S : 10 -20 30
S : -10 20 -30

87 | P a g e
17EC442

Program 47: write a program to implement the


concept of unary operator using
friend function.

Program:

#include<iostream>
using namespace std;
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display(void);
friend void operator –(space &s);
};
void space::getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void space::display(viod)
{
cout<<”x=”<<x<<” “;
cout<<”y=”<<y<<” “;
cout <<”z=”<<z<<endl;
}

88 | P a g e
17EC442

void operator-(space &s)


{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
Int main();
{
space S;
S.getdata(10,-20,30);
cout<<”S:”;
S.display();
-S;
cout<<”-S:”;
S.diaplay();
return 0;
}

Output :

S:x=10 y=-20 z=30


-S:x=-10 y=20 z=-30

89 | P a g e
17EC442

Program 48: write a program to implement concept


of binary operator overloading.

Program:

#include<iostream>
using namespace std;
class complex;
{
float x,y;
public:
complex(){}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(viod);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(viod)
{
cout<<x<<”+j”<<y<<endl;

90 | P a g e
17EC442

}
int main()
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<”c1=”;c1.display();
cout<<”c2=”;c2.display();
cout<<”c3=”;c3.display();
return 0;
}

Output:

c1=2.5+j3.5
c2=1.6+j2.7
c3=4.1+j6.2

91 | P a g e
17EC442

Program 49: write a program to implement concept


of binary operator overloading using
friend function.

Program:

#include<iostream>
using namespace std;
class complex;
{
float x,y;
public:
complex(){}
complex(float real,float imag)
{
x=real;
y=imag;
}
friend complex operator+(complex,complex);
void display(viod);
};
complex operator+(complex a,complex b)
{
return complex((a.x+b.x),(a.y+b.y));
}
void complex::display(viod)
{
cout<<x<<”+j”<<y<<endl;
}
int main()

92 | P a g e
17EC442

{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=operator+(c1+c2);
cout<<”c1=”;c1.display();
cout<<”c2=”;c2.display();
cout<<”c3=”;c3.display();
return 0;
}

Output:

c1=2.5+j3.5
c2=1.6+j2.7
c3=4.1+j6.2

93 | P a g e
17EC442

Program50: Write a program to for multiplication


of vector using operator
overloading with friend function.

Program:

#include<iostream>
using namespace std;
const size=3;
class vector
{
int v[size];
public:
vector();
vector(int *x);
friend vector operator*(int a,vector b);
friend vector operator *(vector b,int a);
friend istream &operator >>(istream &,vector &);
friend ostream &operator <<(ostream &,vector &);
};
vector::vector()
{
for(int i=0;i<size;i++)
v[i]=0;
}
vector::vector(int *x)
{
for(int i=0;i<size;i++)
v[i]=x[i];
}

94 | P a g e
17EC442

vector operator *(int a,vector b)


{
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i];
return c;
}
vector operator *(vector b,int a)
{
vector c;
for(int i=0;i<size;i++)
c.v[i]=a*b.v[i]*a;
return c;
}
istream & operator >>(istream &din,vector &b)
{
for(int i=0;i<size;i++)
din>>b.v[i];
return(din);
}
ostream & operator <<(ostream &dout,vector &b)
{
dout<<”(“<<b.v[0];
for(int i=1;i<size;i++)
dout<<”,”<<b.v[i];
dout<<”)”;
return(din);
}
int x[size]={2,4,5};
int main()
{

95 | P a g e
17EC442

vector m;
vector n=x;
cout<<”enter element of vector m:”<<endl;
cin>>m;
cout<<endl;
cout<<”m=”<<m<<endl;
vector p,q;
p=2*m;
q=n*2;
cout<<endl;
cout<<”p=”<<p<<endl;
cout<<”q=”<<q<<endl;
return 0;
}

Output:

enter element of vector m:


5 10 15
m=(5,10,15)
p=(10,20,30)
q=(4,8,12)

96 | P a g e
17EC442

Program 51: Write a program to perform


mathematical operations on strings.

Program:
#include<iostream.h>
#include<string.h>

class string
{
char *p;
int len;
public:
string() {leb=0; p=0;}
string(const char*s);
string(const string &s);
~string(){delete p;}
};
string::string(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
string::string(const string &s)
{
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
}
string operator+(const string &s, const string &t)
{
string temp;
temp.len=s.len+t.len;
temp.p=new char[temp.len=1];
strcpy(temp.p,s.p);
strcat(temo.p,t.p);
return(temp);
}
int operator<+(const string &s. const string &t)
{
int m= strlen(s.p);
int n= strlen(t.p);
if(m<=n) return(1);
else return (0)l

97 | P a g e
17EC442

}
void show(const string s)
{
cout<<s.p;
}
int main()
{
string s1=”New”;
string s2=”York”;
string s3=”Delhi”;
t1=s1;
t2=s2;
t3=s1+s3;
cout<<”t1=”;show(t1);
cout<<”t2=”;show(t2);
cout<<endl;
cout<<t3=”;show(t3);
cout<<endl;
if(t1<=t3)
{
show(t1);
cout<<”Smaller than”;
show(t3);
cout<<endl;
}
else
{
show(t3);
cout<<”Smaller than”;
show(t1);
cout<<endl;
}
return 0;
}
Output:
t1=New
t2=York
t3=New Delhi

New smaller than New Delhi

98 | P a g e
17EC442

Program 52: Write a program to add and multiply


two matrix objects by overloading of
“+“and “*“operators using friend
function.
Program:

#include<iostream>
using namespace std;
class matrix
{
int **p;
d1;
public:
matrix() { };
matrix (int a);
void getdata();
void putdata();
friend matrix operator + (matrix A, matrix B);
friend matrix operator * (matrix A, matrix B);
};
matrix :: matrix (int a)
{
d1=a;
p=new int * [d1];
for(int i=0;I < d1; i++)
{
p[i]= new int [d1];
}
}
void matrix :: getdata()
{
for(int i=0 ;i< d1; i++)
{
for(int j=0 ;j< d1; j++)
{
cin>>p[i][j];
}}}
void matrix : : putdata()
{
for(int i=0 ;i< d1; i++)
{
for(int j=0 ;j< d1; j++)
{
cout<<p[i][j]<<” “;
}
cout<<endl;

99 | P a g e
17EC442

}
}
matrix operator + (matrix A, matrix B)
{
matrix C(A.d1);
for(int i=0 ;i< d1; i++)
{
for(int j=0 ;j< d1; j++)
{
C.p[i][j]=A.p[i][j]+B.p[i][j];
}
}
return C;
}
matrix operator * (matrix A, matrix B)
{
matrix C(A.d1);
for(int i=0 ;i< d1; i++)
{
for(int j=0 ;j< d1; j++)
{ C.p[i][j]=0;
for(int k=0 ;k< d1; k++)
{
C.p[i][j]= C.p[i][j]+(A.p[i][k]*B.p[k][j]);
}}}
return C;
}
int main()
{
int a;
cout<<”Enter the size of the matrix :”;
cin>>a;
matrix A(a), B(a), C(a);
cout<<”Enter the matrix 1”<<endl;
A.getdata();
cout<<”Enter the matrix 2”<<endl;
B.getdata();
C=A+B;
matrix D(a);
D=A*B;
cout<<”Addition : “<<endl;
C.putdata();
cout<<”Product :”<<endl;
D.putdata();
return 0;
}

100 | P a g e
17EC442

Output:
Enter the size of the matrix : 3
Enter the matrix 1
1 2 3
4 5 6
7 8 9
Enter the matrix 2
1 1 1
1 1 1
1 1 1
Addition :
2 3 4
5 6 7
8 9 10
Product :
6 6 6
15 15 15
24 24 24

101 | P a g e
17EC442

Program 53: Write a program to convert data of


one type to another using two
classes.

Program:
include <iostream>
class example2;

class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id = a;
numbers = b;
cost = c;
}

example1(const example2 & e)


{
*this = e;
}

example1& operator=(const example2 & e);

{
return id;
}
int getnumber() const
{
return numbers;
}
int getcost() const
{
return cost;
}
void display()
{
std::cout << "Example 1" << std::endl;
std::cout << "id " << id << std::endl;

102 | P a g e
17EC442

std::cout << "numbers " << numbers << std::endl;


std::cout << "cost " << cost << std::endl;
}

};

class example2
{
int id;
int value;
public:
example2(int x, int y)
{
id = x;
value = y;
}
example2(const example1 &e)
{
*this = e;
}
{
id = e.getid();
value = e.getnumber() * e.getcost();
return *this;
}

int getid() const


{
return id;
}
int getvalue()const
{
return value;
}
void display()
{
std::cout << "Example 2" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "value " << value << std::endl;
}
};
{
id = e.getid();
numbers = -1; //do real work to get cost and numbers from
e.getvalue()
cost = -1;
return *this;
103 | P a g e
17EC442

int main()
{
example1 s1(100, 5, 140.0);
example2 s2(1, 2);
s2 = s1;
s2.display();
example2 s3(314, 278);
s1 = s3;
s1.display();
return 0;
}
Output:
a = 5
a = 5
b = 10
c = 50

a = 5
b = 20
c = 100

104 | P a g e
17EC442

Program 54: Write a program to implement concept


of single inheritance using public
class

Program:
#include<iostream>
using namespace std;
class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
class D :public B
{
int c;
public:
void mul ();
void display();
};
void B: : get_ab()
{
a=5; b=10;
}
int B: : get_a()
{
return a;
}
void B : : show_a()
{
cout<<”a = “<<a<<endl;
}
void D :: mul ()
{
c=b * get_a();
}
void D : : display()
{
cout<<”a = “<<get_a()<<endl;
cout<<”b = “<<b<<endl;

105 | P a g e
17EC442

cout<<”c = “<<c<<endl<<endl;
}
int main()
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();

d.b=20;
d.mul();
d.display();
return 0;
}

Output:
a = 5
a = 5
b = 10
c = 50

a = 5
b = 20
c = 100

106 | P a g e
17EC442

Program 55: Write a program to implement concept


of single inheritance using private
class

Program:

#include<iostream>
using namespace std;
class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
class D :private B
{
int c;
public:
void mul ();
void display();
};
void B: : get_ab()
{
cout<<”Enter values of a and b: “;
}
int B: : get_a()
{
return a;
}
void B : : show_a()
{
cout<<”a = “<<a<<endl;
}
void D :: mul ()
{
get_ab();
c= b * get_a();
}
void D : : display()
{
show_a();

107 | P a g e
17EC442

cout<<”b = “<<b<<endl;
cout<<”c = “<<c<<endl<<endl;
}
int main()
{
D d;
d.mul();
d.display();

d.mul();
d.display();
return 0;
}

Output:

Enter values of a and b: 5 10


a = 5
b = 10
c = 50

Enter values of a and b: 12 20


a = 12
b = 20
c = 240

108 | P a g e
17EC442

Program 56: write a program to implement concept


of multiple inheritance.
Program:

#include<iostream>
using namespace std;
class M
{
protected:
int m;
public:
void get_m (int x)
{
m=x;
}};
class N
{
protected:
int n;
public:
void get_n (int y)
{
n=y;
}
};
class P: public M, public N
{
public:
void display()
{
cout<<”m = “<<m<<endl;
cout<<”n = “<<n<<endl;
cout<<”m*n = “<<m*n<<endl;
}
};
int main()
{
P p;
P. get_m (20);
P. get_n (10);
P.display();
return 0;

109 | P a g e
17EC442

Output:
m = 20
n = 10
m*n = 200

110 | P a g e
17EC442

Program 57: Write a program to implement the


concept of multilevel inheritance.

Program:

#include<iostream>
using namespace std;
class subject
{
protected:
int roll_number:
public:
void get_number;
void put_number;
};
void student :: get_number(int a)
{
roll_number = a;
}
void student :: put_number()
{
cout<<"roll number:"<<roll_number<<"\n";
}
class test : public student
{
protected:
float sub1;

111 | P a g e
17EC442

float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void test :: put_marks()
{
cout<<"marks in sub1 ="<<sub1<<"\n";
cout<<"marks in sub2 ="<<sub2<<"\n";
}
class result : public test
{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = sub1 + sub2;
put_number();
put_marks();
cout<<"Total = "<<total<<"\n";

112 | P a g e
17EC442

}
int main()
{
result student1;
student1.get_marks(111);
student1.get_marks(75.0,59.5);
student1.display();
return 0;
}

Output:

roll number: 111


marks in sub1: 75
marks in sub2: 59.5
total=134.5

113 | P a g e
17EC442

Program 58: Write a program to implement the


concept of hybrid inheritance.

Program:

#include<iostream>
using namespace std;
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
out<<"Roll no"<<rno<<"\n";
}
};
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)

114 | P a g e
17EC442

{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks
obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sports:"<<score<<"\n";

}
};

class result: public test, public sports


{

115 | P a g e
17EC442

float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total Score="<<total<<"\n";
}
int main()
{
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0;
}

Output:
Roll no: 38

116 | P a g e
17EC442

Marks obtained :
part1= 45
part2= 40
Sports: 46
Total score= 131

117 | P a g e
17EC442

Program 59: Write a program to implement the


concept of virtual base class.

Program:

#include<iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number = a;
}
}
void put_number(void)
{
cout<<"roll no:"<<roll_number<<"\n";
}
};
class test : virtual public student
{
protected:
float part1,part2;
public:

118 | P a g e
17EC442

void get_marks(float x,float y)


{
part1 = x; part2 = y;
}
void put_marks(void)
{
cout<<"marks obtained:"<<"\n";
cout<<"part1 = "<<part1<<"\n";
cout<<"part2 = "<<part2<<"\n";
}
};
class sports : public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score(void)
{
cout<<"sports wt:"<<score<<"\n\n";
}
};
class result : public test,public sports
{

119 | P a g e
17EC442

float total;
public:
void display(void);
};
void result :: display(void)

total = part1 + part2 + score;

put_number();

put_marks();

put_score();

cout<<"total score: "<<total<<"\n";

int main()

result student_1;

student_1.get_number(678);

student_1.get_marks(30.5,25.5);

student_1.get_score(7.0);

student_1.display();

return 0;

Output:
Roll no: 678
Marks obtained:
Part 1: 30.5
Part 2: 25.5
Sport wt = 7
Total score= 63

120 | P a g e
17EC442

Program 60: Write a program to implement the


concept of constructors in an
derived class.

Program:

#include<iostream>
using namespace std;
class alpha
{
int x;
public:
alpha(int i)
{
x = 1;
cout<<"alpha initialized \n";
}
void show_x(void)
{cout<<"x = "<<x<<"\n";
};
class beta
{
float y;
public:
beta(float j)
{
y=j;

121 | P a g e
17EC442

cout<<"beta initialized \n";


}
void show_y(void)
{cout<<"y="<<y<<"\n";}
};
class gamma : public beta, public alpha
{
int m,n;
public:
gamma(int a, float b, int c, int d);
alpha(a), beta(b)
{
m=c;
n=d;
cout<<"gamma initialized \n";
}
void show_mn(void)
{
cout<<"m = "<<m<<"\n";
cout<<"n = "<<n<<"\n";
}
};
int main()
{
gamma g(5, 10.75, 20, 30);
cout<<"\n";
g.show_x();

122 | P a g e
17EC442

g.show_y();
g.show_mn();
return 0;
}

Output:

Beta initialized
Alpha initialized
Gamma initialized
X = 5
Y = 10.5
M = 20
N = 30

123 | P a g e
17EC442

Program 61: Write a program to implement concept


of manipulators of operators using
dereferencing operator.

Program:
#include <iostream>
using namespace std;

class alpha
{
int x;
public:
alpha(int i)
{
x=I;
cout<<”Alpha Constructed”;
}
void show_alpha()
{
cout<<”x=”<<x<<endl;
}
};
class beta
{
float p,q;
public:
beta(float a, float b): p(a),q(b+p)
{
cout<<”Beta Constructed”;
}
};
class gamma: public beta, public alpha
{
int u,v;
public:
gamma(int a, int b, float c):
alpha(a*2), beta(c,c), u(a)
{v=b; cout<<”Gamma Constructed:”}

void show_gamma()
{
cout<<”u=”<<u<<”endl;
cout<<”v=”<<v<<endl;
}
};
int main()
{

124 | P a g e
17EC442

gamma g(2,4,2.5);
cout<<Display member values”<<endl;
g.show_alpha();
g.show_beta();
g.show_gamma();
return 0;
}

Output:
beta constructed
alpha constructed
gamma constructed

Display member values


x= 4
p= 2.5
q= 5
u= 2
v= 4

125 | P a g e
17EC442

Program 62: Write a program to implement concept


of manipulators of operators using
dereferencing operator.

Program:
#include <iostream>
using namespace std;
int main() {
int *pc, c;

c = 5;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;

pc = &c;
cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): " <<
*pc << endl << endl;

c = 11;
cout << "Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " <<
*pc << endl << endl;

*pc = 2;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;

return 0;
}

Output:
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5

Address that pointer pc holds (pc): 0x7fff5fbff80c


Content of the address pointer pc holds (*pc): 5

Address pointer pc holds (pc): 0x7fff5fbff80c


Content of the address pointer pc holds (*pc): 11

Address of c (&c): 0x7fff5fbff80c


Value of c (c): 2

126 | P a g e
17EC442

Program 63: Write a program to perform


arithmetic operations on pointers.

Program:
#include<iostream.h>
void main()
{
int num[]={56,75,22,18,90};
int *ptr;
int I;
clrscr();
cout<”The array values are:”;
for(i=0;I,5;i++)
cout<<num[i]<<endl;
ptr=num;
cout<<”Value of ptr:”*ptr;
cout<<endl;
ptr++;
cout<<”Value of ptr++:”*ptr;
cout<<endl;
ptr--;
cout<<”Value of ptr--:”*ptr;
cout<<endl;
ptr=ptr+2;
cout<<”Value of ptr+2:”*ptr;
cout<<endl;
ptr=ptr-1;
cout<<”Value of ptr-1:”*ptr;
cout<<endl;
ptr+=3;
cout<<”Value of ptr+3:”*ptr;
cout<<endl;
ptr-=2;
cout<<endl;
cout<<”Value of ptr-=2:”<<*ptr;
cout<<endl;
getch();
}

Output:
Enter the count
5
Enter the numbers one by one
10

127 | P a g e
17EC442

16
23
45
34
Sum of even Numbers: 60

128 | P a g e
17EC442

Program 64: Write a program to implement the


array with pointers.

Program:
void main()
{
int numbers[50],*ptr;
int n,I;
cout<<”Enter the count”;
cin>>n;
cout<<”Enter the numbers one by one”;
for(i=0;i<n;i++)
cin>>numbers[i];
ptr=numbers;
int sum=0;
for(i=0;i<n;i++)
{
if(*ptr%2==0)
sum=sum+*ptr;
ptr++;
}
cout<<”Sum of even Numbers:”<<sum;
}

Output:
Enter the count
5
Enter the numbers one by one
10
16
23
45
34
Sum of even Numbers: 60

129 | P a g e
17EC442

Program 65: Write a program to implement concept


of array of pointers.

Program:
#include <iostream>
#include<string.h>
#include<ctype.h>

using namespace std;


void main()
{
int i=0;
char *ptr[10]={“books”,” Television”, ”computer”, ”sports”};
char str[25];
clrscr();
cout<<”Enter your favorite leisure pursuit:”
cin>>str;
for(i=0; i<4;i++)
{
if(lstrcmp(str, *ptr[i]))
{
cout<<Your Favorite pursuit”<<”is available here”<<endl;
break;
}
}
if(i==4)
cout<<”Your favorite”<<”not available here”<<endl;
getch();
return 0;
}

Output:
Enter your favorite leisure pursuit: books
Your favorite pursuit is available here.

130 | P a g e
17EC442

Program 66: Write a program to implement concept


of pointers to functions.
Program:
#include<iostream>
using namespace std;
typedef void (*FunPtr)(int , int );
void Add (int i, int j)
{
cout<<i<<” + “<<j<<” = “<<i+j;
}
void Subtract (int i, int j)
{
cout<<i<<” – “<<j<<” = “<<i-j;
}
int main()
{
FunPtr ptr;
ptr= & Add;
ptr(1,2);
cout<<endl;
ptr= & Subtract;
ptr(3,2);
return 0;
}

Output:
1 + 2 = 3
3 – 2 = 1

131 | P a g e
17EC442

Program 67: Write a program to implement the


concept of pointers to objects.

Program:
#include<iostream>
#include<conio.h>
using namespace std;
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show()
{
cout<<”Code:”<<code<<endl;
cout<<”Price:”<<price<<endl;
}
};
const int size=2;
int main()
{
item *p = newitem[size];
item *d=p;
int x,I;
float y;
for(i=0;i<size;i++)
{
cout<<”Input code and price for item”<<i+1;
cin>>x>>y;
p->getdata(x,y);
p++;
}
for(i=o;i<size;i++)
{
cout<<”Item:”<<i+1<<endl;
d-show();
d++;
}
return 0;
}

132 | P a g e
17EC442

Output:
Input code and price for item1 40 500
Input code and price for item2 50 600
Item: 1
Code: 40
Price: 500
Item: 2
Code: 50
Price: 600

133 | P a g e
17EC442

Program 68: Write a program to implement the


concept of array of pointers to
objects.

Program:
#include<iostream>
#include<conio.h>
using namespace std;
class city
{
protected:
char *name;
int len;
public:
city()
{
len=0;
name=new char[len+1]
}
void getname()
{
char *s;
s=new char[30]
cout<<”Enter city name:”;
cin>>s;
len=strlen(s);
name=new char[len+1];
strcpy(name,s);
}
void printname()
{
cout<<name<<endl;
}
};
int main()
{
city *cptr[10];
int n=1;
int option;
do{
cptr[n]=new city;
cptr[n]->getname();
n++;
out<<Do you want to enter one more name?;
cout<<”(Enter 1 for yes and 0 for no):”;
cin>>option;
}

134 | P a g e
17EC442

while(option);
cout<<endl;
for(int i=1;
I,+n;i++)
{
cptr[i]->printname();
}
return 0;
}

Output:
Enter city name: Hyderabad
Do you want to enter one more name?
(Enter 1 for yes and 0 for no);1
Enter city name: Ahemdabad
Do you want to enter one more name?
(Enter 1 for yes and 0 for no);1
Enter city name: Vadodara
Do you want to enter one more name?
(Enter 1 for yes and 0 for no);0

Hyderabad
Ahemdabad
Vadodara

135 | P a g e
17EC442

Program 69: Write a program to implement the use


of THIS pointer.

Program:
#include<iostream>
using namespace std;
class person
{
char name[20];
float age;
public:
person(char *s, float a)
{
strcpy (name, s);
age=a ;
}
person & person : : greater(person & x)
{
if(x.age > = age)
return x;
else
return *this;
}

void display()
{
cout<<”Name : <<name<<endl<<”Age : “<<age<<endl;
}
};
int main()
{
person P1(“John”, 37.50), P2(“Ahmed”, 29.0),
P3(“Hebber”,40.25);
person P = p1.greater(P3);
cout<<”Elder person is : “<<endl;
P.display();

P = p1.greater(P2);
cout<<”Elder person is : “<<endl;
P.display();
return 0;
}

136 | P a g e
17EC442

Output:
Elder person is :
Name : Hebber
Age : 40.25
Elder person is :
Name : John
Age : 37.50

137 | P a g e
17EC442

Program 70: Write a program to implement the usage


of pointers to derived object

Program:

#include<iostream>
using namespace std;
class BC
{
public:
int b;
void show()
{
cout<<"b="<<b<<"\n";
}
};
class DC:public BC
{
public:
int d;
void show()
{
cout<<"b="<<b<<"\n"
<<"d="<<d<<"\n";
}
};
int main()
{
BC *bptr;
BC base;

138 | P a g e
17EC442

bptr=&base;
bptr->b=100;
cout<<"bptr points to base object \n";
bptr->show();
DC derived;
bptr=&derived;
bptr->b=200;
cout<<"bptr now points to derived object \n";
bptr->show();
DC *dptr;
dptr=&derived;
dptr->d=300;
cout<<"dptr is derived type pointer\n";
dptr->show();
cout<<"using ((DC*)bptr)\n";
((DC*)bptr)->d=400;
((DC*)bptr)->show();
return 0;
}

Output:
bptr points to base object
b=100
bptr now points to derived object
b=200
dptr is derived type pointer
b=200
d=300
using ((DC*)bptr)
b=200d=400

139 | P a g e
17EC442

Program 71: Write a program to implement concept


of virtual Functions.
Program:

#include<iostream>
using namespace std;
class Base
{
public:
void display() {cout<<”Display Base”;}
virtual void show() {cout<<”Show Base”;}
};
class Derived : public Base
{
public:
void display() {cout<<”Display Derived”;}
virtual void show() {cout<<”Show Derived”;}
};
int main()
{
Base B;
Derived D;
base *bptr;
cout<<”bptr points to Base;
bptr=&B;
bptr->display();
bptr->show();
cout<<”bptr points to Derived;
bptr=&D;

}
140 | P a g e
17EC442

Output:
bptr points to base
Display Base
Show base
bptr points to Derived
Display Base
Show derived

141 | P a g e
17EC442

Program 72: Write a program to implement concept


of run time polymorphism using
virtual Functions.
Program:

#include<iostream>
using namespace std;
class media
{
Protected:
Char title[50];
Float price;
Public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
virtual void display(){}
};
class book: public media
{
int pages;
public:
book
(char *s,float a,int p) : media(s,a)
{
pages=p;
}
void display();
};

142 | P a g e
17EC442

class tape :public media


{
float time;
public:
tape(char *s, float a,float t):media(s,a)
{
time=t;
}
void display();
};
void book:: display()
{
cout<<”Title:”<<title;
cout<<”Pages:”<<pages;
cout<<”Price:”<<price;
}
void tape::display()
{
cout<<”Title:”<<title;
cout<<”Play Time”<<time<<”mins”;
cout<<price:”<<price;
}
int main()
{
char*title=new char[30];
float price,time;
int pages;

cout<<”ENTER BOOK DETAILS”;


cout<<”Title:”; cin>>title;
cout<<”Price:”; cin>>price;

143 | P a g e
17EC442

cout<<”Pages:”; cin>>pages;

book book1(title,price,pages);

cout<<”ENTER TAPE DETAILS”;


cout<<”Title:”; cin>>title;
cout<<”Price:”; cin>>price;
cout<<”Play Time(mins):”; cin>>time;

tape tape1(title,price,time);
media*list[2];
list[0]=&book1;
list[1]=&tape1’

cout<<MEDIA DETAILS”;
cout<<”......BOOK......”;
list[0]->display();
cout<<”......TAPE......”;
list[1]->display();
return 0;
}

Output:
ENTER BOOK DETAILS
TITLE: EDC
PRICE: 88
PAGES: 400

ENTER TAPE DETAILS


TITLE: DCD
PRICE: 90
PLAY TIME : 55

144 | P a g e
17EC442

MEDIA DETAILS
......BOOK......
PRICE: 88
PAGES: 400

......TAPE......
TITLE: DCD
PLAY TIME: 55 mins
PRICE:90

145 | P a g e
17EC442

Program 73: Write a program that uses a single


file for both reading and writing the
data.

Program:

#include<iostream>
#include<fstream>
int main()
{
ofstream outf("ITEM");
cout<<"Enter item name:";
char name[30];
cin>>name;
outf<<name<<"\n";
cout<<"Enter item cost:";
float cost;
cin>>cost;
outf <<cost<<"\n";
outf .close();
ifstream inf("ITEM");
inf >>name;
inf >>cost;
cout<<"\n"
cout<<"Item name:"<<name<<"\n";
cout<<"item cost:"<<cost<<"\n";
inf.close();
return 0;
}

146 | P a g e
17EC442

Output:
Enter item name:CD-ROM
Enter item cost:250

Item name:CD-ROM
Item cost:250

147 | P a g e
17EC442

Program 74: Write a program for specifying field


size using width()

Program:
#include<iostream>
using namespace std;
int main()
{
int items[4]={10,8,12,15};
int cost[4]={75,100,60,99};
cout.width(5);
cout<<”ITEMS”;
cout.width(8);
cout<<”COST”;
cout.width(15);
cout<<”TOTAL VALUE”;
int sum=0;
for(int i=0;i<4;i++)
{
cout.width(5);
cout<<items[i];
cout.width(8);
cout<<cost[i];
int value=items[i]*cost[i];
cout.width(15);
cout<<value<<endl;
sum=sum+value;
}
cout<<”Grand Total=”;
cout.width(2);

148 | P a g e
17EC442

cout<<sum<<endl;
return 0;
}

Output:
ITEMS COST TV
10 75 750
8 100 800
12 60 720
15 99 1485

Grand total= 3755

149 | P a g e
17EC442

Program 75: Write a program for precision using


precision()

Program:
#include<iostream>
#include<cmath.h>
using namespace std;
int main()
{
cout<<”Precision set to 3 Digits”;
cout.precison(3);
cout.width(10);
cout<<”VALUE”;
cout.width(15);
cout<<DQRT_OF_VALUE”<<endl;
for(int n=1; n<+5;n++)
{
cout.width(8);
cout<<n;
cout.width(13);
cout<<sqrt(n)<<endl;
cout<<”Precsion set to 5 digits”;
cout.precision(5);
cout<<”sqrt(10)=”sqrt(10)<<endl;
cout.precison(0);
cout<<sqrt(10)=”<<sqrt(10)<<”(default setting”;
return 0;
}

OUTPUT:

150 | P a g e
17EC442

Precision set to 3 digits


VALUE SQRT_OF_VALUE
1 1
2 1.41
3 1.73
4 2
5 2.24

Precision set to 5 Digits


sqrt(10)=3.1623
sqrt(10)=3.162278(Default Setting)

Program 76: Write a program for padding with


fill()

Program:
#include<iostream>
using namespace std;
int main()
{
cout.fill(‘<’);
cout.precision(3);
for(int n=1; n<=6;n++)
{
cout.width(5);
cout<<n;
cout.width(5);
cout<<1.0/float(n)<<endl;

if(n==3)

151 | P a g e
17EC442

cout.fill(‘>’);
}
cout<<”Padding Changed”:
cout.fill(‘#’)
cout.width(15);
cout<<12.345678<<”endl;
return 0;
}

OUTPUT:
<<<<1<<<<<<<<<1
<<<<2<<<<<<<0.5
<<<<3<<<<<0.333
>>>>4>>>>>>0.25
>>>>5>>>>>>>0.2
>>>>6>>>>>0.167
Padding changed
######12.346

152 | P a g e
17EC442

Program 77: Write a program to design your own


manipulator to provide the following
output specifications for printing
money value.

Program:
#include<iostream>
#include<iomanip>
using namespace std;
ostream & currency(ostream &output)
{
output<<"&";
return output;
}
ostream &form(ostream &output)
{
output.self(ios::showpos);
output.self(ios::showpoint);
output.fill('*');
output.precision(2);
output<<setionsflags(ios::fixed)<<setw(10);
return output;
}
int main()
{
cout<<currency<<form<<99.92;
return 0;
}

153 | P a g e
17EC442

Output:
& ****99.92

Program 78: Write a program to create a database


regarding the introduction.

Program:

#include<iostream>
class patient
{
char name[10],doa[10],dod[10];
public:
int getdata()
{
cout<<"enter the name of patient:";
gets(name);
cout<<"\nenter the date of admit:";
gets(doa);
cout<<"\nenter the name of disease:";
gets(dis);
cout<<"\nenter the date of discharge:";
gets(dod);

}
int show()
{

cout<<"\n\nNAME";puts(name);
cout<<"\nDate of ADMIT:";puts(doa);

154 | P a g e
17EC442

cout<<"\nName of DISEASE:";puts(dis);
cout<<"\nDate of DISCHARGE:";puts(dod);

};
int main()
{
patient p[2];
for(int i=0;i<2;i++)
{
p[i].getdata;
}
for(int i=0;i<2;i++)
{
p[i].show;
}
return 0;
}

Output:

enter the name of patient:Archies


enter the date of admit:first of October
enter the name of disease: jaundice
enter the date of discharge: second of November

enter the name of patient:Angelo


enter the date of admit:Fourth of march
enter the name of disease: Asthma
enter the date of discharge:fifth of march
155 | P a g e
17EC442

Name:Archies
date of Admit:first of October
name of DISEASE: jaundice
date of DISCHARGE: second of November

Name:Angelo
date of ADMIT:Fourth of march
name of DISEASE: Asthma
date of DISCHARGE:fifth of march

156 | P a g e
17EC442

Program 79: Write a program to swap a number using


concept of function template.

Program:
#include <iostream.h>
using namespace std;

template <class T>


void swap(T &x, T &y)
{
T temp = x;
x = y;
y = temp;
}
void fun(int m,int n)
{
cout<<”m and n before swap:”<< m <<” ”<< n <<”\n”;
Swap(m,n);
cout<<”m and n after swap:”<< m <<” ”<< n <<”\n”;
}
int main()
{
fun(100,200);
return 0;
}

Output:

m and n before swap: 100 200


m and n after swap: 200 100

157 | P a g e
17EC442

Program 80: Write a program to create a class


template to find the greater of two
numbers.
Program:
#include <iostream.h>
using namespace std;
template <class t1,class t2>
class greater
{
t1 a;
t2 b;
Public:
greater(t1 x,t1 y)
{
a=x;
b=y;
}
int compare()
{
if (a>b)
cout<<a<<”is greater than”<<b<<endl;
else
cout<<b<<”is greater than”<<a;
}
};
int main()
{
greater<float , int>g1(5.4,7);
g1.compare();
greater<int , float>g2(9,9.7);
g2.compare();
return 0;
}

Output:

7 is greater than 5.4


9.7 is greater than 9

158 | P a g e
17EC442

Program 81: Write a program to perform exception


handling operation.
Program:
#include <iostream.h>
#include <conio.h>
using namespace std;
int main()
{
int a,b;
cout<<”enter 2 numbers:”;
cin>>a>>b;
try
{
if (b!=0)
{
float div = (float)a/b;
if (div < 0)
throw ‘e’;
cout<<”a/b =”<<div;
}
else
throw b;
}
catch (int e)
{
cout<<“Exception: Division by zero”;
}
Catch (char st)
{
cout<<“Exception: Division is less than 1”;
}
Catch (int e)
{
cout<<“Exception: Unknown”
}
getch();
return 0;
}

159 | P a g e
17EC442

Output:
Enter 2 numbers: 8 5
a/b = 1.6
Enter 2 numbers: 9 0
Exception: Division by zero
Enter 2 numbers: -1 10
Exception: Division is less than 10

160 | P a g e
17EC442

Program 82: Write a program to perform exception


handling for divide by zero.
Program:
#include <iostream.h>
using nampespace std;
void main()
{
int a,b,c;
float d;
clrscr();
cout<<”enter the value of a:”;
cin>>a;
cout<<”enter the value of b:”;
cin>>b;
cout<<”enter the value of c:”;
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<”result is:”<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<”answer is infinite because a-b is:”<<i;
}
getch();
}

Output:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
Answer is infinite because a-b is: 0

161 | P a g e
17EC442

162 | P a g e

You might also like