C++///定义一个点类(Point) .

问题描述:

C++///定义一个点类(Point) .
定义一个点类(Point)、矩形类(Rectangle)和立方体类(Cube)的层次结构.矩形包括长度和宽度两个新数据成员,矩形的位置从点类继承.立方体类由长度、宽度和高度构成.要求各类提供支持初始化的构造函数和显示自己成员的函数.编写主函数,测试这个层次结构,输出立方体类的相关消息.
1个回答 分类:综合 2014-10-18

问题解答:

我来补答
#include <stdio.h>
#include <stdlib.h>
class Point
{
private:
 int _x;
 int _y;
public:
 Point(int x,int y)
 {
  this->_x=x;
  this->_y=y;
 }
 int getX()
 {
  return this->_x;
 }
 int getY()
 {
  return this->_y;
 }
};
class Rectangle:public Point
{
private:
 int _width;
 int _height;
public:
 Rectangle(int x,int y,int width, int height):Point(x,y)
 {
  this->_width=width;
  this->_height=height;
 }
 int getWidth()
 {
  return this->_width;
 }
 int getHeight()
 {
  return this->_height;
 }
};
class Cube
{
private:
 int _width;
 int _height;
 int _depth;
public:
 Cube(int width,int height,int depth)
 {
  this->_width=width;
  this->_height=height;
  this->_depth=depth;
 }
 int getWidth()
 {
  return this->_width;
 }
 int getHeight()
 {
  return this->_height;
 }
 int getDepth()
 {
  return this->_depth;
 }
};
int main(void)
{
 Point *p=new Point(1,2);
 printf("the point x is %d, y is %d\n",p->getX(),p->getY());
 Rectangle *r=new Rectangle(3,4,5,6);
 printf("the rectangle is position is (%d,%d) and width is %d, height is %d\n",(*r).getX(),(*r).getY(),(*r).getWidth(),(*r).getHeight());
 Cube c(7,8,9);
 printf("the cube's width is %d, height is %d, depth is %d\n",c.getWidth(),c.getHeight(),c.getDepth());
 exit(0);
}
 
 
展开全文阅读
剩余:2000