习题.docx

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

习题.docx

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

习题.docx

习题

类和对象

1.下述程序编译有错,修改后使其能够通过编译(P84)

#include

classStedent

{charname[10];

intage;

floataver;

voidprintStu();

};

voidmain()

{Sudentp1,p2,p3;

p1.age=30;

}

2.修改下述程序,使其运行结果为:

(P84)

StudentNo.is:

20021,ageis:

20.

#include

classStudent

{intano;

intage;

floataver;

voidprintStu();

voidsetSno(intd);

voidsetAge(inta)

};

voidprintStu()

{

cout<<”\nStudentNo.is“<

cout<<”ageis“<

}

voidsetSno(ints)

{sno=s;

}

voidsetAge(inta)

{age=a;

}

voidmain()

{Studentlin;

lin.setSno(20021);

lin.setAge(20);

lin.printStu();

}

正确的:

#include

classStudent

{intsno;

intage;

floataver;

public:

voidprintStu();

voidsetSno(intd);

voidsetAge(inta);

};

voidStudent:

:

printStu()

{cout<<"\nStudentNo.is:

"<

cout<<"ageis:

"<

}

voidStudent:

:

setSno(ints)

{sno=s;

}

voidStudent:

:

setAge(inta)

{age=a;

}

voidmain()

{Studentlin;

lin.setSno(20021);

lin.setAge(20);

lin.printStu();

}

运行结果为:

StudentNo.is:

20021,ageis:

20.

3.下述程序编译有错,修改后使其能够通过编译(P84)

#include

classPoint

{public:

intx,y;

Point(){x=1;y=2;}

};

voidmain()

{Pointcpoint,aaa;

cpoint.x=2;

}

4.下述程序编译有错,修改后使其能够通过编译(P85)

#include

classStack

{public:

Stack(intsz);

public:

char*p;

intsize;

};

Stack:

:

Stack(intsz)

{p=newchar[size=sz];

}

voidmain()

{

Stackstt;

}

5.下述程序编译有错,修改后使其运行结果为:

(P134)

0Studentexist

pleaseinputstudentNo.33

pleaseinputstudentNo.88

2Studentexist,y=33

2Studentexist,y=88

#include

#include

classStudent

{private:

staticintx;

intSno;

public:

Student();

{++x;

cout<<”\npleaseinputstudentNo.”;

cin>>Sno;

}

staticintget_x()

{returnx;}

intget_Sno()

{returnSno;}

};

intStudent:

:

x=0;

voidmain()

{cout<

:

get_x()<<”Studentexist\n”;

Studentstu1;

Student*pstu=newStudent;

cout<

:

get_x()<<”Studentexist,y=”<

cout<

:

get_x()<<”Studentexist,y=”<

}

#include

#include

classStudent

{private:

staticintx;

intSno;

public:

Student()

{++x;

cout<<"\npleaseinputstudentNo.";

cin>>Sno;

}

staticintget_x()

{returnx;

}

intget_Sno()

{returnSno;

}

};

intStudent:

:

x=0;

voidmain()

{cout<

:

get_x()<<"Studentexist\n";

Studentstu1;

Student*pstu=newStudent;

cout<

:

get_x()<<"Studentexist,y="<

cout<

:

get_x()<<"Studentexist,y="<<(*pstu).get_Sno()<<"\n";

}

6.写出下述程序的运行结果(P137)

#include

classexample

{private:

inti;

public:

example(intn)

{i=n;

cout<<"Constructing\n";

}

~example()

{cout<<"Destructing\n";

}

intget_i()

{returni;}

};

intsqr_it(exampleo)

{cout<<"sqr_it"<

returno.get_i()*o.get_i();

}

main()

{examplex(10);

cout<

cout<

return0;

}

运行结果为:

Constructing

10

sqr_it

Destructing

100

Destructing

继承与派生类

7.不使用虚基类时:

写出下述程序的运行结果(P167)

#include

classbase

