17ec442 PDF
17ec442 PDF
Lab Manual
Submitted to
By
Samir Barot
ID no: 17EC442
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.
2|Page
17EC442
Program:
#include<iostream>
int main()
{
cout<<”Hello world”<<endl;
getch();
return 0;
}
Output:
Hello world
3|Page
17EC442
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:
4|Page
17EC442
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:
5|Page
17EC442
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:
#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:
8|Page
17EC442
Program:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
struct student
{
int roll_no;
string name;
int phone_number;
};
9|Page
17EC442
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:
#include <iostream>
usingnamespacestd;
intmain()
{
inta=10;
int&b = a;
return0;
}
Output:
Value of a:10
Value of b:10
11 | P a g e
17EC442
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:
#include<iostream.h>
int main()
{
float radius,area,pi;
void display(void);
display();
area=area(radius,pi=3.14);
cout<<"area of circle="<<area;
13 | P a g e
17EC442
Output:
14 | P a g e
17EC442
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
Output:
16 | P a g e
17EC442
Program:
#include <iostream>
char c = 'a';
int main()
{
char c = 'b';
Output:
Local variable: b
Global variable: a
17 | P a g e
17EC442
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:
18 | P a g e
17EC442
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:
#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:
#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:
#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:
#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
Output:
26 | P a g e
17EC442
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
Output:
Joseph
Louis
Lagrange
Joseph Louis
Joseph Louis Lagrange
28 | P a g e
17EC442
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:
30 | P a g e
17EC442
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:
#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:
#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:
34 | P a g e
17EC442
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:
35 | P a g e
17EC442
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:
#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
int main( )
{
set A;
A.getdata( );
A.display( );
return 0;
}
Output:
38 | P a g e
17EC442
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:
41 | P a g e
17EC442
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
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:
44 | P a g e
17EC442
5 : Quit
45 | P a g e
17EC442
3 : delete an item
4 : display all items
5 : Quit
46 | P a g e
17EC442
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:
#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";
}
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:
#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
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:
55 | P a g e
17EC442
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:
#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:
Transposed matrix:
1 5 7
2 5 8
3 6 9
59 | P a g e
17EC442
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:
#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:
#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&);
};
64 | P a g e
17EC442
}
int main()
{
class1 c1;
class2 c2;
c1.indata(100);
c2.indata(200);
Output:
65 | P a g e
17EC442
66 | P a g e
17EC442
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:
Matrix is
1 2 3
4 5 6
7 8 9
69 | P a g e
17EC442
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:
#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
73 | P a g e
17EC442
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
75 | P a g e
17EC442
76 | P a g e
17EC442
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
#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
#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:
#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:
#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:
#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:
#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;
}
return 0;
}
86 | P a g e
17EC442
Output:
S : 10 -20 30
S : -10 20 -30
87 | P a g e
17EC442
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
Output :
89 | P a g e
17EC442
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:
#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
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
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:
96 | P a g e
17EC442
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
98 | P a g e
17EC442
#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:
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;
}
{
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
};
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 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:
#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:
#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:
108 | P a g e
17EC442
#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:
#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:
113 | P a g e
17EC442
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";
}
};
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:
#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
119 | P a g e
17EC442
float total;
public:
void display(void);
};
void result :: display(void)
put_number();
put_marks();
put_score();
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:
#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
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:
#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
125 | P a g e
17EC442
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
126 | P a g e
17EC442
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:
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:
#include <iostream>
#include<string.h>
#include<ctype.h>
Output:
Enter your favorite leisure pursuit: books
Your favorite pursuit is available here.
130 | P a g e
17EC442
Output:
1 + 2 = 3
3 – 2 = 1
131 | P a g e
17EC442
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:
#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:
#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:
#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
#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
#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
143 | P a g e
17EC442
cout<<”Pages:”; cin>>pages;
book book1(title,price,pages);
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
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:
#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:
#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
149 | P a g e
17EC442
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
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:
#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:
#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:
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:
#include <iostream.h>
using namespace std;
Output:
157 | P a g e
17EC442
Output:
158 | P a g e
17EC442
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
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