题目
(12 ) “ 图形 ” 类 Shape 中定义了纯虚函数 CalArea() ,“ 三角形 ” 类 Triangle 继承了类Shape ,请 将
Triangle 类中的 CalArea 函数补充完整。
class Shape{
public:
virtual int CalArea()=0;
}
class Triangle: public Shape{
public:
Triangle{int s, int h}: side(s),height(h) {}
【 12 】 { return side*height/2 ; }
private:
int side;
int height;
};
第1题
下列shape 类是一个表示形状的抽象类, area() 为求图形面积的函数, total()则是一个通用的用以求不同形状的图形面积总和的函数。请从shape 类派生三角形类(triangle) 、矩形类(rectangle ),并给出具体的求面积函数。
第2题
第3题
第4题
第5题
如下程序声明了一个二维图形类TwoDShape,从其派生出矩形类Rec。
include<iostream>
include<string>
using namespaee std,
class TwoDShape{// 二维图形类
char name[20];
public:
TwoDShape (char * n="unknown") {
strcpy(name, n);
}
Char * getName(){return name;}
【 】=0;
};
class Rec: public TwoDShape{
double width, height
public:
Rec(double w-=0. 0,double h=0. 0):TwoDShape("rectangle")
{ width=w; height=h; }
double getWidth() {return width;}
double getHeight() {return height;}
double area() {return width * height;}
};
int main() {
TwoDShape * shape;
Shape=new Rec(2.1,3.0);
cout<<"object is"<<shape->getName()<<"\n";
cout<<"Area is"<<shape->area()<<"\n";
return 0
}
请将程序补充完整,使程序在运行时输出:
abject is triangle
Area is 6.3
第6题
试题六(共15分)
阅读以下说明、图和Java代码,填补Java代码中的空缺(1)~(6),将解答写在答题纸的
对应栏内。
【说明】
已知对某几何图形绘制工具进行类建模的结果如图6.1所示,其中Shape为抽象(abstract)类,表示通用图形,Box(矩形)、Ellipse(椭圆)和Line(线条)继承(extends)了Shape类,其中,Circle表示圆(即特殊的椭圆)。
下面的Java代码用于实现图 6-1所给出的设计思路,将其空缺处填充完整并编译运行,输出结果为:
Ellipse
Circle
Ellipse
C
E
【Java代码】
(1) class Shape{
public Shape(String name){
this.name= name;
}
(2) void paint();
String getName(){
retum this.name;
}
final String name;
};
//Box 和Line类似下面 Ellipse,其代码略
class Ellipse (3) {
public Ellipse(String name){
super(name);
System.out.println("Ellipse");
}
Void paintO{∥绘制现状示意代码
System.out.println(getName0);
}
};
class Circle (4) {
public Circle(String name){
super(name);
System.out.println("Circle");
}
};
class Diagram{
private Shape shapes[]= new Shape[2];
public void drawAShape(Shape shape){
shape.paint();
}
void erase A Shape(Shape shape){
∥删除形状,代码略
}
void drawShapes(){
shapes*0+= new Circle("C”);
shapes[l]= new Ellipse("E");
for (int i=O; i<2;++i) {
drawAShap(shapes[i]);//绘制形状
}
}
void close(){
for (int i=0;i<2; ++1) { []关闭图,删除所绘制图形
(5) ;
}
}
public static void main(String[] args){
Diagram diagram= (6) ;
diagram.drawShapes();
diagram.close();
}
}
第7题
阅读以下说明和C++代码,将应填入(n)处的字句写上。
[说明]
现有一个显示系统,要显示的图形有线Line、矩形Square,抽象出一个Shape类(接口),有方法显不display()。
需要新增图形Circle,又已知有类XXCircle实现了所需要实现的功能:显示displayIt()。为了继承自shape以提供统一接口,又不希望从头开发代码,希望使用XXCircle。这样将XXcircle作为Circle的一个属性,即Circle的对象包含一个XXCircle对象。当一个Circle对象被实例化时,它必须实例化一个相应的XXCircle对象: Circle对象收到的做任何事的请求都将转发给这个XXCircle对象。通过这种称为Adapter模式,Circle对象就可以通过“让XXCircle做实际工作”来表现自己的行为了。图6-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。
[图6-1]
[C++代码]
class Shape{
public:
(1) void display()=0;
};
class Line:public Shape{//省略具体实现
};
class Square:public Shape{//省略具体实现
};
class XXCircle{
public:
void displayIt(){
//省略具体实现
}
//省略其余方法和属性
};
class Circle:public Shape{
private:
XXCircle *pxc;
public:
Circle();
void display();
};
Circle::Circle(){
pxc=(2);
}
void Circle::display()
{
pxc->(3);
}
class Factory{
public:
(4) getshapeInstance(int type){//生成特定类实例
switch(type){
case 1:return new Square;
case 2:return new Line;
case 3 :return new Circle;
default:return NULL;
}
}
};
void main(int argc,char*argv[]){
if(argc !=2){
cout<<"error parameters!"<<endl;
return;
}
int type=atoi(argv[1]);
Factory factory;
Shape*s=factory. (5);
if(s==NULL){
cout<<"Error get the instance!"<<endl;
return;
}
s->display();
delete s;
return;
}
(1)
第8题
试题五(共15分)
阅读以下说明、图和C++代码,填补C++代码中的空缺(1)~(5),将解答写在答题纸的对应栏内。
【说明】
已知对某几何图形绘制工具进行类建模的结果如图5.1所示,其中Shape为抽象类(应至少包含一个纯虚拟(virtual)函数),表示通用图形,Box表示矩形,Ellipse表示椭圆,Circle表示圆(即特殊的椭圆),Line表示线条。
下面的C++代码用于实现图5-1所给出的设计思路,将其空缺处填充完整并编译运行,输
出结果为:
Ellipse
Circle
Ellipse
C
E
【C++代码】
include <string>
include <iostream>
using namespace std;
class Shape{
public:
Shape(const string& name){
m_name= name;
}
~Shape(){}
(1) void paint() = 0;
stringgetName()const {
retumm name;
}
Private:
string m_name;
};
//Box和 Line类的定义与 Ellipse类似,其代码略
classEllipse (2) {
public:
Ellipse(const string& name) : Shape(name){ cout<<"Ellipse" <<endl; }
Voidpaint() { cout<<getName()<<endl;}
};
classCircle (3) {
public:
Circle(const string& name) : Ellipse(name){ cout<<"Circl"<<endl; }
};
class Diagram {
public:
void drawAShap(Shape* shape){ shape->paint(); }
void drawShapes() {
shapes[0] = new Circle("C");
shapes[l] = new Ellipse("E");
for (int i=O;i<2; ++1) {
drawAShap(shapes[i]);
}
}
void close (){ /*删除形状,代码略 */ }
private:
Shape* shapes[2];
};
int main()
{
Diagram* diagram = (4)
diagram->drawShapes();
diagram->close ();
(5) diagram;
}
第9题
阅读以下说明和C代码,将应填入(n)处的字句写在的对应栏内。
【说明】
在一个简化的绘图程序中,支持的图形种类有点(point)和圆(circle),在设计过程中采用面向对象思想,认为所有的点和圆都是一种图形(shape),并定义了类型shape t、 point t和circle t分别表示基本图形、点和圆,并且点和圆具有基本图形的所有特征。
【C代码】
typedef enum { point,circle } shape type; /* 程序中的两种图形:点和圆 */
typedef struct { /* 基本的图形类型 */
shape_type type; /* 图形中类标识:点或者圆*/
void (*destroy) (); /* 销毁图形操作的函数指针*/
void (*draw) (); /* 绘制图形操作的函数指针*/
} shape_t;
typedef struct { shape_t common; int x; iht y; } point_t; /* 定义点类
型, x, y为点坐标*/
void destroyPoint (point_t* this) { free (this); printf ("Point destoryed!
\n"); } ) /* 销毁点对象*/
void drawPoint(point_t* this) { printf("P(%d,%d)", this->x, this->y); }
/* 绘制点对象*/
shape_t* createPoint (va_list* ap) (/* 创建点对象,并设置其属性*/
point_t* p_point;
if ((p_point= (point_t*)malloc (sizeof (point_t)) ) ==NULL) returnNULL;
p_point->common, type = point; p_point->common, destroy = destroyPoint;
p_point->common.draw = drawPoint;
p_point->x = va_arg(*ap, int); /* 设置点的横坐标*/
p_point->y = va_arg(*ap, int); /* 设置点的纵坐标*/
return (shape_t*)p_ooint; /*返回点对象指针*/
}
typedef struct { /*定义圆类型*/
shape_t common;
point_t 4center; /*圆心点*/
int radius; /*圆半径*/
} circle_t;
void destroyCircle(circle_t* this){
free((1)); free(this); printf("Circle destoryed!\n");
}
void drawCircle(circle_t* this) {
print f ("C (");
(2).draw(this->center); /*绘制圆心*/
printf(",%d) ", this->radius);
}
shape_t* createCircle(va_list4 ap) { /*创建一个圆,并设置其属性*/
circle_t4 p circle;
if ((p_circle = (circle_t4)malloc (sizeof (circle_t)) ) ==NULL ) return NULL;
p_circle->common.type = circle; p_circle->common.destroy = destroy
Circle;
p_circle->common.draw = drawCircle;
(3) = createPoint(ap); /* 设置圆心*/
p_circle->radius = va_arg(*ap, int); /* 设置圆半径*/
return p_circle;
}
shape_t* createShape(shape_type st, "') { /* 创建某一种具体的图形*/
va_list ap; /*可变参数列表*/
&nbs
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!