C++笔试.docx

上传人:b****3 文档编号:27351054 上传时间:2023-06-29 格式:DOCX 页数:20 大小:19.49KB
下载 相关 举报
C++笔试.docx_第1页
第1页 / 共20页
C++笔试.docx_第2页
第2页 / 共20页
C++笔试.docx_第3页
第3页 / 共20页
C++笔试.docx_第4页
第4页 / 共20页
C++笔试.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

C++笔试.docx

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

C++笔试.docx

C++笔试

#include

#include

classPerson{

private:

charName[20];

charSex;

intAge;

public:

voidRegister(char*name,intage,charsex);

voidShowMe();

};

voidPerson:

:

Register(char*name,intage,charsex)

{

strcpy(Name,name);

Age=age;

Sex=(sex=='m'?

'm':

'f');

}

voidPerson:

:

ShowMe()

{

cout<

}

voidmain()

{

charname[20];

charsex;

intage;

Personperson1,person2;

cout<<"Enteraperson'sname,ageandesex:

";

cin>>name>>age>>sex;

person1.Register(name,age,sex);

cout<<"person1:

\t:

";

person1.ShowMe();

person1.Register("Zhang3",19,'m');

cout<<"person1:

\t";

person1.ShowMe();

person2=person1;

cout<<"person2:

\t";

person2.ShowMe();

}

第一题当输入“Wang518f”之后的运行结果:

答案:

Person1:

wang518f

Person2:

zhang319m

Person2:

zhang319m

 

第二题读代码

#include

classDate{

private:

intday;intmonth;intyear;

public:

Date();

Date(int,int,int);

voidinit(int,int,int);

voidprint_ymd();

voidprint_mdy();

};

Date:

:

Date()

{

year=1900;month=1;day=1;

}

Date:

:

Date(intyy,intmm,intdd)

{

init(yy,mm,dd);

}

voidDate:

:

init(intyy,intmm,intdd)

{

month=(mm>=1&&mm<=12)?

mm:

1;

year=(yy>=1900&&yy<=2100)?

yy:

1900;

day=(dd>=1&&dd<=31)?

dd:

1;

}

voidDate:

:

print_ymd()

{

cout<

}

voidDate:

:

print_mdy()

{

cout<

}

voidmain()

{

Datedate1,date2(2013,13,13);

date1.print_ymd();

date2.print_ymd();

date1.init(2013,12,13);

date1.print_ymd();

date2.init(2012,12,33);

date2.print_ymd();

}

答案:

1900-1-1

2013-1-13

2013-12-31

2013-12-1

第三题读代码

#include

#include

classPerson{

private:

charName[20];

charSex;

intAge;

public:

voidRegister(char*name,intage,charsex)

{

strcpy(Name,name);Age=age;

Sex=(sex=='m'?

'm':

'f');

}

voidShowMe()

{

cout<

}

};

classStudent:

publicPerson{

private:

intNumber;

charClassName[10];

public:

voidRegisterStu(char*classname,intnumber,char*name,intage,charsex)

{

strcpy(ClassName,classname);

Number=number;

Register(name,age,sex);

}

voidShowStu()

{

cout<

ShowMe();

}

};

voidmain()

{

Studentstu;

stu.RegisterStu("计算机51",85071011,"张弓长",18,'m');

stu.ShowStu();stu.ShowMe();

}

答案:

85071011计算机51张弓长18m

张弓长18m

 

第四题读代码

#include

#include

classPet

{

public:

virtualvoidSpeak()

{

cout<<"Howdoesapetspeak?

"<

}

};

classCat:

publicPet

{

public:

virtualvoidSpeak()

{

cout<<"miao!

miao!

"<

}

};

classDog:

publicPet

{

public:

virtualvoidSpeak()

{

cout<<"wang!

wang!

"<

}

};

voidmain()

