首页 > 解决方案 > 如何使用类实现多级继承

问题描述

我正在尝试从 Shape 类到 Rectangle、Circle 和 Triangle 类进行一些多级继承。从 Rectangle 我需要继承一个 Square 类并打印区域、信息等。以及来自 Circle 的 Ellipse 和来自 Triangle 的 Isosceles。到目前为止,我的第一个继承类工作正常,但每当我试图让“孙子”类工作时,我都无法让它工作。我不知道我可能做错了什么。这是代码:

 #include <iostream>
 using namespace std;

 class Shape{
 protected:
 string name;
 float area;

 public:
 Shape(string nm):name(nm){}

 //Getters
 string     getName(){    return name;    }
 float    getArea(){}

 //Setters
 virtual void    setArea(){}

 //Print
 virtual void printInfo()
 {
    cout << "Name: " << name << " Color: " << endl;
}

};

class Rectangle :  public Shape{
private:
float length, width;

public:
Rectangle(string nm, float l, float w):Shape::Shape(nm), length(l), width(w){}
Shape::getName();

//Setters
void setArea(){ area = length*width; }

void printInfo(){
    //Shape::printInfo();
    cout << "Name: " << name << " L: " << length << " W: " << width << " A: " << area << endl;
}
};

class Square : public Rectangle{

private:
float length;

public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}
float     getLength(){return length;}

//Setters
void setArea(){ area = length *length; }

};

class Circle : public Shape{
private:
float radius;
const float pi = 3.0;

public:
Circle(string nm, float r):Shape::Shape(nm), radius(r){}

//Setters
void setArea(){ area = pi*radius*radius; }

void printInfo(){
    //Shape::printInfo();
    cout << "Name: " << name << " R: " << radius << " A: " << area <<      endl;
}
};

 //class Ellipse : public Circle{
 //
 //private:
 //    float length, width, radius1, radius2;
   //
//public:
//    Ellipse(string nm, int clr, float l, float w);
//
//    //Setters
//void setArea(){ area = radius1 * radius2; }
//
//};

class Triangle : public Shape{
private:
float a, base, c, height;

public:
Triangle(string nm, float a, float b, float c, float h):Shape::Shape(nm), a(a), base(b), c(c), height(h){}

//Setters
void setArea(){ area = (base*height)/2; }

void printInfo(){
    //Shape::printInfo();
    cout << "Name: " << name << " Color: " << " A: " << a << " Base: " << base <<  " C: " << c  << " H: " << height << " P: " << " A: " << area << endl;
}
};

//class Isosceles : public Triangle{
//
//private:
//    float base, height;
//
//public:
//    Isosceles(string nm, int clr, float l, float w);
//
//    //Setters
//    void setArea(){ area = (base*height)/2; }
//
//}; 

int main() {

Rectangle r("Rectangle", 10, 20);
Circle c("Circle", 1);
Triangle tt("Triangle", 2, 2, 3, 3);
Square ss("Square", 10);

Shape* s;
Shape* t;
Shape* u;
Shape* v;
s = &r;
t = &c;
u = &tt;
v = &ss;

//Set and print area of Rectangle
s->setArea();
s->printInfo();

//Set and print area of Circle
t->setArea();
t->printInfo();

//Set and print area of Triangle
u->setArea();
u->printInfo();

//Set and print area of Rectangle
v->setArea();
v->printInfo();


return 0;
}

在这里设置 Square 类时出现错误:

class Square : public Rectangle{

private:
float length;

public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}

我注释掉了 Ellipse 和 Isosceles 类,这样我就可以正确设置 Square 并且以后不再使用它们。这是我第一次问问题,所以如果有什么不对的地方,请告诉我。感谢您的帮助。

标签: c++oopinheritancemulti-level

解决方案


在你的 Square 课上,我相信我发现了一个错误......

尝试使用您的方形构造函数执行以下操作:

Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){} 

与您所拥有的相反...这将解决您在 Square 类中遇到的错误。

产生差异的原因是,当您从 Square 构造函数向 Rectangle 构造函数传递参数时,您留下了一些未初始化的参数(在 Rectangle 构造函数中)。


推荐阅读