派生函数不返回正确的数据值

时间:2013-05-10 07:05:27

标签: c++ class inheritance base

仍在继承程序,基类是一个Shape,有三个派生类:矩形,圆形和方形(Square从Rectangle派生)。当我通过相应的构造函数设置数据值时,当我显示它们时,我得到每个派生类的数据成员的假值我不是正确设置它们(我的猜测)或者我没有正确显示它们。这是一段代码片段。

class Shape
{
     public:
     Shape(double w = 0, double h = 0, double r = 0)
     {
          width = w;
          height = h;
          radius = r;
     }

     virtual double area() = 0;
     void display();

     protected:
     double width;
     double height;
     double radius;
};

一个派生类:

class Rectangle : public Shape
{
     public:
     Rectangle(double w, double h) : Shape(w, h)
     {
     }

     double area();
     void display();      
};

Rectangle的显示功能:

double Rectangle::area()
{
    return width * height;
}

这是我的主要():

#include<iostream>
#include "ShapeClass.h"

using namespace std;

int main()
{   
    Rectangle r(3, 2);
    Circle c(3);
    Square s(3);

    c.display();
    s.display();
    r.display();


    system ("pause");
    return 0;
}

完成ShapeClass.cpp:

#include<iostream>
#include "ShapeClass.h"

using namespace std;

double Shape::area()
{
    return (width * height);
}

double Rectangle::area()
{
    return width * height;
}

double Circle::area()
{
   return (3.14159 * radius * radius);
}

double Square::area()
{
    return width * width;
}

void Square::display()
{
    cout << "Side length of square: " << width << endl;
    cout << "Area of square: " << this->area() << endl;
}

void Circle::display()
{
    cout << "Radius of circle: " << radius << endl;
    cout << "Area of circle: " << this->area() << endl;
}

void Rectangle::display()
{
    cout << "Width of rectangle: " << width << endl;
    cout << "Height of rectangle: " << height << endl;
    cout << "Area of rectangle: " << this->area() << endl;
}

1 个答案:

答案 0 :(得分:0)

您还需要在area()中虚拟Rectangle功能:

class Rectangle : public Shape
{
     public:
     Rectangle(double w, double h) : Shape(w, h)
     {
     }

     virtual double area();
     void display();      
};

现在,请记住,如果您希望在特定形状中覆盖display()函数(在您的示例中,我看到Rectangle也具有display()函数),那么你需要在两个类中都使它成为虚拟的:

virtual void display();

编辑:我尝试了以下代码,它运行得很好。它基于您的代码,因此您可能会对项目的构建方式或编译/链接方式存在问题。

#include <iostream>

using namespace std;

class Shape
{
     public:
     Shape(double w = 0, double h = 0, double r = 0)
     {
          width = w;
          height = h;
          radius = r;
     }

     virtual double area() = 0;
     virtual void display() = 0;

     protected:
     double width;
     double height;
     double radius;
};

class Rectangle : public Shape
{
     public:
     Rectangle(double w, double h) : Shape(w, h)
     {
     }

     virtual double area() { return width * height; }
     virtual void display() 
     {
        cout << "Width of rectangle: " << width << endl;
        cout << "Height of rectangle: " << height << endl;
        cout << "Area of rectangle: " << this->area() << endl;
     }
};



int main(int argc, char* argv[])
{
    Rectangle r(3, 2);

    r.display();

    system ("pause");

    return 0;
}