Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program that calculates area of triangle and rectangle using inheritance

Posted By: Easy Tutor     Category: C++ Programming     Views: 101527

Write a generalized program that takes measurement of different shapes example

1) Triangle
2) Rectangle

and displays it's area using inheritance.

Code for Program that calculates area of triangle and rectangle using inheritance in C++ Programming




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

class shape
{
 protected:
       double x,y;
  public:
       virtualvoid get_data()=0;
       virtualvoid display_area()=0;
};

class triangle : public shape
{
   public:
    void get_data(void)
    {
       cout<<"\n\n=====Data Entry for Triangle=====\n\n";
       cout<<"Enter base and height respectively : ";
       cin>>x>>y;
    }
    void display_area(void)
    {
       cout<<"\n\n=====Area of Triangle=====\n\n";
       double aot;
       aot = 0.5 * x * y;
       cout<<"Area of Triangle is "<<aot;
    }
};

class rectangle : public shape
{
   public:
    void get_data(void)
    {
       cout<<"\n\n=====Data Entry for Rectangle=====\n\n";
       cout<<"Enter length of two sides  : ";
       cin>>x>>y;
    }
    void display_area(void)
    {
       cout<<"\n\n=====Area of rectangle=====\n\n";
       double aor;
       aor = x * y;
       cout<<"Area of Rectangle is "<<aor;
    }
};


void main()
{
clrscr();
triangle tri;
rectangle rect;
shape *list[2];
list[0]=&tri;
list[1]=&rect;

int choice;
while(1)
{
clrscr();
  cout<<"\n=====MEASURES OF DIFFERENT SHAPE=====\n";
  cout<<"\nChoose your choice\n";
  cout<<"1) Area of Triangle\n";
  cout<<"2) Area of Rectangle\n";
  cout<<"3) Exit\n";
  cout<<"Enter your choice:-";
  cin>>choice;
  switch(choice)
  {
     case 1 : list[0]->get_data();
          list[0]->display_area();
          getch();
          break;
     case 2 : list[1]->get_data();
          list[1]->display_area();
          getch();
          break;
     case 3 : goto end;
     default: cout<<"\n\nInvalid choice\nTry again\n";
          getch();
  }
}
end:
}
  
Share: 



Easy Tutor
Easy Tutor author of Program that calculates area of triangle and rectangle using inheritance is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
William Otto from Japan Comment on: Sep 23
HOW TO WRITE THE ALGORITHMS OF THE FOLLOWING QUESTION

1. Write a program in C++ that would print the words Hello World.
Program Code

2. Write a program in C++ to declare and Initialise Variables


3. Write a program in C++ to produce correct size of various data types
Program Code


4. Write a program to compare to two integers with the use of
relational operators

5. Write a program to accept two numbers and find the greatest.

6. Using a while loop write a program in C++ to accept a number
indicating how many times a block is going to be excuted.

7. Write a program to take an input a grade and display Grade Points
Average GPA

8. Write program in c++ a class with two methods to accept and display a
value from the key board using the object initialised.

9. This program calculates the area of a rectangle and displays it.

10. Write a program to calculate the area of a rectangle with the overloaded
constructor.


11. Write a program to calculate the area and perimeter of a
rectangle to Implement Inheritance


12. Write a Program to insert and display data entered by using pointer
notation.

13. Write a program To perform the write operation with in a file.

14. Write a Program for Exception Handling Divide by zero

15.write aprogrammers to accept and display the prime numbers within the range.

Tosin Ademoye from Japan Comment on: May 18
is not working


View All Comments