您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > C++程序设计经典300例2of3
1.*****************************************//类的多态实现#includeiostreamusingnamespacestd;classsalary{public:salary(){}~salary(){}virtualvoidpay(){}private:};classmember1:publicsalary{public:member1(doublea){_total=a;}~member1(){}voidpay(){cout每月给员工1的薪水为:_total元endl;}private:double_total;};classmember2:publicsalary{public:member2(doublea){_total=a;}~member2(){}voidpay(){cout每月给员工2的薪水为:_total元endl;}private:double_total;};voidmain(){member1m1(4000.0);member2m2(10000.0);salary*sa1=&m1;salary*sa2=&m2;sa1-pay();sa2-pay();}2*********************************************//实例113动物对象进化(继承)#includeiostreamusingnamespacestd;classanimal_ancestor{public:animal_ancestor(){}~animal_ancestor(){}voidrespire()//呼吸{cout可以呼吸endl;}};classape:publicanimal_ancestor{public:ape(){}~ape(){}voidmove()//行走{cout可以行走endl;}};classhuman:publicape{public:human(){}~human(){}voidmiss(){cout可以想念某人endl;}};voidmain(){human_human;cout人类;_human.respire();_human.move();_human.miss();}3.************************************************//实例112求圆的面积和周长#includeiostreamusingnamespacestd;#definePi3.14159265classcircle{public:doublearea(doublex){returnPi*x*x;}doublecircumfer(doubley){return2*Pi*y;}public:doubleradius;};voidmain(){circle_circle;cout请输入圆的半径:;cin_circle.radius;cout圆的面积为:_circle.area(_circle.radius)endl;cout圆的周长为:_circle.circumfer(_circle.radius)endl;}4-**********************************************/实例111乡村生产总值(同类对象数据统计(ABC对象相加))#includeiostreamusingnamespacestd;classCun_product{public:Cun_product(){veget_result=0;fruit_result=0;grain_result=0;}Cun_product(doublex,doubley,doublez){veget_result=x;fruit_result=y;grain_result=z;}~Cun_product(){}friendCun_productoperator+(Cun_productcun1,Cun_productcun2);//重载友元运算符函数voidoutput(){coutveget_result,fruit_result,grain_resultendl;}private:doubleveget_result;doublefruit_result;doublegrain_result;};Cun_productoperator+(Cun_productcun1,Cun_productcun2){Cun_productadd;add.veget_result=cun1.veget_result+cun2.veget_result;add.fruit_result=cun1.fruit_result+cun2.fruit_result;add.grain_result=cun1.grain_result+cun2.grain_result;returnadd;}voidmain(){Cun_productcun_A(1.2,3,4),cun_B(2.3,5,1),cun_C(1.4,3,2),cun;cun=cun_A+cun_B+cun_C;coutABC村总共的蔬菜、水果和稻谷产量分别为:;cun.output();}5*****************************************************//实例110产量统计(计算A村各类农作物的产量)#includeiostreamusingnamespacestd;classCun_A{public:Cun_A(doublex,doubley,doublez){acer_veget=x;acer_fruit=y;acer_grain=z;}~Cun_A(){}voidSetPerProduct(doublea,doubleb,doublec){output_per_acerV=a;output_per_acerF=b;output_per_acerG=c;}voidveget(){doubleresult=acer_veget*output_per_acerV;coutA村蔬菜的产量为:resultendl;}voidfruit(){doubleresult=acer_fruit*output_per_acerF;coutA村水果的产量为:resultendl;}voidgrain(){doubleresult=acer_grain*output_per_acerG;coutA村稻谷的产量为:resultendl;}private:doubleacer_veget;//蔬菜作物的占地doubleoutput_per_acerV;//每亩地产蔬菜多少doubleacer_fruit;//水果作物的占地doubleoutput_per_acerF;//每亩地产水果多少doubleacer_grain;//稻谷作物的占地doubleoutput_per_acerG;//每亩地产稻谷多少};voidmain(){Cun_AA(1.2,2,3);A.SetPerProduct(500,400,700);A.veget();A.fruit();A.grain();}6.*****************************************************#includeiostreamusingnamespacestd;voidcal_num(double*a,int*b,intnumber){for(inti=0;inumber;i++){if(a[i]60)//不及格b[0]++;elseif(a[i]=60&&a[i]70)b[1]++;elseif(a[i]=70&&a[i]80)b[2]++;elseif(a[i]=80&&a[i]90)b[3]++;else//优秀b[4]++;}}voidmain(){doublemath_score[35];//班级有35个学生intnum[5]={0};//5个等级的人数cout请输入35个学生的成绩:endl;for(inti=0;i35;i++)cinmath_score[i];cal_num(math_score,num,35);cout60分的人数:num[0]endl;cout介于60~70分的人数:num[1]endl;cout介于70~80分的人数:num[2]endl;cout介于80~90分的人数:num[3]endl;cout90分的人数:num[4]endl;}7********************************************************#includeiostreamusingnamespacestd;structticket{chartrain_number[5];//车次chartrain_name[50];//从哪儿到哪儿intyingwo_num;//硬卧票数floatyingwo_cash;//硬卧钱intyingzuo_num;//硬座票数floatyingzuo_cash;//硬座钱};ticketTicket[5];//数据库目前只存5条项目voidinit(){strcpy(Ticket[0].train_number,T56);strcpy(Ticket[0].train_name,西安-北京西);Ticket[0].yingwo_num=100;Ticket[0].yingwo_cash=263.5;Ticket[0].yingzuo_num=200;Ticket[0].yingzuo_cash=148.5;strcpy(Ticket[1].train_number,K126);strcpy(Ticket[1].train_name,西安-天津);Ticket[1].yingwo_num=60;Ticket[1].yingwo_cash=289.5;Ticket[1].yingzuo_num=100;Ticket[1].yingzuo_cash=163.5;strcpy(Ticket[2].train_number,T166);strcpy(Ticket[2].train_name,西安-上海);Ticket[2].yingwo_num=150;Ticket[2].yingwo_cash=321.5;Ticket[2].yingzuo_num=240;Ticket[2].yingzuo_cash=180.5;strcpy(Ticket[3].train_number,T266);strcpy(Ticket[3].train_name,西安-广州);Ticket[3].yingwo_num=50;Ticket[3].yingwo_cash=414;Ticket[3].yingzuo_num=40;Ticket[3].yingzuo_cash=236;strcpy(Ticket[4].train_number,T114);strcpy(Ticket[4].train_name,西安-南京);Ticket[4].yingwo_num=10;Ticket[4].yingwo_cash=270.5;Ticket[4].yingzuo_num=20;Ticket[4].yingzuo_cash=152.5;}voidquery(){charcheci[5];boolflag=true;boolwhether=false;//是否有while(flag==true){cout请输入要查询的车次:;cincheci;for(in
本文标题:C++程序设计经典300例2of3
链接地址:https://www.777doc.com/doc-3747329 .html