Friday, February 25, 2011

Inheritance


#include<iostream.h>
#include<conio.h>
#include<math.h>

class shape
{
public:
float a;
};

class triangle: public shape
{
int a,b,c;
float s;

public:
 triangle(int x,int y,int z)
 {
 a=x;b=y;c=z;
 s=(a+b+c)/2.0;
 }

 void area()
 {
 a=sqrt(s*(s-a)*(s-b)*(s-c));
 }

 void show()
 {
 cout<<"\nThe Area of the Triangle is : "<<a;
 }

};

class rectangle: public shape
{
int l,b;

public:
 rectangle(int x,int y)
 {
 l=x;
 b=y;
 }

 void area()
 {
 a=l*b;
 }

 void show()
 {
 cout<<"\n"<<"The Area of the Rectangle is : "<<a;
 }

};

class circle: public shape
{
int r;
public:

 circle(int x)
 {
 r=x;
 }

 void area()
 {
 a=3.14*r*r;
 }

 void show()
 {
 cout<<"\n"<<"Area of the Circle is : "<<a;
 }

};

void main()
{
clrscr();

triangle ob(20,20,20);
ob.area();
ob.show();

rectangle ob1(6,5);
ob1.area();
ob1.show();

circle ob2(7);
ob2.area();
ob2.show();

getch();
}



No comments:

Post a Comment