新程序Word文档下载推荐.docx

上传人:b****5 文档编号:19825258 上传时间:2023-01-10 格式:DOCX 页数:41 大小:21.86KB
下载 相关 举报
新程序Word文档下载推荐.docx_第1页
第1页 / 共41页
新程序Word文档下载推荐.docx_第2页
第2页 / 共41页
新程序Word文档下载推荐.docx_第3页
第3页 / 共41页
新程序Word文档下载推荐.docx_第4页
第4页 / 共41页
新程序Word文档下载推荐.docx_第5页
第5页 / 共41页
点击查看更多>>
下载资源
资源描述

新程序Word文档下载推荐.docx

《新程序Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《新程序Word文档下载推荐.docx(41页珍藏版)》请在冰豆网上搜索。

新程序Word文档下载推荐.docx

voiddis(Pointa,Pointb);

intmain()

Pointa,b;

Distanced;

d.dis(a,b);

return0;

}

Point:

:

Point()

doublen,m;

cout<

<

"

请输入两点坐标"

endl;

cin>

>

n>

m;

x=n;

y=m;

voidDistance:

dis(Pointa,Pointb)

doublec;

c=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));

outputdis"

c<

(4)定义一个矩形类,用成员函数其周长、面积。

实验2运算符重载

1.实验目的

(1)掌握成员函数重载运算符。

(2)掌握友元函数重载运算符。

(3)理解类型转换的必要性,掌握类型转换的使用方法。

2.实验内容

(1)定义空间中的点类(有x,y,z坐标),并重载其++和—运算符。

编写主

函数对该类进行应用。

classpoint

intx;

inty;

intz;

point(inta=0,intb=0,intc=0);

pointoperator++();

pointoperator--();

voidprint();

pointob1(1,2,3),ob;

ob1"

ob1.print();

ob=++ob1;

ob1++"

ob.print();

ob=--ob1;

ob1--"

point:

point(inta,intb,intc)

x=a;

y=b;

z=c;

pointpoint:

operator++()

x=++x;

y=++y;

z=++z;

return*this;

operator--()

x=--x;

y=--y;

z=--z;

voidpoint:

print()

(x,y,z)"

("

x<

"

y<

z<

)"

(2)定义一个复数类,并通过定义运算符重载实现两个复数可以判别是否

相等(==),并给出主函数应用该类。

classcomplex

doublereal;

doubleimag;

intn;

complex(doubler=0.0,doublei=0.0);

complexoperator==(complexa);

complexcom1(1.2,2.2),com2(1.2,2.3);

com1==com2;

complex:

complex(doubler,doublei)

real=r;

imag=i;

complexcomplex:

operator==(complexa)

if(real==a.real&

&

imag==a.imag)

相同"

else

不同"

(3)重载减法运算符,实现两个字符串相减。

例如:

”howareyou”-“are”=”howyou”

string.h"

classstr

char*buffer;

intlength;

str(char*in_str);

conststr&

operator-=(conststr&

);

voidShowstr();

~str();

str:

str(char*in_str)

length=strlen(in_str);

buffer=newchar[length+1];

strcpy(buffer,in_str);

str:

sub_str)

char*temp=buffer;

inti,j,k;

i=0,j=0,k=0;

while(i<

strlen(temp)&

j<

sub_str.length)

if(temp[i]!

=sub_str.buffer[j])

buffer[k]=temp[i];

k++;

j++;

i++;

strlen(temp))

buffer[k]='

\0'

;

delete[]temp;

return*this;

voidstr:

Showstr()

buffer<

~str()

delete[]buffer;

