C++面向对象程序设计上机考试题库_第1页
C++面向对象程序设计上机考试题库_第2页
C++面向对象程序设计上机考试题库_第3页
C++面向对象程序设计上机考试题库_第4页
C++面向对象程序设计上机考试题库_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C++面对对象程序设计上机考试题库

一、第一类题目(20道,每题7分,在word中保留代码并将输出结果窗口保留)

1.定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形态;

可计算盒子体积;可计算盒子的表面积。

#include<iostream>

classBox

{private:

intx,y,z;intv,s;

public:

voidint(intxl=0,intyl=0,intzl=0){x=xl;y=yl;z=zl;}

voidvolue0{v=x*y*z;}

voidarea(){s=2*(x*y+x*z+y*z);}

voidshow()

{cout«/zx=z/<<x«,zy=/z<<y«z,z='/<<z«endl;

cout<<z,s=v=z,<<v<<endl;

)

);

voidmainO

{Boxa;

a.init(2,3,4);

a.volue();

a.area();

a.show();

)

2.有两个长方柱,其长、宽、高分别为:(1)30,20,10;(2)12,10,20。分别求

他们的体积。编一个基于对象的程序,在类中用带参数的构造函数。

#include<iostream>

usingnamespacestd;

classBox

{public:

Box(int,int,int);〃带参数的构造函数

intvolume0;

private:

intlength;

intwidth;

intheight;

);

Box::Box(intlen,inth,intw)

{length=len;

height=h;

width=w;

)

//Box::Box(intlen,intw,int,h):length(len),height(h),width(w){}

intBox::volume()

{return(length*width*height);}

intmain()

(

Boxboxl(30,20,10);

cout<<,,Thevolumeofboxlis/z<<boxl.volume()«endl;

Boxbox2(12,10,20);

cout<<,,Thevolumeofbox2is/z<<box2.volume()<<endl;

return0;

}

3.有两个长方柱,其长、宽、高分别为:(1)12,20,25;(2)10,30,20。分别求

他们的体积。编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无

参数。

^include<iostream>

usingnamespacestd;

classBox

{public:

Box();

Box(intlen,intw,inth):length(len),width(w),height(h){}

intvolume0;

private:

intlength;

intwidth;

intheight;

};

intBox::volume()

{return(length*width*height);

}

intmain()

