




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【编程题】 选做1、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。答案:#include#include#includeclass base /定义一个基类protected:char name20; /姓名char sex3; /性别int age; /年龄 ;class teacher:public base /基类派生出教师类 int sno; /工号char zc20; /职称double wages; /工资 ;class student :public base /基类派生出学生类 int sno; /学号char bj10; /班级char zy10; /专业double score; /入学成绩 ;2、声明一个哺乳动物Mammal类,再由此派生出狗Dog类,声明一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。答案: #includeclass Mammal public:Mammal() coutcall Mammalendl; Mammal() coutDelete base classendl; ;class Dog :public Mammal public:Dog() coutcall Dogn; Dog() coutDelete Dog classn;void main() Dog b; 3、声明一个Shape基类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。答案:#include#define PI 3.1415926class shapeprotected:double s;public:void show()couts= sendl;class Rectangle:public shape double x,y;public:Rectangle(double x1=0,double y1=0) x=x1;y=y1;void GetArea() s=x*y;class Circle:public shape double r;public:Circle(double r1=0) r=r1;void GetArea() s=r*r*PI; ;class Square :public Rectangle double a;public:Square(double a1=0 ) a=a1;void GetArea() s=a*a; ;【编程解答题】4、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。答案:#include#include#includeclass base /定义一个基类protected:char name20; /姓名char sex3; /性别int age; /年龄 ;class teacher:public base /基类派生出教师类 int sno; /工号char zc20; /职称double wages; /工资 ;class student :public base /基类派生出学生类 int sno; /学号char bj10; /班级char zy10; /专业double score; /入学成绩 ;5、声明一个哺乳动物Mammal类,再由此派生出狗Dog类,声明一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。答案:#includeclass Mammal public:Mammal() coutcall Mammalendl; Mammal() coutDelete base classendl; ;class Dog :public Mammal public:Dog() coutcall Dogn; Dog() coutDelete Dog classn;void main() Dog b; 6、声明一个Shape基类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。答案:#include#define PI 3.1415926class shapeprotected:double s;public:void show()couts= sendl;class Rectangle:public shape double x,y;public:Rectangle(double x1=0,double y1=0) x=x1;y=y1;void GetArea() s=x*y;class Circle:public shape double r;public:Circle(double r1=0) r=r1;void GetArea() s=r*r*PI; ;class Square :public Rectangle double a;public:Square(double a1=0 ) a=a1;void GetArea() s=a*a; ;7、建立一个基类Building ,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。答案:程序代码:#include class Buildingpublic:Building(int f,int r,double ft)floors=f;rooms=r;footage=ft;void show( ) cout floors: floorsendl;cout rooms: roomsendl;cout total area: footageendl;protected:int floors;int rooms;double footage;class Housing:public Buildingpublic:Housing(int f,int r,double ft,int bd,int bth):Building(f,r,ft) bedrooms=bd; bathrooms=bth;void show()coutn HOUSING:n;Building:show();cout bedrooms: bedroomsendl;cout bathrooms: bathroomsendl;private:int bedrooms;int bathrooms;class Office:public Building public:Office(int f,int r,double ft,int ph,int ex):Building(f,r,ft) phones=ph; extinguishers=ex;void show()coutn HOUSING:n;Building:show();cout phones: phonesendl;cout extinguishers: extinguishersendl;private:int phones;int extinguishers;void main() Housing hob(5,7,140,2,2);Office oob(8,12,500,12,2);hob.show();oob.show();【编程题】1、定义Point类,有数据成员X和Y,重载+和-运算符,要求同时重载前缀方式和后缀方式。答案:#include class Pointpublic:Point() X=Y=0; int GetX() return X; int GetY() return Y; Point& operator +();Point operator +(int);Point& operator -();Point operator -(int);void Print() coutThe point is (X,Y)endl; private:int X,Y;Point& Point:operator +() X+;Y+;return *this;Point Point:operator +(int) Point temp=*this;+*this;return temp;Point& Point:operator -() X-;Y-;return *this;Point Point:operator -(int) Point temp=*this;-*this;return temp;void main() Point obj;obj.Print();obj+;obj.Print();+obj;obj.Print();obj-;obj.Print();-obj;obj.Print();分值:12难度:4知识点:评析:注意重载前缀单目运算符和后缀单目运算符的区别。前缀单目运算符重载为类的成员函数时,不需要显式说明参数,即函数没有形参;而后缀单目运算符重载为类的成员函数时,函数要带有一个整型形参。2、分别用成员函数和友元函数重载运算符,使对实型的运算符=、+、-、*、/ 适用于复数运算。答案:程序如下#include class complex /复数类声明 public: /外部接口complex(double r=0.0,double i=0.0) /构造函数real=r,imag=i;complex operator +(complex c2); /运算符+重载成员函数complex operator - (complex c2); /运算符-重载成员函数complex operator *(complex );complex operator /(complex);complex operator =(complex c2)real=c2.real;imag=c2.imag;return complex(real,imag);void display(); /输出复数private: /私有数据成员double real; /复数实部double imag; /复数虚部;complex complex:operator + (complex c2) /重载运算符函数实现 return complex(real + c2.real,imag + c2.imag); /创建-个临时无名对象作为返回值complex complex:operator - (complex c2) /重载运算符函数实现 return complex(real - c2.real,imag - c2.imag); complex complex:operator *(complex c2) double x,y;x=real*c2.real-imag*c2.imag;y=real*c2.imag+imag*c2.real;return complex(x,y);complex complex:operator /(complex c2) double x,y,z;x=real*c2.real+imag*c2.imag;y=real*c2.imag-imag*c2.real;z=real*c2.real+imag*c2.imag;return complex(x/z,y/z);void complex:display() cout(real,imag)endl;分值:12难度:4知识点:评析:3、应用抽象类,求圆、圆内接正方形和圆外切正方形的面积和周长。答案:#include #define PI 3.1415926#includeclass base /抽象基类B0声明 public: /外部接口virtual void display( )= 0; /纯虚函数成员;class circle:public base /公有派生 protected:double r,s,p;public:circle(double x=0) r=x;void display( ) cout圆的面积r*r*PIendl;cout圆的周长PI*r*2endl; /虚成员函数;class incircle:public circle /公有派生 double a;public:incircle(double x=0): circle(x) void display( ) a=sqrt(r);cout内接正方形面积a*aendl;cout内接正方形周长4*aendl; /虚成员函数;class outcircle:public circle /公有派生 public:outcircle(double x=0): circle(x) void display( ) cout外切正方形面积4*r*rendl;cout外切正方形周长8*rdisplay( ); void main( ) /主函数 base *p; /声明抽象基类指针circle b1(10); /声明派生类对象incircle d1(9); /声明派生类对象outcircle e1(10);p=&b1; fun(p);p=&d1; fun(p);p=&e1; fun(p);分值:12难度:4知识点:评析:4、声明一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都有GetArea( )函数计算对象的面积,GetPerim( )函数计算对象的周长。答案:#include#includeclass shape public:virtual void getarea()=0;virtual void getperim()=0;class rectangle:public shape int a,b,c;double s,p;public:rectangle(int a1,int b1,int c1) a=a1;b=b1;c=c1;void getperim() p=a+b+c; cout周长 pendl;void getarea() p=(a+b+c)/2.0;s=sqrt(p*(p-a)*(p-b)*(p-c);cout面积sendl;class circle :public shape float r,s,p;public:circle(float r1) r=r1;void getperim() p=2*r*3.1415926;cout周长 pendl;void getarea() s=r*r*3.1415926; cout面积sgetarea();p-getperim();void main() shape *p;rectangle a(3,4,5);circle b(10);p=&a; show(p);p=&b; show(p);分值:12难度:4知识点:评析:5、声明一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义Speak( )成员函数,基类中定义为虚函数。声明一个Dog类的对象,调用Speak()函数,观察运行结果。答案:程序代码#includeclass Mammal public:Mammal() coutcall Mammalendl; virtual void speak() cout call base classendl; ;class Dog :public Mammal public:Dog() coutcall Dogn; void speak() coutcall Dog classn; ;void main() Mammal a;a.speak();Dog b;b.speak();分值:12难度:4知识点:评析:6、声明计数器Counter类,对其重载运算符“+”。答案:#includeclass Counterpublic:Counter()Counter(int initialValue);Counter()int GetItsVal()const return itsVal;void SetItsVal(int x) itsVal=x;Counter operator+(const Counter &);private:int itsVal;Counter:Counter(int initialValue):itsVal(initialValue) Counter:Counter():itsVal(0) Counter Counter:operator+(const Counter &rhs) return Counter(itsVal+rhs.GetItsVal();void main() Counter varOne(2),varTwo(4),varThree;varThree=varOne+varTwo;coutvarOne:varOne.GetItsVal()endl;coutvarTwo:varTwo.GetItsVal()endl;coutvarThree:varThree.GetItsVal()endl;分值:12难度:4知识点:评析:7、声明一个Rectangle类,有长itsWidth、宽itsLength等属性,重载其构造函数Rectanle()和Rectangle(int width,int length)。答案:#includeclass Rectanglepublic:Rectangle();Rectangle(int width,int length);Rectangle() int GetWidth() const return itsWidth;int GetLength() const return itsLength;private:int itsWidth;int itsLength;Rectangle:Rectangle() itsWidht=5;itsLength=10;Rectangle:Rectangle(int width,int length) itsWidth=width;itsLength=length;void main() Rectangle Rect1;cout”Rect1 width:”Rect1.GetWidth()endl;cout”Rect1 length:”Rect1.GetLength()endl;int aWidth,aLength;coutaWidth;coutaLength;Rectangle Rect2(aWidth,aLength);cout”nRect2 width:”Rect2.GetWidth()endl;cout”Rect2 length:”Rect2.GetLength()endl;分值:12难度:4知识点:评析:8、分别用成员函数和友元函数重载运算符,使对实型的运算符=、+、-、*、/ 适用于复数运算。答案:#include class complex /复数类声明 public: /外部接口complex(double r=0.0,double i=0.0) /构造函数real=r,imag=i;complex operator +(complex c2); /运算符+重载成员函数complex operator - (complex c2); /运算符-重载成员函数complex operator *(complex );complex operator /(complex);complex operator =(complex c2)real=c2.real;imag=c2.imag;return complex(real,imag);void display(); /输出复数private: /私有数据成员double real; /复数实部double imag; /复数虚部;complex complex:operator + (complex c2) /重载运算符函数实现 return complex(real + c2.real,imag + c2.imag); /创建-个临时无名对象作为返回值complex complex:operator - (complex c2) /重载运算符函数实现 return complex(real - c2.real,imag - c2.imag); complex complex:operator *(complex c2) double x,y;x=real*c2.real-imag*c2.imag;y=real*c2.imag+imag*c2.real;return complex(x,y);complex complex:operator /(complex c2) double x,y,z;x=real*c2.real+imag*c2.imag;y=real*c2.imag-imag*c2.real;z=real*c2.real+imag*c2.imag;return complex(x/z,y/z);void complex:display() cout(real,imag)endl;分值:5难度:4知识点:评析:9、实现重载函数Double(x),返回值为输人参数的两倍;参数分别为整型、浮点型、双精度型,返回值类型与参数一样。答案:#include template class baseprotected:T m;public:base (T n) m=n+n;void show() coutmendl; ;void main()base a(10);a.show();float x=12.8;base b(x);b.show();double y=12.354356788;base c(y);c.show();分值:12难度:4知识点:评析:七、综合编程题(20分)1、定义一个复数类Complex,它的私有数据成员(类型都是float)有real(实数), imag(虚数),公有成员函数有:GetReal(),它返回复数的实数部分;GetImag(),它返回复数的虚数部分;Complex operator +( Complex &),返回两个复数的加。该复数的构造函数原型为:Complex(float r=1.0, float i=1.0)。要求: 编写类Complex及成员函数,并编写一个主程序,主程序中定义了2个复数(其中有一个复数的参数为默认参数,另一个参数可以是任意有效值),然后,计算2个复数的加,并输出结果。参考答案:编程题(20分,评分标准:写出类Complex得12分,编出main()函数得8分,注意:程序的所有等价写法同样给分)#include class Complex private: float real, imag;public: Complex(float r=1.0, float i=1.0 ) real=r; imag=i; float GetReal() return real;float GetImag() return imag; Complex operator+ (Complex &b ); Complex Complex:operator+ (Complex &b) Complex t; t.real = real + b.real; t.imag = imag + b.imag; return t; void main()Complex c1(4.0,5.0), c2, c3; c3=c1+c2;coutreal=c3.GetReal(),image=c3.GetImag()endl;2、定义一个博士类doctor,它的私有数据成员有Num(编号,为int型)、Name(姓名,为string型)、Age(年龄,为int型)、Sex(性别,为char型)。公用成员函数有:GetNum( ),它返回博士编号;Display( ),它输出博士的上述基本信息(注意:Display( )函数在类外定义)。博士类的构造函数原型为:doctor (int Num0=1001, string Name0=彭博士, int Age0=30,char Sex0=1)。要求: 编写类doctor,并编写一个主程序,主程序中定义了3个博士对象(其中有一个博士的参数为默认参数,另一个参数可以是任意有效值),然后,输出2个博士的基本信息;第3个博士对象用指针动态生成(有参数,参数可以是任意有效值),再输出该博士信息。(提示:输出Name可以这样:coutName.c_str();)参考答案:#include using namespace std;class doctorprivate:int Num; string Name; int Age; char Sex;public:int GetNum() return Num;Doctor(intNum0=1001,string Name0=彭博士, int Age0=30,char Sex0=1)Num = Num0;Name = Name0;Age = Age0;Sex = Sex0;void display();void doctor:display()coutNum=Num Name=Name.c_str() Age= Age Sex=Sexdisplay();delete p;return 0;3、定义一个书本类Book,它的私有数据成员有BookName(书名,为string型),AuthorName(作者姓名,为string型),Publish(出版社,为string型),PageNum(页码,为int型)。公用成员函数有:GetPageNum( ),它返回当前的页码值PageNum;OpenPage(int PNum),打开书本的PNum页,即设置PageNum值为PNum。Book类的构造函数原型为:Book(string BName=C+语言,string AName=张三, string Pub=清华大学出版社, int PNum=0)。要求:(1) 编写类Book,并编写一个主程序,主程序中定义2个书本对象(其中有一个书本的书名参数为默认参数,另一个书本参数可以是任意有效值)。(2)重载输出流,使它能输出对象Book(即输出Book对象的所有私有数据成员),然后使用该输出流输出前2个书本对象的基本信息。(3)定义一个输出文件BookFile.txt,把上述两个书本对象的页码值PageNum输出到这个文件中。参考答案:#include #include #include using namespace std;class Bookprivate:int PageNum; string BookName;string AuthorName;string Publish;public:int GetPageNum() return PageNum;Book(string BName=C+语言,string AName=张三, string Pub=清华大学出版社, int PNum=0)BookName = BName;AuthorName = AName;Publish = Pub;PageNum = PNum;friend ostream& operator(ostream &Output,Book &B);ostream& operator(ostream &Output,Book &B)coutB.BookName endl;coutB.AuthorName endl;coutB.Publish endl;coutB.PageNum endl;return Output;int main()Book b1,b2(数据结构,严教授,高等教育出版社,200);coutb2;ofstream f1(BookFile.txt,ios:out);f1b1.GetPageNum()b2.GetPageNum()endl;f1.close();return 0;4、定义一个教师类teacher,它的私有数据成员有Num(教工号,为int型)、Name(姓名,为string型)、Age(年龄,为int型)、Sex(性别,为char型)。公用成员函数有:GetNum( ),它
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 核心素养下高中技术多元化项目教学
- 某高校大学生物理竞赛电感相关试题及答案
- 全国中学生数学竞赛省级联赛试题及答案
- 2025播音主播考试真题及答案
- 消防设施工程风险控制方案
- 危险化学品领域安全隐患试题及答案
- 2025年企划广告策划试卷及答案
- 房屋结构设计优化方案
- 高考数学截面真题及答案
- 港城产业链融合-洞察与解读
- 保险基础知识培训
- 2025年教育系统学校中层后备干部选拔考试题(含答案)
- 养老院安全培训考试题及答案解析
- DB32-T 5192-2025 工业园区碳排放核算指南
- 湖南省九校联盟2026届高三上学期9月第一次联考日语试题(含答案)
- 时事政治讲座课件
- 锅炉工安全培训知识课件
- 天津地区高考语文五年高考真题汇编-文言文阅读
- 2023年自考全国10月财务管理学试题+答案
- 日语动词分类课件 【高效课堂+备课精研】 高考日语一轮复习
- GA/T 850-2021城市道路路内停车位设置规范
评论
0/150
提交评论