help in C++ programing by answer this question

Discussion in 'C++' started by suly, Jan 3, 2011.

  1. suly

    suly New Member

    Joined:
    Jan 3, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    the question is to creat abase class called shape and we need to use this class to store two double type values that could be used to compute the area of figures.then we need to derive two specific class called tringle and rectangle from the base class shape. after that add the base class amember function get data() to intilize base class data members and another member function display_area to compute and display the area of figures.then we need to make display area as avirtual function and redefine this function in the derived class to suit their requirments.
    the hole idea from the question is to use the three class to design aprogram that will accept dimensions of triangle or rectangle interactively and display the area
    PS: the two values given as input will be treated as lengths of two sides in the case of rectangle and abase and height in the case of triangle
     
  2. Rakesh Kumar Pahwa

    Rakesh Kumar Pahwa New Member

    Joined:
    Jan 13, 2011
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<iostream> 
    using namespace std; 
    class shape 
    { 
    protected: 
            double m_dParam1; 
            double m_dParam2; 
    public: 
            shape(double param1=0,double param2=0):m_dParam1(param1),m_dParam2(param2){} 
            virtual ~shape(){} 
            virtual double calcArea()=0; 
            void displayArea(double area) 
            { 
                    cout<<"Area of " << typeid(*this).name()<<" = "<<area; 
            } 
    }; 
    
    class triangle: public shape 
    { 
    public: 
            triangle(double param1=0,double param2=0):shape(param1,param2){} 
            ~triangle(){} 
            double calcArea() 
            { 
                    cout<<"inside triangle area"<<endl; 
                    //code to calculate area for triangle and return it... 
            } 
    }; 
    
    
    class rectangle: public shape 
    { 
    public: 
            rectangle(double param1=0,double param2=0):shape(param1,param2){} 
            ~rectangle(){} 
            double calcArea() 
            { 
                    cout<<"inside rectangle area"<<endl; 
                    //code to calculate area for rectangle and return it... 
            } 
    }; 
    
    int main() 
    { 
            shape *shapeObj = new triangle(10.5,20.5); 
            shapeObj->displayArea(shapeObj->calcArea());         
            shapeObj = new rectangle(10.5,12.5); 
            shapeObj->displayArea(shapeObj->calcArea());         
            return 0; 
    }
     
  3. Rakesh Kumar Pahwa

    Rakesh Kumar Pahwa New Member

    Joined:
    Jan 13, 2011
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    above code may be helpful to you...:)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice