AIM:
To implement a C++ program to find out the area of the rectangle and triangle using hierarchical inheritance
To implement a C++ program to find out the area of the rectangle and triangle using hierarchical inheritance
ALGORITHM:
Step 1: Include the header files
Step 2: Declare the base class polygon.
Step 3: Declare and define the function input () to get the width and height.
Step 4: Declare the two derived classes rectangle and triangle
Step 5: Calculate the area using width and height
Step 6: Create objects for the derived classes.
Step 7: Declare the derived class object, call the functions input () and calculate the area
PROGRAM:
#include<iostream.h>
#include<conio.h>
class polygon
{
protected:
int width, height;
public:
void input(int x, int y)
{
width = x;
height = y;
}
};
class rectangle : public polygon
{
public:
int areaR ()
{
return (width * height);
}
};
class triangle : public polygon
{
public:
int areaT ()
{
return (width * height / 2);
}
};
void main ()
{
clrscr();
rectangle rect;
triangle tri;
rect.input(6,8);
tri.input(6,10);
cout <<"Area of Rectangle: "<<rect.areaR()<< endl;
cout <<"Area of Triangle: "<<tri.areaT()<< endl;
getch();
}
SAMPLE OUTPUT:
Area of Rectangle: 48
Area of triangle: 30
RESULT:
Thus a C++ program to find out the area of the rectangle and triangle using hierarchical inheritance is implemented successfully.
No comments:
Post a Comment