26.8.15

Create a base class called 'SHAPE'

 Create a base class called 'SHAPE' having

-          two data members of type double

-          member function get-data( ) to initialize base class data members

-          pure virtual member function display-area( ) to compute and display the area of

the geometrical object.

Derive two specific classes 'TRIANGLE' and 'RECTANGLE' from the base class. Using these three classes design a program that will accept dimension of a triangle / rectangle interactively and display the area.

#include<iostream.h>

#include<conio.h> class shape

{
protected:double x, y;
public:void getdata(double a, double b)
{
x=a;
y=b;
}
virtual void display_area()=0;
};
class triangle:public shape
{

double triangle_area; void display_area()
{

triangle_area=(1*x*y)/2;
cout<<"area of triangle is:"<<triangle_area<<endl;
}
};
class rectangle:public shape
{
double rectangle_area; void display_area()

{
rectangle_area=x*y;
cout<<"area of rectangle is:"<<rectangle_area;
}
};
int main()
{


clrscr(); shape *p; triangle t; rectangle r;

p=&t; p->getdata(10,30); p->display_area(); p=&r; p->getdata(20,30); p->display_area(); getch();

return 0;
}

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

area of triangle is:150 area of rectangle is:600


Write a C++ program to illustrate ‘this’ pointer

Write a C++ program to illustrate ‘this’ pointer and pointers to derived classes.


#include<iostream.h>

#include<conio.h> class BC
{

public: int b;

void show()
{
cout<<"b= "<<b<<endl;
}
BC findlarge(BC obj)
{
if(b>obj.b) return *this;

else
return obj;
}
};
class DC:public BC
{
public: int d;

void show()
{
cout<<"b= "<<b<<endl; cout<<"d= "<<d<<endl;

}
};
int main()
{
clrscr(); BC b1,b2; b1.b=10; b2.b=20;

BC Large=b1.findlarge(b2); cout<<"\n Largest is :"; Large.show();

BC *bptr; BC base; bptr=&base; bptr->b=100;

cout<<"Base pointer to base class\n"; bptr->show();
DC derived; bptr=&derived; bptr->b=200;

cout<<"Base pointer to base class\n";
bptr->show(); DC *dptr; dptr=&derived; dptr->d=300;
cout<<"Derived pointer to derived class\n"; dptr->show();
((DC*)bptr)->d=400; cout<<"Type conversion\n"; ((DC*)bptr)->show(); getch();
return 0;

}

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

Largest is :b= 20
Base pointer to base class b= 100
Base pointer to base class b= 200
Derived pointer to derived class b= 200
d= 300

Type conversion b= 200
d= 400

Create a 'STRING' class which overloads ‘ = = ' operator

Create a 'STRING' class which overloads ‘ = = ' operator to compare two STRING

objects.

#include<iostream.h>

#include<conio.h>
#include<string.h> class string

{

char *p; int len;
public:

string(){ } string( char *s)

{

len=strlen(s); p=new char[len+1]; strcpy(p,s);
}
friend int operator = =( string &s,string &t);
};

int operator = = ( string &s1,string &s2)

{
if(strcmp(s1.p,s2.p)= =0) return(1);
else return(0);

}

int main()

{
string s1,s2;

char str1[20],str2[20]; clrscr();

cout<<"\nEnter the first string : "; cin>>str1;
cout<<"\nEnter the second string : "; cin>>str2;
s1=str1;

s2=str2; if(s1= =s2)

cout<<"\nStrings are equal"; else
cout<<"\nStrings are not equal"; getch();
return 0;

}




*******************************OUTPUT 1********************************

Enter the first string : cpc

Enter the second string : cs

Strings are not equal

******************************OUTPUT 2**********************************

Enter the first string : CPC

Enter the second string : CPC

Strings are equal


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