You are on page 1of 23

c

**********************************************************
//PROGRAM FOR MULTILEVEL INHERITANCE
**********************************************************
#include<iostream.h>
#include<conio.h>
class stud{
protected:
int roll;
public:
voidgetdata(int);
voidputdata(void);
};
void stud::getdata(int a)
{
roll=a;
}
void stud::putdata()
{
cout<<"Roll Number:"<<roll<<endl;
}
classtest:public stud
{

  

protected:
float sub1;
float sub2;
public:
voidgetm(float,float);
voidputm(void);
};
void test::getm(float x,float y)
{
sub1=x;
sub2=y;
}
void test::putm()
{
cout<<"Marks in subject 1:"<<sub1<<endl;
cout<<"Marks in subject 2:"<<sub2<<endl;
}
classresult:public test
{
float total;
public:
void display(void);
};
  

void result::display(void)
{
total=sub1+sub2;
putdata();
putm();
cout<<"Total="<<total<<endl;
}
void main()
{
clrscr();
result s1;
s1.getdata(149);
s1.getm(87,93);
s1.display();
getch();
}

c
c
c
c
c
c

  

c
c
c
c
c
c
c
c
c
c
:
******************************************************************
Roll Number : 149
Marks insubject 1: 87
Marks in subject 2: 93
Total = 180
******************************************************************

  

**********************************************************
//PROGRAM FOR HYBRID INHERITANCE
**********************************************************

#include<iostream.h>
#include<conio.h>
class stud{
protected:
int roll;
public:
voidgetdata(int);
voidputdata(void);
};
void stud::getdata(int a)
{
roll=a;
}
void stud::putdata()
{
cout<<"Roll Number:"<<roll<<endl;
}

  

classtest:virtual public stud


{
protected:
float sub1;
float sub2;
public:
voidgetm(float,float);
voidputm(void);
};
void test::getm(float x,float y)
{
sub1=x;
sub2=y;
}
void test::putm()
{
cout<<"Marks in subject 1:"<<sub1<<endl;
cout<<"Marks in subject 2:"<<sub2<<endl;
}
classsports:virtual public stud
{
protected:
float score;
  

public:
voidgetsc(float s)
{
score=s;
}
voidputsc(void)
{
cout<<"Sports wt:"<<score<<endl;
}
};
classresult:publictest,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=sub1+sub2;
putdata();
putm();
putsc();
cout<<"Total="<<total<<endl;
  

}
void main()
{
clrscr();
result s1;
s1.getdata(149);
s1.getm(97.5,95.5);
s1.getsc(7.0);
s1.display();
getch();
}

c
c
c
c
c
c
c
c
c
c
  

c
c
c
c
c
c
c
c
c
c
c
:
******************************************************************
Roll Number : 149
Marks in subject 1 : 97.5
Marks in subject 2 : 95.5
Total = 193
******************************************************************

  

**********************************************************
//PROGRAM FOR FUNCTION OVERLOADING
**********************************************************

#include<iostream.h>
#include<conio.h>
voidamt(float princ,inttime,float rate);
voidamt(float princ,int time);
voidamt(float princ,float rate);
voidamt(inttime,float rate);
voidamt(float princ);
void main()
{
clrscr();
cout<<"Case 1:"<<endl;
amt(2000.0f);
cout<<"Case 2:"<<endl;
amt(2500.0f,3);
cout<<"Case 3:"<<endl;
amt(2300.0f,3,0.11f);
cout<<"Case 4:"<<endl;

  

amt(2000.0f,0.12f);
cout<<"Case 5:"<<endl;
amt(6,0.07f);
getch();
}
voidamt(float princ,inttime,float rate)
{
cout<<"Principal amount:Rs."<<princ<<endl;
cout<<"Time:"<<time<<endl;
cout<<"Rate:"<<rate<<endl;
cout<<"Interest amount:"<<(princ*rate*time)<<endl;
}
voidamt(float princ,int time)
{
cout<<"Principal amount:Rs."<<princ<<endl;
cout<<"Time:"<<time<<endl;
cout<<"Rate:0.08"<<endl;
cout<<"Interest amount:"<<(princ*0.08*time)<<endl;
}
voidamt(float princ,float rate)
{
cout<<"Principal amount:Rs."<<princ<<endl;
cout<<"Time:2 years"<<endl;
  

cout<<"Rate:"<<rate<<endl;
cout<<"Interest amount:"<<(princ*rate*2)<<endl;
}
voidamt(inttime,float rate)
{
cout<<"Principal amount:Rs.2000"<<endl;
cout<<"Time:"<<time<<endl;
cout<<"Rate:"<<rate<<endl;
cout<<"Interest amount:"<<(2000*rate*time)<<endl;
}
voidamt(float princ)
{
cout<<"Principal amount:Rs."<<princ<<endl;
cout<<"Time:2 years"<<endl;
cout<<"Rate:0.08"<<endl;
cout<<"Interest amount:"<<(princ*0.08*2)<<endl;
}

