1、copy(Myclass &my) this-x=my.x;y=my.y;*/-not use this-void Myclass: x=my.x; y=my.y;void main() Myclass my(10,20),t(30,40); my.print( ); my.copy(t);/*(2)设计一个类,实现两个复数的四则运算。实现加减乘除功能的函数用友元函数实现。*/#include0) coutre+imreal1image1;输入第二个复数的实部和虚部:real2image2; complex myp1(real1,image1),myp2(real2,image2); add(
2、myp1,myp2); sub(myp1,myp2); pls(myp1,myp2); div(myp1,myp2);3/*分析下面程序,给出横线部分的语句,并分析程序的输出,和m_count的功能*/class A static int m_counter; A(); A(); static void display();int A:m_counter=0;/ /将m_counter初始化为;A:A() m_counter+;A() m_counter-;void A:display()There are:m_counterobjects of class A.nint main() A a
3、1; A a2; A a3; A:display(); a1.display();4 /*商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。 本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格*/定义Box类class Box float weight; /每一箱的重量
4、float price; /每一箱的单价 friend class Carlo; /Carlo是Box的友元类,故可以直接调用Box的私有成员 Box(float x=0,float p=0) /构造函数 weight=x;price=p;/定义Carlo类class Carlo static int num; /交易次数 static float totalweight;/定义总重量 static float totalprice;/定义总价值 Carlo(); Carlo()num-; void buy(Box& p); void sell(Box&/静态变量初始化int Carlo:nu
5、m=0;float Carlo:totalprice=0;totalweight=0;/-/函数实现Carlo:Carlo() num+;The numth business /交易之前的货物总价值和总重量The totalweight of the goods shop has is totalweightThe totalprice of the goods shop has is totalprice0)/首先判断当前货物是否满足卖出重量 totalweight=totalweight-p.weight;The totalweight after selling is totalpric
6、e=totalprice-p.price*p.weight;The totalprice after selling is Dont have the enough goods Box b1(10,2),b2(3,3); Carlo shop; shop.buy(b1); shop.sell(b2); Carlo shop2; shop2.buy(b1);/*5.1)。编写一个类Node,声明一个数据成员member和已经静态成员count,另构造函数初始化数据成员,并把静态数据成员加,另析构函数把静态数据成员减。class Node int member; static int count;
7、 Node(int x=0) member=x; count=+1; Node()count-=1;int Node:count=0;/*5.2)。在)的基础上编写应用程序,创建个对象,然后显示他们的数据成员和静态成员,再析构每个对象,并显示他们对静态数据成员的影响。 count+; Node() count-;析构以后count=count void showM()coutmember=member void showC()coutcount= Node n1(1),n2(2),n3(3); n1.showM(); n1.showC(); n2.showM(); n2.showC(); n3
8、.showM(); n3.showC();/*5.3修改),让静态成员函数访问静态数据成员,并让静态数据成员是私有的。 static void showC()cout Node:showC();/*(6)个类分别为整型数集合类和实型数集合数类。将缺少的内容补齐。并完成要求的其它内容。如:(1)在Intset中再增加一个成员函数,将对象的整型数据拷贝的到floatset的对象中此成员函数的原型为:void settofloat(floatset &set);/形参为拷贝的目标对象/class floatset;class floatset float num3; /friend class In
9、tset; floatset( float x,float y, float z) /添加初始化内容 num0=x; num1=y; num2=z; void set1(float x,float y,float z) void print( ) /打印数据 for(int i=0;i3;i+) coutnum=numinum0; set.num1=this-num1; set.num2=this-num2;/*7.1分析以下程序的功能,把程序用三种方法(公有数据成员)补充完整,实现对对象peppy的成员*/class Animal;/void SetValue(Animal&, int);,
10、 int, int);class Animal Animal():itsWeight(0),itsAge(0) void show()coutitsWeightendlitsAge int itsWeight; int itsAge;void SetValue(Animal& ta, int tw) / 设置itsWeight值 ta.itsWeight=tw; ta, int tw, int tn) / 设置itsWeight和itsAge值 ta.itsAge=tn; Animal peppy; SetValue(peppy, 5); peppy.show(); SetValue(peppy, 7, 9); return 0;7.2*分析以下程序的功能,把程序用三种方法(友元访问私有数据成员) friend void SetValue(Animal& ta, int tw); ta, int tw, int tn);/*7.3分析以下程序的功能,把程序用三种方法(用成员函数访问私有数据成员) void set1( int tw, int tn ) itsWeight = tw; itsAge = tn; void set2( int tw ) ta.set2(tw); ta.set1(tw,tn); peppy.show()
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2