{

Petobj,*p1;

Dogdog1;

Catcat1;

obj=dog1;obj.Speak();

p1=&cat1;p1->Speak();

p1=&dog1;p1->Speak();

Pet&p4=cat1;p4.Speak();

}

答案:

Howdoesapetspeak?

Miao!

miao!

Wang!

wang!

Miao!

miao!

多态性:

基类的指针指向子类对象;子类的对象给基类的引用赋值

 

第五题读代码

#include

voidtestfun(intStudentAge)

{

try

{

if(StudentAge<0)

throw"输入的学生年龄必须是正整数!

";

if(StudentAge>20)

throwStudentAge;

cout<<"学生年龄是:

"<

}

catch(inti)

{

cout<<"发生异常:

学生年龄是"<

"<

}

catch(constchar*Message)

{

cout<<"发生异常:

"<

}

}

voidmain()

{

testfun(12);

testfun(-9);

testfun(99);

}

答案:

学生年龄是:

12

发生异常:

输入的学生年龄必修是正整数!

发生异常:

学生年龄是99岁,太大了!

 

第六题填空题

#include

#include

usingnamespacestd;

intmain(intargc,char*argv[])

{

charch;

if(argc!

=2)

{

cout<<"Usage:

ProgramName\n";

return1;

}

ifstreamin(argv[1],ios:

:

in|ios:

:

binary);

if(

(1))

{

cout<<"Cannotopenthefile.";

return1;

}

while(

(2))

{//inwillbefalsewheneofisreached

in.get(ch);

if(in)cout<<(3);

}

return0;

}

答案:

(1)!

in

(2)!

in.eof()(或in)(3)ch

第七题填空题

#include

usingnamespacestd;

template<

(1)>

TPower(Ta,intexp)

{

while(--exp>0)

ans*=a;

return

(2);

}

以下为参考:

intmain()

{

cout<<"3^5="<

cout<<"1.1^2="<

return0;

}

答案:

(1)TypenameT(或ClassT)

(2)Tans=a;

第八题读代码

#include

#include

usingnamespacestd;

classPerson

