面向对象C习题参考解答.docx

上传人:b****1 文档编号:14074753 上传时间:2023-06-20 格式:DOCX 页数:22 大小:17.47KB
下载 相关 举报
面向对象C习题参考解答.docx_第1页
第1页 / 共22页
面向对象C习题参考解答.docx_第2页
第2页 / 共22页
面向对象C习题参考解答.docx_第3页
第3页 / 共22页
面向对象C习题参考解答.docx_第4页
第4页 / 共22页
面向对象C习题参考解答.docx_第5页
第5页 / 共22页
面向对象C习题参考解答.docx_第6页
第6页 / 共22页
面向对象C习题参考解答.docx_第7页
第7页 / 共22页
面向对象C习题参考解答.docx_第8页
第8页 / 共22页
面向对象C习题参考解答.docx_第9页
第9页 / 共22页
面向对象C习题参考解答.docx_第10页
第10页 / 共22页
面向对象C习题参考解答.docx_第11页
第11页 / 共22页
面向对象C习题参考解答.docx_第12页
第12页 / 共22页
面向对象C习题参考解答.docx_第13页
第13页 / 共22页
面向对象C习题参考解答.docx_第14页
第14页 / 共22页
面向对象C习题参考解答.docx_第15页
第15页 / 共22页
面向对象C习题参考解答.docx_第16页
第16页 / 共22页
面向对象C习题参考解答.docx_第17页
第17页 / 共22页
面向对象C习题参考解答.docx_第18页
第18页 / 共22页
面向对象C习题参考解答.docx_第19页
第19页 / 共22页
面向对象C习题参考解答.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

面向对象C习题参考解答.docx

《面向对象C习题参考解答.docx》由会员分享,可在线阅读,更多相关《面向对象C习题参考解答.docx(22页珍藏版)》请在冰点文库上搜索。

面向对象C习题参考解答.docx

面向对象C习题参考解答

4-8定义一个Dog类,包含age,weight等属性,以及对这些属性操作的方法。

实现并测试这个类。

#include

usingnamespacestd;

classDog

{

public:

voidsetAge(inta)

{

age=a;

}

intgetAge()

{

returnage;

}

voidsetWeight(floatw)

{

weight=w;

}

floatgetWeight()

{

returnweight;

}

private:

intage;

floatweight;

};

voidmain()

{

Dogd;

d.setAge(3);

d.setWeight(30);

<

?

搼朮瑥敗杩瑨?

?

岁,重?

搼朮瑥杁?

?

尼小狗:

潣瑵?

}

的矩形类,其属性为矩形的左下角与右上角设计并测试一个名为4-9Rectangle两个点的坐标,根据坐标能计算矩形的面积。

#include

#include

usingnamespacestd;

classRectangle

{

public:

Rectangle(intxx1,intyy1,intxx2,intyy2)

{

x1=xx1;

y1=yy1;

x2=xx2;

y2=yy2;

}

floatgetArea()

{

returnfabs(x2-x1)*fabs(y2-y1);

}

private:

intx1,y1;

intx2,y2;

};

voidmain()

{

Rectanglerec(0,0,10,20);

潣瑵?

矩形面积:

<

}

4-11定义并实现一个矩形类,有长、宽两个属性,由成员函数计算矩形的面积。

#include

usingnamespacestd;

classRectangle

{

public:

Rectangle(intl,intw)

{

length=l;

width=w;

}

floatgetArea()

{

returnlength*width;

}

private:

intlength;

intwidth;

};

voidmain()

{

Rectanglerec(10,20);

<

潣瑵?

}

,计radiusCircle4-13定义一个类,有数据成员(半径),成员函数getArea()的对象进行测试。

Circle算圆的面积,构造一个

#include

usingnamespacestd;

constfloatPI=3.1415;

classCircle

{

public:

Circle(floatr)

{

radius=r;

}

floatgetArea()

{

returnradius*PI*PI;

}

private:

floatradius;

};

voidmain()

{

Circlec(5.5);

<

潣瑵?

}

4-20定义一个复数类Complex,使得下面的代码能够工作。

Complexc1(3,5);

Complexc2=4.5;

c1.add(c2);

c1.show()?

;

//源程序如下:

#include

usingnamespacestd;

classComplex

