Yash PDF
Yash PDF
Yash PDF
YASH ARORA
1
40990202018 YASH ARORA
OUTPUT:
2
40990202018 YASH ARORA
3
40990202018 YASH ARORA
OUTPUT:
4
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int sum=0,n,x;
float avg;
cout<<"\nEnter total number of numbers to be inserted \n";
cin>>n;
cout<<"enter numbers \n";
for(int i=1;i<=n;i++)
{cin>>x;
sum=sum+x;
}
avg=sum/n;
cout<<"\nthe sum = "<<sum<<"\nthe average = "<<avg;
getch();
}
5
40990202018 YASH ARORA
OUTPUT:
6
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{int n,a=0,b=1,c;
clrscr();
cout<<"\nEnter the number of terms : ";
cin>>n;
cout<<"The Fobonacci Series : \n";
for(int i=1;i<=n;i++)
{cout<<a<<" ";
c=a+b;
a=b;
b=c;
}
getch();
}
7
40990202018 YASH ARORA
OUTPUT:
8
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{int m,n,p,q,c,d,k,sum=0;
int first[10][10],second[10][10],multiply[10][10];
clrscr();
cout<<"\nEnter number of rows and columns of first matrix\n";
cin>>m>>n;
cout<<"Enter elements of first matrix\n";
for(c=0;c<m;c++)
{for(d=0;d<n;d++)
cin>>first[c][d];
}
cout<<"Enter number of rows and columns of second matrix\n";
cin>>p>>q;
if (n!=p)
cout<<"\nThe matrices can't be multiplied with each other.\n";
else
{cout<<"Enter elements of second matrix\n";
for(c=0;c<p;c++)
9
40990202018 YASH ARORA
{for(d=0;d<q;d++)
cin>>second[c][d];
}
for(c=0;c<m;c++)
{for(d=0;d<q;d++)
{for(k=0;k<p;k++)
{sum=sum+first[c][k]*second[k][d];
}
multiply[c][d]=sum;
sum=0;
}
}
clrscr();
cout<<"\nProduct of the matrices:\n";
for(c=0;c<m;c++)
{for(d=0;d<q;d++)
cout<<multiply[c][d]<<" ";
cout<<'\n';
}
}
getch();
}
10
40990202018 YASH ARORA
OUTPUT:
11
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{int n,c=0; clrscr();
cout<<" \n Enter a number :";
cin>>n;
if(n==1)
cout<<"The number entered is not a prime";
else
{for(int i=1;i<=n;i++)
{if(n%i==0)
c++;
}
if(c<=2)
cout<<"\nThe number entered is a prime number ";
else
cout<<"\nThe number entered is not a prime number ";
}
getch();
}
12
40990202018 YASH ARORA
OUTPUT:
13
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{int n,f=1;
clrscr();
cout<<"\nEnter a number : ";
cin>>n;
for(int i=n;i>=1;i--)
f=f*i;
cout<<"\n\nThe factorial = "<<f;
getch();
}
14
40990202018 YASH ARORA
OUTPUT:
15
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void swap(int&, int&);
void main()
{int x, y;
clrscr();
cout<<"\nEnter the value of x and y\n";
cin>>x>>y;
cout<<"Before Swapping\nx = "<<x<<"\ny = "<<y;
swap(x,y);
cout<<"\nAfter Swapping\nx = "<<x<<"\ny = "<<y;
getch();
}
void swap(int &a, int &b)
{a=a+b;
b=a-b;
a=a-b;
}
16
40990202018 YASH ARORA
OUTPUT:
17
40990202018 YASH ARORA
18
40990202018 YASH ARORA
OUTPUT:
19
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i=25;
float f=40.78;
cout<<"\ni= "<<i;
cout<<"\nf= "<<f;
cout<<"\nfloat(i)= "<<float(i);
cout<<"\nint(f)= "<<int(f);
getch();
}
20
40990202018 YASH ARORA
OUTPUT:
21
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
inline float multi(float x,float y)
{return(x*y);
}
inline float divi(float p,float q)
{return(p/q);
}
void main()
{clrscr();
float i=20.567,j=6.35;
cout<<"\ni * j = "<<multi(i,j);
cout<<"\ni / j = "<<divi(i,j);
getch();
}
22
40990202018 YASH ARORA
OUTPUT:
23
40990202018 YASH ARORA
q[i]=i+1;
cout<<"Value store in block of memory: ";
for(i=0;i<n;i++)
cout<<q[i]<<" ";
}
delete p;
delete r;
delete[] q;
getch();
}
25
40990202018 YASH ARORA
OUTPUT:
26
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
struct student
{
char n[20]; int roll;
int marks[5];
}s;
void avg()
{int sum=0;
float avgr;
for(int i=0;i<5;i++)
sum=sum+s.marks[i];
avgr=sum/5;
cout<<"\n Average : "<<avgr;
}
void main()
{
clrscr();
cout<<"\nenter name\t";
27
40990202018 YASH ARORA
cin>>s.n;
cout<<"\nenter roll no\t";
cin>>s.roll;
cout<<"\nenter marks of 5 subjects\n";
for(int i=0;i<5;i++)
cin>>s.marks[i];
clrscr();
cout<<"Student Name is "<<s.n;
cout<<"\nRoll Number is "<<s.roll;
cout<<"\nThe Marks of subjects ";
for( i=0;i<5;i++)
cout<<'\n'<<s.marks[i];
avg();
getch();
}
28
40990202018 YASH ARORA
OUTPUT:
29
40990202018 YASH ARORA
Q14. Write a program to swap two entered numbers using call by value
concept.
CODE:
#include<iostream.h>
#include<conio.h>
void swap(int x,int y)
{ x=x+y;
y=x-y;
x=x-y;
cout<<"x="<<x<<"\n"<<"y="<<y;}
void main()
{int a,b;
clrscr();
cout<<"enter the value of x and y";
cin>>a>>b;
swap(a,b);
getch();
}
30
40990202018 YASH ARORA
OUTPUT:
31
40990202018 YASH ARORA
Q15. Write a program to calculate the volume of a box in which you will
take the inputs from the user. Implement this program using objects and
classes.
CODE:
#include<iostream.h>
#include<conio.h>
class box
{
public:
double length;
double breadth;
double height;
double getvol();
};
double box::getvol()
{
return length *breadth *height;
}
void main()
{
box box1;
clrscr();
32
40990202018 YASH ARORA
33
40990202018 YASH ARORA
OUTPUT:
Q16. Write a C++ program to create student class to read and print
student details.
34
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
class stu
{char name[20];
int roll;
int marks[5];
public:
void get();
void disp();
};
void stu::get()
{cout<<“\n enter name”;
cin>>name;
cout<<“\n enter roll number “;
cin>>roll;
cout<<“\nenter marks in 5 subjects “;
for(int i=0;i<5;i++)
cin>>marks[i];
}
void stu::disp()
{clrscr();
35
40990202018 YASH ARORA
void main()
{ stu s;
s.get();
s.disp();
getch();
}
36
40990202018 YASH ARORA
OUTPUT:
37
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class employee
{char name[30];
float age;
public:
void getdata();
void putdata();
};
void employee :: getdata()
{cout<<"ENTER NAME: ";
cin>>name;
cout<<"ENTER AGE: ";
cin>>age;
}
void employee :: putdata()
{cout<<"NAME: "<<name<<"\n";
cout<<"AGE: "<<age<<"\n";
}
const int size=3;
void main()
{clrscr();
38
40990202018 YASH ARORA
employee manager[size];
for(int i=0;i<size;i++)
{cout<<"\nDetails of manager"<<i+1<<"\n";
manager[i].getdata();
}
cout<<"\n";
for(i=0;i<size;i++)
{
cout<<"\nManager"<<i+1<<"\n";
manager[i].putdata();
}
getch();
}
39
40990202018 YASH ARORA
OUTPUT:
Q18. Write a C++ program to create a class to read and add two
distances.
CODE:
40
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class dis
{float d1;
float d2;
float tdis;
int x;
public:
void get();
void add();
void disp();
};
void dis::get()
{ cout<<" press 1. to enter distances in kms";
cout<<"\n press 2 to enter distance in metres. ";
cout<<"\n enter your choice";
cin>>x;
if(x==1)
{cout<<"\n enter 1st distance";
cin>>d1;
d1=d1*1000;
cout<<"\n enter 2nd distance";
41
40990202018 YASH ARORA
cin>>d2;
d2=d2*1000;
}
if(x==2)
{cout<<"\n enter 1st distance";
cin>>d1;
cout<<"\n enter 2nd distance";
cin>>d2;
}
else
cout<<"Error !!! wrong choice ";
}
void dis::add()
{tdis=d1+d2;
}
void dis::disp()
{clrscr();
cout<<"\n 1st distance : "<<d1;
cout<<"\n 2nd distance : "<<d2;
cout<<"\n Sum of distances = "<<tdis<<" m.";
}
void main()
{clrscr();
42
40990202018 YASH ARORA
dis s;
s.get();
s.add();
s.disp();
getch();
}
OUTPUT:
43
40990202018 YASH ARORA
Q19. A shopping list is created for which an order is placed with the
dealer every month, this list includes details such as the code number
and price of each item perform operations like adding or deleting an
44
40990202018 YASH ARORA
item from the list and pricing of total order using C++ objects and
classes. You can assume any variable or function you want for the same.
CODE:
#include<iostream.h>
#include<conio.h>
const m=50;
class item{
int itemcode[m];
float itemprice[m];
int count;
public:
void cnt(){
count=0;}
void getitem();
void displaysum();
void remove();
void displayitem();};
void item :: getitem(){
cout<<"ENTER ITEM CODE: ";
cin>>itemcode[count];
cout<<"ENTER ITEM COST: ";
cin>>itemprice[count];
45
40990202018 YASH ARORA
count++;}
void item :: displaysum(){
float sum=0;
for(int i=0;i<count;i++)
sum=sum+itemprice[i];
cout<<"\nTotal value: "<<sum<<"\n";}
void item :: remove(){
int a;
cout<<"ENTER ITEM CODE: ";
cin>>a;
for(int i=0;i<count;i++)
if(itemcode[i]==a)
itemprice[i]=0;}
void item :: displayitem(){
cout<<"\nCode Price\n";
for(int i=0;i<count;i++){
cout<<"\n"<<itemcode[i];
cout<<" "<<itemprice[i];}
cout<<"\n";}
void main(){
clrscr();
item order;
order.cnt();
46
40990202018 YASH ARORA
int x;
do{
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.displayitem();
break;
case 5: break;
default : cout<<"Error in input; try again\n";}
}while(x!=5);
getch();
47
40990202018 YASH ARORA
OUTPUT:
48
40990202018 YASH ARORA
49
40990202018 YASH ARORA
50
40990202018 YASH ARORA
51
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class sample{
int a;
int b;
public:
void setvalue() {a=25;b=40;}
friend float mean(sample s);};
float mean(sample s){
return float(s.a+s.b)/2.0;}
void main(){
clrscr();
sample x;
x.setvalue();
cout<<"mean value="<<" "<<mean(x);
getch();
}
52
40990202018 YASH ARORA
OUTPUT:
53
40990202018 YASH ARORA
Q21. Write a C++ program to create student class to read and print
student details.
CODE:
#include<iostream.h>
#include<conio.h>
class abc;
class xyz
{
int data;
public:
void setvalue(int value)
{
data=value;}
friend void add(xyz,abc);
};
class abc
{
int data;
public:
void setvalue(int value)
{
data=value;}
54
40990202018 YASH ARORA
55
40990202018 YASH ARORA
OUTPUT:
Q22. Write a C++ program to create student class to read and print
student details.
56
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
class class_2;
class class_1
{
int value1;
public:
void intdata(int a) {value1=a;}
void display(void) {cout<<value1<<"\n";}
friend void exchange(class_1&,class_2&);
};
class class_2
{
int value2;
public:
void intdata(int a) {value2 = a;}
void display(void) {cout<<value2<<"\n";}
friend void exchange(class_1&,class_2&);};
void exchange (class_1&x,class_2&y){
int temp=x.value1;
x.value1=y.value2;
57
40990202018 YASH ARORA
y.value2=temp;}
void main(){
clrscr();
class_1 c1;
class_2 c2;
c1.intdata(100);
c2.intdata(200);
cout<<"value before swapping"<<"\n";
c1.display();
c2.display();
exchange(c1,c2);
cout<<"values after swapping"<<"\n";
c1.display();
c2.display();
getch();
}
58
40990202018 YASH ARORA
OUTPUT:
#include<iostream.h>
#include<conio.h>
class item
{static int count;
int number;
public:
void getdata(int a)
{number=a;
count++;
}
void getcount()
{cout<<"count:";
cout<<count<<"\n";
}
};
int item::count;
void main()
{clrscr();
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
60
40990202018 YASH ARORA
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"after reading data"<<"\n";
a.getcount();
b.getcount();
c.getcount();
getch();
}
61
40990202018 YASH ARORA
OUTPUT:
62
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class test
{int code;
static int count;
public:
void setcode()
{code=++count;
}
void showcode()
{cout<<"object number:"<<code<<"\n";
}
static void showcount()
{cout<<"count:"<<count<<"\n";
}
};
int test::count;
void main()
{clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
63
40990202018 YASH ARORA
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
64
40990202018 YASH ARORA
OUTPUT:
65
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
class time
{int hours;
int minutes;
public:
void gettime(int h,int m)
{ hours=h; minutes=m;}
void puttime()
{cout<<hours<<" "<<"hours and"<<" ";
cout<<minutes<<" "<<"minutes"<<" "<<"\n";
}
void sum(time, time);
};
void time::sum(time t1,time t2)
{minutes=t1.minutes+t2.minutes;
minutes=hours/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}
void main()
66
40990202018 YASH ARORA
{clrscr();
time T1,T2,T3;
T1.gettime(2,50);
T2.gettime(3,40);
T3.sum(T1,T2);
cout<<" "<<"T1="<<" " ;T1.puttime();
cout<<" "<<"T2="<<" " ;T2.puttime();
cout<<" " <<"T3="<<" " ;T3.puttime();
getch();
}
67
40990202018 YASH ARORA
OUTPUT:
CODE:
#include<iostream.h>
#include<conio.h>
class matrix
{int m[3][3];
public:
void read()
{cout<<"enter the elements of the 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()
{int i,j;
for(i=0;i<3;i++)
{cout<<"\n";
for(j=0;j<3;j++)
{cout<<m[i][j]<<"\t";
}
69
40990202018 YASH ARORA
}
}
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);
}
void main()
{clrscr();
matrix mat1,mat2;
mat1.read();
cout<<"you entered the following matrix";
mat1.display();
mat2=trans(mat1);
cout<<"\n transposed matrix:";
mat2.display();
getch();
}
70
40990202018 YASH ARORA
71
40990202018 YASH ARORA
OUTPUT:
72
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
float amount;
float value(float p, int n, float r=0.15);
amount=value(3000.00,5);
cout<<"\n final value="<<amount<<"\n\n";
getch();
}
float value(float p,int n,float r)
{int year = 1;
float sum=p;
while(year<=n)
{sum=sum*(1+r);
year=year+1;
}
return(sum);
}
73
40990202018 YASH ARORA
OUTPUT:
#include<iostream.h>
#include<conio.h>
class point
{int x,y;
public:
74
40990202018 YASH ARORA
point(int a, int b)
{x=a;
y=b;
}
void display()
{cout<<"("<<x<<","<<y<<")\n";
}
};
void main()
{clrscr();
point p1(1,1);
point p2(5,10);
cout<<"point p1=";
p1.display();
cout<<"point p2";
p2.display();
getch();
}
75
40990202018 YASH ARORA
OUTPUT:
76
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class code
{int id;
public:
code(){ }
code(int a)
{id=a;}
code(code&x)
{id=x.id;
}
void display()
{cout<<id;
}
};
void main()
{clrscr();
code A(100);
code B(A);
code C=A;
77
40990202018 YASH ARORA
code D;
D=A;
cout<<"\n id of A:";A.display();
cout<<"\n id of B:";B.display();
cout<<"\n id of C:";C.display();
cout<<"\n id of D:";D.display();
getch();
}
78
40990202018 YASH ARORA
OUTPUT:
79
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class play
{int count,number;
public:
play(); //default constructor
play(int a,int b); //parametrized contructor
play(play &p); //copy contructor
};
play::play()
{count=0; number=0;
cout<<"\nCount = "<<count<<" Number = "<<number<<endl;
}
play::play(int a,int b)
{count=a; number=b;
cout<<"\nCount = "<<count<<" Number = "<<number<<endl;
}
play::play(play &p)
{count=p.count; number=p.number;
cout<<"\nCount = "<<count<<" Number = "<<number<<endl;
80
40990202018 YASH ARORA
}
void main()
{clrscr();
play p;
play q(10,3);
play r(p);
getch();
}
81
40990202018 YASH ARORA
OUTPUT:
82
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class test
{
int *a;
public:
test(int size);
~test();
};
test::test(int size)
{a=new int(size);
cout<<"\nconstructor message:\ninteger array of size"<<"
"<<size<<" "<<"created..";
}
test::~test()
{delete a;
cout<<"\n\ndestructor message: \nreleased the memory allocated
for integer array";
getch();
}
83
40990202018 YASH ARORA
void main()
{clrscr();
int s;
cout<<"\nenter the size of the array ";
cin>>s;
cout<<"\n\ncreating an object an object of test class";
test t(s);
getch();
}
84
40990202018 YASH ARORA
OUTPUT:
85
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class space
{int x;
int y;
int 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<<"\nx="<<" "<<x<<" ";
cout<<"\ny="<<y<<" "<<" ";
cout<<"\nz="<<z<<" "<<"\n ";
86
40990202018 YASH ARORA
}
void space::operator-()
{x=-x;
y=-y;
z=-z;
}
void main()
{
clrscr(); space c;
c.getdata(12,7,9);
cout<<"\nc:"<<" ";
c.display();
-c;
cout<<"\n-c:"<<" ";
c.display();
getch();
}
87
40990202018 YASH ARORA
OUTPUT:
88
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class bi
{
double x;
double y;
public:
bi(){}
bi(double real,float img);
bi operator+(bi);
void display();
};
bi::bi(double real,float img)
{x=real;y=img;
}
bi bi::operator+(bi d)
{bi temp;
temp.x=x+d.x;
temp.y=y+d.y;
return(temp);
89
40990202018 YASH ARORA
}
void bi :: display(void)
{cout<<'\n'<<x<<"+"<<y<<"\n";
}
void main()
{clrscr();
bi h1,h2,h3,h4; h1=bi(4.12,6.2);
h2=bi(7.99,1.9);
h3=bi(2.88,7.7);
h4=h1+h2+h3;
cout<<"\nh1 =";h1.display();
cout<<"\nh2 =";h2.display();
cout<<"\nh3 =";h3.display();
cout<<"\nh4 =";h4.display();
getch();
}
90
40990202018 YASH ARORA
OUTPUT:
91
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class num
{int x1, x2;
public:
void accept();
friend num operator*(num c1, num c2);
void display();
};
void num::accept()
{cout<<"\n Enter Two Numbers :\n ";
cin>>x1>>x2;
}
void num::display()
{cout<<x1<<"*"<<x2<<"\n";
}
num operator*(num c1, num c2)
{num c;
92
40990202018 YASH ARORA
c.x1=c1.x1*c2.x1;
c.x2=c1.x2*c2.x2;
return(c);
}
void main()
{clrscr();
num n1,n2,sum;
n1.accept();
n2.accept();
sum=n1*n2;
cout<<"\n Entered Values : \n";
n1.display();
n2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
sum.display();
getch();
}
OUTPUT:
93
40990202018 YASH ARORA
94
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class arr
{int a[5];
public:
arr(int *s);
int operator [] (int k)
{return (a[k]);
}
};
arr::arr(int *s)
{int i;
for(i=0;i<5;i++)
a[i]=s[i];
}
void main()
{clrscr();
int x[5]={1,2,3,4,5};
arr z(x);
for(int i=0;i<5;i++)
95
40990202018 YASH ARORA
cout<<'\n'<<x[i];
getch();
}
96
40990202018 YASH ARORA
OUTPUT:
97
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class b
{int x;
public:
int z;
void setxz();
int get();
void show();
};
class d:public b
{int c;
public:
void mul();
void disp();
};
void b::setxz()
{x=5;
z=10;
}
98
40990202018 YASH ARORA
int b::get()
{return x;
}
void b::show()
{cout<<"\nx= "<<x<<'\n';
}
void d::mul()
{c=z*get();
}
void d::disp()
{cout<<"\nx= "<<get()<<'\n';
cout<<"z= "<<z<<'\n';
cout<<"c= "<<c<<"\n\n";
}
void main()
{clrscr();
d s;
s.setxz();
s.mul();
s.show();
s.disp();
s.z=30;
s.mul();
99
40990202018 YASH ARORA
s.disp();
getch();
}
100
40990202018 YASH ARORA
OUTPUT:
101
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class student
{protected:
int rno, m1, m2;
public:
void get();
};
void student::get()
{cout << "\nEnter the Roll no :"; cin>>rno;
cout << "Enter the two marks :"; cin >> m1>>m2;
}
class sports
{protected:
int sm;
public:
void getsm();
};
void sports::getsm()
{cout << "\nEnter the sports mark :";
102
40990202018 YASH ARORA
cin>>sm;
}
103
40990202018 YASH ARORA
OUTPUT:
104
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class base
{public:
int x;
void getdata();
};
void base::getdata()
{cout<<"\nEnter value of x= ";
cin>>x;
}
class derive1:public base
{public:
int y;
void readdata();
};
void derive1::readdata()
{cout<<"\nEnter value of y= ";
cin>>y;
}
105
40990202018 YASH ARORA
OUTPUT:
107
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class A
{public:
int x, y;
void getdata()
{cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A
{public:
void product()
{cout << "\nProduct= " << x * y;
}
};
class C : public A
{public: void sum()
{cout << "\nSum= " << x + y;
108
40990202018 YASH ARORA
}
};
void main()
{clrscr();
B obj1;
C obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
getch();
}
109
40990202018 YASH ARORA
OUTPUT:
110
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
int a,b,c,d,e;
class A
{public:
void getab() {cout<<"\n Enter a and b value:";
cin>>a>>b; }
};
class B:public A
{public:
void getc() { cout<<"Enter c value:"; cin>>c; }
};
class C
{
public:
void getd() { cout<<"Enter d value:"; cin>>d; }
};
class D:public B,public C
{public:
void result()
111
40990202018 YASH ARORA
{getab();
getc();
getd();
e=a+b+c+d;
cout<<"\n Addition is :"<<e;
}
};
void main()
{clrscr();
D d1;
d1.result();
getch();
}
112
40990202018 YASH ARORA
OUTPUT:
113
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class m
{public:
void display();
};
class n
{public:
void display();
};
class p:public m,public n
{public:
void display();
};
void m::display()
{cout<<"\nclass m\n";
}
void n::display()
114
40990202018 YASH ARORA
{cout<<"class n\n";
}
void p::display()
{m::display();
}
void main()
{clrscr();
p x;
x.display();
x.n::display();
getch();
}
115
40990202018 YASH ARORA
OUTPUT:
116
40990202018 YASH ARORA
#include <iostream.h>
#include<conio.h>
class s{
public:
virtual int getArea() = 0;
void setWidth(int w);
void setHeight(int h);
protected:
int width;
int height;
};
class r: public s
{public:
int getArea();
};
class t: public s
{public:
int getArea();
};
117
40990202018 YASH ARORA
void s::setWidth(int w)
{ width = w;
}
void s::setHeight(int h)
{ height = h;
}
int r::getArea()
{return (width * height);
}
int t::getArea()
{return (width * height)/2;
}
void main()
{clrscr();
r r1;
t t1;
r1.setWidth(17);
r1.setHeight(34);
cout<<"\nTotal Rectangle area: "<<r1.getArea()<<endl;
t1.setWidth(14);
t1.setHeight(33);
cout<<"\nTotal Triangle area: "<<t1.getArea()<<endl;
118
40990202018 YASH ARORA
getch();
}
119
40990202018 YASH ARORA
OUTPUT:
120
40990202018 YASH ARORA
#include<iostream.h>
#include<conio.h>
class a
{int x;
public:
a(int i)
{x=i;
cout<<"\nAlpha initialized \n";
}
void showx()
{cout<<"x= "<<x<<'\n';
}
};
class b
{float y;
public:
b(float j)
{y=j;
cout<<"\nBeta initialized\n";
}
121
40990202018 YASH ARORA
void showy()
{cout<<"y= "<<y<<'\n';
}
};
class g:public b,public a
{int n,m;
public:
g(int aa,float bb,int c,int d):
a(aa),b(bb)
{m=c;
n=d;
cout<<"\nGama initialized\n";
}
void showmn()
{cout<<"m= "<<m<<"\nn= "<<n<<'\n';
}
};
void main()
{clrscr();
g g1(5,10.5,33,8);
cout<<'\n';
g1.showx();
g1.showy();
122
40990202018 YASH ARORA
g1.showmn();
getch();
}
123
40990202018 YASH ARORA
OUTPUT:
124
40990202018 YASH ARORA
{
Area A1, A2;
int temp;
A1.GetLength();
temp = A1.AreaCalculation();
A1.DisplayArea(temp);
getch();
}
126
40990202018 YASH ARORA
OUTPUT:
127
40990202018 YASH ARORA
};
class D
{
public:
D()
{
cout<<"D's constructor called" << endl;
}
};
class
E:public C,public D
{
public:
E()
{
cout<<"E's constructor called" << endl;
}
};
class F:B,virtual E
{
public:
F()
{
129
40990202018 YASH ARORA
130
40990202018 YASH ARORA
OUTPUT:
131
40990202018 YASH ARORA
{
private:
float balance,d,w;
public:
void withdraw(float ww);
void deposit(float d){balance=d;}
cur_acct(char *n,char *t,int number,float dp,float wd):
account(n,t,number){
d=dp;
w=wd;
deposit(d);
withdraw(w);}
void display();
};
void cur_acct::withdraw(float ww){
if(balance<ww)
cout<<" sorry your balance is less than your withdrawal amount
\n";
else
{
balance-=ww;
if(balance<minimum){
133
40990202018 YASH ARORA
134
40990202018 YASH ARORA
}
void interest();
sav_acct(char *n,char *t,int number,float dp,float wd):
account(n,t,number){
float d,w;
d=dp;
w=wd;
deposite(d);
interest();
withdraw(w);
}};
void sav_acct::withdraw(float w){
if(balance<w)
cout<<" sorry your balance is less than your withdrawal amount
\n";
else
{
balance-=w;
if(balance<minimum){
cout<<"\n your current balance is :"<<balance<<" which is less
than"<<minimum<<"\n your account is discharged by
"<<service_charge<<"TK \n"<<" You must
store"<<minimum<<"TK to avoid discharge \n "<<" Do you want
to withdraw ? press 1 for yes press 0 for no"<<" what is your
option ?";
135
40990202018 YASH ARORA
int test;
cin>>test;
if(test==0)
balance+=w;
}
else
}
}
void sav_acct::display(){
cout<<"\n Now balance = "<<balance;}
void sav_acct::interest()
{
int D[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int d1,y1,m1;
cout<<" Enter today's date :(i,e day,month,year) ";
cin>>d1>>m1>>y1;
int iday,fday;
iday=d;
fday=d1;
for(int i=0;i<m1;i++)
{
fday+=D[i];
}
136
40990202018 YASH ARORA
for(i=0;i<m;i++)
{
iday+=D[i];}
int tday;
tday=fday-iday;
float ty;
ty=float(tday)/365+y1-y;
balance=balance*pow((1+r),ty);}
void main()
{
clrscr();
float d;
cout<<" Enter customer name :";
char name[100];
gets(name);
cout<<" Enter account number :";
int number;
cin>>number;
cout<<" Enter your deposit amount : ";
cin>>d;
cout<<" Enter your withdrawal amount :";
float w;
cin>>w;
137
40990202018 YASH ARORA
sav_acct c("savings",name,number,d,w);
c.display();
getch();
}
138
40990202018 YASH ARORA
OUTPUT:
139
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
class staff{
public:
int code;
char name[100];
public:
void set_info(char *n,int c){
strcpy(name,n);
code=c;}};
class teacher : public staff{
protected:
char sub[100],publication[100];
public:
void set_details(char *s,char *p){
140
40990202018 YASH ARORA
strcpy(sub,s);strcpy(publication,p);}
void show(){
cout<<"name"<<setw(8)<<"code"<<setw(15)<<"subject"<<setw(2
5)
<<"publication"<<endl<<name<<setw(8)<<code<<setw(25)<<sub
<<setw(22)<<publication<<endl;}};
class officer:public staff{
char grade[100];
public:
void set_details(char *g){
strcpy(grade,g);}
void show(){
cout<<" name "<<setw(15)<<"code"<<setw(15)<<"Category
"<<endl
<name<<setw(10)<<code<<setw(15)<<grade<<endl;}};
class typist: public staff{
protected:
float speed;
public:
void set_speed(float s){
speed=s;}};
class regular:public typist{
protected:
float wage;
141
40990202018 YASH ARORA
public:
void set_wage(float w){wage=w;}
void show(){
cout<<"name"<<setw(16)<<"code"<<setw(15)<<"speed"<<setw(1
5)
<<"wage"<<endl<<name<<setw(10)<<code<<setw(15)<<speed
<<setw(15)<<wage<<endl;}};
class causal:public typist{
float wage;
public:
void set_wage(float w){wage=w;}
void show(){
cout<<"name"<<setw(16)<<"code"<<setw(15)<<"speed"<<setw(1
5)
<<"wage"<<endl<<name<<setw(10)<<code<<setw(15)<<speed
<<setw(15)<<wage<<endl;}};
void main(){
clrscr();
teacher t;
t.set_info("Ataur",420);
t.set_details("programming with c++"," Tata McGraw Hill");
officer o;
o.set_info("Md. Rashed",222);
142
40990202018 YASH ARORA
o.set_details("First class");
regular rt;
rt.set_info("Robiul Awal",333);
rt.set_speed(85.5);
rt.set_wage(15000);
causal ct;
ct.set_info("Kawser Ahmed",333);
ct.set_speed(78.9);
ct.set_wage(10000);
cout<<" About teacher: "<<endl;
t.show();
cout<<" About officer:"<<endl;
o.show();
cout<<" About regular typist :"<<endl;
rt.show();
cout<<" About causal typist :"<<endl;
ct.show();
getch();
}
143
40990202018 YASH ARORA
OUTPUT: -
144
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void show() { cout<<" In Base \n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived \n"; }
};
void main(void)
{
clrscr();
Base *bp = new Derived;
bp->show();
getch();
}
145
40990202018 YASH ARORA
OUTPUT: -
CODE:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
146
40990202018 YASH ARORA
void show ()
{ cout<< "show derived class" <<endl; }
};
void main()
{
clrscr();
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
getch();
}
147
40990202018 YASH ARORA
OUTPUT:
148
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
class a {
public:
virtual void foo() = 0;
};
class b : public a
{public:
b() { foo = &b::caseOne; }
void caseOne() { cout << "Hello One" << endl; }
void caseTwo() { cout << "Hello Two" << endl; }
void (b::*foo)();
void chooseOne() { foo = &b::caseOne; }
void chooseTwo() { foo = &b::caseTwo; }
};
int main() {
b b1;
b1.(*foo)();
}
149
40990202018 YASH ARORA
OUTPUT:
150
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int count=0;
char c;
cout<<"Input Text\n";
cin.get(c);
while(c!='\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout<<"\nNumber of characters = "<<count<<"\n";
getch();
}
151
40990202018 YASH ARORA
OUTPUT: -
CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size=20;
152
40990202018 YASH ARORA
char city[20];
cout<<"Enter city name: \n";
cin>>city;
cout<<"City name: "<<city<<"\n\n";
cout<<"Enter city name again: \n";
cin.getline(city,size);
cout<<"City name now: "<<city<<"\n\n";
cout<<"Enter another city name: \n";
cin.getline(city,size);
cout<<"New city name: "<<city<<"\n\n";
getch();
}
153
40990202018 YASH ARORA
OUTPUT: -
154
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *string1="C++";
char *string2="Programming";
int m = strlen(string1);
int n = strlen(string2);
for(int i=1;i<n;i++)
{
cout.write(string2,i);
cout<<"\n";
}
for(i=n;i>0;i--)
{
cout.write(string2,i);
cout<<"\n";
155
40990202018 YASH ARORA
}
cout.write(string1,m).write(string2,n);
cout<<"\n";
cout.write(string1,10);
getch();
}
156
40990202018 YASH ARORA
OUTPUT:
157
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int item[4]={10,8,5,7};
int cost[4]={75,100,45,76};
cout.width(5);
cout<<"ITEMS";
cout.width(8);
cout<<"COST";
cout.width(15);
cout<<"TOTAL VALUE"<<"\n";
int sum=0;
for(int i=0;i<4;i++)
{
cout.width(5);
cout<<item[i];
158
40990202018 YASH ARORA
cout.width(8);
cout<<cost[i];
int value=item[i]*cost[i];
cout.width(15);
cout<<value<<"\n";
sum=sum+value;
}
cout<<"\n Grand Total= ";
cout.width(2);
cout<<sum<<"\n";
getch();
}
159
40990202018 YASH ARORA
OUTPUT:
160
40990202018 YASH ARORA
CODE:
#include<conio.h>
#include<iostream.h>
template<class t1, class t2>
class test
{
t1 a;
t2 b;
public:
test(t1 x,t2 y)
{
a=y;
b=y;
}
void show()
{
cout<<a<<" and "<<b<<"\n";
}
};
161
40990202018 YASH ARORA
void main()
{
clrscr();
cout<<"Instantiating the class template as test1 with float and int
datatypes..\ntest1: ";
test<float,int>test1(1.23,123);
test1.show();
162
40990202018 YASH ARORA
OUTPUT:
163
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
template<class T1=int, class T2=int>
class Test
{
T1 a;
T2 b;
public:Test(T1 x, T2 y)
{
a=x;
b=y;
}
void show()
{
cout<<a<<" and "<<b<<"\n";
}
};
void main()
164
40990202018 YASH ARORA
{
clrscr();
Test<float,int> test1(1.23, 123);
Test<int,char> test2(100, 'W');
Test<> test3('a', 12.983);
test1.show();
test2.show();
test3.show();
getch();
}
165
40990202018 YASH ARORA
OUTPUT: -
166
40990202018 YASH ARORA
CODE:
#include<conio.h>
#include<iostream.h>
template<class T>
void swap(T &x, T &y)
{
T temp = x;
x = y;
y = temp;
}
void fun(int m, int n, float a, float b)
{
cout<<"m and n before swap: "<<m<<" "<<n<<"\n";
swap(m,n);
cout<<"m and n after swap: "<<m<<" "<<n<<"\n";
void main()
{
clrscr();
fun(100,200,11.22,33.44);
getch();
}
168
40990202018 YASH ARORA
OUTPUT: -
169
40990202018 YASH ARORA
CODE:
#include<conio.h>
#include<iostream.h>
template<class T>
void bubble(T a[], int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
template<class X>
void swap(X &a, X &b)
{
X temp=a;
a=b;
b=temp;
170
40990202018 YASH ARORA
}
void main()
{
clrscr();
int x[5]={10,50,30,40,20};
float y[5]={1.1,5.5,3.3,4.4,2.2};
bubble(x,5);
bubble(y,5);
cout<<"Sorted x-array: ";
for(int i=0;i<5;i++)
cout<<x[i]<<" ";
cout<<endl;
cout<<"Sorted y-array: ";
for(int j=0;j<5;j++)
cout<<y[j]<<" ";
cout<<endl;
getch();
}
171
40990202018 YASH ARORA
OUTPUT: -
172
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter values of a and b \n";
cin>>a;
cin>>b;
int x=a-b;
try
{
if(x!=0)
{
cout<<"Result(a/x)= "<<a/x<<"\n";
}
else
{
173
40990202018 YASH ARORA
throw(x);
}
}
catch(int i)
{
cout<<"Exception caught: DIVIDE BY ZERO\n";
}
cout<<"END";
getch();
}
174
40990202018 YASH ARORA
OUTPUT:
175
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
void test(int x)
{
try
{
if(x==1)throw x;
else
if(x==0)throw 'x';
else
if(x==-1)throw 1.0;
cout<<"End of try-block \n";
}
catch(char c)
{
cout<<"Caught a character \n";
}
catch(int m)
{
176
40990202018 YASH ARORA
177
40990202018 YASH ARORA
OUTPUT:
178
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
void test(int x)
{
try
{
if(x== 0) throw x;
if(x== -1) throw 'x';
if(x== 1) throw 1.0;
}
catch(...)
{
cout<<"Caught an exception \n";
}
}
void main()
{
Clrscr();
179
40990202018 YASH ARORA
180
40990202018 YASH ARORA
OUTPUT: -
181
40990202018 YASH ARORA
CODE:
#include<iostream.h>
#include<conio.h>
void divide(double x, double y)
{
cout<<"Inside function \n";
try
{
if(y==0.0)
throw y;
else
cout<<"Division= "<<x/y<<"\n";
}
catch(double)
{
cout<<"Caught double inside function \n";
throw;
}
cout<<"End of function \n\n";
}
182
40990202018 YASH ARORA
int main()
{
cout<<"Inside main \n";
try
{
divide(10.5,2.0);
divide(20.0,0.0);
}
catch(double)
{
cout<<"Caught double inside main \n";
}
cout<<"End of main \n";
return 0;
}
183
40990202018 YASH ARORA
OUTPUT: -
184