简体   繁体   中英

Inheriting from an abstract class in CPP

I've been learning about CPP inheritance and I cam across this question:

   Class polygon contains data member width and height and public method set_value() to assign values to width and height.
 Class Rectangle and Triangle are inherited from polygon class. 
the classes contain public method calculate_area() to calculate the area of Rectangle and Triangle.
 Use base class pointer to access the derived class object and show the area calculated. 

I wrote the following program for this problem:

#include <iostream>
using namespace std;
class Polygon{
    float height,width;
    public:
    void set_value(float height, float width){
        this->height=height;
        this->width=width;
    }
    virtual void calculate_area()=0;
};
class Rectangle:public Polygon{
    float height,width;
    public:
    void calculate_area(){
        cout << "Area of the rectangle is " << height*width <<endl;
    }
};
class Triangle:public Polygon{
    float height,width;
    public:
    void calculate_area(){
        cout << "Area of the triangle is " << 0.5*height * width << endl;
    }
};
int main(){
    Rectangle r1;
    r1.set_value(10,20);
    Triangle t1;
    t1.set_value(10,20);
    Polygon *p1;
    Polygon *p2;
    p1=&r1;
    p2=&t1;
    p1->calculate_area();
    p2->calculate_area();
    return 0;
}

However, on executing, it shows area of rectangle and triangle as zero. On further debugging, I found out that all dimensions (ie height and width of rectangle and triangle have value as zero). Can someone tell what's wrong with the code?

In your code Triangle and Rectangle inherit from Polygon , hence they also inherit Polygon s members height and width . Its those members that are used in Polygon s methods. Though they are private , hence cannot be accessed outside of Polygon .

Triangle and Rectangle have members height and width in addition to those inherited from Polygon . In Triangle and Rectangle s methods you are using those members. They are distinct from the members inherited from Polygon . Members of Triangle and Rectangle you do not initialize.

TL;DR: One possible fix is to grant derived classes access to the members by declaring them protected and remove the superflous definition of members in the derived classes:

#include <iostream>
using namespace std;
class Polygon{
    protected:
    float height,width;
    public:
    void set_value(float height, float width){
        this->height=height;
        this->width=width;
    }
    virtual void calculate_area()=0;
};
class Rectangle:public Polygon{
    public:
    void calculate_area() override {
        cout << "Area of the rectangle is " << height*width <<endl;
    }
};
class Triangle:public Polygon{    
    public:
    void calculate_area() override {
        cout << "Area of the triangle is " << 0.5*height * width << endl;
    }
};
int main(){
    Rectangle r1;
    r1.set_value(10,20);
    Triangle t1;
    t1.set_value(10,20);
    Polygon *p1;
    Polygon *p2;
    p1=&r1;
    p2=&t1;
    p1->calculate_area();
    p2->calculate_area();
    return 0;
}

Output :

Area of the rectangle is 200
Area of the triangle is 100

Note that I added override to the implementations of the virtual methods. When the overrides are ok, then it has not impact, but it will trigger a compiler error in case the method marked as override does actually not override (something that can happen easily by typos).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM