C++课程实验3文档格式.docx
《C++课程实验3文档格式.docx》由会员分享,可在线阅读,更多相关《C++课程实验3文档格式.docx(27页珍藏版)》请在冰点文库上搜索。
![C++课程实验3文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/5/756cce63-3259-461b-8c56-08b857bc1b0c/756cce63-3259-461b-8c56-08b857bc1b0c1.gif)
copy(Myclass&
my)
this->
x=my.x;
y=my.y;
}*/
//----------notusethis--------
voidMyclass:
x=my.x;
y=my.y;
voidmain()
Myclassmy(10,20),t(30,40);
my.print();
my.copy(t);
/*
(2)设计一个类,实现两个复数的四则运算。
实现加减乘除功能的函数用友元函数实现。
*/
#include<
classcomplex//定义复数类
//私有数据成员
doublereal;
//复数的实部
doubleimage;
//复数的虚部
//外部接口
complex(doublex=0,doubley=0)//构造函数
{real=x;
image=y;
//分别声明实现四则运算函数的友元函数
friendvoidadd(complex&
p1,complex&
p2);
//加法
friendvoidsub(complex&
//减法
friendvoidpls(complex&
//乘法
friendvoiddiv(complex&
//除法
//函数的实现
voidadd(complex&
p2)
doublere=p1.real+p2.real;
doubleim=p1.image+p2.image;
Theresuleis"
;
if(im>
0)
cout<
re<
+"
im<
i"
else
voidsub(complex&
doublere=p1.real-p2.real;
doubleim=p1.image-p2.image;
voidpls(complex&
doublere=p1.real*p2.real-p1.image*p2.image;
doubleim=p1.real*p2.image+p1.image*p2.real;
voiddiv(complex&
doublere=(p1.real*p2.real+p1.image*p2.image)/(p2.real*p2.real+p2.image*p2.image);
doubleim=(p1.image*p2.real-p1.real*p2.image)/(p2.real*p2.real+p2.image*p2.image);
doublereal1,real2,image1,image2;
输入第一个复数的实部和虚部:
cin>
>
real1>
image1;
输入第二个复数的实部和虚部:
real2>
image2;
complexmyp1(real1,image1),myp2(real2,image2);
add(myp1,myp2);
sub(myp1,myp2);
pls(myp1,myp2);
div(myp1,myp2);
3./*分析下面程序,给出横线部分的语句,并分析程序的输出,和m_count的功能*/
classA
staticintm_counter;
A();
~A();
staticvoiddisplay();
intA:
m_counter=0;
//——————————//将m_counter初始化为;
A:
A()
m_counter++;
~A()
m_counter--;
voidA:
display()
Thereare:
m_counter<
objectsofclassA.\n"
intmain()
Aa1;
Aa2;
Aa3;
A:
display();
a1.display();
4./*商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,
各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量
和总价值。
编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。
本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,
用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新
的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格*/
//定义Box类
classBox
floatweight;
//每一箱的重量
floatprice;
//每一箱的单价
friendclassCarlo;
//Carlo是Box的友元类,故可以直接调用Box的私有成员
Box(floatx=0,floatp=0)//构造函数
{weight=x;
price=p;
//定义Carlo类
classCarlo
staticintnum;
//交易次数
staticfloattotalweight;
//定义总重量
staticfloattotalprice;
//定义总价值
Carlo();
~Carlo(){num--;
voidbuy(Box&
p);
voidsell(Box&
//静态变量初始化
intCarlo:
num=0;
floatCarlo:
totalprice=0;
totalweight=0;
//---------------------------------
//函数实现
Carlo:
Carlo()
num++;
The"
num<
thbusiness"
//交易之前的货物总价值和总重量
Thetotalweightofthegoodsshophasis"
totalweight<
Thetotalpriceofthegoodsshophasis"
totalprice<
voidCarlo:
buy(Box&
p)//实现买货物行为
totalweight=totalweight+p.weight;
Thetotalweightafterbuyingis"
totalprice=totalprice+p.price*p.weight;
Thetotalpriceafterbuyingis"
sell(Box&
p)//实现卖货物行为
if(totalweight-p.weight>
0)//首先判断当前货物是否满足卖出重量
{
totalweight=totalweight-p.weight;
Thetotalweightaftersellingis"
totalprice=totalprice-p.price*p.weight;
Thetotalpriceaftersellingis"
}
Don'
thavetheenoughgoods"
Boxb1(10,2),b2(3,3);
Carloshop;
shop.buy(b1);
shop.sell(b2);
Carloshop2;
shop2.buy(b1);
/*5.1)。
编写一个类Node,声明一个数据成员member和已经静态成员count,
另构造函数初始化数据成员,并把静态数据成员加,另析构函数把静态数
据成员减。
classNode
intmember;
staticintcount;
Node(intx=0)
member=x;
count=+1;
~Node(){count-=1;
intNode:
count=0;
/*5.2)。
在)的基础上编写应用程序,创建个对象,
然后显示他们的数据成员和静态成员,再析构每个对象,
并显示他们对静态数据成员的影响。
count++;
~Node()
count--;
析构以后count="
count<
voidshowM(){cout<
member="
member<
voidshowC(){cout<
count="
Noden1
(1),n2
(2),n3(3);
n1.showM();
n1.showC();
n2.showM();
n2.showC();
n3.showM();
n3.showC();
/*5.3修改),让静态成员函数访问静态数据成员,并让静态数据成员是私有的。
staticvoidshowC(){cout<
Node:
showC();
/*(6)个类分别为整型数集合类和实型数集合数类。
将缺少的内容补齐。
并完成要求的其它内容。
如:
(1)在Intset中再增加一个成员函数,将对象的整型数据拷贝的到floatset的对象中此成员函数的原型为:
voidsettofloat(floatset&
set);
//形参为拷贝的目标对象
//classfloatset;
classfloatset
floatnum[3];
//friendclassIntset;
floatset(floatx,floaty,floatz)
{//添加初始化内容
num[0]=x;
num[1]=y;
num[2]=z;
voidset1(floatx,floaty,floatz)
voidprint()
{//打印数据
for(inti=0;
i<
3;
i++)
cout<
num["
]="
num[i]<
classIntset
intnum[3];
Intset(intx,inty,intz)
voidsettofloat(floatset&
set)
set.set1(num[0],num[1],num[2]);
IntsetI(1,2,3);
floatsetF(4.2,5.2,6.2);
显示原数据"
I.print();
F.print();
I.settofloat(F);
拷贝以后的数据"
/*6.2一个友元函数来实现上述的功能。
friendclassIntset;
set.num[0]=this->
num[0];
set.num[1]=this->
num[1];
set.num[2]=this->
num[2];
/*7.1分析以下程序的功能,把程序用三种方法(公有数据成员)
补充完整,实现对对象peppy的成员*/
classAnimal;
//voidSetValue(Animal&
int);
int,int);
classAnimal
Animal():
itsWeight(0),itsAge(0){}
voidshow(){cout<
itsWeight<
endl<
itsAge<
intitsWeight;
intitsAge;
voidSetValue(Animal&
ta,inttw)
//设置itsWeight值
ta.itsWeight=tw;
ta,inttw,inttn)
//设置itsWeight和itsAge值
ta.itsAge=tn;
Animalpeppy;
SetValue(peppy,5);
peppy.show();
SetValue(peppy,7,9);
return0;
7.2*分析以下程序的功能,把程序用三种方法(友元访问私有数据成员)
friendvoidSetValue(Animal&
ta,inttw);
ta,inttw,inttn);
/*7.3分析以下程序的功能,把程序用三种方法(用成员函数访问私有数据成员)
voidset1(inttw,inttn)
itsWeight=tw;
itsAge=tn;
voidset2(inttw)
ta.set2(tw);
ta.set1(tw,tn);
peppy.show()