简单的C++题Word下载.docx
- 文档编号:7864341
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:24
- 大小:18.53KB
简单的C++题Word下载.docx
《简单的C++题Word下载.docx》由会员分享,可在线阅读,更多相关《简单的C++题Word下载.docx(24页珍藏版)》请在冰点文库上搜索。
poundsweight"
endl;
jack.setAge(7);
jack.setWeight(20);
Nowjackis"
return0;
}
/*定义一个Circle类,有数据成员radius(半径)成员函数getArea(),计算圆的面积,构造一个
Circle的对象进行测试。
*/
classCircle
Circle(floatradius)
this->
radius=radius;
~Circle(){}
floatgetArea()
return3.14*radius*radius;
floatradius;
请输入圆的半径:
cin>
>
radius;
Circlep(radius);
半径为"
radius<
的圆的面积为:
p.getArea()<
/*定义一个Tree(树)类,有成员ages(树龄),
成员函数grow(intyears)对ages加上years,
age()显示tree对象的ages值。
classTree
Tree(intages=0)
ages=ages;
~Tree(){show();
voidgrow(intyears)
ages+=years;
voidshow()
cout<
树的年龄为:
ages<
intages;
Treee1(10);
e1.show();
e1.grow(5);
/*定义一个复数类Complex,使得下面的代码能够工作
Complexc1(3,5);
//用复数3+5i初始化c1
Complexc2=4.5;
//用实数4.5初始化c2
c1.add(c2);
//将c1与c2想加,结果保存在c1中
c1.show();
//将c1输出(这时的结果应该是7.5+5i)*/
classComplex
Complex(doubler,doublei)
real=r;
image=i;
Complex(doubler)
image=0;
voidshow()
real<
+"
image<
i"
voidadd(Complexc2)
real+=c2.real;
image+=c2.image;
doublereal;
doubleimage;
Complexc1(3,5);
Complexc2(4.5);
c1.show();
c1.add(c2);
/*定义一个Cat类,拥有静态数据成员numOfCats记录Cat的个体数目,静态成员函数getNumOfCats()存取numOfCats,设计程序测试这个类,体会静态数据成员和静态成员函数的用法。
classCat
Cat(intage)
{itsAge=age;
numOfCats++;
virtual~Cat()
{numOfCats--;
virtualintgetAge()
{returnitsAge;
virtualvoidsetAge(intage)
}
staticintgetNumOfCats()
{returnnumOfCats;
intitsAge;
staticintnumOfCats;
intCat:
:
numOfCats=0;
voidtelepathicFunction()
Thereare"
Cat:
getNumOfCats()<
catsalive!
\n"
constintmaxCats=5;
Cat*catHouse[maxCats];
inti;
for(i=0;
i<
maxCats;
i++)
catHouse[i]=newCat(i);
telepathicFunction();
for(i=0;
deletecatHouse[i];
/*
在函数fn1()中定义一个静态变量n
fn1()中对n的值加1,在主函数中,
调用fn1()10次,显示n的值。
voidfn1()
staticintn=0;
n++;
n的值为:
n<
10;
fn1();
/*实现一个名为SimpleCircle的简单圆类
其数据成员int*itsRadius为一个指向其半径的指针
设计对数据成员的各种操作,给出这个类完整的实现
并且测试这个类
classSimpleCircle
SimpleCircle()
itsRadius=newint(5);
SimpleCircle(intradius)
itsRadius=newint(radius);
SimpleCircle(constSimpleCircle&
rhs)
intval=rhs.getRadius();
itsRadius=newint(val);
~SimpleCircle(){}
intgetRadius()const
return*itsRadius;
int*itsRadius;
SimpleCircleCircleOne,CircleTwo(9);
CircleOne:
CircleOne.getRadius()<
CircleTwo:
CircleTwo.getRadius()<
/*定义一个Employee类,其中包括鞭尸姓名、地址城市、和邮编等属性,包括setName()和display()等函数。
display()使用cout语句显示姓名、地址、城市和邮编等属性。
函数setName()改变对象的姓名属性,实现并测试这个类。
string>
classEmployee
charname[30];
charstreet[30];
charcity[15];
charzip[15];
Employee(char*n,char*s,char*c,char*z)
strcpy(name,n);
strcpy(street,s);
strcpy(city,c);
strcpy(zip,z);
voidsetName(char*n)
voiddisplay()
name<
\t"
street<
city<
zip<
Employeee1("
张三"
"
平安大街"
北京"
100000"
);
e1.display();
e1.setName("
李四"
/*定义一个Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象的面积。
使用Rectangle类创建一个派生类Square。
classShape
Shape(){}
~Shape(){}
virtualfloatgetArea(){return-1;
classCircle:
publicShape
Circle(floatradius):
itsRadius(radius){}
floatgetArea(){return3.14*itsRadius*itsRadius;
floatitsRadius;
classRectangle:
Rectangle(floatlen,floatwidth):
itsLength(len),itsWidth(width){};
~Rectangle(){};
virtualfloatgetArea(){returnitsLength*itsWidth;
virtualfloatgetLength(){returnitsLength;
virtualfloatgetWidth(){returnitsWidth;
floatitsLength;
floatitsWidth;
classSquare:
publicRectangle
Square(floatlen);
~Square(){};
Square:
Square(floatlen):
Rectangle(len,len)
Shape*sp;
sp=newCircle(5);
TheareaoftheCircleis"
sp->
getArea()<
deletesp;
sp=newRectangle(4,6);
TheareaoftheRctangleis"
sp=newSquare(5);
TheareaoftheSquareis"
定义一个哺乳动物类Mammal,再由此派生出Dog类,定义一个Dog类的对象,观察与派生类的构造函数的调用顺序
Manmalconstructor...
Dogconstructor...
Mammalsound!
Tailwagging...
jackis1yearold
Dogdestructor...
Mammaldestructor...*/
enummyColor{
BLACK,WHITE
classMammal{
//constructors
Mammal();
~Mammal();
//accesasors
intgetArea()const{
returnitsAge;
voidsetAge(intage){
itsAge=age;
intgetWeight()const{
returnitsWeight;
voidsetWeight(intweight){
itsWeight=weight;
//Othermenthods
voidspeak()const{
protected:
intitsWeight;
classDog:
publicMammal{
Dog();
~Dog();
myColorgetColor()const{
returnitsColor;
voidsetColor(myColorcolor){
itsColor=color;
voidwagTail(){
Tailwagging...\n"
myColoritsColor;
Mammal:
Mammal():
itsAge
(1),itsWeight(5){
Mammalconstructor...\n"
~Mammal(){
Mammaldestructor...\n"
Dog:
Dog():
itsColor(WHITE){
Dogconstructor...\n"
~Dog(){
Dogdestructor...\n"
intmain(){
Dogjack;
jack.speak();
jack.wagTail();
jackis"
jack.getArea()<
yearsold\n"
/*编写一个哺乳动物类Mammal,再由此派生出狗类Dog,
二者都声明speak()成员函数,该函数在基类中被声明为虚函数
,声明一个Dog类的对象,通过此对象调用speak函数,观察运行结果。
classMammal
Mammal(){
virtual~Mammal(){
virtualvoidspeak()const{
Mammalspeak!
Dog(){
DogConstructor...\n"
~Dog(){
Woof!
Mammal*pDog=newDog;
pDog->
speak();
deletepDog;
/*请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,
二者都有计算对象面积的函数getArea(),计算对象周长的函数getPerim()。
virtualfloatgetArea()=0;
virtualfloatgetPerim()=0;
itsRadius(radius){};
floatgetPerim(){return6.28*itsRadius;
floatgetPerim(){return2*itsLength+2*itsWidth;
virtualfloatGetLength(){returnitsLength;
virtualfloatGetWidth(){returnitsWidth;
ThePerimeteroftheCircleis"
getPerim()<
TheareaoftheRectangleis"
TheperimteroftheRectangleis"
/*对类Point重载++(自增)、--(自减)运算符,要求同时重载前缀和后缀的形式。
classPoint
Point&
operator++();
Pointoperator++(int);
operator--();
Pointoperator--(int);
Point(){_x=_y=0;
intx(){return_x;
inty(){return_y;
int_x,_y;
Point&
Point:
operator++()
_x++;
_y++;
return*this;
PointPoint:
operator++(int)
Pointtemp=*this;
++*this;
returntemp;
operator--()
_x--;
_y--;
return*this;
operator--(int)
Pointtemp=*this;
--*this;
Pointa;
a的值为:
a.x()<
a.y()<
a++;
++a;
a--;
--a;
/*使用I/O流以文本方式建立一个文件test1.txt,
写入字符“已成功写入文件!
”,用其他字处理程序
(例如Windows的记事本程序Notepad)打开,看看是否正确写入。
fstream>
o
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 简单 C+
![提示](https://static.bingdoc.com/images/bang_tan.gif)