+ -
当前位置:首页 → 问答吧 → 求助~ 派生问题

求助~ 派生问题

时间: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

这里有个例子,lz可以参考一下:

#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

这是2步一起做的么
引用 7 楼 hxlsjr 的回复:

代码如下(有什么疑问可以问我):
#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