{protected:

inta;

public:

base()

{a=5;cout<<"basea="<

};

classbase1:

publicbase

{public:

base1()

{a=a+10;cout<<"base1a="<

};

classbase2:

publicbase

{public:

base2()

{a=a+20;cout<<"base2a="<

};

classderived:

publicbase1,publicbase2

{public:

derived()

{cout<<"base1:

:

a="<

:

a<

cout<<"base2:

:

a="<

:

a<

}

};

main()

{derivedobj;

return0;

}

运行结果为:

 

8.虚基类的使用:

写出下述程序的运行结果(P169)

#include

classbase

{protected:

inta;

public:

base()

{a=5;cout<<"basea="<

};

classbase1:

virtualpublicbase

{public:

base1()

{a=a+10;cout<<"base1a="<

};

classbase2:

virtualpublicbase

{public:

base2()

{a=a+20;cout<<"base2a="<

};

classderived:

publicbase1,publicbase2

{public:

derived()

{cout<<"deriveda="<

}

};

main()

{derivedobj;

return0;

}

运行结果为:

 

9.含有虚基类的派生类构造函数的执行顺序:

写出下述程序的运行结果(P170)

#include

classbase

{protected:

inta;

public:

base(intsa)

{a=sa;cout<<"Constructingbase"<

};

classbase1:

virtualpublicbase

{protected:

intb;

public:

base1(intsa,intsb):

base(sa)

{b=sb;cout<<"Constructingbase1"<

};

classbase2:

virtualpublicbase

{protected:

intc;

public:

base2(intsa,intsc):

base(sa)

{c=sc;cout<<"Constructingbase2"<

};

classderived:

publicbase1,publicbase2

{protected:

intd;

public:

derived(intsa,intsb,intsc,intsd):

base(sa),base1(sa,sb),base2(sa,sc)

{d=sd;cout<<"Constructingderived"<

}

};

main()

{derivedobj(2,4,6,8);

return0;

}

运行结果为:

 

10.写出下述程序的运行结果(P183)

#include

#include

classPerson

{protected:

char*m_name;

intm_age;

public:

Person(char*name,intage)

{m_name=newchar[strlen(name)+1];

strcpy(m_name,name);

m_age=age;

cout<<"PersonName="<

cout<<"Personm_age="<

}

~Person()

{delete[]m_name;}

};

intmain()

{Personobj("Angel",20);

return0;

}

运行结果为:

PersonName=Angel

Personm_age=20

又:

#include

#include

classPerson

{protected:

char*m_name;

intm_age;

public:

Person(char*name,intage)

{m_name=newchar[strlen(name)+1];

strcpy(m_name,name);

m_age=age;

cout<<"PersonName="<

cout<<"Personm_age="<

}

~Person()

{delete[]m_name;}

};

classStudent:

publicvirtualPerson

{protected:

longintm_classnu;

public:

Student(char*name,intage,longintclassnu):

Person(name,age)

{m_classnu=classnu;

cout<<"StudentName="<

cout<<"Studentm_age="<

cout<<"Studentclassnu="<

}

~Student(){}

};

intmain()

{Studentobj("Angel",20,20000103);

return0;

}

运行结果为:

PersonName=Angel

Personm_age=20

StudentName=Angel

Studentm_age=20

Studentclassnu=20000103

11.写出下述程序的运行结果(P183)

#include

#include

classPerson

{protected:

char*m_name;

intm_age;

public:

Person(char*name,intage)

{m_name=newchar[strlen(name)+1];

strcpy(m_name,name);

m_age=age;

cout<<"PersonName="<

}

~Person()

{deletem_name;}

};

classStudent:

publicvirtualPerson

{protected:

longintm_classnu;

public:

Student(char*name,intage,longintclassnu):

Person(name,age)

{m_classnu=classnu;

cout<<"StudentName="<

}

~Student(){}

};

classEmployee:

publicvirtualPerson

{protected:

floatm_wage;

public:

Employee(char*name,intage,floatwage):

Person(name,age)

{m_wage=wage;

cout<<"EmployeeName="<

}

~Employee(){}

};

classSideLine:

publicStudent,publicEmployee

{public:

SideLine(char*name,intage,longintclassnu,Floatwage):

Person(name,age)

Student(name,age,classnu),Employee(name,age,wage)

{cout<<"SideLineName="<

~SideLine(){}

};

intmain()

{SideLineobj("Angel",20,20000103,3000);

return0;

}

此程序编译有错!

Compiling...

ccc.cpp

e:

\my-1\jiaoxue\c++\c++\c++讲义\lt\ccc\ccc.cpp(39):

errorC2629:

unexpected'classSideLine('

e:

\my-1\jiaoxue\c++\c++\c++讲义\lt\ccc\ccc.cpp(39):

errorC2334:

unexpectedtoken(s)preceding':

';skippingapparentfunctionbody

e:

\my-1\jiaoxue\c++\c++\c++讲义\lt\ccc\ccc.cpp(45):

errorC2660:

'SideLine:

:

SideLine':

functiondoesnottake4parameters

Errorexecutingcl.exe.

ccc.obj-3error(s),0warning(s)

 

修改如下:

#include

#include

classPerson

{protected:

char*m_name;

intm_age;

public:

Person(char*name,intage)

{m_name=newchar[strlen(name)+1];

strcpy(m_name,name);

m_age=age;

cout<<"PersonName="<

}

~Person()

{delete[]m_name;}

};

classStudent:

publicvirtualPerson

{protected:

longintm_classnu;

public:

Student(char*name,intage,longintclassnu):

Person(name,age)

{m_classnu=classnu;

cout<<"StudentName="<

}

~Student(){}

};

classEmployee:

publicvirtualPerson

{protected:

floatm_wage;

public:

Employee(char*name,intage,floatwage):

Person(name,age)

{m_wage=wage;

cout<<"EmployeeName="<

}

~Employee(){}

};

classSideLine:

publicStudent,publicEmployee

{public:

SideLine(char*name,intage,longintclassnu,floatwage):

\

Person(name,age),Student(name,age,classnu),Employee(name,age,wage)

{cout<<"SideLineName="<

~SideLine(){}

};

intmain()

{SideLineobj("Angel",20,20000103,3000);

return0;

}

运行结果为:

PersonName=Angel

StudentName=Angel

EmployeeName=Angel

SideLineName=Angel

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

当前位置:首页 > 解决方案 > 学习计划

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

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