(

Boxboxl(10,20,25);

cout«z/Thevolumeofboxlis,,<<boxl.volume0<<endl;

Boxbox2(10,30,20);

cout<<z/Thevolumeofbox2isz,<<box2.volume()<<endl;

return0;

4.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小

数。

^include<iostream>

usingnamespacestd;

template<classnumtype>〃声明一个类模板

classCompare

{public:

Compare(numtypea,numtypeb)

{x=a;y=b;}

numtypemax()

{return(x>y)?x:y;}

numtypemin()

{return(x<y)?x:y;}

private:

numtypex,y;

);

intmain()

{Compare<int>cmpl(3,7);

cout<<cmpl.max()<<z/istheMaximumoftwointedernumbers.,z«endl;

cout<<cmpl.min0<<,zistheMinimumoftwointeder

numbers.zz<<endl<<endl;

Compare<float>cmp2(45.78,93.6);

cout<<cmp2.max()<<,zistheMaximumoftwofloatnumbers.z,«endl;

cout<<cmp2.min()<<z,istheMinimumoftwofloatnumbers.,,<<endl«endl;

Compare<char>cmp3('a','A');

cout<<cmp3.max()<<z,istheMaximumoftwocharacters./z<<endl;

cout<<cmp3.min()<<z,istheMinimumoftwocharacters.z\<endl;

return0;

5.建立一个对象数组,内放5个学生的数据(学号、成果),用指针指向数组首元素,

输出第1,3,5个学生的数据。初值自拟。

#include<iostream>

usingnamespacestd;

classStudent

{public:

Student(intn,doubles):num(n),score(s){}

voiddisplay();

private:

intnum;

doublescore;

);

voidStudent::display()

{cout<<num<<,/,,«score<<endl;}

intmain()

{Studentstud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

Student*p=stud;

for(inti=0;i<=2;p=p+2,i++)

p->display();

return0;

)

6.建立一个对象数组,内放5个学生的数据(学号、成果),设立一个函数max,用指

向对象的指针作函数参数,在max函数中找出5个学生中成果最高者,并输出其学号。

初值自拟。

Sinclude<iostream>

usingnamespacestd;

classStudent

{public:

Student(intn,floats):num(n),score(s){}

intnum;

floatscore;

voidmainO

{Studentstud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

voidmax(Student*);

Student*p=&stud[0];

max(p);

)

voidmax(Student*arr)

{floatmax_score=arr[0].score;

intk=0;

for(inti=l;i<5;i++)

if(arr[i].score>max_score){max_score=arr[i].score;k=i;}

cout<<arr[k].num<<,zz,<<max_score<<endl;

7.用new建立一个动态一维数组,并初始化5"10]={1,2,3,4,5,6,7,8,9,10},用指针

输出,最终销毁数组所占空间。

#include<iostream>

#include<string>

usingnamespacestd;

voidmain(){

int*p;

p=newint[10];

for(inti=l;i<=10;i++)

*(p+i-l)=i;

cout〈〈*(p+iT)<<〃〃;

}

cout<<endl;

delete[]p;

return;

)

8.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算

符函数重载为非成员、非友元的一般函数。编写程序,求两个复数之和。初值自拟。

ttinclude<iostream>

usingnamespacestd;

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

doubleget_real();

doubleget_imag();

voiddisplay();

private:

doublereal;

doubleimag;

};

doubleComplex::get_real()

{returnreal;}

doubleComplex::get_imag()

{returnimag;}

voidComplex::display()

{cout<<z,(,,«real«,z,/,<<imag<<,,i)z,<<endl;}

Complexoperator+(Complex&cl,Complex&c2)

(

return

Complex(cl.get_real()+c2・get_real(),cl.get_imag()+c2・get_imag());

)

intmain()

{Complexcl(3,4),c2(5,-10),c3;

c3=cl+c2;

cout〈<〃c3=〃;

c3.display();

return0;

)

9.定义一个复数类Complex,重载运算符“+”,“一”,使之能用于复数的加,减运

算,运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之和,

差。初值自拟。

usingnamespacestd;

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complexoperator+(Complex&c2);

Complexoperator-(Complex&c2);

voiddisplay();

private:

doublereal;

doubleimag;

};

ComplexComplex::operator+(Complex&c2)

{Complexc;

c.real=real+c2.real;

c.imag=imag+c2.imag;

returnc;}

ComplexComplex::operator-(Complex&c2)

{Complexc;

c.real=real-c2.real;

c.imag=imag-c2.imag;

returnc;}

voidComplex::display()

{cout<<〃〃<<imag<X〃i)〃<<endl;}

intmain()

{Complexcl(3,4),c2(5,-10),c3;

c3=cl+c2;

cout<<,,cl+c2=//;

c3.display();

c3=cl-c2;

cout<<,,cl-c2=zz;

c3.display();

return0;

)

10.定义一个复数类Complex,重载运算符“*”,“/",使之能用于复数的乘,除。

运算符重载函数作为Complex类的成员函数。编程序,分别求出两个复数之积和商。初

值自拟。提示:两复数相乘的计算公式为:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i。两复数

相除的计算公式为:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。

^include<iostream>

usingnamespacestd;

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complexoperator*(Complex&c2);

Complexoperator/(Complex&c2);

voiddisplay();

private:

doublereal;

doubleimag;

};

ComplexComplex::operator*(Complex&c2)

{Complexc;

c.real=real*c2.real-imag*c2.imag;

c.imag=imag*c2・real+real*c2.imag;

returnc;}

ComplexComplex::operator/(Complex&c2)

{Complexc;

c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2・imag);

c.imag=:(imag*c2・real-real*c2.imag)/(c2.real*c2・real+c2.imag*c2.imag);

returnc;}

voidComplex::display()

{cout<<,/(,,«real«z,,/,<<imag<<,,i),z<<endl;}

intmain()

{Complexcl(3,4),c2(5,-10),c3;

c3=cl*c2;

cout<〈〃cl*c2二〃;

c3.display();

c3=cl/c2;

cout«,,cl/c2=,/;

c3.display();

return0;

}

11.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加

运算的两个运算量可以都是类对象,也可以其中有一个是整数,依次随意。例如:

cl+c2,i+cl,cl+i均合法(设i为整数,cl,c2为复数)。编程序,分别求两个复数

之和、整数和复数之和。初值自拟。

#include<iostream.h>

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complexoperator+(Complex&c2);

Complexoperator+(int&i);

friendComplexoperator+(int&,Complex&);

voiddisplay();

private:

doublereal;

doubleimag;

);

ComplexComplex::operator+(Complex&c)

{returnComplex(real+c.real,imag+c.imag);}

