C++ Triangle and Rectangle - calculate area of Triangle and rectangle using data members from base class

C++ Triangle and Rectangle - calculate area of Triangle and rectangle using data members from base class

2.create class. Shape accepts two values (Data Type: Double). Create two derived classes Triangle and Rectangle ; calculate area of Triangle and rectangle using data members from base class. Use one virtual function to display data.

View Answers

May 8, 2012 at 3:51 PM

The given code defines a base class "Shape" to represent two different derived classes Triangle and Rectangle. Each of these two classes has a member function displayArea()to display the area of relevant shape on the screen and a member function getdata() to initialize base class data members. In case of Triangle class, these two values of base class will treated as base and height whereas in Rectangle these two values of base class will treated as length and breadth of rectangle.

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

    class Shape
    {
     protected :
     double x0, y0;
     public :
     void getdata(double x, double y)
     {
     x0 = x;
     y0 = y;
     }
     virtual void displayArea() = 0;
    };
    class Triangle : public Shape
    {
     public :
     void getdata(double x, double y)
     {
     Shape :: getdata(x,y);
     }
     void displayArea()
     {
     double area = (x0 * y0)/2.0;
     cout << "\n Area of triangle = " << area;
     }
    };
    class Rectangle : public Shape
    {
     public :
     void getdata(double x, double y)
     {
     Shape :: getdata(x,y);
     }
     void displayArea()
     {
     double area = (x0 * y0);
     cout << "\n Area of rectangle = " << area;
     }
    };
    void main()
    {
     Triangle *tptr;
     Rectangle *rptr;
     Shape *sptr[2];
     int i;
     tptr = new Triangle;
     rptr = new Rectangle;
     tptr->getdata(20.0,30.0);
     rptr->getdata(20.0,20.0);
     sptr[0] = tptr;
     sptr[1] = rptr;
     for(i=0; i<2; i++)
     sptr[i]->displayArea();
     getch();
    }

Ads