{charName[10];//姓名

intAge;//年龄

public:

Person(char*name,intage)

{

strcpy(Name,name);

Age=age;

cout<<"constructorofperson"<

}

~Person()

{cout<<"deconstrutorofperson"<

};

classStudent:

publicPerson

{

charClassName[10];//班级

PersonMonitor;//班长

public:

Student(char*name,intage,char*classname,char*name1,intage1)

:

Person(name,age),Monitor(name1,age1)

{

strcpy(ClassName,classname);

cout<<"constructorofStudent"<

}

~Student()

{cout<<"deconstrucorofStudent"<

};

intmain()

{

Studentstu("XiaoLi",18,"计算机51","XiaoWang",20);

return0;

}

答案:

ConstructorofpersonXiaoLi

ConstructorofpersonXiaoWang

Constructorofperson

deconstructorofperson

deconstructorofpersonXiaoWang

deconstructorofpersonXiaoLi

父亲-朋友-自己(构造)

自己-朋友-父亲(析构)

第九题读代码

#include

#include

usingnamespacestd;

classPerson

{protected:

charName[10];

charSex;

intAge;

public:

voidRegister(char*name,intage,charsex)

{strcpy(Name,name);

Sex=(sex=='m'?

'm':

'f');

Age=age;

}

voidShowMe()

{cout<<"姓名:

"<

cout<<"性别:

"<<(Sex=='m'?

"男":

"女")<

cout<<"年龄:

"<

}

};

classTeacher:

publicPerson

{charDept[20];

intSalary;

public:

Teacher(char*name,intage,charsex,char*dept,intsalary);

voidShowMe()

{Person:

:

ShowMe();

cout<<"工作单位:

"<

cout<<"月薪:

"<

}

};

Teacher:

:

Teacher(char*name,intage,charsex,char*dept,intsalary)

{Register(name,age,sex);

strcpy(Dept,dept);

Salary=salary;

}

classStudent:

publicPerson

{charID[12];

charClass[12];

public:

Student(char*name,intage,charsex,char*id,char*classid);

voidShowMe(){

cout<<"学号:

"<

Person:

:

ShowMe();

cout<<"班级:

"<

};

Student:

:

Student(char*name,intage,charsex,char*id,char*classid)

{Register(name,age,sex);

strcpy(ID,id);

strcpy(Class,classid);

}

intmain()

{Teacheremp1("章立早",38,'m',"电信学院",2300);

Studentstd1("李木子",22,'f',"02035003","能动01");

emp1.ShowMe();

std1.ShowMe();

return0;

}

答案:

姓名:

章立早

性别:

年龄:

38

工作单位:

电信学院

月薪:

2300

学号:

02035003

姓名:

李木子

性别:

年龄:

22

班级:

能动01

第十题填空题

#include

usingnamespacestd;

template

Tsum(Ta[],intn)

{

inti;

(1);

for(i=0;i<=n;i++)

s=s+a[i];

(2);

}

答案:

(1)Ts=0

(2)returns;

 

第十一题填空题

下面程序的功能是输入两个数作除法,当输入的数位数是0时作异常处理

#include

usingnamespacestd;

doubleDiv(doublea,doubleb);

intmain()

{doublen1,n2,result;

cout<<"Inputtwonumbers:

"<

cin>>n1>>n2;

(1);

{result=Div(n1,n2);

cout<

attemptedtodivideby"<

"<

return0;

}

doubleDiv(doublea,doubleb)

{

if(b==0.0)

(2);

return(3);

}

答案:

(1)Try

(2)throwb(3)a/b

第十二题读代码

如果将下面的程序编译成可执行文件,当前目录下有一个test.txt文本文件,里面的内容如下两行:

Hello

Whatyouseemaybewrong

那么执行结果是?

#include

#include

usingnamespacestd;

intmain()

{

ifstreamin("test.txt");

charch;

if(!

in){

cout<<"Cannotopengradefile.\n";

return1;

}

while(in)

{

in.get(ch);

if(in)

cout<

}

return0;}

(1)用面向对象的方法编写程序求圆柱体的周长和面积

#include

usingnamespacestd;

classyzt

{

private:

doubler;

doubleh;

public:

yzt(doubledr,doubledh)

{

r=dr;

h=dh;

}

doublearea()

{

return2*3.1416*r*(h+r);

}

doublevolume()

{

return3.1416*r*r*h;

}

voidprint()

{

cout<<"周长为:

"<

cout<<"面积为:

"<

}

};

voidmain()

{

yztt(3,3);

t.print();

}

(2)用面向对象的方法编写程序求长方形的周长和面积

#include

usingnamespacestd;

classcft

{

private:

doublel;

doublew;

public:

cft(doubledl,doubledw)

{

l=dl;

w=dw;

}

doublezc()

{

return2*l+2*w;

}

doublemj()

{

returnl*w;

}

voidprint()

{

cout<<"周长为:

"<

cout<<"面积为:

"<

}

};

voidmain()

{

cftt(3,4);

t.print();

}

(3)运算符重载,友元函数重载,实部虚部计算

#include

usingnamespacestd;

classcomplex

{

private:

doublereal;

doubleimag;

public:

complex(doubler=0,doublei=0);

friendcomplexoperator+(constcomplexc1,constcomplexc2);

voidprint();

};

complex:

:

complex(doubler,doublei)

{

real=r;

imag=i;

}

complexoperator+(constcomplexc1,constcomplexc2)

{

complextemp;

temp.real=c1.real+c2.real;

temp.imag=c1.imag+c2.imag;

returntemp;

}

voidcomplex:

:

print()

{

cout<<"("<

}

voidmain()

{

complexc1(2.5,3.7),c2(4.2,6.5);

complexc;

c=c1+c2;

c.print();

}

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

当前位置:首页 > 表格模板

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

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