求助~ 派生问题

挥霍笑声 2011-12-05 06:22:29
哪位好心人帮忙写下。
题目要求:
1.学习声明和使用类的继承关系,声明派生类;
2.熟悉不同继承方式下对基类成员的访问控制。
声明一个形状(shape)基类,具有size,position,color等成员变量,move,draw等成员函数。由此派生出矩形(Rectangle)类,三角形(Triangle)类和圆(circle)类。
还有一个关于多态性的,就是把上面那个题目的move,draw等成员函数声明为虚函数,在主函数中用 抽象类指针调用move,draw.
要求不高,能用到构造函数。
菜鸟求帮忙 拜托啦。
在线等。
...全文
226 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
blink31 2011-12-06
  • 打赏
  • 举报
回复
HXLSJR 2011-12-06
  • 打赏
  • 举报
回复
哦,我用的VC++6.0,你稍微修改一下就是了
挥霍笑声 2011-12-05
  • 打赏
  • 举报
回复
用的dev c++ 那句过不去啊 47 `main' must return `int'
HXLSJR 2011-12-05
  • 打赏
  • 举报
回复
这里用void没错
挥霍笑声 2011-12-05
  • 打赏
  • 举报
回复
- -没那么夸张吧。。
我会搞懂他的。。
[Quote=引用 9 楼 zhizichina 的回复:]

楼上的所有人都是杀人凶手。
[/Quote]
Ethan_Jnu 2011-12-05
  • 打赏
  • 举报
回复
你近视吧。就一个给他贴码。

[Quote=引用 9 楼 zhizichina 的回复:]
楼上的所有人都是杀人凶手。
[/Quote]
挥霍笑声 2011-12-05
  • 打赏
  • 举报
回复
void main()
{
shape *p;
Rectangle rec;
p=&rec;
display(p); //抽象类指针调用成员函数move,draw
}


这里void好像过不去啊。。
HXLSJR 2011-12-05
  • 打赏
  • 举报
回复
对,满足你给的所有要求,这道题是很基础的类问题,相信你自己也能独立做出来
支持英文数字 2011-12-05
  • 打赏
  • 举报
回复
楼上的所有人都是杀人凶手。
挥霍笑声 2011-12-05
  • 打赏
  • 举报
回复
这是2步一起做的么[Quote=引用 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; //纯虚函数,有纯虚函数的类为……
[/Quote]
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; //纯虚函数,有纯虚函数的类为抽象类
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
}
xcjsj 2011-12-05
  • 打赏
  • 举报
回复
自己做吧
HXLSJR 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);
}
herocxgood 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()
}
HXLSJR 2011-12-05
  • 打赏
  • 举报
回复
稍等哈,不过建议你以后尽量自己做哈
挥霍笑声 2011-12-05
  • 打赏
  • 举报
回复
基础比较差啦。。 明天就得交了。 - -
Ethan_Jnu 2011-12-05
  • 打赏
  • 举报
