从两个相同派生类派生一个类

时间:2016-12-19 12:05:58

标签: c++ class virtual derived-class

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()函数?

2 个答案:

答案 0 :(得分:2)

此处的问题是您拥有从shaperectangletriangle的虚拟继承,因此rectangletriangle的构造函数不会初始化shape。您的someShape构造函数等同于:

root::someShape::someShape(double rh, double rw, double th, double tw) : 
    rectangle(rh,rw), triangle(th,tw), shape() {

}

这就是您获得默认值11的原因。另请注意,由于您具有虚拟继承,因此无法为矩形和三角形存储不同的heightwidth。你的构造函数应该是这样的:

root::someShape::someShape(double h, double w) : 
    rectangle(h, w), triangle(h, w), shape(h, w) {

}

或者,如果您不想拥有shape的单个实例,则应删除rectangletriangle的虚拟继承。

另见c++ virtual inheritance

答案 1 :(得分:1)

问题在于你正在使用virtual关键字进行虚拟继承,即矩形和三角形继承形状。因此,在派生类之间共享一个shape类的实例。因此,编译器完全跳过从矩形和三角形到形状的构造函数调用。

检查以获取更多详细信息: http://www.cprogramming.com/tutorial/virtual_inheritance.html https://isocpp.org/wiki/faq/multiple-inheritance