/* 编程实现:定义Boat与Car两个类,两者都有weight成员表示重量,并为每个类设计构造函数(可赋初值也可以不赋初值),设置新值函数Set, 打印成员值函数Print。再定义两个类共同的一个友元函数TotalWeight(Boat B, Car C),计算B、C两者的重量之和。 */#include<iostream>#include<stdlib.h>usingnamespacestd;classBoat;classCar;voidoutputTotalWeight(constBoat&boat,constCar&car);classBoat{public:Boat(doubleweight=0):weight_(weight){}~Boat(){}friendvoidoutputTotalWeight(constBoat&boat,constCar&car);voidweightSetValue(doubleweight){weight_=weight;}voiddisplay(){cout<<"boat.weight="<<weight_<<endl;}private:doubleweight_;};classCar{public:Car(doubleweight=0):weight_(weight){}~Car(){}friendvoidoutputTotalWeight(constBoat&boat,constCar&car);voidweightSetValue(doubleweight){weight_=weight;}voiddisplay(){cout<<"car.weight="<<weight_<<endl;}private:doubleweight_;};voidoutputTotalWeight(constBoat&boat,constCar&car){cout<<"The total weight is "<<boat.weight_+car.weight_<<"."<<endl;}intmain(){Boatboat(1);Carcar(2);outputTotalWeight(boat,car);system("pause");}
/*构建一个球类Ball,其成员为球心坐标(x,y,z)和球半径r,并设计构造函数(可以赋初值也可以不赋初值),复制构造函数,析构函数(打印信息,表示其被调用),设置新值成员函数Set( ), 取球心坐标成员函数GetX( )、GetY( )、GetZ( ),取球半径成员函数GetR( ),打印成员值成员函数Print(),计算球体积成员函数Volume()。并用此类分别定义一个长度为10的静态对象数组,一个长度为20的动态对象数组,每个对象的球心坐标(x,y,z)和球半径r由随机数rand()产生(球半径r的值应该为正数),打印每个球对象的(x,y,z)、r和体积;然后分别将对象数组按照其r值从小到大排序,并打印排序后每个对象的(x,y,z)、r和体积;并计算打印出所有对象的平均(x,y,z)、平均r和平均体积。(并释放动态对象数组,注意观察析构顺序与构造顺序的差异。)//随机数样例#include <stdlib.h>void main(){ int i, x, y, z, r; srand(0); // srand为随机数序列赋初值,可以随意给初值 for (i = 0; i < 10; i++) { x = rand(); y = rand(); z = rand(); r = abs(rand()); }} */#include<stdlib.h>#include<iostream>usingnamespacestd;classBall{private:doublex,y,z;doubler;public:Ball(doublexx=0,doubleyy=0,doublezz=0,doublerr=0):x(xx),y(yy),z(zz),r(rr){}Ball(Ball&b):x(b.x),y(b.y),z(b.z),r(b.r){}~Ball(){cout<<"~Ball() called.\n";}voidxSet(doublexx){x=xx;}voidySet(doubleyy){y=yy;}voidzSet(doublezz){z=zz;}voidrSet(doublerr){r=rr;}doublexValue(){returnx;}doubleyValue(){returny;}doublezValue(){returnz;}doublerValue(){returnr;}doublevolume(){return4.0/3*3.1415926*r*r*r;}voiddisplay(){cout<<"center point ("<<x<<","<<y<<","<<z<<")"<<'\t'<<"radius = "<<r<<'\t'<<"volume = "<<this->volume()<<endl;}};/*选择排序——元素p[a]~p[b]升序排序*/voidselesort(Ballp[],inta,intb){inti,j,k;Balld;//用来交换的变量for(i=a;i<=b-1;i++)//对最后一个元素p[n-1]不用操作{k=i;//记录现在到哪里了for(j=i+1;j<=b;j++)if(p[j].rValue()<p[k].rValue())//比较的是半径k=j;//从i的下一个开始找,如果有比i小元素(第j个)的就让k为j//本质目的是找出i后面最小的一个if(k!=i)//如果i项不是最小的,那么换!{d=p[i];//排序的是元素p[i]=p[k];p[k]=d;}}}intmain(){Balla[10];Ball*b=newBall[20];doublesumx=0,sumy=0,sumz=0,sumr=0,sumv=0;// asumx=sumy=sumz=sumr=sumv=0;for(inti=0;i<=10-1;i++){a[i].xSet(rand()/100.0);sumx+=a[i].xValue();a[i].ySet(rand()/100.0);sumy+=a[i].yValue();a[i].zSet(rand()/100.0);sumz+=a[i].zValue();a[i].rSet(abs(rand())/10000.0);sumr+=a[i].rValue();sumv+=a[i].volume();a[i].display();}selesort(a,0,9);for(inti=0;i<=10-1;i++)a[i].display();cout<<"AVERAGE:"<<endl;cout<<"center point ("<<sumx<<","<<sumy<<","<<sumz<<")"<<'\t'<<"radius = "<<sumr<<'\t'<<"volume = "<<sumv<<endl;// bsumx=sumy=sumz=sumr=sumv=0;for(inti=0;i<=20-1;i++){b[i].xSet(rand()/100.0);sumx+=b[i].xValue();b[i].ySet(rand()/100.0);sumy+=b[i].yValue();b[i].zSet(rand()/100.0);sumz+=b[i].zValue();b[i].rSet(abs(rand())/10000.0);sumr+=b[i].rValue();sumv+=b[i].volume();b[i].display();}selesort(b,0,19);for(inti=0;i<=20-1;i++)b[i].display();cout<<"AVERAGE:"<<endl;cout<<"center point ("<<sumx<<","<<sumy<<","<<sumz<<")"<<'\t'<<"radius = "<<sumr<<'\t'<<"volume = "<<sumv<<endl;// ENDdelete[]b;system("pause");}