Related Tutorials/Questions & Answers:
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example... tutorial, we are going to describe how to draw a triangle, rectangle, arc, line or a round rectangle in one small application. Although we have already explained
triangle
also form the sides of a right-angle triangle The class should include proper data...triangle  Write a program that makes use of a class called Trianglethat: Reads three positive, nonzero integer values from the keyboard (assume
Advertisements
Rectangle
Rectangle  Could anybody help me on this problem, Write two Rectangle objects with the following properties: Rectangle1: Height 15 width 53 Y 25 X 15 Rectangle2: height 47 Width 60 Y 12 X 0 It's to be used four-argument
triangle
triangle  how to draw triangle numbers with stars in html with the help of javascript
By using Applet display a circle in a rectangle
By using Applet display a circle in a rectangle  Write a java applet to display a circle in a rectangle
Draw a Triangle using a Line2D
a Line2D. To draw a triangle, we are using Line2D class of package java.awt.geom.... draw three line segments using the class Line2D to create a triangle. ... Draw a Triangle using a Line2D   
J2ME Draw Triangle
J2ME Draw Triangle       As you already aware of the canvas class and it's use in J2ME application, we are using canvas class to draw the triangle on the screen. In this example
Java draw triangle draw method?
method for an triangle using the 'public void draw (graphics g ) method? im...; Here is an example that draws a triangle using drawPolygon method. import... want to use Line2D class to draw a triangle, then please go through
Triangle program
for this triangle   class Tri{ public static void main(String args[]){ for(int...Triangle program  1 121 12321 1234321 123454321 kindly give... a program for this triangle    1 121 12321 1234321 write a program
Triangle Program
;public class Triangle { public static void main(String[] args...Triangle Program  i want program code for 1 2 3...(""); } } }   include using namespace std; main(){ int n,l; cin>>n
plotting of points inside a rectangle
inside a rectangle such that any two points are at a distance of at least 3 units from each other. I want all possible configurations that are possible.For example ,if I have a Rectangle of 20m by 14m then one possible placement of points
Triangle pgm
Triangle pgm  how to write code fa this??? 1 222 33333 4444444 55555555 666666666
print rectangle pattern in java
print rectangle pattern in java  * * * * * * * how... this will helpful for you public class PrintRectangle { public static void main...++) { System.out.print(" "); for(int c=1; c<7; c
Code for a triangle.....
Code for a triangle.....   1 1 5 1 1 5 25 5 1 What is the source Code for above output
What will be the total cost for Golden Triangle Package tour from Delhi
What will be the total cost for Golden Triangle Package tour from Delhi   Hi, Can anyone give me the idea about Golden Triangle Package tour cost from New Delhi? I want's to travel by car. Thanks
ModuleNotFoundError: No module named 'rectangle'
ModuleNotFoundError: No module named 'rectangle'  Hi, My Python... 'rectangle' How to remove the ModuleNotFoundError: No module named 'rectangle' error? Thanks   Hi, In your python environment you
Triangle problem
Triangle problem  what's the logic for create pattern program how can i create any pattern program with in minute. like this 1 2 5 3 6 8 4 7 9 10
triangle shape
triangle shape  Hi, I have to create a program which its output is: + ++ +++ ++++ and this what I did so far : package symbols; import java.util.*; public class Symbols { public static void main(String[] args) { Scanner
Retrive the data from the table in data base using jdbc
Retrive the data from the table in data base using jdbc  Retrive the data from the table in data base using jdbc   JDBC Tutorials
Write a program to construct a triangle with the ?*?
programming will teach you the coding for constructing a shape of triangle by using '*'. First of all make a class named 'triangle' under the Java I/O package... Write a program to construct a triangle
ModuleNotFoundError: No module named 'triangle'
ModuleNotFoundError: No module named 'triangle'  Hi, My Python... 'triangle' How to remove the ModuleNotFoundError: No module named 'triangle... to install padas library. You can install triangle python with following
MySQL Area
MySQL Area  This example illustrates how to find the area of rectangle in MySQL. In this example we create a procedure to find the area of rectangle. In the query below we declare two integer type variable w and h with default
Golden triangle tours
Golden triangle tours  Hi, Can anyone tell me something about Golden triangle tours? Thanks   Hi, Golden Triangle tour cover Delhi, Agra and Jaipur. Please check India Golden Triangle Tours. Thanks
Golden Triangle Tours
Golden Triangle Tours  What is golden triangle tours ..and how it is related to the Agra and Jaipur and Delhi trip? Please explain me. Thanks
Floyd triangle
Floyd triangle  1 01 101 0101 10101 plz give me code for this   Hi, friend a sample code for displaying as above is as follows : public class FloydTriangleExample { public static void main(String[] args
Rectangle Image in Java
of all, class name for the rectangle component. The rectangle2D class describes... (width , height). Rectangle2D.Double(): This class is used to define a rectangle... Rectangle Image in Java      
ModuleNotFoundError: No module named 'rectangle-overlap'
ModuleNotFoundError: No module named 'rectangle-overlap'  Hi, My... named 'rectangle-overlap' How to remove the ModuleNotFoundError: No module named 'rectangle-overlap' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'rectangle-packer'
ModuleNotFoundError: No module named 'rectangle-packer'  Hi, My... named 'rectangle-packer' How to remove the ModuleNotFoundError: No module named 'rectangle-packer' error? Thanks   Hi, In your
Retrieving data from data base using jsp combo box
Retrieving data from data base using jsp combo box  Hi guys please help me , i have on GUI page int that Server type(like apache,jboss,weblogic) one... of the server it has to display the process name from database into the process name
Golden Triangle Tour - Enchanting Golden Triangle Tour
The best part of Golden Triangle Tour is that all these three cities are just four to five hours driving distance from each other
Java Swing: Draw rectangle on mouse click
; the method mouseReleased allowed us to erase the previous rectangle from the panel. The method g.drawRect() of Graphics class draws the rectangle...Java Swing: Draw rectangle on mouse click In this tutorial, you will learn how

Ads