我有一个Rectangle类和方形类,都具有相同的参数的构造函数(名称,宽度,高度)从基类继承构造函数?
于是我想到了创建一个基类称为形状和定义Shape.h构造和让Rectangle类和Square类继承Shape类的构造函数。
我面临的问题是,我真的不知道如何从Shape类继承构造函数到Rectangle和Square类。
对不起,如果我问一个简单的问题,因为我仍然是新的C++。
Shape.h
#include <iostream>
#ifndef Assn2_Shape_h
#define Assn2_Shape_h
class Shape {
public:
Shape() {
name = " ";
width = 0;
height = 0;
}
Shape(std::string name, double width, double height);
private:
std::string name;
double width,height;
};
#endif
Rectangle.h
#include <iostream>
#ifndef Assn2_Rectangle_h
#define Assn2_Rectangle_h
class Rectangle : public Shape {
//how to inherit the constructor from Shape class?
public:
Rectangle() {
}
private:
};
#endif
Square.h
#include <iostream>
#ifndef Assn2_Square_h
#define Assn2_Square_h
class Square: public Shape {
//how to inherit the constructor from Shape class?
public:
Square() {
}
private:
};
#endif
基础的默认构造函数将自动调用。 – billz
我明白了。非常感谢 – user2935569