求助~ 派生问题
时间:2011-12-05
来源:互联网
题目要求:
1.学习声明和使用类的继承关系,声明派生类;
2.熟悉不同继承方式下对基类成员的访问控制。
声明一个形状(shape)基类,具有size,position,color等成员变量,move,draw等成员函数。由此派生出矩形(Rectangle)类,三角形(Triangle)类和圆(circle)类。
还有一个关于多态性的,就是把上面那个题目的move,draw等成员函数声明为虚函数,在主函数中用 抽象类指针调用move,draw.
要求不高,能用到构造函数。
菜鸟求帮忙 拜托啦。
在线等。
作者: youzhibc 发布时间: 2011-12-05
作者: Ethan_Jnu 发布时间: 2011-12-05
作者: youzhibc 发布时间: 2011-12-05
作者: HXLSJR 发布时间: 2011-12-05
#include<iostream.h>
#include<math.h>
class Graph //定义一个图形基类
{
float x,y;
public:
Graph(float xx,float yy) {x=xx; y=yy;}
float area(){return 0; }
};
class Rect:public Graph//由图形类派生出立方体
{
float length,width,high;
public:
Rect(float x,float y):Graph(x,y) { length=x;width=y;}
float area() {return length*width;}
};
class Circle:public Graph //由图形类派生出圆类
{
double radius;
public:
Circle(float x,float y):Graph(x,y){ radius=sqrt(x*x+y*y);}
double area() {return 3.1416*radius*radius;}
};
void main()
{
Graph s0(10,10);
Rect s1(10,10);
Circle s2(10,10);
cout<<"原面积="<<s0.area()<<endl; //调用基类的area()
cout<<"矩形面积="<<s1.area()<<endl; //调用矩形的area()
cout<<"圆的面积="<<s2.area()<<endl; //调用圆类的area()
cout<<"面积="<<s2.Graph::area ()<<endl;//调用基类的area()
}
作者: herocxgood 发布时间: 2011-12-05
#include<iostream>
using namespace std;
class shape
{
public:
shape(int s,int p,char c){size=s;position=p;color=c;}
virtual void move()=0;
virtual void draw()=0;
private:
int size;
int position;
char color;
};
class Rectangle:public shape
{
public:
Rectangle(int s=0,int p=0,char c=0):shape(s,p,c){}
void move(){cout<<"Rectangle:move()函数被调用!"<<endl;}
void draw(){cout<<"Rectangle:draw()函数被调用!"<<endl;}
};
class Triangle:private shape
{
public:
Triangle(int s=0,int p=0,char c=0):shape(s,p,c){}
void move(){cout<<"Triangle:move()函数被调用!"<<endl;}
void draw(){cout<<"Triangle:draw()函数被调用!"<<endl;}
};
class circle:protected shape
{
public:
circle(int s=0,int p=0,char c=0):shape(s,p,c){}
void move(){cout<<"circle:move()函数被调用!"<<endl;}
void draw(){cout<<"circle:draw()函数被调用!"<<endl;}
};
void display(shape *ptr)
{
ptr->move();
ptr->draw();
}
void main()
{
shape *p;
Rectangle rec;
p=&rec;
display(p);
}
作者: HXLSJR 发布时间: 2011-12-05
作者: xcjsj 发布时间: 2011-12-05
#include<iostream>
using namespace std;
class shape //抽象类shape
{
public:
shape(int s,int p,char c){size=s;position=p;color=c;}//构造函数
virtual void move()=0; //纯虚函数,有纯虚函数的类为抽象类
virtual void draw()=0;
private:
int size;
int position;
char color;
};
class Rectangle:public shape //公有继承
{
public:
Rectangle(int s=0,int p=0,char c=0):shape(s,p,c){}
void move(){cout<<"Rectangle:move()函数被调用!"<<endl;}
void draw(){cout<<"Rectangle:draw()函数被调用!"<<endl;}
};
class Triangle:private shape //私有继承
{
public:
Triangle(int s=0,int p=0,char c=0):shape(s,p,c){}
void move(){cout<<"Triangle:move()函数被调用!"<<endl;}
void draw(){cout<<"Triangle:draw()函数被调用!"<<endl;}
};
class circle:protected shape //保护继承
{
public:
circle(int s=0,int p=0,char c=0):shape(s,p,c){}
void move(){cout<<"circle:move()函数被调用!"<<endl;}
void draw(){cout<<"circle:draw()函数被调用!"<<endl;}
};
void display(shape *ptr)
{
ptr->move();
ptr->draw();
}
void main()
{
shape *p;
Rectangle rec;
p=&rec;
display(p); //抽象类指针调用成员函数move,draw
}
作者: HXLSJR 发布时间: 2011-12-05
代码如下(有什么疑问可以问我):
#include<iostream>
using namespace std;
class shape //抽象类shape
{
public:
shape(int s,int p,char c){size=s;position=p;color=c;}//构造函数
virtual void move()=0; //纯虚函数,有纯虚函数的类为……
作者: youzhibc 发布时间: 2011-12-05
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28