0

I have created a base class Polygon and declared two data members - height and width as public. Also a virtual function - calculateArea(). Then the two derived classes rectangle and triangle have a function calculateArea(). I want to override the base class function and each of the derived class use its own calculateArea(). The output is not coming as expected and it is 0 or some random value. You can see the code below and tell me where I am going wrong.

#include<bits/stdc++.h>
using namespace std;

class polygon{  
    public:
        int height;
        int width;
        void setValue()
        {
            cout<<"\nEnter height and width ";
            cin>>height>>width;
        }
        virtual int calculateArea(){
        }
};

//Inheritance
class rectangle:public polygon
{
    private:
        string name;
    public:
        int calculateArea()
        {
            return height*width;
        }
};

class triangle:public polygon
{
    public:
        int calculateArea()
        {
            return (height*width)/2;
        }
};

int main()
{
    polygon p;
    rectangle r;
    triangle t;
    
    polygon* base;
    base = &p;
    base->setValue();
    
    base=&r;
    cout<<"\nArea of rectangle is "<<base->calculateArea();
    
    base=&t;
    cout<<"\nArea of triangle "<<base->calculateArea(); 
    return 0;
}
3
  • It can. But r's width and height are uninitialized. So are t's width and height. Commented Jun 21, 2022 at 10:52
  • You are never setting any width and height for r or t. Also calculateArea in polygon should probably be pure virtual. Commented Jun 21, 2022 at 10:54
  • It can. But before the call of base->setValue(), base points at p so the call of base->setValue() reads values of p.height and p.width. Then you assign base so it points at a r, who's height and width members are unintialised. public access does not mean that p and r somehow share the same data - they are distinct objects. Commented Jun 21, 2022 at 10:58

1 Answer 1

4

There are 3 distinct objects in main. A polygon called p, a rectangle called r, and a triangle t. You call setValue only on the polygon object.

The other objects have their members uninitialized. You should initialize the members and then I suppose you want to call setValue for the other instances as well:

class polygon{  
    public:
        int height = 0; // initializer for members
        int width = 0;
// ....

// in main
// ok
polygon* base;
base = &p;
base->setValue();

base=&r;
base->setValue(); // <-- this was missing
std::cout<<"\nArea of rectangle is "<<base->calculateArea();

base=&t;
base->setValue(); // <-- this was missing
std::cout<<"\nArea of triangle "<<base->calculateArea(); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.