c
c
c
c
c

  

c
c
c
c
:
******************************************************************

Case 1:
Principal amount : Rs.2000
Time : 2 years
Rate : 0.08
Interest amount : 320
Case 2:
Principal amount : Rs.2500
Time : 3 years
Rate : 0.08
Interest amount : 600
Case 3:
Principal amount : Rs.2300
Time : 3 years
Rate : 0.11
Interest amount : 759

  

Case 4:
Principal amount : Rs.2000
Time : 2 years
Rate : 0.12
Interest amount : 480
Case 5:
Principal amount : Rs.2000
Time : 6 years
Rate : 0.07
Interest amount : 840

c
c
c
c

  

**********************************************************
//PROGRAM FOR OPERATOR(-) OVERLOADING
**********************************************************

#include<iostream.h>
#include<conio.h>
class stud{
intx,y,z;
public:
voidgetdata(void);
voidputdata(void);
void operator -(void);
};
void stud::getdata(void)
{
cout<<"Enter the values of x,y and z:";
cin>>x>>y>>z;
}
void stud::putdata(void)
{
cout<<"The values of x,y and z are:"<<x<<"\t"<<y<<"\t"<<z<<endl;
}

  

void stud::operator-(void)
{
x=-x;
y=-y;
z=-z;
}
void main()
{
clrscr();
stud s1;
s1.getdata();
s1.putdata();
-s1;
s1.putdata();
getch();
}

c
c
c
c
c
c

  

c
c
c
c
c
c
c
c
c
:
******************************************************************
Enter the values of x , y and z :
32
56
98
The values of x , y and z are : 32

5698

The values of x , y and z are : -32-56-98

  

**********************************************************
//PROGRAM FOR OPERATOR OVERLOADING(-) USING FRIEND
FUNCTION
**********************************************************
#include<iostream.h>
#include<conio.h>
class stud{
intx,y,z;
public:
voidgetdata(void);
voidputdata(void);
friend void operator -(stud &s);
};
void stud::getdata(void)
{
cout<<"Enter the values of x,y and z:";
cin>>x>>y>>z;
}
void stud::putdata(void)
{
cout<<"The values of x,y and z are:"<<x<<"\t"<<y<<"\t"<<z<<endl;
}
  

void operator-(stud &s)


{
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
void main()
{
clrscr();
stud s1;
s1.getdata();
s1.putdata();
-s1;
s1.putdata();
getch();
}

c
c
c
c
c
c

  

c
c
c
c
c
c
c
c
:

Enter the values of x , y and z :


87
91
44
The values of x , y and z are : 879144
The values of x , y and z are : -87-91-44

  

**********************************************************
//PROGRAM FOR BINARY OPERATOR OVERLOADING
**********************************************************

#include<iostream.h>
#include<conio.h>
classcmp{
intx,y;
public:
cmp(){}
cmp(float real,floatimg)
{
x=real;
y=img;
}
cmp operator+(cmp);
voidputdata(void);
};
voidcmp::putdata(void)
{
cout<<x<<"+i"<<y<<endl;

  

}
cmpcmp::operator-(cmp p)
{
cmp temp;
temp.x=x-p.x;
temp.y=y-p.y;
return(temp);
}
void main()
{
clrscr();
cmp s1,s2,s3;
s1=cmp(2.5,3.5);
s2=cmp(1.6,2.7);
s3=s1+s2;
cout<<"C1=";
s1.putdata();
cout<<"C2=";
s2.putdata();
cout<<"C3=";
s3.putdata();
getch();
}
  

c
c
c
c
c
c
c
******************************************************************
C1 = 2.5 + i 3.5
C2 = 1.6 + i 2.7
C3 = 4.1 + i 6.2
******************************************************************

  

You might also like