C++.docx

上传人:b****8 文档编号:23826225 上传时间:2023-05-21 格式:DOCX 页数:33 大小:21.80KB
下载 相关 举报
C++.docx_第1页
第1页 / 共33页
C++.docx_第2页
第2页 / 共33页
C++.docx_第3页
第3页 / 共33页
C++.docx_第4页
第4页 / 共33页
C++.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

C++.docx

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

C++.docx

C++

1、定义一个满足如下要求的Date类.

包含Year,Month,Day三个数据成员,以及构造函数,和设置日期函数

(1)用下面的格式输出日期:

2012年3月5日

(2)重新设置日期

并编出应用程序,定义日期对象,设置日期,输出该对象提供的日期.

#include

usingnamespacestd;

classDate{

intYear;

intMonth;

intDay;

public:

Date()

{Year=0;

Month=0;

Day=0;}

voidolddate(intya,intmo,intda);

voidsetdate(intya,intmo,intda);

};

voidDate:

:

olddate(intya,intmo,intda){

Year=ya;

Month=mo;

Day=da;

cout<<"原日期"<

}

voidDate:

:

setdate(intya,intmo,intda){

Year=ya;

Month=mo;

Day=da;

cout<<"修改后日期"<

}

intmain()

{

Datemydate;

inty,d,m,y1,d1,m1;

cin>>y>>m>>d;

cin>>y1>>m1>>d1;

mydate.olddate(y,m,d);

mydate.setdate(y1,m1,d1);

return0;

}

2、该类的数据成员有Name,Age,Height,Weight,成员函数有构造函数People,进食Eating,运动Sporting,显示Show,

其中构造函数用已知参数姓名nm、年龄a、身高h、体重w构造对象,进食函数使体重加1,运动函数使身高加1,显示函数用于显示姓名、年龄、身高、体重。

要求数据成员都是private,成员函数都是public访问权限

#include

#include

usingnamespacestd;

classPeople

{

private:

stringName;

intAge;

intHeight;

intWeight;

public:

People(stringna,intag,intht,intwt);

voidEating(inte)

{Height=Height+e;

;

}

voidSporting(ints)

{

Weight=Weight+s;

}

voidShow();

};

People:

:

People(stringna,intag,intht,intwt)

{

Name=na;

Age=ag;

Height=ht;

Weight=wt;

}

voidPeople:

:

Show()

{

cout<<"姓名"<

cout<<"年龄"<

cout<<"身高"<

cout<<"体重"<

}

voidmain()

{intag,ht,wt,e,s;

stringna;

cin>>na>>ag>>ht>>wt;

cin>>e>>s;

Peopleperson(na,ag,wt,ht);

person.Eating(e);

person.Sporting(s);

person.Show();

}

3、编写一个程序,采用一个类求一个给定的长方形的周长和面积。

classrectangle

{

intlen,wid;

public:

//自行设计

};

主函数首先接收长和宽的值,然后用rectangle类实例化对象,并求周长和面积后输出。

#include

usingnamespacestd;

classrectangle

{

intlen,wid;

public:

rectangle(int,int);

voidsetArea();

voidsetPerimeter();

};

rectangle:

:

rectangle(intl,intw)

{

len=l;

wid=w;

}

voidrectangle:

:

setArea()

{

cout<

}

voidrectangle:

:

setPerimeter()

{

cout<<(len+wid)*2<

}

voidmain()

{

intl,w;

cin>>l>>w;

rectangler1(l,w);

r1.setPerimeter();

r1.setArea();

}

4、设计一个日期(Date)类,其中有数据成员intyear,month,dat;,有静态数据成员数组intdays[12];,用于分别存放一年12个月的天数,初始化为{31,28,31,30,31,30,31,31,30,31,30,31}。

在具体应用时,根据对象的成员year年是否为闰年调整2月份的天数days[1]的值。

请设计一个成员函数计算对象的日期在当年是第几天。

类的成员函数可以设计如下:

Date(intYear=2000,intMonth=1,intDay=1);//构造函数

voidSet(intYear,intMonth,intDay);

intisLeapyear();//判断是否为闰年

intDays();//计算是该年的第几天

voidShow();//显示年月日

并且,在类外编写一个测试函数:

voidtest(inty,intm,intd)

#include

usingnamespacestd;

classDate{

private:

intyear;

intmonth;

intdat;

public:

intn;

voidDays(int,int,int);

voidshow()

{

cout<<"是"<

}

};

voidDate:

:

Days(inty,intm,intd)

{

inta=31,b=28,c=30;

intnum=0;

year=y;

month=m;

dat=d;

n=num;

if((year%4==0&&year%100!

=0)||(year%400==0))

b=29;

switch(month)

{

case1:

n=dat;break;

case2:

n=a+dat;break;

case3:

n=a+b+dat;break;

case4:

n=2*a+b+dat;break;

case5:

n=2*a+b+c+dat;break;

case6:

n=3*a+b+c+dat;break;

case7:

n=3*a+b+2*c+dat;break;

case8:

n=4*a+b+2*c+dat;break;

case9:

n=5*a+b+2*c+dat;break;

case10:

n=5*a+b+3*c+dat;break;

case11:

n=6*a+b+3*c+dat;break;

case12:

n=6*a+b+4*c+dat;break;

}

}

