资源描述
C++练习题
一、编程题:所有的题需要在主函数中定义对象并测试类的功能。
1、编写一个三角形类,有计算面积,显示面积的功能等。要求有无参数的和带参数的构造函数。
2、设计一个立方体类Box,它能计算并输出立方体的体积和表面积。要求有无参数的和带参数的构造函数。
3、定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。
4、利用Clock类,派生一个带“AM”、“PM”的新时钟类NewClock。
5、建立一个基类Building ,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。
6、设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。
7、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。
8、声明一个Shape基类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。
9、使用虚函数编写程序求球体和圆柱体的体积及表面积。由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类circle作为基类。在circle类中定义一个数据成员radius和两个虚函数area()和volume()。由circle类派生sphere类和column类。在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的体积及表面积。
二、写出程序结果
1、某学校对教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干教师的月工资。
#include<iostream>
using namespace std;
class Teacher{
protected:
double salary;
int workhours;
public:
Teacher(int wh=0){workhours=wh;}
virtual void cal_salary()=0;
void print(){cout<<salary<<endl;}
};
class Prof:public Teacher{
public:
Prof(int wh=0):Teacher(wh){}
void cal_salary(){
salary=workhours*50+5000;
}
};
class Vice_Prof:public Teacher{
public:
Vice_Prof(int wh=0):Teacher(wh){}
void cal_salary(){
salary=workhours*30+3000;
}
};
class Lecture:public Teacher{
public:
Lecture(int wh=0):Teacher(wh){}
void cal_salary(){
salary=workhours*20+2000;
}
};
int main(){
Teacher *pt;
Prof prof(200);
pt=&prof;
pt->cal_salary();
prof.print();
Vice_Prof vice_prof(250);
pt=&vice_prof;
pt->cal_salary();
vice_prof.print();
Lecture lecture(100);
pt=&lecture;
pt->cal_salary();
lecture.print ();
return 0;
}
2、有一个交通工具类vehicle,将它作为基类派生小车类car、卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。
#include<iostream>
#include<cstring>
using namespace std;
class Vehicle{
public:
virtual void showinfo()=0;
protected:
char Name[20];
};
class Car:public Vehicle{
public:
Car(char *name){
strcpy(Name,name);
}
void showinfo(){cout<<Name<<endl;}
protected:
int Radius;
};
class Truck:public Vehicle{
public:
Truck(char *name){
strcpy(Name,name);
}
void showinfo(){cout<<Name<<endl;}
};
class Boat:public Vehicle{
public:
Boat(char *name){
strcpy(Name,name);
}
void showinfo(){cout<<Name<<endl;}
};
int main(){
Vehicle *vp;
Car car("奔驰");
Truck truck("运输卡车");
Boat boat("游艇");
vp=&car;
vp->showinfo ();
vp=&truck;
vp->showinfo ();
vp=&boat;
vp->showinfo ();
return 0;
}
3、 定义一个shape抽象类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area( )。
#include<iostream>
#include<cstring>
using namespace std;
class Shape{
public:
virtual void Area()=0;
};
class Rectangle:public Shape{
public:
Rectangle(double w,double h){
width=w,height=h;
}
void Area(){cout<<width*height<<endl;}
protected:
double width,height;
};
class Circle:public Shape{
public:
Circle(double r){
radius=r;
}
void Area(){cout<<3.1415*radius*radius<<endl;}
protected:
double radius;
};
int main(){
Shape *sp;
Rectangle re1(10,6);
Circle cir1(4.0);
sp=&re1;
sp->Area ();
sp=&cir1;
sp->Area ();
return 0;
}
4、 定义猫科动物Animal类,由其派生出猫类(Cat)和豹类(Leopard),二者都包含虚函数 sound( ),要求根据派生类对象的不同调用各自重载后的成员函数。
#include<iostream>
using namespace std;
class Animal{
public:
virtual void Speak()=0;
};
class Cat :public Animal{
void Speak(){
cout<<"My name is Cat"<<endl;
}
};
class Leopard:public Animal{
void Speak(){
cout<<"My name is Leopard"<<endl;
}
};
int main(){
Animal *pa;
Cat cat;
pa=&cat;
pa->Speak();
Leopard leopard;
pa=&leopard;
pa->Speak();
return 0;
}
5、建立一个基类Building ,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。
程序代码:
#include <iostream>
using namespace std;
class Building
{public:
Building(int f,int r,double ft)
{floors=f;
rooms=r;
footage=ft;
}
void show()
{ cout<<" floors: "<<floors<<endl;
cout<<" rooms: "<<rooms<<endl;
cout<<" total area: "<<footage<<endl;
}
protected:
int floors;
int rooms;
double footage;
};
class Housing:public Building
{public:
Housing(int f,int r,double ft,int bd,int bth):Building(f,r,ft)
{ bedrooms=bd;
bathrooms=bth;
}
void show()
{cout<<"\n HOUSING:\n";
Building::show();
cout<<" bedrooms: "<<bedrooms<<endl;
cout<<" bathrooms: "<<bathrooms<<endl;
}
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()
{cout<<"\n HOUSING:\n";
Building::show();
cout<<" phones: "<<phones<<endl;
cout<<" extinguishers: "<<extinguishers<<endl;
}
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();
}
6、看程序写结果
#include <iostream>
using namespace std;
class parent
{ int i;
protected:
int x;
public:
parent( ) {x=0; i=0;}
void change( ) {x++; i++; }
void display();
};
class son: public parent
{ public:
void modify();
};
void parent:: display() {cout<<"x="<<x<<endl; }
void son::modify() {x++; }
void main()
{ son A; parent B;
cout<<"Display derived class object A:\n";
A.display(); A.change();
A.display(); A.modify();
A.display(); cout<<"Display base class object B:\n";
B.change(); B.display();
}
7、看程序写结果
#include <iostream>
using namespace std;
class base
{ public:
void show() {cout<<"class base show() is called.\n"; }
};
class derived: public base
{ public:
void show() {cout<<"class derived show() is called.\n"; }
};
void main()
{base demo1;
derived demo2;
demo1.show();
demo2.show();
demo2.base::show();
}
8、看程序写结果
#include<iostream>
using namespace std;
class B1{
public:
B1(int i){ cout<<"constructing B1 "<<i<<endl; }
~B1( ){ cout<<"destructing B1 "<<endl; }
};
class B2 {
public:
B2( ){ cout<<"constructing B3 *"<<endl; }
~B2( ){ cout<<"destructing B3"<<endl; }
};
class C:public B2, virtual public B1 {
int j;
public:
C(int a,int b,int c):B1(a),memberB1(b) ,j(c){}
private:
B1 memberB1;
B2 memberB2;
};
int main( ){
C obj(1,2,3);
}
展开阅读全文