ComplexComplex::operator+(int&i)

{returnComplex(real+i,imag);)

voidComplex::display()

{cout«,/(,,<<real«,z,//<<imag<<,,i)z,<<endl;}

Complexoperator+(int&i,Complex&c)

{returnComplex(i+c.real,c.imag);}

intmain()

{Complexcl(3,4),c2(5,-10),c3;

inti=5;

c3=cl+c2;

cout<<,,cl+c2=z,;

c3.display();

c3=i+cl;

cout<<,,i+cl=,z;

c3.display();

c3=cl+i;

cout<<z,cl+i=,z;

c3.display0;

return0;

)

12.有两个矩阵a和b,均为2行3歹求两个矩阵之和。重载运算符“+”,使之能用

于矩阵相加。如c=a+b。初值自拟。

#include<iostream.h>

classMatrix

{public:

Matrix();

friendMatrixoperator+(Matrix&,Matrix&);

voidinput();

voiddisplay();

private:

intmat[2][3];

);

Matrix::Matrix()

{for(inti=0;i<2;i++)

for(intj=0;j<3;j++)

mat[i][j]=0;

)

Matrixoperator+(Matrix&a,Matrix&b)

{Matrixc;

for(inti=0;i<2;i++)

for(intj=0;j<3;j++)

{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}

returnc;

)

voidMatrix::input()

{cout<<,zinputvalueofmatrix:"z<<endl;

for(inti=0;i<2;i++)

for(intj=0;j<3;j++)

cin»mat[i]Lj];

)

voidMatrix::display()

{for(inti=0;i<2;i++)

{for(intj=0;j<3;j++)

{cout«mat[i][j]<<z,z,;}

cout«endl;}

)

intmain()

{Matrixa,b,c;

a.input();

b.input();

cout<<endl<<,,Matrixa:,,«endl;

a.display();

cout<<endl<<z,Matrixb:,z«endl;

b.display();

c=a+b;

cout<<endl<<,,Matrixc=Matrixa+Matrixb:〃<<endl;

c.display();

return0;

)

13.将运算符“十”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,

作为Complex类的友元函数。初值自拟。

#include<iostream.h>

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler){real=r;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

friendComplexoperator+(Complex&cl,Complex&c2);

voiddisplay0;

private:

doublereal;

doubleimag;

};

Complexoperator+(Complex&cl,Complex&c2)

(

returnComplex(cl.real+c2.real,cl.imag+c2.imag);

)

voidComplex::display()

{cout<<z,(,,«real«,z,z,«imag<<z,i)zz<<endl;}

intmain()

{Complexc1(3,4),c2(5,-10),c3;

c3=cl+c2;

cout<<,,cl=/z;cl.display();

cout<〈〃c2=〃;c2.display();

cout<<,,cl+c2=/z;c3.display();

return0;

)

14.定义一个字符串类String,用来存放不定长的字符串,重载运算符“==",,用

于两个字符串的等于比较运算。初值自拟。

#include<iostream.h>

ttinclude<string.h>

classString

{public:

String0{p=NULL;}

String(char*str);

friendbooloperator==(String&stringl,String&string2);

voiddisplay();

private:

char*p;

);

String::String(char*str)

{p=str;

}

voidString::display()

{cout<<p;}

booloperator==(String&stringl,String&string2)

{if(strcmp(stringl.p,string2.p)==0)

returntrue;

else

returnfalse;

)

voidcompare(String&stringl,String&string2)

{if(operator==(stringl,string2)==l)

{stringl.display();cout<</z=zz;string2.display0;}

cout<<endl;

)

intmainO

{Stringstringl("Hello"),string2("Hello");

compare(stringl,string2);

return0;

)

15.定义一个字符串类String,用来存放不定长的字符串,重载运算符〃<〃,用于两个字

符串的小于的比较运算。初值自拟。

^include<iostream.h>

#include<string.h>

classString

{public:

String(){p=NULL;}

String(char*str);

friendbooloperator<(String&stringl,String&string2);

voiddisplay();

private:

char*p;

};

String::String(char*str)

{p=str;

)

voidString::display()

{cout«p;}

booloperator<(String&stringl,String&string2)

