首页 新闻 会员 周边

c++类的派生与继承、构造函数、函数重载、多态性问题

0
悬赏园豆:100 [已解决问题] 解决于 2012-06-02 11:26

实现一个图形系统,它提供了用于各种图形的类:矩形、正方形、三角形、圆等。例如,矩形可能包括高度、宽度和中心点数据成员,而正方形只有中心点和边长数据成员,圆只有中心点和半径数据成员。

Figure类是基类。添加从Figure派生的Rectangle和Triangle类。每个类都定义了原型成员函数erase和draw。每个成员函数都输出一条消息,指出调用的是什么函数,以及调用对象属于哪个类。由于只是原型函数,所以它们只需输出这些消息就可以了。成员函数center首先调用erase函数来删除图形,再调用draw函数在中心点重画图形。由于erase和draw函数只是原型函数,所以center并不能真的执行“居中”操作,只是简单的调用erase和draw这两个成员函数。在函数center中输出一条消息,宣布center已经调用。成员函数不接收任何参数。

请编写如下三个版本(分别保存并运行):

1、不用虚函数而写出类定义,编译并测试;

2、采用虚函数技术,编译并测试;

3、修改各个成员函数的代码,让他们执行真正的绘图操作。(提示:绘出“*”点阵)

c++
问题补充:

程序要加上注释。

谢谢了

Playboy88的主页 Playboy88 | 初学一级 | 园豆:64
提问于:2012-05-28 16:34
< >
分享
最佳答案
0

不用虚函数:

四个头文件:

#include "stdafx.h"
#include "Figure.h"
#include <iostream>


void Figure::Draw()
{
std::cout<<"Figure's Draw fun being called"<<std::endl;
}

void Figure::Erase()
{
std::cout<<"Figure's Erase fun being called"<<std::endl;
}

void Figure::Center()
{
Erase();
Draw();
std::cout<<"Figure's Center fun being called"<<std::endl;
}

 

#include "stdafx.h"
#include "Figure.h"
class Triangle:public Figure
{
public:
Triangle(){}
~Triangle(){}
void Erase();
void Draw();
void Center();

private:
double point_x;
double point_y;


};
#endif

 

#ifndef _RECTANGLE_H_
#define _RECTANGLE_H_

#include "stdafx.h"
#include "Figure.h"
class Rectangle:public Figure
{
public:
Rectangle(){}
~Rectangle(){}
void Erase();
void Draw();
void Center();

private:
double point_x;
double point_y;


};
#endif

 

#ifndef _CIRCLE_H_
#define _CIRCLE_H_

#include "stdafx.h"
#include "Figure.h"
class Circle:public Figure
{
public:
Circle(){}
~Circle(){}
void Erase();
void Draw();
void Center();

private:
double point_x;
double point_y;


};
#endif

四个源文件:

#include "stdafx.h"
#include "Figure.h"
#include <iostream>


void Figure::Draw()
{
std::cout<<"Figure's Draw fun being called"<<std::endl;
}

void Figure::Erase()
{
std::cout<<"Figure's Erase fun being called"<<std::endl;
}

void Figure::Center()
{
Erase();
Draw();
std::cout<<"Figure's Center fun being called"<<std::endl;
}

 

#include "stdafx.h"
#include "Rectangle.h"
#include <iostream>

void Rectangle::Draw()
{
std::cout<<"Rectangle's Draw fun being called"<<std::endl;
}

void Rectangle::Erase()
{
std::cout<<"Rectangle's Erase fun being called"<<std::endl;
}

void Rectangle::Center()
{
Erase();
Draw();
std::cout<<"Rectangle's Center fun being called"<<std::endl;
}

 

#include "stdafx.h"
#include "Triangle.h"
#include <iostream>

void Triangle::Draw()
{
std::cout<<"Triangle's Draw fun being called"<<std::endl;
}

void Triangle::Erase()
{
std::cout<<"Triangle's Erase fun being called"<<std::endl;
}

void Triangle::Center()
{
Erase();
Draw();
std::cout<<"Triangle's Center fun being called"<<std::endl;
}

 

#include "stdafx.h"
#include "Circle.h"
#include <iostream>

void Circle::Draw()
{
std::cout<<"Circle's Draw fun being called"<<std::endl;
}

void Circle::Erase()
{
std::cout<<"Circle's Erase fun being called"<<std::endl;
}

void Circle::Center()
{
Erase();
Draw();
std::cout<<"Circle's Center fun being called"<<std::endl;
}

收获园豆:60
kunkka_ | 菜鸟二级 |园豆:273 | 2012-05-29 21:58

剩下的真得你自己做。你用虚函数试试。参考着C++primier的15章。看看用虚函数时,到底调用的是哪个

kunkka_ | 园豆:273 (菜鸟二级) | 2012-05-29 22:03

<p>嗯嗯,谢谢了。

Playboy88 | 园豆:64 (初学一级) | 2012-05-30 12:09
其他回答(2)
1

作业要自己做,对你有好处。

收获园豆:20
sinhbv | 园豆:2579 (老鸟四级) | 2012-05-29 08:08

<p>我要是会做就不过来了啊。没有思路啊

支持(0) 反对(0) Playboy88 | 园豆:64 (初学一级) | 2012-05-29 10:41
1
View Code用虚函数写的,具体功能实现没做。
收获园豆:20
菩_提_子 | 园豆:222 (菜鸟二级) | 2012-05-29 13:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册