2013-07-04 94 views
0

我是C++新手,並且存在一些問題。我想創建一個抽象類,以及兩個從該類繼承的抽象類。最後,這兩個類通常用具體的類來實現。這裏是我的頭文件和源文件:具有虛函數的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); 
} 

我的編譯器給了我不同的錯誤,因爲我試圖修改代碼來完成某種方式有類矩形,圓形和圓柱非抽象的。我不知道應該在哪裏放置方法定義(它們是否應該複製到繼承的類中),以及它們實際的適當形式。對不起,長文本。我希望有一個人可以幫助我。在此先感謝

山姆

+0

你會得到什麼錯誤? – CosmicComputer

回答

6

您沒有實現純虛擬方法。只要你有純粹的虛擬方法(virtual void whatever()=0),就會使這個類變得抽象。抽象類不能被實例化。爲了被實例化,具體的子類必須實現來自祖先類的所有純虛擬方法。

例如,Rectangle缺少scope方法從Shape2D繼承,並從Shape繼承了area方法。這將導致「」的錯誤無法實例化Rectangle類,因爲它是抽象的。「看起來您已將這些方法的實現放在文件中較低的位置,但它們不在Rectangle類的任何位置聲明。