我是C ++的新手,并且遇到了一些问题。我想创建一个抽象类,以及从该类继承的两个更抽象的类。最后,这两个类用于由具体类实现。这是我的标题和源文件:
/*
* Cylinder.h
*/
#ifndef CYLINDER_H_
#define CYLINDER_H_
class Shape{
public:
virtual ~Shape();
virtual const double area()=0;
virtual const void printDetails();
};
class Shape2D: virtual public Shape{
public:
virtual const double scope()=0;
};
class Shape3D: virtual public Shape{
public:
virtual const double volume()=0;
};
class Rectangle: public Shape2D{
private:
double a;
double H;
public:
Rectangle();
Rectangle(double a, double H);
Rectangle(Rectangle &r);
double getA() const;
double getH() const;
void setA(double a);
void setH(double H);
};
class Circle: public Shape2D{
private:
double r;
public:
Circle();
Circle(double r);
Circle(Circle &c);
double getR() const;
void setR(double r);
};
class Cylinder: public Shape3D{
private:
Circle base;
Rectangle wrapper;
public:
Cylinder();
Cylinder(Rectangle &r, Circle &c);
Cylinder(double a, double H, double r);
Cylinder(Cylinder &c);
double getHeight() const;
double getBaseRadius() const;
double getBaseArea() const;
double getBaseScope() const;
double getWrapperArea() const;
double getWrapperScope() const;
void setHeight(double H);
void setRadius(double r);
Rectangle* getWrapper() const;
Circle* getBase() const;
};
#endif /* CYLINDER_H_ */
/*
* Cylinder.cpp
*/
#include <iostream>
#include "Cylinder.h"
#include <math.h>
using namespace std;
void Shape2D::printDetails() const{
cout<<"\nArea: "<<area();
cout<<"\nScope: "<<scope();
}
void Shape3D::printDetails() const{
cout<<"\nArea: "<<area();
cout<<"\nVolume: "<<volume();
}
Rectangle::Rectangle(){
H=0.0;
a=0.0;
}
Rectangle::Rectangle(double a, double H){
this->H=H;
this->a=a;
}
Rectangle::Rectangle(Rectangle &r){
a=r.a;
H=r.H;
}
Rectangle::~Rectangle(){
cout<<"Rectangle deallocated.";
}
double Rectangle::getA() const{
return a;
}
double Rectangle::getH() const{
return H;
}
void Rectangle::setA(double a){
this->a=a;
}
void Rectangle::setH(double H){
this->H=H;
}
double Rectangle::scope() const{
return 2*a+2*H;
}
double Rectangle::area() const{
return a*H;
}
void Rectangle::printDetails() const{
cout<<"\nDimensions of rectangle: "<<a<<"*"<<H;
Shape::printDetails();
}
Circle::Circle(){
r=0.0;
}
Circle::Circle(double r){
this->r=r;
}
Circle::Circle(Circle &c){
r=c.r;
}
Circle::~Circle(){
cout<<"Circle deallocated.";
}
double Circle::getR() const{
return r;
}
void Circle::setR(double r){
this->r=r;
}
double Circle::scope() const{
return 2*r*M_PI;
}
double Circle::area() const{
return r*r*M_PI;
}
void Circle::printDetails() const{
cout<<"\nRadius of circle: "<<r;
Shape::printDetails();
}
Cylinder::Cylinder(){
wrapper=new Rectangle(0.0,0.0);
base=new Circle(0.0);
}
Cylinder::Cylinder(Rectangle &r, Circle &c){
base=c;
wrapper=r;
}
Cylinder::Cylinder(double a=0.0, double H=0.0, double r=0.0){
wrapper=new Rectangle(a,H);
base=new Circle(r);
}
Cylinder::Cylinder(Cylinder &c){
c.base=new Circle(c.base);
c.wrapper=new Rectangle(c.wrapper);
}
Cylinder::~Cylinder(){
delete base;
delete wrapper;
}
double Cylinder::getHeight() const{
return wrapper.getH();
}
double Cylinder::getBaseRadius() const{
return base.getR();
}
double Cylinder::getBaseArea() const{
return base.area();
}
double Cylinder::getBaseScope() const{
return base.scope();
}
double Cylinder::getWrapperArea() const{
return wrapper.area();
}
double Cylinder::getWrapperScope() const{
return wrapper.scope();
}
void Cylinder::setHeight(double H){
wrapper.setH(H);
}
void Cylinder::setRadius(double r){
base.setR(r);
wrapper.setA(2*base.getR()*M_PI);
}
double Cylinder::volume() const{
return base*wrapper.getH();
}
double Cylinder::area() const{
return 2*base.area+wrapper.area;
}
void Cylinder::printDetails() const{
cout<<"\nRadius of base of Cilynder: "<<base.getR();
cout<<"\nHeight of wrapper: "<<wrapper.getH();
Shape::printDetails();
}
Rectangle* Cylinder::getWrapper() const{
return new Rectangle(wrapper);
}
Circle* Cylinder::getBase() const{
return new Circle(base);
}
我的编译器给了我不同的错误,因为我尝试修改代码以某种方式完成将Rectangle,Circle和Cylinder作为非抽象类。我不知道我应该在哪里放置方法定义(如果它们在继承的类中重复),实际上它们的形式是什么。抱歉,长文字。我希望有一个人可以帮助我。提前致谢
萨姆
答案 0 :(得分:6)
您没有实现纯虚方法。只要你有纯虚方法(virtual void whatever()=0
),就会使类抽象化。抽象类无法实例化。为了实例化,具体的子类必须实现祖先类中的所有纯虚方法。
例如,Rectangle
缺少从scope
继承的Shape2D
方法,以及从area
继承的Shape
方法。这将导致错误“无法实例化Rectangle类,因为它是抽象的。”看起来你在文件中有较低的那些方法的实现,但它们没有在任何地方声明在Rectangle
类中。