exercise.h 如下
#ifndef EXERCISE_H_
#define EXERCISE_H_
// ROOT namespace
namespace root{
// USHORT definition
typedef unsigned short ushort;
// PI DEFINITION
const double PI = 3.141592;
class shape {
double height;
double width;
public:
shape(double h = 1, double w = 1);
virtual ~shape(){}
double getHeight() const;
double getWidth() const;
virtual double area() const = 0;
};
class rectangle : virtual public shape{
public:
rectangle(double height = 1, double width = 1);
double area() const;
};
class triangle : virtual public shape {
public:
triangle(double h = 1, double w = 1);
double area() const;
};
class someShape : public rectangle, public triangle{
public:
someShape(double rh = 1, double rw = 1, double th = 2, double tw = 2);
double area() const;
double trySomething() const;
};
} // NAMESPACE
#endif /* EXERCISE_H_ */
exercise.cpp 就像这样
#include <iostream>
#include <cmath>
#include "exercise.h"
using std::cout;
using std::cin;
using std::endl;
using root::ushort;
// BEGIN SHAPE CLASS
root::shape::shape(double h, double w) : height(h), width(w){
}
double root::shape::getHeight() const{
return this->height;
}
double root::shape::getWidth() const{
return this->width;
}
// END SHAPE CLASS
// BEGIN RECTANGLE CLASS
root::rectangle::rectangle(double h, double w) : shape(h,w){
}
double root::rectangle::area() const{
return this->getHeight() * this->getWidth();
}
// END RECTANGLE CLASS
// BEGIN TRIANGNLE CLASS
root::triangle::triangle(double h, double w) : shape(h,w){
}
double root::triangle::area() const{
return this->getHeight() * this->getWidth() / 2;
}
// END TRIANGLE CLASS
root::someShape::someShape(double rh, double rw, double th, double tw) : rectangle(rh,rw), triangle(th,tw){
}
double root::someShape::area() const {
return rectangle::area();
}
double root::someShape::trySomething() const{
return triangle::getHeight() * rectangle::getWidth();
}
并在主
#include <iostream>
#include "exercise.h"
using std::cout;
using std::cin;
using std::endl;
int main(){
root::shape *ptrShape;
ptrShape = new root::someShape(3,2,4,3);
cout << "shape area: " << ptrShape->area() << endl;
delete ptrShape;
}
当我创建someShape对象时,我只获得了默认值。虽然我启动了派生类。还有另外一件事。事实上,我们分别导出了矩形和三角形,并从这些对象中导出了另一个对象。编译器将如何决定使用哪个area()函数?
答案 0 :(得分:2)
此处的问题是您拥有从shape
到rectangle
和triangle
的虚拟继承,因此rectangle
和triangle
的构造函数不会初始化shape
。您的someShape
构造函数等同于:
root::someShape::someShape(double rh, double rw, double th, double tw) :
rectangle(rh,rw), triangle(th,tw), shape() {
}
这就是您获得默认值1
和1
的原因。另请注意,由于您具有虚拟继承,因此无法为矩形和三角形存储不同的height
和width
。你的构造函数应该是这样的:
root::someShape::someShape(double h, double w) :
rectangle(h, w), triangle(h, w), shape(h, w) {
}
或者,如果您不想拥有shape
的单个实例,则应删除rectangle
和triangle
的虚拟继承。
答案 1 :(得分:1)
问题在于你正在使用virtual关键字进行虚拟继承,即矩形和三角形继承形状。因此,在派生类之间共享一个shape类的实例。因此,编译器完全跳过从矩形和三角形到形状的构造函数调用。
检查以获取更多详细信息: http://www.cprogramming.com/tutorial/virtual_inheritance.html https://isocpp.org/wiki/faq/multiple-inheritance