C++实践题.docx

上传人:b****6 文档编号:5818706 上传时间:2023-01-01 格式:DOCX 页数:32 大小:22.10KB
下载 相关 举报
C++实践题.docx_第1页
第1页 / 共32页
C++实践题.docx_第2页
第2页 / 共32页
C++实践题.docx_第3页
第3页 / 共32页
C++实践题.docx_第4页
第4页 / 共32页
C++实践题.docx_第5页
第5页 / 共32页
点击查看更多>>
下载资源
资源描述

C++实践题.docx

《C++实践题.docx》由会员分享,可在线阅读,更多相关《C++实践题.docx(32页珍藏版)》请在冰豆网上搜索。

C++实践题.docx

C++实践题

C++实践题

这是本人刷了N遍才收集到的,都只有正确答案,可以通过看它的编号来找相应的答案,如prog151等

正文

//Prog151

#include

usingnamespacestd;

classSalary{

public:

Salary(constchar*id,doublethe_base,doublethe_bonus,doublethe_tax)

//ERROR**********found**********

:

base(the_base),bonus(the_bonus),tax(the_tax)

{

staff_id=newchar[strlen(id)+1];

strcpy(staff_id,id);

}

//ERROR**********found**********

~Salary(){delete[]staff_id;}

doublegetGrossPay()const{returnbase+bonus;}//返回应发项合计

doublegetNetPay()const{returngetGrossPay()-tax;}//返回实发工资额

private:

char*staff_id;//职工号

doublebase;//基本工资

doublebonus;//奖金

doubletax;//代扣个人所得税

};

intmain(){

Salarypay("888888",3000.0,500.0,67.50);

cout<<"应发合计:

"<

cout<<"应扣合计:

"<

//ERROR**********found**********

cout<<"实发工资:

"<

return0;

}

Prog4

//proj1.cpp

#include

usingnamespacestd;

