C++ 从两个相同的派生类派生一个类
练习。h如下所示C++ 从两个相同的派生类派生一个类,c++,class,virtual,derived-class,C++,Class,Virtual,Derived Class,练习。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: sha
#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_ */
锻炼。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();
}
#包括
#包括
#包括“exercise.h”
使用std::cout;
使用std::cin;
使用std::endl;
使用root::ushort;
//开始形体课
根::形状::形状(双h,双w):高度(h),宽度(w){
}
双根::形状::getHeight()常量{
返回此->高度;
}
双根::形状::getWidth()常量{
返回此->宽度;
}
//端部形状类
//开始矩形类
根::矩形::矩形(双h,双w):形状(h,w){
}
双根::矩形::面积()常数{
返回此->getHeight()*此->getWidth();
}
//结束矩形类
//开始三角形课程
根::三角形::三角形(双h,双w):形状(h,w){
}
双根::三角形::面积()常数{
返回this->getHeight()*this->getWidth()/2;
}
//结束三角形类
根::someShape::someShape(双右,双右,双th,双tw):矩形(右,右),三角形(th,tw){
}
双根::someShape::area()常量{
返回矩形::区域();
}
双根::someShape::trySomething()常量{
返回三角形::getHeight()*矩形::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;
}
#包括
#包括“exercise.h”
使用std::cout;
使用std::cin;
使用std::endl;
int main(){
根::形*ptrShape;
ptrShape=新根::someShape(3,2,4,3);
这里的问题是你有一个从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
。另外,请注意,由于您有虚拟继承,您不能为矩形和三角形存储不同的高度和宽度。您的构造函数应该类似于:
root::someShape::someShape(double h, double w) :
rectangle(h, w), triangle(h, w), shape(h, w) {
}
或者,如果不希望有一个形状
的实例,则应删除矩形
和三角形
的虚拟继承
另请参见。这里的问题是,您有一个从形状
到矩形
和三角形
的虚拟继承,因此矩形
和三角形
的构造函数不会初始化形状
。您的someShape
构造函数相当于:
root::someShape::someShape(double rh, double rw, double th, double tw) :
rectangle(rh,rw), triangle(th,tw), shape() {
}
这就是为什么您会得到默认值1
和1
。另外,请注意,由于您有虚拟继承,您不能为矩形和三角形存储不同的高度和宽度。您的构造函数应该类似于:
root::someShape::someShape(double h, double w) :
rectangle(h, w), triangle(h, w), shape(h, w) {
}
或者,如果不希望有一个形状
的实例,则应删除矩形
和三角形
的虚拟继承
另请参见。问题在于您正在执行虚拟继承,即矩形和三角形使用virtual关键字继承shape。因此,派生类之间共享一个shape类实例。因此编译器完全跳过从矩形和三角形到shape的构造函数调用
查看此项了解更多详细信息:
问题在于您正在执行虚拟继承,即矩形和三角形使用virtual关键字继承shape。因此,派生类之间共享一个shape类实例。因此编译器完全跳过从矩形和三角形到shape的构造函数调用
查看此项了解更多详细信息:
您得到哪个输出?您期望哪些值?void root::someShape::trySomething()const{cout您得到哪个输出?您期望哪些值?void root::someShape::trySomething()const{cout