计算机程序设计C++ MOOC
测试与作业C++基础练习100题
##第十一周基本练习
本周内容为类的继承
- 继承点类定义圆类
#include <iostream>
using namespace std;
class POINT
{
private:
double x, y;
public:
POINT(){ x = 0; y = 0; }
POINT(double a, double b){ x = a; y = b; }
void set(double a, double b){ x = a; y = b; };
void show()
{
cout << "x=" << x << ' ' << "y=" << y;
}
};
class CIRCLE :public POINT
{
private:
double radius;
public:
CIRCLE() :POINT(){ radius = 0; }
CIRCLE(double a, double b, double r) :POINT(a, b){ radius = r; }
void set(double a, double b, double r){ POINT::set(a, b); radius = r; }
void show()
{
POINT::show(); cout << " r=" << radius;
}
};
int main()
{
CIRCLE a, b(1, 2, 3);
double x, y, r;
a.show(); cout << endl;
b.show(); cout << endl;
cin >> x >> y >> r;
a.set(x, y, r);
a.show(); cout << endl;
return 0;
}
- 继承点类定义矩形类
#include <iostream>
using namespace std;
class POINT
{
private:
double x, y;
public:
POINT(){ x = 0; y = 0; }
POINT(double a, double b){ x = a; y = b; }
void set(double a, double b){ x = a; y = b; };
void show()
{
cout << "x=" << x << ' ' << "y=" << y;
}
};
class RECT :public POINT
{
private:
double width, height;
public:
RECT() :POINT(){ width = 0; height = 0; }
RECT(double a, double b, double c, double d) :POINT(a, b){ width = c; height = d; }
void set(double a, double b, double c, double d){ POINT::set(a, b); width = c; height = d; }
void show()
{
POINT::show();
cout << " width=" << width << " height=" << height;
}
};
int main()
{
RECT a, b(1, 2, 10, 5);
double x, y, w, h;
a.show(); cout << endl;
b.show(); cout << endl;
cin >> x >> y >> w >> h;
a.set(x, y, w, h);
a.show(); cout << endl;
return 0;
}
- 继承房间类定义教室类
#include <iostream>
#include <string.h>
using namespace std;
class ROOM
{
private:
char RoomId[20];
public:
ROOM(){ strcpy(RoomId, "0000"); }
ROOM(char *id){ strcpy(RoomId, id); }
void set(char *id){ strcpy(RoomId, id); }
void show()
{
cout <<"Number:"<< RoomId;
}
};
class CLASSROOM :public ROOM
{
private:
int seats;
public:
CLASSROOM() :ROOM(){ seats = 0; }
CLASSROOM(char *id, int s) :ROOM(id){ seats = s; }
void set(char *id, int s){ ROOM::set(id); seats = s; }
void show()
{
ROOM::show();
cout << " seats:" << seats;
}
};
int main()
{
CLASSROOM a, b("工程馆505",130);
char number[20];
int seats;
a.show(); cout << endl;
b.show(); cout << endl;
cin >> number >> seats;
a.set(number, seats);
a.show(); cout << endl;
return 0;
}
- 继承车辆类定义客车类
#include <iostream>
#include <string.h>
using namespace std;
class VEHICLE
{
private:
char Number[20], Owner[20];
public:
VEHICLE(){ strcpy(Number, "00000"); strcpy(Owner, "unknown"); }
VEHICLE(char *n, char *o){ strcpy(Number, n); strcpy(Owner, o); }
void set(char *n, char *o){ strcpy(Number, n); strcpy(Owner, o); }
void show()
{
cout << "Number:" << Number << " owner:" << Owner;
}
};
class BUS :public VEHICLE
{
private:
int seats;
public:
BUS() :VEHICLE(){ seats = 0; };
BUS(char *n, char *o, int s) :VEHICLE(n, o){ seats = s; }
void set(char *n, char *o, int s){ VEHICLE::set(n, o); seats = s; }
void show()
{
VEHICLE::show();
cout << " seats:" << seats;
}
};
int main()
{
BUS a, b("SHAN-1234567", "ZhangSan", 40);
char number[20], name[20];
int seats;
a.show(); cout << endl;
b.show(); cout << endl;
cin >> number >> name >> seats;
a.set(number, name, seats);
a.show(); cout << endl;
return 0;
}
以上为第十一周的基础练习。