voidmain()

{

inty,m,d;

Datet1,t2,t3,t4,t5;

cin>>y>>m>>d;

t1.Days(y,m,d);

cin>>y>>m>>d;

t2.Days(y,m,d);

cin>>y>>m>>d;

t3.Days(y,m,d);

cin>>y>>m>>d;

t4.Days(y,m,d);

cin>>y>>m>>d;

t5.Days(y,m,d);

t1.show();

t2.show();

t3.show();

t4.show();

t5.show();

}

5、设计Point类描述平面直角坐标系中的点,其中有数据成员doublex,y;,再设计一个该类的友元函数用于计算两个给定的Point类对象之间的距离。

如:

frienddoubledistance(constPoint&p1,constPoint&p2);

主函数如下:

intmain()

{

Pointa,b(1,2);

a.Show();b.Show();

cout<

return0;

}

程序运行结果见后面的描述。

#include

#include

usingnamespacestd;

classPoint;

classPoint{

public:

Point(intx=0,inty=0):

x(x),y(y){}

intgetX(){returnx;}

intgetY(){returny;}

frienddouble_distance(constPoint&p1,constPoint&p2);

voidshow()

{

cout<<"("<

}

private:

intx,y;

};

double_distance(constPoint&p1,constPoint&p2)

{

doublex=p1.x-p2.x;

doubley=p1.y-p2.y;

return(double)sqrt(x*x+y*y);

}

voidmain()

{

Pointp1(0,0),p2(3,4);

p1.show();

p2.show();

cout<<_distance(p1,p2)<

}

6、根据以下主函数的功能来设计计算器类Calculator,使其能正确运行。

类Calculator中应该具有描述运算数的a和b及其表示a和b运算结果的三个数据成员和相应计算并显示结果的成员函数。

intmain()

{

floata,b;

cin>>a>>b;//从键盘输入运算数a、b

Calculatorcal(a,b);//用a和b初始化创建的Calculator类对象cal

cal.add();//计算a+b并显示结果

cal.subtract();

cal.multiply();

cal.divide();

return0;

}

#include

usingnamespacestd;

classCalculator{

private:

floata;

floatb;

public:

Calculator(float,float);

voidadd();

voidsubtract();

voidmultiply();

voiddivide();

};

Calculator:

:

Calculator(floatx,floaty)

{

a=x;

b=y;}

voidCalculator:

:

add()

{

cout<

}

voidCalculator:

:

subtract()

{

cout<

}

voidCalculator:

:

multiply()

{

cout<

}

voidCalculator:

:

divide()

{

cout<

";

cout<<"\n编号:

"<

cout<<"姓名:

"<

cout<<"性别:

"<

cout<<"工资:

"<

}

Employee:

:

ShowStatic()

{

cout<<"人数:

"<

cout<<"总工资:

"<

returntotalWage;

}

intEmployee:

:

count=0;

intEmployee:

:

totalWage=0;

Employee:

:

Employee(intnu,char*nm,char*sx,intwg)

{

num=nu;

wage=wg;

strcpy(name,nm);

strcpy(sex,sx);

++count;

totalWage+=wg;

}

Employee:

:

~Employee(){}

intmain()

{

intn;

cin>>n;

Employee*m=newEmployee[n];

intnu,wg;

charnm[18],sx[3];

for(inti=0;i

{

cin>>nu>>nm>>sx>>wg;

m[i]=Employee(nu,nm,sx,wg);

}

Employee:

:

ShowStatic();

for(i=0;i

m[i].ShowBase();

return0;

delete[]m;

}

9、定义一个时间类Time,能提供和设置由时、分、秒组成的时间。

编写程序,提供类的构造函数用于根据输入初始化时间类Time的对象;提供函数用于输出时间类对象的时间,按照如下格式输出:

hh:

mm:

ssAM/PM

#include

usingnamespacestd;

classTime{

inthour;

intminute;

intsecond;

public:

voidtime(inth,intm,ints)

{

hour=h;

minute=m;

second=s;

}

voidtest()

{

if((hour>24)||(hour<0)||(minute>60)||(minute<0)||(second>60)||(second<0))

cout<<"ERRORTIME"<

elseif(hour<=9&&hour>=0)

{

if(minute<=9&&minute>=0)

{

if(second<=9&&second>=0)

cout<<"0"<

0"<

0"<

else

cout<<"0"<

0"<

"<

}

if(minute>9)

{

if(second<=9&&second>=0)

cout<<"0"<

"<

0"<

else

cout<<"0"<

"<

"<

}

}

elseif(hour<12)

{

if(minute<=9&&minute>=0)

{

if(second<=9&&second>=0)

cout<

0"<

0"<

else

cout<

0"<

"<

}

if(minute>9)

{

if(second<=9&&second>=0)

cout<

"<

0"<

else

cout<

"<

"<

}

}

elseif(hour==12)

{

if(minute<=9&&minute>=0)

{

if(second<=9&&second>=0)

cout<

0"<

0"<

else

cout<

0"<

"<

}

if(minute>9)

{

if(second<=9&&second>=0)

cout<

"<

0"<

else

cout<

"<

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

当前位置:首页 > 高中教育 > 高考

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

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