You are on page 1of 31

Program:

#include<iostream.h>
#include<conio.h>
class simple
{
private:
int p,n;
float r,si;
public:
void getdata()
{
cout<<"Enter the value of p,n,r";
cin>>p>>n>>r;
}
void cal()
{

si=(p*n*r)/100;
cout<<"The simple interest is= "<<si;
}
};
void main()
{
simple s;
clrscr();
s.getdata();
s.cal();
getch();
}
Output:

Enter the values of p,n,r


1000
2
10
The simple interest is equal to
200
Program:

#include<iostream.h>
#include<conio.h>
class simple
{
private:
int si;
public:
void cal(float p,int y=5,float r=10.0)
{
si=(p*y*r)/100;
cout<<"Simple interest= "<<si<<endl;
}
};
void main()
{
simple s1,s2;
clrscr();
s1.cal(5000,10);
s2.cal(5000);
getch();
}
Output

5000
2500
Program:

#include<iostream.h>
#include<conio.h>
class fibonacci
{
private:
int f0,f1,fib;
public:
fibonacci();
void display();
void increment();
};
fibonacci::fibonacci()
{
f0=0;
f1=1;
fib=f0+f1;
}
void fibonacci::increment()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
void fibonacci::display()
{
cout<<fib<<'\b';
}
void main()
{
clrscr();
fibonacci f;
for(int i=0;i<15;i++)
{
f.display();
f.increment();
}
getch();
}
Output:

1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
Program:

#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n No:of obj created"<<count;
}
~alpha()
{
cout<<"\n No:of obj destroyed"<<count;
count;
}
};
void main()
{
cout<<"\n Enter main";
alpha a1,a2,a3,a4;
{
cout<<"\n Enter block 1\n";
alpha a5;
}
{
cout<<"\n Enter block 2\n";
alpha a6;
}
cout<<"\n Reenter main\n";
getch();
}
Output:

No:of object created 1


No:of object created 2
No:of object created 3
No:of object created 4
Enter block 1

No:of object created 5


No:of object destroyed 5
Enter block 2

No:of object created 6


No:of object destroyed 6
Reenter main

No:of object destroyed 6


No:of object destroyed 6
No:of object destroyed 6
No:of object destroyed 6
Program:

#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code(){}
code(int a){id=a;}
code(code &x)
{
id=x.id;
}
int display(void)
{
return(id);
}
};
void main()
{
clrscr();
code a(100);
code b(a);
code c=a;
code d;
d=a;
cout<<"\n id of a:"<<a.display();
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();
return0;
}
Output:

Id of a:100
Id of b:100
Id of c:100
Id of d:100
Program:

#include<iostream.h>
#include<conio.h>
#include<math.h>
void volume(int a)
{
cout<<"\n Volume of cube"<<a*a*a;
}
void volume(float r,float h)
{
cout<<"\n Volume of cylinder"<<3.14*r*h;
}
void volume(int l,int b,int h)
{
cout<<"\n Volume of cuboid"<<l*b*h;
}
void main()
{
clrscr();
volume(5);
volume(10,20);
volume(5,6,10);
getch();
}
Output:

Volume of cube 125


Volume of cylinder 628
Volume of cuboid 300
Program:

#include<iostream.h>
#include<conio.h>
#include<math.h>
void area(int a)
{
cout<<"\n Area of square "<<a*a;
}
void area(int l,int b)
{
cout<<"\n Area of rectangle "<<l*b;
}
void circle(float r)
{
cout<<"\n Area of circle "<<3.14*r*r;
}
void main()
{
clrscr();
float r=5.2;
area(5);
area(10,20);
circle(5.2);
getch();
}
Output:

Area of square 25
Area of rectangle 200
Area of circle 84.905594
Program:

#include<iostream.h>
#include<conio.h>
int sum(int a,int b)
{
int c;
c=a+b;
return(c);
}
float sum(double a,double b)
{
return(a+b);
}
void main()
{
clrscr();
cout<<"sum1= "<<sum(10,5);
cout<<"sum2= "<<sum(.5,5.10);
getch();
}
Output:

Sum 1=15
Sum 2=5.6
Program:

#include<iostream.h>
#include<conio.h>
class complex
{
private:
float real;
float image;
public:
complex()
{
real=image=0.0;
}
void getdata();
void outdata(char *msg);
friend complex operator(complex c1)
{
complex c;
c.real=-c1.real;
c.image=-c1.image;
return(c);
}
void read data();
};
void complex::read data()
{
cout<<"Real part:";
cin>>real;
cout<<"Image part:";
cin>>image;
}
void complex::outdata(char *msg)
{
cout<<endl<<msg;
cout<<"("real;
cout<<","<<image<<")";
}
void main()
{
clrscr();
complex c1,c2;
cout<<"Enter complex c1: "<<endl;
c1.readdata();
c2=-c1;
c1.outdata("complex.c1");
c2.outdata("complex.c2=-complex c1:");
getch();
}
Output:

