Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何用类实现多级继承_C++_Oop_Inheritance_Multi Level - Fatal编程技术网

C++ 如何用类实现多级继承

C++ 如何用类实现多级继承,c++,oop,inheritance,multi-level,C++,Oop,Inheritance,Multi Level,我正在尝试从Shape类到Rectangle、Circle和Triangle类进行多级继承。从矩形我需要继承一个正方形类,并打印面积,信息等。。以及椭圆从圆和等腰三角形。到目前为止,我的第一个继承课程运行良好,但每当我试图让“孙子”课程运行时,我都无法让它运行。我不知道我可能做错了什么。代码如下: #include <iostream> using namespace std; class Shape{ protected: string name; float are

我正在尝试从Shape类到Rectangle、Circle和Triangle类进行多级继承。从矩形我需要继承一个正方形类,并打印面积,信息等。。以及椭圆从圆和等腰三角形。到目前为止,我的第一个继承课程运行良好,但每当我试图让“孙子”课程运行时,我都无法让它运行。我不知道我可能做错了什么。代码如下:

 #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;
}
我注释掉了Ellipse和Isosecles类,这样我就可以正确地设置Square,以后再使用它们。 这是我第一次问问题,所以如果有不正确的地方请告诉我。
谢谢你的帮助。

在你的Square课上,我想我发现了一个错误

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

Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){} 
与你所拥有的相反。。。这将修复您在Square类中遇到的错误


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

在Square类中,我相信我发现了一个错误

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

Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){} 
与你所拥有的相反。。。这将修复您在Square类中遇到的错误


产生差异的原因是,当您从方形构造函数向矩形构造函数传递参数时,您未初始化某些参数(在矩形构造函数中)。

应该有一些错误指示,否则,您如何知道您有问题?编译程序时有错误吗?当您执行它时,它不工作吗?如果不工作,它会做什么?“我在设置Square类时出错”错误是什么?很可能您需要将
Square(string nm,float l):length(l),Rectangle::Rectangle(nm){}
更改为类似
Square(string nm,float l):Rectangle(nm),length(l){}
,当然,
Rectangle()
构造函数接受多个参数,因此这甚至不是答案……在某些情况下,您的继承基于属性的数量(椭圆与圆)和其他时间(矩形与正方形)。在实践中,两者都不好,最好使用单一级别(即与矩形处于同一级别的正方形)或只保留更一般的形状(即矩形、椭圆和三角形)。应该有一些错误指示,否则,您如何知道您有问题?编译程序时有错误吗?当您执行它时,它不工作吗?如果不工作,它会做什么?“我在设置Square类时出错”错误是什么?很可能您需要将
Square(string nm,float l):length(l),Rectangle::Rectangle(nm){}
更改为类似
Square(string nm,float l):Rectangle(nm),length(l){}
,当然,
Rectangle()
构造函数接受多个参数,因此这甚至不是答案……在某些情况下,您的继承基于属性的数量(椭圆与圆)和其他时间(矩形与正方形)。在实践中,两者都不好,最好使用单一级别(即与矩形处于同一级别的正方形)或只保留更一般的形状(即矩形、椭圆和三角形)。哇,我真的尝试了很多选项,除了这一个。这给了我一些警告,但至少程序可以编译。非常感谢。哇,我真的尝试了很多选择,除了这个。这给了我一些警告,但至少程序可以编译。谢谢。