c++程序设计题文档格式.docx
- 文档编号:14562430
- 上传时间:2022-10-23
- 格式:DOCX
- 页数:6
- 大小:18.13KB
c++程序设计题文档格式.docx
《c++程序设计题文档格式.docx》由会员分享,可在线阅读,更多相关《c++程序设计题文档格式.docx(6页珍藏版)》请在冰豆网上搜索。
floatR;
voidmain()
{Shape*sp;
sp=newCircle(10);
cout<
<
sp->
GetPerim()<
endl;
sp=newRectangle(6,4);
GetPerim()<
}__
1.编写类String的构造函数、析构函数和赋值函数和测试程序。
已知类String的原型为:
#include<
iostream.h>
string.h>
classString
String(constchar*str=NULL);
//普通构造函数
String(constString&
other);
//拷贝构造函数
~String();
//析构函数
String&
operator=(constString&
//赋值函数
voidshow()
{cout<
m_data<
char*m_data;
//用于保存字符串
String:
:
~String()
{delete[]m_data;
//由于m_data是内部数据类型,也可以写成deletem_data;
String(constchar*str)
{if(str==NULL)
{m_data=newchar[1];
//若能加NULL判断则更好
*m_data=\0;
else
{intlength=strlen(str);
m_data=newchar[length+1];
//若能加NULL判断则更好
strcpy(m_data,str);
other)
{intlength=strlen(other.m_data);
strcpy(m_data,other.m_data);
String:
operator=(constString&
{if(this==&
return*this;
delete[]m_data;
intlength=strlen(other.m_data);
{Stringstr1("
aa"
),str2;
str1.show();
str2=str1;
str2.show();
Stringstr3(str2);
str3.show();
1.已知交通工具类定义如下。
要求:
(1)实现这个类;
(2)定义并实现一个小车类car,是它的公有派生类,小车本身的私有
属性有载人数,小车的函数有init(设置车轮数,重量和载人数),getpassenger(获取载人数
),print(打印车轮数,重量和载人数)。
classvehicle
{protected:
intwheels;
//车轮数
floatweight;
//重量
public:
voidinit(intwheels,floatweight);
intget_wheels();
floatget_weight();
voidprint();
voidvehicle:
init(intwheels,floatweight)
{this->
wheels=wheels;
this->
weight=weight;
wheels<
intvehicle:
get_wheels()
{returnwheels;
floatvehicle:
get_weight()
{returnweight;
print()
"
车轮数:
"
重量:
weight<
classcar:
publicvehicle
{private:
intpassengers;
voidinit(intwheels,floatweight,intpass);
intgetpassenger();
};
voidcar:
init(intwheels,floatweight,intpass)
{vehicle:
init(wheels,weight);
passengers=pass;
intcar:
getpassenger()
{returnpassengers;
print();
可载人数:
passengers<
1.求n(n=3)个学生的最高分和最低分及姓名,已有student类声明和main函数,完成
student类的实现部分。
classstudent
{charname[10];
intdeg;
student(charna[]="
intd=0);
char*getname();
friendintcompare(student&
s1,student&
s2);
intgetdeg();
{studentst[]={student("
王强"
74),student("
李刚"
68),student("
张雪"
84)};
inti=0,min=0,max=0;
for(i=1;
i<
3;
i++)
{if(compare(st[max],st[i])==-1)
max=i;
if(compare(st[min],st[i])==1)
min=i;
最高分:
st[max].getdeg()<
姓名:
st[max].getname()<
最低分:
(*(st+min)).getdeg()<
student:
student(charna[],intd)
{strcpy(name,na);
deg=d;
char*student:
getname(){returnname;
intcompare(student&
s2)
{if(s1.deg>
s2.deg)
return1;
elseif(s1.deg==s2.deg)
return0;
elsereturn-1;
intstudent:
getdeg()
{returndeg;
1.写一个程序,定义一个抽象类Shape,由它派生3个类:
Square(正方形)、Trapezoid(梯形
)和Triangle三角形。
用虚函数分别计算几种图形面积、并求它们的和。
要求用基类指针数组
,使它每一个元素指向一个派生类对象。
#include<
classShape
{public:
virtualdoublearea()const=0;
};
classSquare:
Square(doubles):
side(s){}
doublearea()const{returnside*side;
doubleside;
classTrapezoid:
Trapezoid(doublei,doublej,doublek):
a(i),b(j),h(k)
{}
doublearea()const{return((a+b)*h/2);
doublea,b,h;
classTriangle:
Triangle(doublei,doublej):
w(i),h(j)
doublearea()const{return(w*h/2);
doublew,h;
{Shape*p[5];
Squarese(5);
Trapezoidtd(2,5,4);
Trianglete(5,8);
p[0]=&
se;
p[1]=&
td;
p[2]=&
te;
doubleda=0;
for(inti=0;
{da+=p[i]->
area();
总面积是:
da<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 程序设计