{if(strcmp(stringl.p,string2.p)<0)

returntrue;

else

returnfalse;

voidcompare(String&stringl,String&string2)

{if(operator<(stringl,string2)==1)

{stringl.display();cout«z/<z,;string2.display();}

cout<<endl;

)

intmain()

{Stringstringl(/,Book,/),string2("Computer");

compare(stringl,string2);

return0;

)

16.定义一个字符串类String,用来存放不定长的字符串,重载运算符〃>〃,用于两个

字符串的大于的比较运算。初值自拟。

ttinclude<iostream.h>

^include<string.h>

classString

{public:

String(){p=NULL;}

String(char*str);

friendbooloperator>(String&stringl,String&string2);

voiddisplay();

private:

char*p;

);

String::String(char*str)

{p=str;

}

voidString::display()

{cout«p;}

booloperator>(String&stringl,String&string2)

{if(strcmp(stringl.p,string2.p)>0)

returntrue;

else

returnfalse;

)

voidcompare(Stringfcstringl,String&string2)

{if(operator>(stringl,string2)==l)

{stringl.display();cout«/z>/z;string2.display();}

cout<<endl;

)

intmain()

{Stringstringl("Hello"),string2(〃Book〃);

compare(stringl,string2);

return0;}

17.定义一个描述学生基本状况的类,数据成员包括姓名、学号、C++成果、英语和数学

成果,成员函数包括输出数据,求出总成果和平均成果。数据自拟。

#includez,string.h〃

Sinclude<iostream.h>

classCStuScore

{public:

charstrName[12];

charstrStuN0[9];

voidSetScore(charsname[12],charN0[9],floatsO,float

si,floats2)

(

strcpy(strName,sname);

strcpy(strStuNO,NO);

fScore[0]=sO;

fScore[l]=si;

fScore[2]=s2;}

voidprint()

{cout«

cout<〈〃姓名:,z<<strName;

cout<X〃学号:z,<<strStuN0;

cout«z/C++成果:〃《fScore[0"<〃英语成果:〃<<fScore[l]<<〃数学成果:

z/<<fScore[2]<<endl;}

floatGetSUMO

{return(float)((fScore[0]+fScore[l]+fScore[2]));}

floatGetAverage();

private:

floatfScore[3];

};

floatCStuScore::GetAverage()

{return(float)((fScore[0]+fScore[l]+fScore[2])/3.0);}

voidmain()

{CStuScoreone;

floata,b,c;

charName[12];

charStuN0[9];

cout<<〃姓名:";

cin>>Name;

cout<<〃学号:〃;

cin>>StuNO;

cout«z/成果1:«成果2:〃<<〃成果3:〃<<〃\n〃;

cin>>a»b>>c;

one.SetScore(Name,StuNO,a,b,c);

one.print();

cout<X〃平均成果为〃<<one.GetAverage()<<〃\n〃;

cout<〈〃总成果〃<<one.GetSUMO«〃\n〃;}

18.先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出

一个Circle(圆)类,增加数据成员r(半径),再以Circle类为干脆基类,派生出一

个Cylinder(圆柱体)类,在增加数据成员h(高)。编写程序,重载运算符和

“>>”,使之能够用于输出以上类对象。

ttinclude<iostream.h>

classPoint

{public:

Point(float=0,float=0);

voidsetPoint(float,float);

floatgetX()const{returnx;}

floatgetY()const{returny;}

friendostream&operator<<(ostream&,constPoint&);

protected:

floatx,y;

);

Point::Point(floata,floatb)

{x=a;y=b;}

voidPoint::setPoint(floata,floatb)

{x=a;y=b;}

ostream&operator<<(ostream&output,constPoint&p)

{output<<,?[,z«p,x<〈〃,〃<<p.y<<〃]"<<endl;

returnoutput;

)

classCircle:publicPoint

{public:

Circle(floatx=0,floaty=0,floatr=0);

voidsetRadius(float);

floatgetRadius()const;

floatarea()const;

friendostream&operator«(ostream&,constCircle&);

protected:

floatradius;

);

Circle::Circle(floata,floatb,floatr):Point(a,b),radius(r){}

voidCircle::setRadius(floatr)

{radius=r;}

floatCircle::getRadius()const{returnradius;}

floatCircle::area()const

{return3.14159*radius*radius;}

ostream&operator<<(ostream&output,constCircle&c)

{output<<,zCenter=x«,z,/z<<c.y«z,],r=,,<<c.radius<<z,,

area=,,<<c.area()<<endl;

returnoutput;

)

classCylinder:publicCircle

{public:

Cylinder(floatx=0,floaty=0,floatr=0,floath=0);

voidsetHeight(float);

floatgetHeight0const;

floatarea()const;

floatvolume()const;

friendostream&operator«(ostream&,constCylinderfc);

protected:

floatheight;

);

Cylinder::Cylinder(floata,floatb,floatr,floath)

:Circle(a,b,r),height(h){}

voidCylinder::setHeight(floath){height=h;}

floatCylinder::getHeight0const{returnheight;}

floatCylinder::area()const

{return2*Circle::area()+2*3.14159*radius*height;}

floatCylinder::volume()const

{returnCircle::area()*height;}

ostream&operator<<(ostream&output,constCylinderfecy)

{output<<,zCenter=[/z<<cy.x<<〃,〃<<cy・y«zz],r=z\<cy.radius«,z,

h=/,<<cy.height

<</,\narea=,,«cy.area()<<,z,volume=,,«cy.volume()<<endl;

returnoutput;

intmain()

{Cylindercyl(3.5,6.4,5.2,10);

cout<<,,\noriginalcylinder:\nx=zz<<cyl.getX()<<,z,y=z/<<cyl.getY()<<〃,r=〃

<<cyl.getRadius()<<〃,h=/,«cyl.getHeight()<<,,\narea=/,«cyl.area()

<<z,,volume=〃<<cyl.volume()«endl;

cyl.setHeight(15);

cyl.setRadius(7.5);

cyl.setPoint(5,5);

cout<<,,\nnewcylinder:\n,z<<cyl;

Point&pRef=cyl;

cout<<z,\npRefasapoint:/z<<pRef;

Circle&cRef=cyl;

cout<<,,\ncRefasaCircle:,,«cRef;

return0;

19.写一个程序,定义抽象类型Shape,由他派生三个类:Circle(圆形),Rectangle

(矩形),Trapezoid(梯形),用一个函数printArea分别输出三者的面积,3个图形

的数据在定义对象是给定。

ttinclude<iostream>

usingnamespacestd;

classShape

{public:

virtualdoublearea()const=0;

);

classCircle:publicShape

{public:

Circle(doubler):radius(r){}

virtualdoublearea()const{return3.14159*radius*radius;};

protected:

doubleradius;

);

classRectangle:publicShape

{public:

Rectangle(doublew,doubleh):width(w),height(h){}

virtualdoublearea()const{returnwidth*height;}

protected:

doublewidth,height;

);

classTrapezoid:publicShape

{public:

Trapezoid(doublew,doubleh,double

len):width(w),height(h),length(len){}

virtualdoublearea()const{return0.5*height*(width+length);}

protected:

doublewidth,height,length;

);

voidprintArea(constShape&s)

{cout<<s.area()<<endl;}

intmain()

(

Circlecircle(12.6);

cout<<,,areaofcircle二〃;

printArea(circle);

Rectanglerectangle(4.5,8.4);

cout<<z'areaofrectangle=〃;

printArea(rectangle);

Trapezoidtrapezoid(4.5,8.4,8.0);

cout<<,,areaoftrapezoid二〃;

printArea(trapezoid);

return0;

)

20.定义一个人员类Cperson,包括数据成员:姓名、编号、性别和用于输入输出的成

员函数。在此基础上派生出学生类CStudent(增加成果)和老师类Cteacher(增加教龄),

并实现对学生和老师信息的输入输出。

#include<iostream.h>

ttinclude<string.h>

classCPerson

(

public:

voidSetData(char*name,char*id,boolisman=1)

{intn=strlen(name);

strncpy(pName,name,n);pName[n]='\0';

n=strlen(id);

strncpy(pID,id,n);

pID[n]='\0';

bMan=isman;

)

voidOutput()

cout<<〃姓名:,,«pName«endl;

cout<〈〃编号:z/<<pID«endl;

char*str=bMan?〃男〃:〃女〃;

cout<X〃性别:z,<<str«endl;

)

private:

charpName[20];

charpID[20];

boolbMan;

);

classCStudent:publicCPerson

(

public:

voidInputScore(doublescorel,doublescore2,doublescore3)

{

dbScore[0]=scorel;

dbScore[l]=score2;

dbScore[2]=score3;

)

voidPrint()

{

Output();

for(inti=0;i<3;i++)

cout<<〃成果〃<〃:〃<XdbScore[i]〈〈endl;

}

private:

doubledbScore[3];

);

classCteacher:publicCPerson

(

public:

voidInputage(doubleage)

(

tage=age;

)

voidPrint()

(

Output();

cout<<〃教龄:"z<<tage<<endl;

)

private:

doubletage;

};

voidmain()

(

CStudentstu;

Cteachertea;

stu.SetData(,,LiMingz,,“21010211");

stu.InputScore(80,76,91);

stu.Print();

tea.SetData(〃zhangli〃,〃001〃);

tea.Inputage(12);

tea.Print();

)

二、其次类题目(20道,每题9分,请自行设计输出格式)

1.某商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱

的重量不一样,因此,商店须要登记目前库存货物的总量,要求把商店货物购进和卖出

的状况模拟出来。

#include<iostream>

usingnamespacestd;

classGoods

{public:

Goods(intw){weight=w;totalWeight+=w;};

~Goods(){totalWeight-=weight;};

intWeight(){returnweight;};

staticintTotalWeight(){returntotalWeight;}

private:intweight;

staticinttotalWeight;

);

intGoods::totalWeight=0;

main()

{intw;

cin»w;Goods*gl=newGoods(w);

cin»w;Goods*g2=newGoods(w);

cout<<Goods::TotalWeight()<<endl;

deleteg2;

cout<<Goods::TotalWeight()<<endl;

2.设计一个Time类,包括三个私有数据成员:hour,minute,sec,用构造函数初始化,内

设公用函数display(Date&d),设计一个Date类,包括三个私有数据成员:

month,day,year,也用构适函数初始化;分别定义两个带参数的对象

tl(12,30,55),dl(3,25,2023),通过友员成员函数的应用,输出dl和tl的值。

#include<iostream>

usingnamespacestd;

classDate;

classTime

{public:

Time(int,int,int);

voiddisplay(constDate&);

private:

inthour;

intminute;

intsec;

);

Time::Time(inth,intm,ints)

{hour=h;

minute=m;

sec=s;

)

classDate

{public:

Date(int,int,int);

friendvoidTime::display(constDate&);

private:

intmonth;

intday;

intyear;

);

Date::Date(intm,intd,inty)

{month=m;

day=d;

year=y;

)

voidTime::display(constDate&da)

{cout<<da.month«z,//z<<da.day<<z,/,z«da.year<<endl;

cout«hour<<zz:,z<<minute<<z,:,,«sec<<endl;

intmain()

{Timetl(12,30,55);

Datedl(3,25,2023);

tl.display(dl);

return0;

3.设计一个Time类,包括三个私有数据成员:hour,minute,sec,用构造函数初始化,,

设封一一个Date类,包括三个私有数据成员:month,day,year,也用构适函数初始化;设

计一个一般函数display(…),将display分别设置为Time类和Date类的友元函数,

在主函数中分别定义两个带参数的对象11(12,30,55),dl(3,25,2023),

调用desplay,输出年、月、日和时、分、秒。

Sinclude<iostream>

usingnamespacestd;

classDate;

classTime

{public:

Time(int,int,int);

friendvoiddisplay(constDate&,constTime&);

private:

inthour;

intminute;

intsec;

);

Time::Time(inth,intm,ints)

{hour=h;

minute=m;

sec=s;

}

classDate

{public:

Date(int,int,int);

friendvoiddisplay(constDate&,constTime&);

private:

intmonth;

intday;

intyear;

);

Date::Date(intm,intd,inty)

{month=m;

day=d;

year=y;

}

voiddisplay(constDate&d,constTime&t)

(

cout<<d.month<<,,/,,<<d.day<</,/,,<<d.year<<endl;

cout«t.hour<<z,:zz<<t.minute<<〃:〃<<t・sec<<endl;

intmain()

Timetl(10,30,55);

Datedl(3,25,2023);

display(dl,tl);

return0;

)

4.可以定义点类(Point),再定义一个类(Distance)描述两点之间的距离,其数据成

员为两个点类对象,两点之间距离的计算可设计由构造函数来实现。

#include<iostream>

#include<math.h>

usingnamespacestd;

classPoint

{public:

Point(doublexi,doubleyi){X=xi;Y=yi;}

doubleGetX(){returnX;}

doubleGetY(){returnY;}

private:doubleX,Y;

);

classDistance

(

public:

Distance(Pointp,Pointq);

doubleGetdist(){returndist;}

private:

Pointa,b;

doubledist;

};

Distance::Distance(Pointql,Pointq2):a(ql),b(q2)

{doubledx=double(a.GetX()-b.GetX());

doubledy=double(a.GetY()-b.GetY());

dist=sqrt(dx*dx+dy*dy);

)

intmain()

{Pointpl(3.0,5.0),p2(4.0,6.0);

Distancedis(pl,p2);

cout<<"Thisdistanceis:〃<<dis.Getdist()<<endl;

return0;}

5.定义点类(Point),

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论