{

public:

Complex(floatr=0.0,floati=0.0)

{

real=r;

image=i;

}

voidadd(Complexb)

{

real=real+b.real;

image=image+b.image;

}

voidshow()

{

cout<

}

private:

floatreal;//实部

虚部floatimage;//};

voidmain()

{

Complexc1(3,5);

Complexc2(4.5);Complexc2=4.5;//相当于c1.add(c2);

c1.show();

}

的个体数目;静态记录类,拥有静态数据成员定义一个5-7CatnumOfCats,Cat。

设计程序测试这个类,体会静态数getNumOfCats()成员函数,读取numOfCats据成员和静态成员函数的用法。

#include

usingnamespacestd;

classCat

{

public:

Cat()

{

numOfCats++;

}

~Cat()

{

numOfCats--;

}

staticintgetNumOfCats()

{

returnnumOfCats;

}

private:

staticintnumOfCats;

};

intCat:

:

numOfCats=0;

voidmain()

{

<

:

getNumOfCats()<

潣瑵?

现在的CatCata;

<

现在的数量:

Catb;

潣瑵?

现在的Cat数量:

<

}

5-14定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和。

#include

usingnamespacestd;

classCar;

classBoat

{

public:

Boat(floatw)

{

weight=w;

}

friendfloatgetTotalWeight(Boatb,Carc);

private:

floatweight;

};

classCar

{

public:

Car(floatw)

{

weight=w;

}

friendfloatgetTotalWeight(Boatb,Carc);

private:

floatweight;

};

floatgetTotalWeight(Boatb,Carc)

{

returnb.weight+c.weight;

}

voidmain()

{

Boatboat(3500);

Carcar(1000);

<

?

朼瑥潔慴坬楥桧?

潢瑡挬牡?

尼船和汽车共重潣瑵?

}

,二者都有CircleRectangleShape7-5定义一个基类,在此基础上派生出和Square类创建一个派生类RectanglegetArea()函数计算对象的面积。

使用。

#include

usingnamespacestd;

constfloatPI=3.14;

classShape

{

public:

Shape(floata,floatb=0.0)

{

this->a=a;

this->b=b;

}

protected:

floata,b;

};

classRectangle:

publicShape

{

public:

Rectangle(floatl,floatw):

Shape(l,w)

{

}

floatgetArea()

{

returna*b;

}

};

classCircle:

publicShape

{

public:

Circle(floatr):

Shape(r)

{

}

floatgetArea()

{

returna*PI*PI;

}

};

classSquare:

publicRectangle

{

public:

Square(floatl):

Rectangle(l,l)

{

}

floatgetArea()

{

returna*a;

}

};

voidmain()

{

Rectangler(10,20);

Circlec(5);

Squares(10);

潣瑵?

矩形的面积:

<

潣瑵?

圆的面积:

<

潣瑵?

正方形的面积:

<

}

7-6定义一个哺乳动物类Mammal,再由此派生出狗类Dog,定义一个Dog类的对象,观察基类与派生类的构造函数和析构函数的调用顺序。

#include

usingnamespacestd;

classMammal

{

public:

Mammal()

{

cout<

}

~Mammal()

{

cout<

}

};

classDog:

publicMammal

{

public:

Dog()

{

cout<

}

~Dog()

{

cout<

}

};

voidmain()

{

Dogd;

}

增加有数据成员Document7-8定义一个类,name类,Book派生出Document,从pageCount数据成员。

#include

usingnamespacestd;

classDocument

{

public:

Document(char*n)

{

strcpy(name,n);

}

voidshow()

{

cout<

}

private:

charname[50];

};

classBook:

publicDocument

{

public:

Book(char*n,intp):

Document(n),pageCount(p)

{

}

voidshow()

{

潣瑵?

书名:

;

Document:

:

show();

潣瑵?

湥汤?

页数:

<

}

private:

intpageCount;

};

voidmain()

{

529);Bookbook(C++语言程序设计

book.show();

}

Boxweight定义一个7-10Object类,有数据成员及相应的操作函数,由此派生出对象,观察及相应的操作函数,声明一个widthBox和类,增加数据成员height构造函数与析构函数的调用顺序。

#include

usingnamespacestd;

classObject

{

public:

Object()

{

cout<

}

~Object()

{

cout<

}

voidsetWeight(intw)

{

weight=w;

}

intgetWeight()

{

returnweight;

}

private:

intweight;

};

classBox:

publicObject

{

public:

Box()

{

cout<

}

~Box()

{

cout<

}

voidsetHeight(inth)

{

height=h;

}

intgetHeight()

{

returnheight;

}

voidsetWidth(intw)

{

width=w;

}

intgetWidth()

{

returnwidth;

}

private:

intheight;

intwidth;

};

voidmain()

{

Boxbox;

box.setHeight(5);

box.setWidth(10);

box.setWeight(8);

潣瑵?

盒子:

高?

戼硯朮瑥效杩瑨?

?

,重,?

戼硯朮瑥楗瑤?

?

尼宽<

}

8-4

#include

usingnamespacestd;

classCounter

{

public:

Counter(intii=0)

{

i=ii;

}

voidprint()

{

cout<

}

Counteroperator+(inta)

{

Countertemp;

temp.i=i+a;

returntemp;

}

private:

inti;

};

voidmain()

{

Counterc;

c=c+3;

c.print();

c=c+5;

c.print();

}

8-5

#include

usingnamespacestd;

classMammal

{

public:

virtualvoidspeak()

{

cout<

<

}

};

classDog:

publicMammal

{

public:

virtualvoidspeak()

{

cout<

<

}

};

voidmain()

{

Dogd;

d.speak();

Mammal*p=&d;

p->speak();

}

8-7

#include

usingnamespacestd;

classPoint

{

public:

Point(intx=0,inty=0)

{

X=x;

Y=y;

}

voidprint()

{

cout<<(<

}

Point&operator++()

{

X++;

Y++;

return*this;

}

Pointoperator++(int)

{

Pointtemp=*this;

X++;

Y++;

returntemp;

}

private:

intX,Y;

};

voidmain()

{

Pointp;

(++p).print();

(p++).print();

(++p).print();

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2