26.8.15

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


Create a class called 'TIME'

Create a class called 'TIME' that has

-     three integer data members for hours, minutes and seconds

-     constructor to initialize the object to zero

-     constructor to initialize the object to some constant value

-     member function to add two TIME objects

-     member function to display time in HH:MM:SS format

Write a main function to create two TIME objects, add them and display the result in HH:MM:SS format.

#include<iostream.h>

#include<conio.h> class Time

{
int hrs,min,sec;

public: Time()

{
hrs=0;min=0;sec=0;
}

Time(int a,int b,int c)

{
hrs=a; min=b;sec=c;
}

void addTime(Time tt1,Time tt2)

{
sec=tt1.sec+tt2.sec;
min=sec/60;
sec=sec%60;
min=min+tt1.min+tt2.min;
hrs=min/60;
min=min%60;
hrs=hrs+tt1.hrs+tt2.hrs;
}

void display()

{
cout<<hrs<<":"<<min<<":"<<sec<<endl;
}

};

int main()

{

clrscr();
Time t1(10,48,30);

Time t2(2,22,35); Time t3; t3.addTime(t1,t2); cout<<"\nTime 1 is :"; t1.display(); 
cout<<"\nTime 2 is :"; t2.display();

           cout<<"\nTime 1 + Time 2 is :";
          
          
           t3.display();
           
           getch();
           
    
  
           return(0);
}


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

Time 1 is :10:48:30

Time 2 is :2:22:35

Time 1 + Time 2 is :13:11:5