回复
楼主自己写比较好啦,锻炼一下。
C+ + Bezier 曲线类的构造X 中图法分类号: TP302. 4 Bezier 曲线作为一种特殊的参数多项式曲线, 一经问世, 就曾受到CAGD 学术界的广泛重 视. 尽管如今在CAD 领域有许多种不同的自由型曲线和曲面的构造方法, 但使用Bezier 曲线 仍不失为一种重要的备选方案. 例如国内外多种矢量字库的构建, 仍然广泛使用Bezier 曲线技 术. Bezier 曲线的实现方法传统上主要求助于de Casteljau 算法. 但随着计算机硬件技术的不 断进步, 计算机的处理速度越来越快, 算法的高效尽管仍很重要, 但代码的易于维护性和可重 用性即显得日见重要. 本文利用C+ + 面向对象的特性, 将Bezier 曲线的定义和生成建立在矩 阵运算类的基础上, 从而使描述和生成Bezier 曲线的代码变得简单明了, 且有着很好的可扩展 性. 1 Bezier 曲线的矩阵表示 记B n= [ Bn0 ( t ) , Bnn ( t ) ] , 其中B n i ( t ) = Ci nt i ( 1- t ) n- i , t I [ 0, 1] . Tn = [ 1 t , t n ] , Vn= b0 b 1 s bn 1 , Mn = m00 m01 , m0n m10 m11 , m1n , , , , mn 0 mn1 , mnn 其中mij = ( - 1) ij * Ci n * Cj i , 且当j > i 时, mij = 0, 则以b0, b 1, ,, bn 为控制顶点的Bezier 曲线 可以表为P( t ) = Tn * Mn * Vn . 2 几个通用模板的定义 为了能够方便地使用计算机来处理上述简便的Bezier 矩阵表达式, 从而大大简化计算机 图形软件的开发, 显然我们首先必须能够方便地使用计算机来处理矩阵和向量等对象, 为此目 的我们利用C+ + 中的最新特征引入了如下向量模板和矩阵模板的概念. 第21 卷 第2 期 江西师范大学学报( 自然科学版) Vol. 21 No. 2 1997 年5 月 JOURNAL OF JIANGXI NORMAL UNIVERSITY May 1997 X 收稿日期: 1997- 01- 20 2. 1 向量模板的定义 template 3class T4 class VectorTemplate { private: int numElement s; T * element s; protected: public: VectorTemplate( void) ; VectorTemlate( int) ; VectorTemplate( VectorTemplate3T4 &vSrc;) ; -VectorTemplate( void) ; T & operator[ ] ( unsigned int index ) { return elements [ index] ; } ; int size( void) { return numElement s; } ; void resize ( int sizeIn) ; VectorTemplate3T4& operator= ( VectorTemplate3T4 & ) ; VectorTemplate3T4& operator= ( T) ; float leng th( void) ; VectorTemplate3T4 &normalize;( void) ; friend T operator * ( VectorTemplate3T4 & v1, VectorT emplate3T4 & v2) ; friend VectorTemplate 3T 4 operator + ( VectorTemplate3T 4 & v1, VectorTemplate3T 4 &v 2) ; friend VectorTemplate3T4 operator-( VectorT emplate3T4 & v1, VectorTemplate3T4 & v2) ; friend VectorTemplate3T4 operator * ( VectorTemplate3T4 & v1, T scalar) ; friend VectorTemplate3T4 operator * ( T scalar, VectorTemplate3T4 & v1) ; VectorTemplate3T4 & operator+ = ( vectorTemplate3T4 & v) ; VectorTemplate3T4 & operator * = ( T scalar) ; VectorTemplate3T4 & operator- = ( VectorTemplate3T4 & v) ; VectorTemplae3T4 cross( VectorT emplate3T4 & v) ; } 在向量模板的定义中, 通过运算符的重载, 使我们能像对待普通的数学运算一样来引述两 个向量或向量与标量之间的四则运算. 例如: 对于两个向量V1、V2 的内积, 只须简单地表示为 V1* V2, 而对于一个标量s 与向量V1 的乘积亦只须记着s* V1, 而丝毫不会引起混淆. 2. 2 矩阵模板的定义 template 3class T4 class Mat rixTemplate { private: int numRow s; int numColumns; 1 28 江西师范大学学报( 自然科学版) 1997 年 VectorTemplate3T4 * element s; Void create( int , int ) ; protected: public: Mat rixTemplate( void) { numRow s= 0; numColumns= 0; elements= NULL; } ; Mat rixTemplate( int size) { create( size, size) ; } ; Mat rixTemplate( int, int ) ; Mat rixTemplate( Mat rixTemplate3T4 & mSrc) ; -Mat rixT emplate( void) ; VectorTemplate3T4& operator[ ] ( int row ) { return elements[ row ] ; } ; int nrows( void) { return numRows; } ; int ncols( void) { return numColumns; } ; void resize( int new-ncols, int new-nrows) ; friend Mat rixT emplate3T4 operator * ( Mat rixTemplate3T4 & , Mat rixTemplate3T4 & ) ; friend VectorTemplate3T4 operator * ( MatrixTemplate3T4 & , VectorTemplate3T4 & ) ; Mat rixTemplate3T4& operator * = ( Mat rixTemplate3T4 & m) { * this= m * ( * this) ; return * this; } ; Mat rixTemplate3T4& operator= ( MatrixT emplate3T4 & ) ; float determinant ( void) ; Mat rixTemplate3T4& t ranslate( VectorTemplate3T4 & ) ; Mat rixTemplate3T4& scale( VectorTemplate3T4 & ) ; Mat rixTemplate3T4& t ranslate( float , float ) ; Mat rixTemplate3T4& scale( f loat, f loat) ; Mat rixTemplate3T4& rotate( f loat ) ; Mat rixTemplate3T4& t ranslate( float , float , f loat) ; Mat rixTemplate3T4& scale( f loat, f loat, float) ; Mat rixTemplate3T4& rotate( f loat , f loat, f loat) ; Mat rixTemplate3T4& setIdent ity( void) ; Mat rixTemplate3T4& ident ity( void) { return set Identity( ) ; } ; } ; typedef Mat rixTemplate3f loat4 Matrix; 在矩阵模板中, 我们不但定义了各种常用的运算, 而且还封装了平移、缩放、旋转等成员函 数, 这一切都是为了使图象的生成和处理变得更简便些. 但仅有这两个类当然还不够. 我们的 目的是要生成Bezier 曲线, 然而我们知道, 在计算机表示中曲线是由折线来逼近的, 而折线又 是由线段组成的. 此外, 两点决定一条线段. 因此, 为了一般的表示一条Bezier 曲线, 我们需要 先对点、线段和折线有一个统一的描述, 同时, 这也是为了符合GKS-3D 图形标准. 第2 期 陈国华 C+ + Bezier 曲线类的构造 12 9 点的功能无非是用来确定空间的一个坐标, 所以我们不难从向量类中来派生一个点类, 如: ( 1) 点类 Class SplinePoint: public Vector{ public ,, void setCoord( Vector & v) ; void getCoord( Vector & v) ; ,, } 有了点类之定义, 我们可以把折线看成是由一系列顶点控制确定的, 但为了使这一系列顶 点的排列有一定的顺序, 并且便于索引访问, 我们又不得不增加一个简单的线性表类来对之加 以管理. ( 2) 简单表类 template 3class T4 class SimpleTable{ private: int curSize; T * * items; ,, public: ,, T & operator[ ] ( int ) ; ,, } ( 3) 折线类 class SplinePolyline{ protected: SimpleT able3SplinePoint4 point s; public: SplinePolyline( void) ; SplinePolyline( int np) ; SplinePoint & operator[ ] ( unsigned int index) ; void draw ( ) ; } 3 Bezier 曲线类的构造 如上所述, 计算机所表示的曲线实际上只是一条折线, Bezier 曲线也不例外, 只不过根据 用户的精度要求不同增加插值点的个数有所不同而已. 据此, 我们不难从折线类来派生我们所 1 30 江西师范大学学报( 自然科学版) 1997 年 要求的Bezier 曲线类. class BezCurve: public SplinePolyline { private: int smooth; public: BezCurve( void) { } ; BezCurve( int numPoint In) : SplinePolyline( numPoint In+ 1) { smooth= 5; } void set smooth( int s In) { smooth= s In; } int getsmooth( void) { return smooth; } void draw( void) ; void draw( class SplinePerspective& ) ; } 在上述构造中, 我们缺省地取光滑度smooth= 5, 即在每两个顶点之间加5 个插值点, 其 中令人感兴趣的当然是draw ( ) 例程, 缺省情况下按2 维的要求来绘制Bezier 曲线, 如果给出 适当的透视参数( 由class Spline Perspective 定义) , 则可按透视要求来绘制Bezier 曲线. 4 应用 在以往的基于C 或Pascal 等语言的Bezier 曲线的生成过程中, 往往是一面定义一条Bez-i er 曲线, 然后调用一外部过程来绘制它, 因此涉及复杂的参数传递等问题, 而在此处, 每一条 Bezier 曲线都被定义为知道如何绘制自己了. 因此, 要定义一条Bezier 曲线并实际地画出它, 只须简单地遵循以下几步即可. 第一步: 定义一条Bezier 曲线 BezCurve bezcurve( n) ; 如果欲使该曲线更光滑, 则可进一步设置其光滑度 bezcurve. set smooth( sIn) ; 第二步: 设定n 个顶点的坐标 bezcurve. setCoord( x , y ) 或bezcurve. setCoord( x , y , z ) ; 或bezcurve. setCoord( vec) ; 第三步: 绘制曲线 bezcurve. draw( ) ; 或对空间曲线bezcurve. draw ( proj) ; 即可完成整条曲线的定义和绘制过程. 第2 期 陈国华 C+ + Bezier 曲线类的构造 13 1 参 考 文 献 1 Marc Berger. Computer g raphics wit h Pascal. T he Benjamin/ Cummings Pub Inc, 1986. 1~ 340 2 易忠亮. C+ + 矩阵运算类与Windows 应用软件. 北京: 清华大学出版社, 1995. 1~ 380 3 罗振东, 廖光裕. 计算机图示学原理和方法. 上海: 复旦大学出版社, 1993. 120~ 140 4 蔡士杰等. 三维图形系统PHIGS 的原理与技术. 南京: 南京大学出版社, 1993. 62~ 89 5 施法中. 计算机辅助几何设计与非均匀有理B 样条. 北京: 北京航空航天大学出版社, 1994. 121~ 173 The Construction of C+ + Bezier Curve Class Chen Guohua ( Department of Comput er Science, Guangdong Inst itut e for Nat ionalities, Guangzhou 510633) Abstract: In this paper, we have used the C+ + OOP feature to encapsulate the procedure of both defining and draw ing of a Bezier curve, so greatly simplifed the programming of draw ing a Bezier curve. Key words: OOP, class, template, Bezier curve ( 上接第120 页) 参 考 文 献 1 刘余善. 实用管理系统工程. 杭州: 浙江人民出版社, 1983. 266~ 268 2 魏国华, 傅家良, 周仲良. 实用运筹学. 上海: 复旦大学出版社, 1987. 363~ 365 3 王俊峰. 确定有批量价格折扣问题的经济订购批量的最大利润法. 系统工程理论与实践, 1996, ( 5) : 60 ~ 63 EOQModels Concerning the Questions of Permissible Shortage and Quantity Discount Wan Baozhen ( Commercial College of Jiangxi Normal University, Nanchang 330027) Abstract: With a study of the improvement on the broadening of sphere of applicat ion and the re-establishment of cost project, the paper presents a new SDEOQ model that is more suitable to the pract ices in the area of product ion and circulat ion under the condit ions of socialist market economy and a discussion about the opt imality of SDEOQ model w ith both it s part ial and whole opt imal solut ions being achieved. Key words: economic order quant ity( EOQ) , permissble shortage, quant ity discount , opt imal-i ty

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