classMyClass{

public:

//ERROR**********found**********

MyClass(inti)

{value=i;cout<<"Constructorcalled."<

intMax(intx,inty){returnx>y?

x:

y;}//求两个整数的最大值

//ERROR**********found**********

intMax(intx,inty,intz)//求三个整数的最大值

{

if(x>y)

returnx>z?

x:

z;

else

returny>z?

y:

z;

}

intGetValue()const{returnvalue;}

~MyClass(){cout<<"Destructorcalled."<

private:

intvalue;

};

intmain()

{

MyClassobj(10);

//ERROR**********found**********

cout<<"Thevalueis"<

cout<<"Maxnumberis"<

return0;

}

//Prog9

//proj9.cpp

#include

#include

usingnamespacestd;

classMyPoint{//表示平面坐标系中的点的类

doublex;

doubley;

public:

MyPoint(doublex,doubley){this->x=x;this->y=y;}

doublegetX()const{returnx;}

doublegetY()const{returny;}

voidshow()const{cout<<'('<

};

classMyTriangle{//表示三角形的类

MyPointpoint1;//三角形的第一个顶点

MyPointpoint2;//三角形的第二个顶点

MyPointpoint3;//三角形的第三个顶点

public:

MyTriangle(MyPointp1,MyPointp2,MyPointp3);

doubleperimeter()const;//返回三角形的周长

doublearea()const;//返回三角形的面积

};

//**1************found**********

MyTriangle:

:

MyTriangle(MyPointp1,MyPointp2,MyPointp3):

point1(p1),point2(p2),point3(p3){}

doubledistance(MyPointp1,MyPointp2)//返回两点之间的距离

{

returnsqrt((p1.getX()-p2.getX())*(p1.getX()-p2.getX())+

(p1.getY()-p2.getY())*(p1.getY()-p2.getY()));

}

//**2************found**********

doubleMyTriangle:

:

perimeter()const

{

returndistance(point1,point2)+distance(point2,point3)+distance(point3,point1);

}

doubleMyTriangle:

:

area()const

{//**3************found**********

doubles=perimeter()/2;;//使用perimeter函数

returnsqrt(s*(s-distance(point1,point2))*

(s-distance(point2,point3))*

(s-distance(point3,point1)));

}

intmain()

{

MyTriangletri(MyPoint(0,2),MyPoint(2,0),MyPoint(0,0));

cout<

return0;

}

//proj10.cpp

#include

usingnamespacestd;

classMyClass{

public:

//ERROR**********found**********

MyClass(inti=0):

value(i)

{count++;}

voidPrint()

{cout<<"Thereare"<

private:

constintvalue;

staticintcount;

};

//ERROR**********found**********

intMyClass:

:

count=0;

intmain()

{

MyClassobj1,obj2;

//ERROR**********found**********

obj2.Print();

return0;

}

//proj12.cpp

#include

#include

usingnamespacestd;

classMyPoint{//表示平面坐标系中的点的类

doublex;

doubley;

public:

MyPoint(doublex,doubley){this->x=x;this->y=y;}

doublegetX()const{returnx;}

doublegetY()const{returny;}

voidshow()const{cout<<'('<

};

classMyCircle{//表示圆形的类

MyPointcen;//圆心

doublerad;//半径

public:

MyCircle(MyPoint,double);

MyPointcenter()const{returncen;}//返回圆心

doubleradius()const{returnrad;}//返回圆半径

frienddoublearea(MyCircle);//返回圆的面积

};

//**1************found**********

MyCircle:

:

MyCircle(MyPointp,doubler):

cen(p),rad(r){}

#definePI3.1415926535

doubleperimeter(MyCirclec)//返回圆c的周长

{//**2************found**********

returnPI*2*c.radius();

}

//**3************found**********

doublearea(MyCirclea)//返回圆a的面积

{

returnPI*a.rad*a.rad;

}

intmain()

{

MyCirclec(MyPoint(1,2),5.0);

c.center().show();

cout<<','<

return0;

}

//prog16

#include

classSample

{

public:

Sample()

{

x=0;

cout<<"constructingnormally"<

}

Sample(intm)

{

x=m;

cout<<"constructingwithanumber:

"<

}

//ERROR**********found**********

~Sample()

{

cout<<"destructing"<

}

voiddisplay()

{

cout<<"displayanumber:

"<

}

protected:

//ERROR**********found**********

intx;

};

voidmain()

{

Sampleobj1;

//ERROR**********found**********

Sampleobj2(20);

obj1.display();

obj2.display();

}

//prog17

#include

classVector

{

intx,y;

public:

Vector(){}

Vector(inti,intj){x=i;y=j;}

voiddisplay()

{

cout<<"("<

}

//**********found**********

friendVectoradd(Vectorv1,Vectorv2)

{

Vectorv;

v.x=v1.x+v2.x;

v.y=v1.y+v2.y;

returnv;

}

//**********found**********

friendVectorsub(Vectorv1,Vectorv2)

{

Vectorv;

v.x=v1.x-v2.x;

v.y=v1.y-v2.y;

returnv;

}

};

voidmain()

{

Vectorv1(10,20),v2(4,5),v3;

//**********found**********

v3=add(v1,v2);

cout<<"输出结果:

"<

cout<<"";v1.display();cout<<"+";v2.display();

cout<<"=";v3.display();cout<

//**********found**********

v3=sub(v1,v2);

cout<<"";v1.display();cout<<"-";v2.display();

cout<<"=";v3.display();cout<

}

//prog20

#include

#include

classTriangle

{

intx,y,z;

doublearea;

public:

Triangle(inti,intj,intk)

{

doubles;

x=i;y=j;z=k;

s=(x+y+z)/2.0;

//**********found**********

area=sqrt(s*(s-x)*(s-y)*(s-z));

}

voiddisplay()

{

cout<<"Area="<

}

//**********found**********

frienddoubleoperator+(Trianglet1,Trianglet2)

{

//**********found**********

returnt1.area+t2.area;

}

};

voidmain()

{

Trianglet1(3,4,5),t2(4,5,6);

doubles;

cout<<"t1:

";t1.display();

cout<<"t2:

";t2.display();

//**********found**********

s=t1+t2;

cout<<"总面积="<

}

//prog29

#include

#include

classstudent

{

charname[10];

intdeg;

public:

student(charna[],intd)

{

strcpy(name,na);

//**********found**********

deg=d;

}

char*getname(){returnname;}

//**********found**********

friendintcompare(students1,students2)

{

if(s1.deg>s2.deg)return1;

elseif(s1.deg==s2.deg)return0;

elsereturn-1;

}

};

voidmain()

{

studentst[]={student("王华",78),student("李明",92),student("张伟",62),student("孙强",88)};

intmin=0;//存放最低分的位置

intmax=0;//存放最高分的位置

for(inti=1;i<4;i++)

{

if(compare(st[max],st[i])==-1)

//**********found**********

max=i;

elseif(compare(st[min],st[i])==1)

//**********found**********

min=i;

}

cout<<"输出结果:

"<

cout<<"最高分者:

"<

cout<<"最低分者:

"<

}

//Prog46

#include

usingnamespacestd;

classMyClass

{

public:

//ERROR********found********请勿更改参数名

MyClass(intx):

flag(x){}

voidJudge();

private:

intflag;

};

//ERROR********found********

voidMyClass:

:

Judge()

{

switch(flag)

{

case0:

cout<<"False"<

//ERROR********found********

break;

default:

cout<<"True"<

break;

}

}

intmain()

{

MyClassobj(0);

obj.Judge();

return0;

}

//prog49

#include

#include

usingnamespacestd;

classBase

{

//ERROR********found********

public:

char*msg;

public:

Base(char*str)

{

//ERROR********found********

msg=newchar[strlen(str)+1];

strcpy(msg,str);

cout<<"Base:

"<

}

//ERROR********found********

~Base(){delete[]msg;}

};

classDerived:

publicBase

{

public:

Derived(char*str):

Base(str){}

voidShow(){cout<<"Derived:

"<

};

intmain()

{

Derivedobj("GoodLuck!

");

obj.Show();

return0;

}

//prog53

#include

usingnamespacestd;

classPoint

{

public:

Point(inti,intj):

x(i),y(j){}

intGetX(){returnx;}

intGetY(){returny;}

//ERROR**********found**********

voidMove(intxOff=0,intyOff=0){x=xOff;y=yOff;}

private:

intx,y;

};

intmain()

{

Pointone(5,5);

//ERROR**********found**********

Point&second=one;;

second.Move();

//ERROR**********found**********

cout<<"Theoneis("<

return0;

}

//prog55

#include

usingnamespacestd;

constintSize=4;

classMyClass

{

public:

MyClass(intx=0):

value(x){}

voidSet(intx){value=x;}

friendvoidJudge(MyClass&obj);

private:

intvalue;

};

//ERROR**********found**********

voidJudge(MyClass&obj)

{

if(obj.value==Size)

cout<<"largest"<

else

cout<<"smaller"<

}

intmain()

{

MyClass*ptr=newMyClass[Size];

for(inti=0;i

{

//ERROR**********found**********

(ptr+i)->Set(i+1);

Judge(*(ptr+i));

}

//ERROR**********found**********

delete[]ptr;

return0;

}

//proj80.cpp

#include

usingnamespacestd;

classMember{

//ERROR********found********

public:

Member(intval):

value(val){}

intvalue;

};

classMyClass{

Member_m;

public:

//ERROR********found********

MyClass(intval):

_m(val){}

intGetValue()const{return_m.value;}

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

当前位置:首页 > 经管营销

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

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