从两个相同的类中得出一个类
Derive a class from two same-Derived classes
corest.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_ */
练习.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();
}
,在Main
中#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对象时,我只会得到默认值。尽管我启动了派生班。还有另一件事。事实是,我们分别得出了矩形和三角形,并且从那些对象中得出了另一个对象。编译器将如何确定要使用的区域()函数?
这里的问题是您具有从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
的虚拟继承。
另请参见C 虚拟继承。
问题在于您正在使用虚拟的矩形和三角形继承形状,使用虚拟关键字。因此,在派生类之间共享一个形状类的实例。因此,从矩形和三角形到形状的构造函数完全跳过了编译器。
检查此信息以获取更多详细信息:http://www.cprogramming.com/tutorial/virtual_inheritance.htmlhttps://isocpp.org/wiki/faq/multiple-inheritance
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 使用一个循环与两个循环
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 在Qt/C++中从一个代码构建两个略有不同的项目
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- 给定一个大小为 N 的数组 S,检查是否可以将序列拆分为两个序列
- 将两个 cpp 文件合并为一个 cpp 文件
- 为什么将两个浮点数相加会得到一个整数C++?
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 如何得到一个两个数字都相等或这个数字更大