C++类的派生与继承题
编写程序,创建类Point,成员变量是坐标X,Y,成员方法包括访问X,Y的方法及计算面积的方法area。在Point类的基础上,创建子类圆Circle和长方形Rectan...
编写程序,创建类Point,成员变量是坐标X,Y,成员方法包括访问X,Y的方法及计算面积的方法area。在Point类的基础上,创建子类圆Circle和长方形Rectangle, 成员变量分别是半径R,长L和宽W, 并重写area方法。通过Circle类派生圆柱类Column,成员变量是高H,重写area方法,并增加计算体积的方法volume。在main函数中,编写测试代码,创建上述类的对象并计算面积或体积。
展开
1个回答
展开全部
自己再改改吧,这个和你要求差不多,也是一个派生与继承,基类是Rectangle, 派生类是Cube.
Cube.h
-------------------------------------------------------------------
#ifndef CUBE_H
#define CUBE_H
#include "Rectangle.h"
class Cube: public Rectangle
{
protected:
double height;
double volume;
public:
Cube() : Rectangle()
{
height= 0.0;
volume= 0.0;
}
Cube(double w, double len, double h) : Rectangle(w,len)
{
height= h;
volume= getArea() * h;
}
double getHeight() const
{
return height;
}
double getVolume() const
{
return volume;
}
};
#endif
Rectangle.h
------------------------------------------------------------
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
Rectangle()
{
width= 0.0;
length= 0.0;
}
Rectangle(double w, double len)
{
width= w;
length= len;
}
double getWidth() const
{
return width;
}
double getLength() const
{
return length;
}
double getArea() const
{
return width * length;
}
};
#endif
main.cpp (主函数)
---------------------------------------------------------------------
#include <iostream>
#include "Cube.h"
using namespace std;
int main()
{
double cubeWidth;
double cubeHeight;
double cubeLength;
cout<< "Enter the dimension of a Cube: \n";
cout<< "Width: ";
cin>> cubeWidth;
cout<< "Length: ";
cin>> cubeLength;
cout<< "Height: ";
cin>> cubeHeight;
Cube myCube(cubeWidth, cubeLength, cubeHeight);
cout<< "Here are the Cube's properties: \n";
cout<< "Width: "<<myCube.getWidth()<<endl;
cout<< "Length: "<<myCube.getLength()<<endl;
cout<< "Height: "<<myCube.getHeight()<<endl;
cout<< "Base area: "<<myCube.getArea()<<endl;
cout<< "Volume: "<<myCube.getVolume()<<endl;
return 0;
}
Cube.h
-------------------------------------------------------------------
#ifndef CUBE_H
#define CUBE_H
#include "Rectangle.h"
class Cube: public Rectangle
{
protected:
double height;
double volume;
public:
Cube() : Rectangle()
{
height= 0.0;
volume= 0.0;
}
Cube(double w, double len, double h) : Rectangle(w,len)
{
height= h;
volume= getArea() * h;
}
double getHeight() const
{
return height;
}
double getVolume() const
{
return volume;
}
};
#endif
Rectangle.h
------------------------------------------------------------
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
Rectangle()
{
width= 0.0;
length= 0.0;
}
Rectangle(double w, double len)
{
width= w;
length= len;
}
double getWidth() const
{
return width;
}
double getLength() const
{
return length;
}
double getArea() const
{
return width * length;
}
};
#endif
main.cpp (主函数)
---------------------------------------------------------------------
#include <iostream>
#include "Cube.h"
using namespace std;
int main()
{
double cubeWidth;
double cubeHeight;
double cubeLength;
cout<< "Enter the dimension of a Cube: \n";
cout<< "Width: ";
cin>> cubeWidth;
cout<< "Length: ";
cin>> cubeLength;
cout<< "Height: ";
cin>> cubeHeight;
Cube myCube(cubeWidth, cubeLength, cubeHeight);
cout<< "Here are the Cube's properties: \n";
cout<< "Width: "<<myCube.getWidth()<<endl;
cout<< "Length: "<<myCube.getLength()<<endl;
cout<< "Height: "<<myCube.getHeight()<<endl;
cout<< "Base area: "<<myCube.getArea()<<endl;
cout<< "Volume: "<<myCube.getVolume()<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询