strs1("

happybirthdaytoyou"

),s2("

birthday"

s1-=s2;

s1.Showstr();

设计人民币类,其数据成员为fen(分)、jiao(角)、yuan(元)。

重载这个类

的加法、减法运算符。

classmoney

intyuan;

intjiao;

intfen;

money(inty=0,intj=0,intf=0);

moneyoperator+(moneya);

moneyoperator-(moneya);

moneyob1(20,5,6),ob2(15,3,7),total;

total=ob1+ob2;

total.print();

total=ob1-ob2;

money:

money(inty,intj,intf)

yuan=y;

jiao=j;

fen=f;

moneymoney:

operator+(moneya)

moneytemp;

temp.yuan=yuan+a.yuan;

temp.jiao=jiao+a.jiao;

temp.fen=fen+a.fen;

if(temp.fen>

10)

temp.fen=temp.fen%10;

temp.jiao=temp.jiao+1;

if(temp.jiao>

temp.jiao=temp.jiao%10;

temp.yuan=temp.yuan+1;

returntemp;

operator-(moneya)

temp.yuan=yuan-a.yuan;

temp.jiao=jiao-a.jiao;

if(temp.jiao<

0)

temp.jiao=temp.jiao+10;

temp.yuan=temp.yuan-1;

temp.fen=fen-a.fen;

if(temp.fen<

temp.fen=temp.fen+10;

temp.jiao=temp.jiao-1;

voidmoney:

yuan<

yuan"

jiao<

jiao"

fen<

fen"

编写一个程序,用成员函数重载运算符“+”和“”,实现两个二维数组

相加和相减,要求第一个二维数组的值由构造函数设置,另一个二维数组

的值由键盘输入。

constintrow=2;

constintcol=3;

classarray

intvar[row][col];

array()

inti,j,k=1;

for(i=0;

i<

row;

i++)

for(j=0;

col;

j++)

var[i][j]=k;

k=k+1;

voidget_array()

inti,j;

pleaseinput2*3data"

var[i][j];

voiddisplay()

setw(5)<

arrayoperator+(arrayx)

arraytemp;

temp.var[i][j]=var[i][j]+x.var[i][j];

arrayoperator-(arrayx)

temp.var[i][j]=var[i][j]-x.var[i][j];

arrayx,y,z;

y.get_array();

Displayobjectx"

x.display();

Displayobjecty"

y.display();

z=x+y;

Displayobjectz=x+y"

z.display();

z=x-y;

Displayobjectz=x-y"

定义一个计数器类,重载其++和—运算符,并给出main()

对该类进行应用。

#include<

iostream>

#include"

classcounter{

counter(doublea);

counteroperator++();

counteroperator--(int);

voidstop();

intb;

counterc(b);

input:

do{

b;

++c;

}while(b=='

\n'

||b=='

\r'

'

c.print();

counter:

counter(doublea)

countercounter:

++x;

operator--(int)

x--;

voidcounter:

x-1<

实验3组合、继承与多态性

一、实验目的

(1)掌握继承机制。

(2)掌握虚函数。

(3)理解并掌握虚基类。

二、实验内容

(1)编写一个程序:

设计一个汽车类,数据成员有轮子个数、车重。

小车

类是汽车类的私有派生类,包含载客量。

卡车类是汽车类的私有派生类,包含载

客数和载重量。

每个类都有数据的输出方法。

#include<

classbus

intnum;

intweight;

voidInitb(intn=0,intw=0)

num=n;

weight=w;

intgetnum(){returnnum;

intgetweight(){returnweight;

classcar:

privatebus

intpassengers;

bus:

getnum;

getweight;

voidInitc(intn,intw,intp)

Initb(n,w);

passengers=p;

intgetpassengers(){returnpassengers;

classdruck:

intload;

voidInitd(intn,intw,intp,intl)

load=l;

intgetload(){returnload;

busa;

a.Initb(4,10);

a.getnum()<

'

a.getweight()<

carb;

b.Initc(4,8,5);

car:

b.getnum()<

b.getweight()<

b.getpassengers()<

druckc;

c.Initd(8,20,2,30);

druck:

c.getnum()<

c.getweight()<

c.getpassengers()

c.getload()<

(2)虚基类为Shape,从Shape派生出矩形类(左上角点、宽、高)、椭圆

类(横轴、纵轴)。

给出各个类的构造函数,成员初始化,在基类中定义虚函数

GetArea()(计算面积),在派生类中改写。

写出该程序的实现。

classshape

doublep;

doubleq;

shape(doublex,doubley):

p(x),q(y){}

virtualdoubleGetArea()const=0;

classrect:

virtualpublicshape

doublen;

doublem;

rect(doublex=2,doubley=3):

shape(0,0),n(x),m(y){}

doubleGetArea()const

returnn*m;

classellipse:

doublea;

doubleb;

ellipse(doublex=1,doubley=3):

shape(0,0),a(x),b(y){}

return3.14*a*b;

rectA;

矩形面积是:

A.GetArea()<

ellipseB;

椭圆面积是:

B.GetArea()<

定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有

成员函数fn1()、fn2(),fn1()是虚函数,DerivedClass也有成员函数fn1()、

fn2(),在主程序中定义一个DerivedClass的对象,分别用BaseClass和

DerivedClass的指针来调用fn1()、fn2()。

usingnamespacestd;

classBaseClass

voidfn1();

voidfn2();

voidBaseClass:

fn1()

调用基类的函数fn1()"

endl;

fn2()

调用基类的函数fn2()"

classDerivedClass:

publicBaseClass

voidDerivedClass:

cout<

"

调用派生类的函数fn1()"

调用派生类的函数fn2()"

DerivedClassaDerivedClass;

DerivedClass*pDerivedClass=&

aDerivedClass;

BaseClass*pBaseClass=&

aDerivedClass.fn1();

aDerivedClass.fn2();

pBaseClass->

fn1();

fn2();

pDerivedClass->

fn1();

pDerivedClass->

(4)使用private和protected继承从基类创建两个新类。

然后通过派生类

的对象访问基类的私有数据成员(通过函数)。

classmammal

{private:

intm;

inty(intn)

m=n;

n<

classdog:

privatemammal

{public:

intf1()

y(9);

classcat:

protectedmammal

intf2()

y(8);

{dogd;

catc;

d.f1();

c.f2();

(5)定义一个哺乳动物Mammal类,再由此派生出狗Dog类和Cat类,各

定义一个Dog类和Cat类的对象,观察基类与派生类的构造函数与析构函数的

调用顺序。

classmammal//基类

mammal()

{cout<

mammalcon"

~mammal()

mammaldecon"

publicmammal//派生类

dog()

dogcon"

~dog()

dogdecon"

cat()

catcon"

~cat()

catdecon"

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

当前位置:首页 > 医药卫生 > 基础医学

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

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