继承虚函数
实验七 继承 + 实验八 多态(完整:全部题目与参考代码)
════════════════════════════════════
实验七 继承
════════════════════════════════════
【Date类】(指导书给定,实现如下)
class Date
{
private:
int year,month,day;
public:
Date(int year=1990,int month=1,int day=1);
Date(Date &r);
void Print();
};
实现:
Date::Date(int year,int month,int day)
{
this->year=year;
this->month=month;
this->day=day;
}
Date::Date(Date &r)
{
year=r.year;
month=r.month;
day=r.day;
}
void Date::Print()
{
cout<<year<<'-'<<month<<'-'<<day<<endl;
}
────────────────────────────────────
【实验七 · 第1题 · 题目原文】
────────────────────────────────────
在上面的工程中添加Person类,包括姓名name(字符数组,包括结束符最长20字符),性别gender(字符),生日birthday(Date类对象),人数统计变量count(静态),自行设计成员函数,保证满足以下测试要求(在main函数中调用Test1函数):
void Test1()
{
Person p1,p2("zhang",'f',Date(1991,1,1)),p3(p2);
p3.Print();//输出: 姓名:zhang;性别: 女;生日:1991-1-1;
cout<<"收支为:"<<p3.GetCashFlow()<<"元"<<endl; //输出:0元
Person *p=new Person("li",'m',Date(1990,2,2));//创建堆对象(调用有参构造)
p->Print();//输出堆对象基本信息(原书若写 p>Print 为笔误,应为 p->Print)
delete p;//释放堆对象
}
────────────────────────────────────
【实验七 · 第2题 · 题目原文】
────────────────────────────────────
从Person类以public方式派生Student类,添加学号stuId,就读时间(年)stuDuration每年学费tuition(固定为5000元),自行设计成员函数,要求满足以下测试要求:
void Test2()
{
Student s1,s2("zhang",'m',Date(1992,1,1),1001);
s2.Print(); //输出如下:
//姓名:zhang; 性别: 男; 生日:1992-1-1;
//学号:1001; 就读时间:0年
cout<<endl;
s2.SetDuration(2);
s2.Print();
//姓名:zhang; 性别: 男; 生日:1992-1-1;
//学号:1001; 就读时间:2年
cout<<endl<<"学费支出"<<s2.GetCashFlow()<<"元"<<endl;
//学费支出:10000元
Student *p=new Student; //堆对象创建(调用无参构造)
delete p;//释放堆对象
}
main函数如下:
void main(){
Test1();//测试Person类
Test2();//测试Student类
}
【参考代码 · 实验七合订(Date+Person+Student+Test1+Test2+main)】
#include <cstring>
#include <iostream>
using namespace std;
class Date {
private:
int year, month, day;
public:
Date(int y = 1990, int m = 1, int d = 1) : year(y), month(m), day(d) {}
Date(const Date& r) : year(r.year), month(r.month), day(r.day) {}
void Print() const { cout << year << '-' << month << '-' << day; }
};
class Person {
protected:
char name[21];
char gender;
Date birthday;
public:
Person() : gender('?') { name[0] = '\0'; birthday = Date(); }
Person(const char* n, char g, const Date& d) : gender(g), birthday(d) {
strncpy(name, n, 20); name[20] = '\0';
}
Person(const Person& o) : gender(o.gender), birthday(o.birthday) {
strncpy(name, o.name, 21);
}
virtual double GetCashFlow() const { return 0; }
virtual void Print() const {
cout << "姓名:" << name << ";性别:"
<< (gender == 'f' || gender == 'F' ? "女" : "男")
<< ";生日:";
birthday.Print();
cout << ";\n";
}
virtual ~Person() = default;
};
class Student : public Person {
private:
int stuId;
int stuDuration;
static const int tuition = 5000;
public:
Student() : stuId(0), stuDuration(0) {}
Student(const char* n, char g, const Date& d, int id)
: Person(n, g, d), stuId(id), stuDuration(0) {}
void SetDuration(int y) { stuDuration = y; }
double GetCashFlow() const override {
return 1.0 * tuition * stuDuration;
}
void Print() const override {
Person::Print();
cout << "学号:" << stuId << "; 就读时间:" << stuDuration << "年\n";
}
};
void Test1() {
Person p1;
Person p2("zhang", 'f', Date(1991, 1, 1));
Person p3(p2);
p3.Print();
cout << "收支为:" << p3.GetCashFlow() << "元" << endl;
Person* p = new Person("li", 'm', Date(1990, 2, 2));
p->Print();
delete p;
}
void Test2() {
Student s1;
Student s2("zhang", 'm', Date(1992, 1, 1), 1001);
s2.Print();
cout << endl;
s2.SetDuration(2);
s2.Print();
cout << endl << "学费支出" << s2.GetCashFlow() << "元" << endl;
Student* p = new Student;
delete p;
}
int main() {
Test1();
cout << endl;
Test2();
return 0;
}
════════════════════════════════════
实验八 多态(虚函数)
════════════════════════════════════
────────────────────────────────────
【实验八 · (1) · 题目原文】(选作)
────────────────────────────────────
已知类Number定义如下:(选作)
class Number{
int value;
public:
Number( int i=0 ){ value=i; }
// 其它成员函数,根据需要填写
};
为此类添加如下2个重载操作符函数,并在主函数中说明如何使用这2个重载操作符。
1)+= 完成2个Number类对象相加;
2)++和-- 完成Number类对象的自加和自减(注意:++和—有前置和后置两种)
【参考代码】
#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
explicit Number(int i = 0) : value(i) {}
int get() const { return value; }
Number& operator+=(const Number& b) {
value += b.value;
return *this;
}
Number& operator++() { ++value; return *this; }
Number operator++(int) { Number old(value); ++value; return old; }
Number& operator--() { --value; return *this; }
Number operator--(int) { Number old(value); --value; return old; }
};
int main() {
Number a(3), b(4);
a += b;
cout << a.get() << endl;
++a;
a++;
cout << a.get() << endl;
return 0;
}
────────────────────────────────────
【实验八 · (2) · 题目原文】(必做)
────────────────────────────────────
已知矩形类定义如下:
class Rect {
protected:
float width , length, area;
public:
Rect( ) { width = length = area = 0.0f ; }
Rect ( float w, float l ) {
width = w; length = l;
area = width * length;
}
virtual float getArea( ) { return area; }
float getLen( ) { return length; }
float getWidth( ){ return width; }
};
要求:
1)在Rect类基础之上公有派生出长方体类Cube,新增一个私有数据成员:高度height,并设计函数成员中getArea函数,覆盖Rect中的同名虚函数,用于计算长方体的表面积。
2)设计main函数,对以上的两个类进行测试,其中cube类中getArea函数要求使用基类Rect的指针通过赋值兼容规则进行调用。
【参考代码】
#include <iostream>
using namespace std;
class Rect {
protected:
float width, length, area;
public:
Rect() { width = length = area = 0.0f; }
Rect(float w, float l) {
width = w; length = l;
area = width * length;
}
virtual float getArea() { return area; }
float getLen() { return length; }
float getWidth() { return width; }
virtual ~Rect() = default;
};
class Cube : public Rect {
private:
float height;
public:
Cube(float w, float l, float h) : Rect(w, l), height(h) {}
float getArea() override {
return 2.0f * (width * length + width * height + length * height);
}
};
int main() {
Rect* p = new Cube(2, 3, 4);
cout << p->getArea() << endl;
delete p;
return 0;
}
