26.8.15

Illustrate multiple inheritance

Write a c++ program :

b) to illustrate multiple inheritance

#include<iostream.h>

#include<conio.h> class m

{

protected : int m; public:

void getm()

{

cout<<"\nEnter the value for m : "; cin>>m;

}

}; class n

{

protected : int n; public:
void getn()

{

cout<<"\nEnter the value for n : "; cin>>n;
}

};

class p : public m , public n

{ public:

void display()

{

cout<<"\nM="<<m;

cout<<"\nN="<<n;

cout<<"\nM*N="<<m*n;

}


};

int main()

{

clrscr();

p p1;

p1.getm();

p1.getn();

p1.display();

getch();

return 0;

}


************************************OUTPUT******************************

Enter the value for m : 12

Enter the value for n : 10

M=12

N=10

M*N=120

illustrate multilevel inheritance

Write a c++ program :

a)  to illustrate multilevel inheritance.

#include<iostream.h>

#include<conio.h> class student

{

protected : int rollno; public : void get_num();

void put_num();

};

void student::get_num()

{

cout<<"\nEnter the roll number:\t"; cin>>rollno;
}

void student::put_num()

{

cout<<"Rollnumber: "<<rollno;

}

class test:public student

{

protected : float sub1,sub2; public:
void get_marks()

{

cout<<"\nEnter the sub1 marks: "; cin>>sub1;
cout<<"\nEnter the sub2 marks: "; cin>>sub2;

}

void put_marks()

{


cout<<"\nSub1="<<sub1;

cout<<"\nSub2="<<sub2;

}

};

class result : public test

{

float total;

public:

void display()

{

total=sub1+sub2; put_num(); put_marks();

cout<<"\nTotal= "<<total;

}

};

int main()

{

clrscr();

result r;

r.get_num();

r.get_marks();

r.display();

getch();

return 0;

}

*****************************OUTPUT******************************

Enter the roll number: 11111

Enter the sub1 marks: 67

Enter the sub2 marks: 56

Rollnumber: 11111

Sub1=67

Sub2=56

Total= 123

Derive a class ‘MAT’ from MATRIX class

Derive a class ‘MAT’ from MATRIX class created in program No. 8. Add a member function to overload ‘*’ operator to multiply two objects. (Single Inheritance)

#include<iostream.h>

#include<conio.h> class matrix

{

public: int m,n;

int a[20][20];

void mat_to_zero(int x,int y); void readmat();
void display();

};

void matrix::mat_to_zero(int x,int y)

{

m=x;

n=y;

for(int i=0;i<m;i++) for(int j=0;i<n;i++)

a[i][j]=0;

}

void matrix::readmat()

{

cout<<"enter the matrix element"; for(int i=0;i<m;i++)

for(int j=0;j<n;j++) cin>>a[i][j];

}

void matrix::display()

{

for(int i=0;i<m;i++)

{

cout<<"\n";


for(int j=0;j<n;j++)


cout<<" "<<a[i][j];

}

}

class mat:public matrix

{

public: mat operator * (mat obj);

};

mat mat::operator *(mat obj)

{

mat temp; temp.mat_to_zero(m,n); for(int i=0;i<m;i++)

for(int j=0;j<n;j++)

{

temp.a[i][j]=0; for(int k=0;k<n;k++)

{

temp.a[i][j]=temp.a[i][j]+a[i][j]*obj.a[j][i];

}

}

return temp;

}

int main()

{

int m1,n1; clrscr();

cout<<"enter the order of matrix: "; cin>>m1>>n1;

mat a,b,c; a.mat_to_zero(m1,n1); b.mat_to_zero(m1,n1); cout<<"enter the matrix 1: \n"; a.readmat();
cout<<"enter the matrix 2: \n";

b.readmat();

c=a*b;

cout<<"\n matrix 1: ";

a.display();

cout<<"\n matrix 2: ";

b.display();

cout<<"\n sum of two matrix: ";

c.display();

getch();

return 0;

}




*************************OUTPUT********************************

enter the order of matrix: 2 2 enter the matrix 1:

enter the matrix element2 2 2 2 enter the matrix 2:
enter the matrix element2 2 2 2


matrix 1: 
2 2 2 2

matrix 2: 
2 2 2 2

sum of two matrix: 
8 8 8 8

Create a 'MATRIX' class of size m X n

Create a 'MATRIX' class of size m X n. Overload the ‘+’ operator to add two MATRIX objects. Write a main function to implement it.


#include<iostream.h>
#include<conio.h> class mat

{

int m,n,a[20][20]; public:

mat(int x,int y); void readmat(); mat operator +(mat); void display();
};

mat :: mat(int x,int y)
{
m=x;n=y;
for(int i=0;i<m;i++)
{

for(int j=0;j<n;j++) a[i][j]=0;
}

}
void mat :: readmat()
{

cout<<"\nenter matrix elements\n"; for(int i=0;i<m;i++)
for(int j=0;j<n;j++) cin>>a[i][j];
}

mat mat:: operator +(mat obj)
{
mat temp(m,n); for(int i=0;i<m;i++) for(int j=0;j<n;j++)
{

temp.a[i][j]=a[i][j]+obj.a[i][j];
}
return temp;
}
void mat:: display()
{
int i,j; for(i=0;i<m;i++)

{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<"\t"<<a[i][j];
}

}
int main()

{

int m1,n1; clrscr();

cout<<"\nEnter the size(m,n) of matrix: "; cin>>m1>>n1;
mat a(m1,n1),b(m1,n1),c(m1,n1); cout<<"\nEnter martix 1: "; a.readmat();

cout<<"\nEnter matrix 2: "; b.readmat();
c=a.operator +(b); cout<<"\nFirst Matrix :\n"; a.display(); cout<<"\nSecond Matrix :\n"; b.display();

cout<<"\nmatrix 1+matrix 2: "; c.display();
getch(); return 0;
}

********************************OUTPUT**********************************

Enter the size(m,n) of matrix: 2 2

Enter martix 1: enter matrix elements 3 3 3 3


Enter matrix 2: enter matrix elements 4 4 4 4


First Matrix :
3
3
3
3
Second Matrix :
4
4
4
4

matrix 1 + matrix 2:
7
7
7
7

Create a class 'COMPLEX' to hold a complex number

Create a class 'COMPLEX' to hold a complex number. Write a friend function to add two complex numbers. Write a main function to add two COMPLEX objects.

#include<iostream.h>

#include<conio.h> class complex
{

float real,imag; public: void get_complex();

void show_complex();
friend complex add_complex(complex c1,complex c2);
};

void complex::get_complex()

{
cout<<"Enter real number :"; cin>> real;
cout<<"Enter Imaginary number :"; cin>>imag;
}
void complex::show_complex()
{
cout<<real<<"+i"<<imag;
}
complex add_complex(complex c1,complex c2)
{

complex c; c.real=c1.real+c2.real; c.imag=c1.imag+c2.imag; return c;

}

int main()

{
clrscr();
complex c1,c2,c3; c1.get_complex(); c2.get_complex(); c3=add_complex(c1,c2); cout<<"\nComplex Number 1 = "; c1.show_complex(); cout<<"\nComplex Number 2 = "; c2.show_complex();
cout<<"\nSum of Complex Number 1 and 2 = "; c3.show_complex();
getch(); 
return 0;


}

*******************************OUTPUT*****************************

Enter real number :12

Enter Imaginary number :10
Enter real number :3
Enter Imaginary number :5

Complex Number 1 = 12+i10

Complex Number 2 = 3+i5
Sum of Complex Number 1 and 2 = 15+i15