如何使用 类 实现多级继承
How to achieve Multilevel Inheritance with classes
我正在尝试做一些从形状 class 到矩形、圆形和三角形 class 的多级继承。我需要从 Rectangle 继承一个 Square class 并打印面积、信息等,以及从 Circle 继承 Ellipse,从 Triangle 继承 Isosceles。到目前为止,我的第一个继承 classes 工作正常,但每当我尝试使 "grandchildren" class 工作时,我都无法使它工作。我不知道我可能做错了什么。这是代码:
#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 时出现错误:
class Square : public Rectangle{
private:
float length;
public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}
我注释掉了椭圆和等腰线 classes 只是为了我可以正确设置正方形并在以后不使用它们。
这是我第一次提问,所以如果有什么不正确的地方请告诉我。
谢谢你的帮助。
在你的广场class我相信我发现了一个错误...
尝试使用方形构造函数执行以下操作:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){}
与您所拥有的相反...这将修复您在 Square class 中遇到的错误。
造成差异的原因是,当您将参数从 Square 构造函数传递给 Rectangle 构造函数时,您留下了一些未初始化的参数(在 Rectangle 构造函数中)。
我正在尝试做一些从形状 class 到矩形、圆形和三角形 class 的多级继承。我需要从 Rectangle 继承一个 Square class 并打印面积、信息等,以及从 Circle 继承 Ellipse,从 Triangle 继承 Isosceles。到目前为止,我的第一个继承 classes 工作正常,但每当我尝试使 "grandchildren" class 工作时,我都无法使它工作。我不知道我可能做错了什么。这是代码:
#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 时出现错误:
class Square : public Rectangle{
private:
float length;
public:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm){}
我注释掉了椭圆和等腰线 classes 只是为了我可以正确设置正方形并在以后不使用它们。 这是我第一次提问,所以如果有什么不正确的地方请告诉我。 谢谢你的帮助。
在你的广场class我相信我发现了一个错误...
尝试使用方形构造函数执行以下操作:
Square(string nm, float l):length(l),Rectangle::Rectangle(nm, l, l){}
与您所拥有的相反...这将修复您在 Square class 中遇到的错误。
造成差异的原因是,当您将参数从 Square 构造函数传递给 Rectangle 构造函数时,您留下了一些未初始化的参数(在 Rectangle 构造函数中)。