Enter complex c1:


Real part: 2
Image part: -5
Complex c1(2,-5)
Complex c2=-complex c1(-2,5)
Program:

#include<iostream.h>
class complex
{
private:
float real;
float image;
public:
complex()
{
}
complex(int realpart)
{
real=realpart;
}
void readdata()
{
cout<<"Real part:";
cin>>real;
cout<<"Image part:";
cin>>image;
}
void outdata(char *msg)
{
cout<<endl<<msg;
cout<<"("<<real;
cout<<","<<image<<")";
}
friend complex operator +(complex c1,complex c2)
};
complex operator +(complex c1,complex c2)
{
complex c;
c.real=c1.real+c2.real;
c.image=c1.image+c2.image;
return(c);
}
void main()
{
complex c1,c2,c3=3.0;
cout<<"Enter complex c1 "<<endl;
c1.readdata();
cout<<"Enter complex c2 "<<endl;
c2.readdata()
c3=c1+c2;
c3.outdata("Result of c3=c1+c2");
getch();
}
Output:

Enter complex c1
Real part: 5
Image part: -2
Enter complex c2
Real part: 3
Image part: -4
Result of c3=c1+c2:(8,-6)
Program:

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int x;
float y;
public:
sample(int,float);
void operator=(sample abc);
void display();
};
sample::sample(int one,float two)
{
x=one;
y=two;
}
void sample::operator=(sample abc)
{
x=abc.x;
y=abc.y;
}
void sample::display()
{
cout<<"integer number(x)=:"<<x<<endl;
cout<<"integer value(y)=:"<<y;
}
void main()
{
clrscr();
sample obj1(10,-22.55);
sample obj2(20,-33.44);
obj1.operator=(obj2);
cout<<"\n Contents of the first object\n";
obj1.display();
cout<<"\n Contents of the second object\n";
obj2.display();
getch();
}
Output:

Contents of the first object


Integer number(x)=20
Integer value(y)=-33.439999
Contents of the second object
Integer number(x)=20
Integer value(y)=-33.439999
Program:

#include<iostream.h>
#include<conio.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D:public B
{
int c;
public:
void mul(void);
void display(void);
};
void B::get_ab(void)
{
a=5;
b=10;
}
int B::get_a()
{
return a;
}
void B::show_a()
{
cout<<"a="<<a<<"\n";
}
void D::mul()
{
c=b*get_a();
}
void D::display()
{
cout<<"a="<<get_a()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n";
}
void main()
{
clrscr();
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
getch();
}
Output:

a=5
a=5
b=10
c=50
a=5
b=20
c=100
Program:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
float b=420.5;
cout<<"int(10.4)="<<int(10.4)<<"\n";
cout<<"int(10.99)="<<int(10.99)<<"\n";
cout<<"b="<<b<<"\n";
a=int(b);
cout<<"a=int(b)= "<<a<<"\n";
b=float(a)+1.5;
cout<<"b=float(a)+1.5= "<<b;
getch();
}
Output:

int(10.4)=10
int(10.99)=10
b=420.5
a=int(b)=420
b=float(b)+1.5=421.5
Program:

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
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;
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<<"\n Marks in sub1 "<<sub1<<"\n";
cout<<"\n 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<<"\nTotal "<<total<<"\n";
}
void main()
{
clrscr();
result student;
student.get_number(111);
student.get_marks(75.5,59.5);
student.display();
getch();
}
Output:

Roll_number: 111
Marks in sub1: 75.5
Marks in sub2: 59.5
Total: 135
Program:

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
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;
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<<"\n Marks in sub1 "<<sub1<<"\n";
cout<<"\n Marks in sub2 "<<sub2<<"\n";
}
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"\n Sports"<<score<<"\n";
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2+score;
put_number();
put_marks();
put_score();
cout<<"\nTotal "<<total<<"\n";
}
void main()
{
clrscr();
result stud;
stud.get_number(1234);
stud.get_marks(27.5,33.0);
stud.get_score(6.0);
student.display();
getch();
}
Output:

Roll number: 1234


Marks in sub1: 27.5
Marks in sub2: 33.0
Sports 6
Total=66.5

You might also like