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

 
 


Azim Ajmeri
C++ Triangle and Rectangle - calculate area of Triangle and rectangle using data members from base class
1 Answer(s)      a year ago
Posted in : Hibernate

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();
    }









Related Pages:
print rectangle triangle with ?*? using loops
print rectangle triangle with ?*? using loops  ...*; class Traingles { public static void main(String args[]) { for(int i=1;i<=5...(" "); } }   class Traingles { public static void main(String args
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
print a rectangle - Java Beginners
print a rectangle  how do I print a rectangleof stars in java using simple while loop?Assuming that the length n width of the rectangle is given...*; public class TrianglesDemo{ final static int WIDTH = 10; public static void main
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
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
;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
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
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
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
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
triangle
triangle  how to draw triangle numbers with stars in html with the help of javascript
Rectangle Canvas MIDlet Example
of rectangle in J2ME. We have created CanvasRectangle class in this example that extends to the Canvas class to draw the given types of rectangle. In this figure first two rectangle draw from the solid and dotted line and the other two
pascal triangle
the following link: C Pascal's Triangle Thanks...pascal triangle   given string : char str[]="123456789"; write a program in 'c'-language that displays the following: 1 232
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
DATA BASE
the data using a J Table.   Here is a code that connects to MySql...:3306/test", "root", "root" ); String sql = "Select name,address from data...DATA BASE  Create a program to establish a connection to the PCTBC
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   
Draw Ellipse in Rectangle
a rectangle and a circle respectively.  By using draw() method of class... Draw Ellipse in Rectangle     ... the rectangle. To draw an Ellipse inside the rectangle, we have defined two classes
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
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
How to make a Rectangle type pdf
How to make a Rectangle type pdf   ... make a pdf file in the rectangle shape irrespective of the fact whether... to perform. Inside the class declare a main method inside which we are going to write
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
Write a program for calculating area and perimeter of a rectangle
Write a program for calculating area and perimeter of a rectangle... of a rectangle.  First of all create a class named RecArea under Java.io.... This is an instance of class method and is used to convert a string to an integer. Define the area
Floyd's triangle
Floyd's triangle  Write a 'for' statment that will print Floyd's traingle.   Hi Friend, Try the following code: public class FloydTriangleExample { public static void main(String[] args) throws Exception { int k=0
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
Java Program Floyd's Triangle
. Rows of this triangle contains the consecutive numbers started from the number...Java Program Floyd's Triangle In this section you will read about how to write a Java program to print Floyd's triangle. This tutorial is about
c++
c++  write a programme that calculates the area and circumference...; class rect { int l,b; int area; int peri; public: void get_rect() { cout<<"\n... The area of the rectangle is :"<<area; cout<<"\n The perimeter
Class
is declared. First class is square which is using for getting the Area and Perimeter of the square and another class is rectangle which is using for getting... Area of rectangle : 6 Value of x in another class : 3 Area
India Golden Triangle Tours
become a world class metropolitan. The next destination in your golden triangle...India Golden Triangle Tours Golden Triangle India is a heady concoction.... These three destinations forming a triangle offer best of India in a single platter
Data base related question
Data base related question  sir my table has only one element(that is pno),i am using ms-access as backend. i put only one element i want to retrieve that element .how can i retrieve that element,using jdbc technology. please
Golden Triangle Tours India
Apart from these, there are numerous other architectural marvels in the city... infrastructure, world-class means of transportation like metro trains and dazzling shopping malls, the city has become a world class metropolitan
C Print Pascal Triangle
C Print Pascal Triangle In this section, you will learn how to display Pascal's triangle. A Pascal's triangle is a geometric arrangement of the binomial coefficients in a triangle.The rows of Pascal's triangle are conventionally
c++
c++  write a programme that calculates the area and circumference of a rectangle
class - Java Beginners
) Abstract class Shapes has one abstract method-area() and a concrete method display(). Class Rectangle should implement the abstract class shapes Interface Shapes has abstract methods-area(),display(). Classes Rectangle and Triangle should
class - Java Beginners
) Abstract class Shapes has one abstract method-area() and a concrete method display(). Class Rectangle should implement the abstract class shapes Interface Shapes has abstract methods-area(),display(). Classes Rectangle and Triangle should
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
C Program to Print Pascal Triangle - Development process
C Program to Print Pascal Triangle  I want C Program to Print Pascal Triangle.  Hi Friend, Try the following code: #include #include void main(){ int a[15][15],i,j,rows,num=25,k; printf("\n enter
insert excel value in to oracle data base
the data from the excel file using JDBC. For this you need to create dsn...insert excel value in to oracle data base  Hi All I am using... org.apache.poi.poifs.filesystem.POIFSFileSystem; public class Insert
triangle output
triangle output  program to get the following output
Golden Triangle Tour with Mata Vaishno Devi
Golden Triangle Tour with Mata Vaishno Devi Duration : 9 Nights 10 Days... triangle tour with Mata Vaishno Devi package, you will head... tourist attraction present in the city. Later go to visit area like Chandni
Java create triangle pattern
Java create triangle pattern In this tutorial, you will learn how to create a triangle shape. For this, user is allowed to enter number of lines to display the triangle of that length and a symbol to show the triangle in a particular
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      
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
how to perform a equqlity triangle in java
how to perform a equqlity triangle in java  below the output 1 2 3 4 6 5 7 9 10 8   Here is an example of pattern like 1 2 3 4 5 6 7 8 9 10 Example : public class NumberTriangleClass{ public static void
triangle output
triangle output  program to get the following output: * *   Hi Friend, Please specify the pattern properly. Thanks
Triangle pgm
Triangle pgm  how to write code fa this??? 1 222 33333 4444444 55555555 666666666
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
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
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
Data base - JDBC
"); con.close(); }//main }//class For this should i create a user defined data type...Data base  I want to do this Inserting a record with Object type column using PreparedStatement but wht should b the column datatype this is te
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.