我有一個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