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

上传人:b****3 文档编号:5448192 上传时间:2022-12-16 格式:DOCX 页数:17 大小:17.45KB
下载 相关 举报
面向对象C习题参考解答.docx_第1页
第1页 / 共17页
面向对象C习题参考解答.docx_第2页
第2页 / 共17页
面向对象C习题参考解答.docx_第3页
第3页 / 共17页
面向对象C习题参考解答.docx_第4页
第4页 / 共17页
面向对象C习题参考解答.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

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

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

面向对象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);

cout<<"小狗:

"<

"<

}

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

#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);

cout<<"矩形面积:

"<

}

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

#include

usingnamespacestd;

classRectangle

{

public:

Rectangle(intl,intw)

{

length=l;

width=w;

}

floatgetArea()

{

returnlength*width;

}

private:

intlength;

intwidth;

};

voidmain()

{

Rectanglerec(10,20);

cout<<"矩形面积:

"<

}

4-13定义一个Circle类,有数据成员radius(半径),成员函数getArea(),计算圆的面积,构造一个Circle的对象进行测试。

#include

usingnamespacestd;

constfloatPI=3.1415;

classCircle

{

public:

Circle(floatr)

{

radius=r;

}

floatgetArea()

{

returnradius*PI*PI;

}

private:

floatradius;

};

voidmain()

{

Circlec(5.5);

cout<<"圆的面积:

"<

}

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-7定义一个Cat类,拥有静态数据成员numOfCats,记录Cat的个体数目;静态成员函数getNumOfCats(),读取numOfCats。

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

#include

usingnamespacestd;

classCat

{

public:

Cat()

{

numOfCats++;

}

~Cat()

{

numOfCats--;

}

staticintgetNumOfCats()

{

returnnumOfCats;

}

private:

staticintnumOfCats;

};

intCat:

:

numOfCats=0;

voidmain()

{

cout<<"现在的Cat数量:

"<

:

getNumOfCats()<

Cata;

cout<<"现在的Cat数量:

"<

Catb;

cout<<"现在的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);

cout<<"船和汽车共重"<

"<

}

7-5定义一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有getArea()函数计算对象的面积。

使用Rectangle类创建一个派生类Square。

#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);

cout<<"矩形的面积:

"<

cout<<"圆的面积:

"<

cout<<"正方形的面积:

"<

}

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

#include

usingnamespacestd;

classMammal

{

public:

Mammal()

{

cout<<"ConstructingMammal."<

}

~Mammal()

{

cout<<"DesstructingMammal."<

}

};

classDog:

publicMammal

{

public:

Dog()

{

cout<<"ConstructingDog."<

}

~Dog()

{

cout<<"DesstructingDog."<

}

};

voidmain()

{

Dogd;

}

7-8定义一个Document类,有数据成员name,从Document派生出Book类,增加数据成员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()

{

cout<<"书名:

";

Document:

:

show();

cout<

"<

}

private:

intpageCount;

};

voidmain()

{

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

book.show();

}

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

#include

usingnamespacestd;

classObject

{

public:

Object()

{

cout<<"ConstructingObject."<

}

~Object()

{

cout<<"DestructingObject."<

}

voidsetWeight(intw)

{

weight=w;

}

intgetWeight()

{

returnweight;

}

private:

intweight;

};

classBox:

publicObject

{

public:

Box()

{

cout<<"ConstructingBox."<

}

~Box()

{

cout<<"DestructingBox."<

}

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);

cout<<"盒子:

高"<

}

8-4

#include

usingnamespacestd;

classCounter

{

public:

Counter(intii=0)

{

i=ii;

}

voidprint()

{

cout<<"i="<

}

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<<"MammalSpeak!

"<

}

};

classDog:

publicMammal

{

public:

virtualvoidspeak()

{

cout<<"DogSpeak!

"<

}

};

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();

}

仅供个人用于学习、研究;不得用于商业用途。

Forpersonaluseonlyinstudyandresearch;notforcommercialuse.

NurfürdenpersönlichenfürStudien,Forschung,zukommerziellenZweckenverwendetwerden.

Pourl'étudeetlarechercheuniquementàdesfinspersonnelles;pasàdesfinscommerciales.

 толькодлялюдей,которыеиспользуютсядляобучения,исследованийинедолжныиспользоватьсявкоммерческихцелях. 

以下无正文

仅供个人用于学习、研究;不得用于商业用途。

Forpersonaluseonlyinstudyandresearch;notforcommercialuse.

NurfürdenpersönlichenfürStudien,Forschung,zukommerziellenZweckenverwendetwerden.

Pourl'étudeetlarechercheuniquementàdesfinspersonnelles;pasàdesfinscommerciales.

 толькодлялюдей,которыеиспользуютсядляобучения,исследованийинедолжныиспользоватьсявкоммерческихцелях. 

以下无正文

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

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1