欢迎来到夜的世界
using namespace std;
class Teacher{ (2分)
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{ (2分)
Prof(int wh=0):Teacher(wh){}
void cal_salary(){
salary=workhours*50+5000;
}
class Vice_Prof:public Teacher{ (2分)
Vice_Prof(int wh=0):Teacher(wh){}
salary=workhours*30+3000;
class Lecture:public Teacher{ (2分)
Lecture(int wh=0):Teacher(wh){}
salary=workhours*20+2000;
int main(){ (2分)
Teacher *pt;
Prof prof(200);
pt=&prof;
pt->cal_salary();
prof.print();
Vice_Prof vice_prof(250);
pt=&vice_prof;
vice_prof.print();
Lecture lecture(100);
pt=&lecture;
lecture.print ();
return 0;
#include
class Vehicle{
virtual void showinfo()=0; (2分)
char Name[20];
class Car:public Vehicle{
Car(char *name){
strcpy(Name,name);
void showinfo(){cout<<Name<<endl;} (2分)
int Radius;
class Truck:public Vehicle{
Truck(char *name){
class Boat:public Vehicle{
Boat(char *name){
Vehicle *vp;
Car car("奔驰");
Truck truck("运输卡车");
Boat boat("游艇");
vp=&car;
vp->showinfo ();
vp=&truck;
vp=&boat;
class Animal{
virtual void Speak()=0; (3分)
class Cat :public Animal{
void Speak(){
cout<<"My name is Cat"<<endl; (2分)
class Leopard:public Animal{
cout<<"My name is Leopard"<<endl; (2分)
int main(){ (3分)
Animal *pa;
Cat cat;
pa=&cat;
pa->Speak();
Leopard leopard;
pa=&leopard;
class Shape{
virtual void Area()=0; (2分)
class Rectangle:public Shape{
Rectangle(double w,double h){ (1分)
width=w,height=h;
void Area(){cout<<width*height<<endl;} (2分)
double width,height;
class Circle:public Shape{
Circle(double r){ (1分)
radius=r;
void Area(){cout<<3.1415*radius*radius<<endl;} (2分)
double radius;
Shape *sp;
Rectangle re1(10,6);
Circle cir1(4.0);
sp=&re1;
sp->Area ();
sp=&cir1;
enum AMPM{AM=1,PM};
class Building{ (1分)
Building(char *);
void ShowBuilding(){
cout<<Name;
char Name[30];
Building::Building(char *name){ (1分)
class Teach_Building: public Building { (1分)
Teach_Building(char *,int,int,int,int);
void ShowTeach_Building(){
Building::ShowBuilding();
cout<<" No:"<<No;
cout<<" Floors:"<<Floors;
cout<<" ClassRooms:"<<ClassRooms;
cout<<" Area:"<<Area<<endl;
int No,Floors,ClassRooms,Area;
Teach_Building::Teach_Building(char *name,int no,int fl,int cr,int ar):Building(name){ (1分)
No=no;Floors=fl;ClassRooms=cr;Area=ar;
class Dorm_Building:public Building { (2分)
Dorm_Building(char *,int,int,int,int,int);
void ShowDorm_Building(){
cout<<" DormRooms:"<<DormRooms;
cout<<" Area:"<<Area;
cout<<" StudentNum:"<<StudentNum<<endl;
int No,Floors,DormRooms,Area,StudentNum;
Dorm_Building::Dorm_Building(char *name,int no,int fl,int dr,int ar,int sn):Building(name){
No=no;Floors=fl;DormRooms=dr;Area=ar;StudentNum=sn; (2分)
Teach_Building tb("主教学楼",59,6,66,18000);
Dorm_Building db("北苑男生宿舍",41,5,75,3750,300);
tb.ShowTeach_Building();
db.ShowDorm_Building();
class vehicle // 定义汽车类 (3分)
{
int wheels; // 车轮数
float weight; // 重量
vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
class car:public vehicle // 定义小车类 (3分)
int passenger_load; // 载人数
car(int wheels,float weight,int passengers=4);
int get_passengers();
vehicle::vehicle(int wheels1,float weight1) //(1分)
wheels=wheels1;
weight=weight1;
int vehicle::get_wheels()
return wheels;
float vehicle::get_weight()
return weight;
void vehicle::show() (1分)
cout << "车轮:" << wheels << "个" << endl;
cout << "重量:" << weight << "公斤" << endl;
car::car(int wheels, float weight,
int passengers) :vehicle(wheels, weight)
passenger_load=passengers;
int car::get_passengers ()
return passenger_load;
void car::show()
cout <<" 车型:小车" << endl;
vehicle::show();
cout << "载人:" << passenger_load << "人" << endl;
cout << endl;
void main ()
car car1(4,2000,5); (1分)
cout << "输出结果" << endl;
car1. show (); (1分)
class car;(1分)
class boat{
private:
int weight; //(1分)
boat(int w):weight(w){} //(1分)
friend int totalweight(boat b1,car c1); //(2分)
class car{ /(1分)
int weight; (1分)
car(int w):weight(w){};
friend int totalweight(boat b1,car c1); (1分)
int totalweight(boat b1,car c1) //(1分)
return b1.weight+c1.weight;
void main()
car c1(1000);
boat b1(2000);
cout<<totalweight(b1,c1)<<endl;(1分)
class rectangle //(2分)
int x1,y1,x2,y2; // (2分)
rectangle(int xx1,int yy1,int xx2,int yy2) //(1分)
x1=xx1;y1=yy1;x2=xx2;y2=yy2;
int getarea() //(2分)
return abs((x2-x1)*(y1-y2));
rectangle rect1(3,7,8,5); (2分)
cout<<rect1.getarea()<<endl; (1分)
#include<stdlib.h>
class A {
int *a; int n; int MaxLen;
A(): a(0), n(0), MaxLen(0) {}
A(int *aa, int nn, int MM) {
n=nn;
MaxLen=MM;
if(n>MaxLen) exit(1);
___(1)_); //由a指向长度为MaxLen的动态数组
for(int i=0; i<n; i++) a[i]=aa[i];
~A() {delete []a;}
int GetValue(int i) ___(2)_ //函数体返回a[i]的值
int b[10]={1,2,3,4,5,6,7,8,9,10};
A r(b,10,10);
int i,s=0;
for(i=0; i<10; i++); ___(3)_ //把r对象的a数据成员中的每个
//元素值依次累加到s中
cout<<"s="<<s<<endl;
(1) (2) (3)
程序填空16. 一个类定义如下:
class Goods
char gd_name[20]; //商品名称
int weight; //商品重量
static int totalweight; //同类商品总重量
Goods(char*str,int w){ //构造函数
strcpy(gd_name,str);
weight=w;
totalweight+=weight;
~ Goods(){totalweight -= weight;}
char* GetN(){_(1)_;} //返回商品名称
int GetW(){return weight;}
_(2)_ GetTotal_Weight() { //定义静态成员函数返回总重量
_(3)_;
int a,b;
___(1)_ //定义构造函数,使参数aa和bb的默认值为0,
//在函数体中用aa初始化a,用bb初始化b
main() {
A *p1, *p2;
___(2)_ ; //调用无参构造函数生成由p1指向的动态对象
___(3)_; //调用带参构造函数生成由p2指向的动态对象,
//使a和b成员分别被初始化为4和5
A(int aa=0, int bb=0) ___(1)_{} //分别用aa和bb对应初始化a和b
___(2)_ ; //定义类A的对象x并用5初始化,同时定义y并用x初始化
___(3)_ ; //定义p指针,使之指向对象x
char *a;
___(1)_ //定义无参构造函数,使a的值为空
A(char *aa) {
a=___(2)_;
strcpy(a,aa); //用aa所指字符串初始化a所指向的动态存储空间
___(3)_ //定义析构函数,删除a所指向的动态存储空间
程序填空11.class A {
int a;
A() {a=0;}
_(1)_{} //定义构造函数,用参数aa初始化数据成员a
_(2)_; //定义类A的指针对象p
_(3)_; //用p指向动态对象并初始化为整数5
程序填空10. 已知一个利用数组实现栈的类定义如下:
const int ARRAY_SIZE=10;
class Stack {
void Init() {top=-1;} //初始化栈为空
void Push(int newElem); //向栈中压入一个元素
int Pop(); //从栈顶弹出一个元素
bool Empty() { //判栈空
if(top==-1) return true;else return false;}
int Depth() {return top+1;} //返回栈的深度
void Print();
//按照后进先出原则依次输出栈中每个元素,直到栈空为止
int elem[ARRAY_SIZE]; //用于保存栈元素的数组
int top; //指明栈顶元素位置的指针
void Stack::Push(int newElem) {
if(_(1)_) {
cout<<"栈满!"<<endl;
exit(1); //中止运行
_(2)_;
elem[top]=_(3)_;
class AA {
int a[10];
int n;
void SetA(int aa[], int nn); //用数组aa初始化数据成员a,
//用nn初始化数据成员n
int MaxA(); //从数组a中前n个元素中查找最大值
void SortA(); //采用选择排序的方法对数组a中前n个元素
//进行从小到大排序
void InsertA();//采用插入排序的方法对数组a中前n个元素进行从小到大排序
void PrintA(); //依次输出数组a中的前n个元素
//最后输出一个换行
使用该类的主函数如下:
int a[10]={23,78,46,55,62,76,90,25,38,42};
AA x;
___(1)_;
int m=___(2)_;
___(3)_;
cout<<m<<endl;
该程序运行结果为:
23 78 46 55 62 76
78
class fract{
int den; //分子
int num; //分母
fract(int d=0,int n=1):den(d),num(n){} //1行
friend fract& operator+=(fract,fract&); //2行
void show(){ cout<<den<<'/'<<num;} //3行
}; //4行
friend fract& operator+=(fract f1,fract& f2) //5行
{ //7行
f1.den=f1.den*f2.num+f1.num*f2.den; //8行
f1.num*=f2.num; //9行
return f1; //10行
void main(){
fract fr(3,4);
fr+=fract(5,7);
fr.show();
错误行的行号为______和________。
分别改正为______________________________
和______________________________。
#include<iostream.h>
class A { //1行
int a[10]; int n; //2行
public: //3行
A(int aa[], int nn): n(nn) { //4行
for(int i=0; i<n; i++) aa[i]=a[i]; //5行
} //6行
int Get(int i) {return a[i];} //7行
int SumA(int n); //8行
}; //9行
int A::SumA(int n) { //10行
int s=0; //11行
for(int j=0; j<n; j++) s+=a[j]; //12行
return s; //13行
} //14行
void main() { //15行
int a[]={2,5,8,10,15,20}; //16行
A x(a,6); //17行
int d=1; //18行
for(int i=0; i<4; i++) d*=x.a[i]; //19行
int f=SumA(6); //20行
cout<<"d="<<d<<’,’; //21行
cout<<"f="<<f<<endl; //22行
} //23行
错误行的行号为______、________和________。
分别改正为____________________、________________和___________________。
class Franction { //定义分数类
int nume; //定义分子
int deno; //定义分母
//把*this化简为最简分数,具体定义在另外文件中实现
void FranSimp();
//返回两个分数*this和x之和,具体定义在另外文件中实现
Franction FranAdd(const Franction& x);
//置分数的分子和分母分别0和1
void InitFranction() {nume=0; deno=1;}
//置分数的分子和分母分别n和d
void InitFranction(int n, int d) {nume=n; deno=d;}
//输出一个分数
void FranOutput() {cout<<nume<<'/'<<deno<<endl;}
void main() //1行
{ //2行
Franction a,b,c; //3行
a.InitFranction(6,15); //4行
b.InitFranction(1); //5行
c.InitFranction(); //6行
c=FranAdd(a,b); //7行
cout<<c.nume<<’/’<<c.deno<<endl; //8行
} //9行
Franction *a=new Franction; //3行
Franction *b=new Franction; //4行
a->InitFranction(6,15); //5行
b.InitFranction(3,4); //6行
Franction c; //7行
c.InitFranction(); //8行
c=a.FranAdd(b); //9行
cout<<c.FranOutput()<<endl; //10行
} //11行
class CE //1行
int a,b; //3行
int getmin() {return (a<b? a:b);} //4行
public //5行
int c; //6行
void SetValue(int x1,int x2, int x3) { //7行
a=x1; b=x2; c=x3; //8行
int GetMin(); //10行
int GetMin() { //12行
int d=getmin(); //13行
return (d<c? d:c); //14行
} //15行
int a,b; //2行
const int c; //3行
public //4行
A():c(0);a(0);b(0) {} //5行
A(int aa, int bb) c(aa+bb); {a=aa; b=bb;} //6行
}; //7行
A a,b(1,2); //8行
A *x=&a, &y=b; //9行
A *z=new A, w[10]; //10行
分别改正为____________________、_____________________
和______________________________________。
public: //4行
A() {a=b=c=0;} //5行
A(int aa, int bb):c(aa+bb) {a=aa; b=bb;} //6行
A a,b(1,2,3); //8行
A x(2,3), y(4); //9行
错误原因分别为___________________、__________________和__________________。
int a; //2行
A(int aa=0):a(aa){} //4行
}; //5行
class B { //6行
int a,b; //7行
const int c; //8行
A d; //9行
public: //10行
B():c(0) {a=b=0;} //11行
B(int aa, int bb):d(aa+bb) { //12行
a=aa; b=bb; c=aa-bb; //13行
B a,b(1,2); //16行
B x=a,y(b),z(1,2,3),; //17行
错误原因分别为_____________________、______________________
和_____________________。
(1) 函数名是在类名前加上波浪号。
(2) 没有返回值。
(3) 不能指定参数。
(4) 不能重载。
(5) 被系统自动调用。
posted on 2022-04-05 11:09 二十四桥_明月夜 阅读(317) 评论(0) 收藏 举报