当前位置: 首页 > news >正文

继承虚函数

实验七 继承 + 实验八 多态(完整:全部题目与参考代码)

════════════════════════════════════

实验七 继承

════════════════════════════════════

【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;

}

http://www.jsqmd.com/news/804690/

相关文章:

  • 日本电子产业转型启示:从技术过剩到商业模式创新
  • 宿主机切分“小鸡”全攻略:KVM、LXC、Docker到底怎么选?
  • Windows 10 PL2303驱动修复终极指南:3种方案解决串口设备兼容性问题
  • OpenClaw从入门到应用——工具(Tools):diff
  • FCC新规下电子产品入美测试合规指南:供应链安全与应对策略
  • 【力扣100题】22. 矩阵置零
  • 3分钟掌握Krita AI抠图:点一下就能完成的智能选区革命
  • 深入解析干扰素-γ(IFN-γ):宿主防御机制与治疗潜力新洞察
  • 拾亩绿光纯亚麻籽微粉效果怎么样
  • OpenClaw从入门到应用——工具(Tools):
  • 智飞生物:一家代理巨擘的百亿亏损与“只剩渣”的投资者残局
  • 【Google搜索增强黄金窗口期】:错过这波Gemini API权限开放,你将落后至少6个月开发节奏
  • FastGithub终极指南:5分钟让你的GitHub访问速度提升300%
  • 基于NestJS的智能代码评审代理:从AST解析到规则引擎实践
  • 【DeepSeek开发者垂直搜索实战指南】:3大行业落地案例+5个避坑要点,限时公开内部调优参数
  • 别再手动算脉冲了!STM32CubeMX配置定时器编码器模式,轻松读取直流电机转速(附防溢出处理代码)
  • 免费开源AI软件.桌面单机版,可移动的AI知识库,察元 AI桌面版:全模型支持的第一个例子 给察元AI挂上Ollama的下午
  • 如何在Windows电脑上安装安卓应用?APK Installer完整指南
  • 计算机毕业设计 | SpringBoot+vue高校教师电子名片系统(附源码+论文)
  • 厚街婚纱摄影哪家值得推荐:秒杀婚纱摄影全城优选 - 17322238651
  • ImageToSTL:让每一张照片都拥有立体的生命
  • 别再傻傻分不清了!一文搞懂L2范数、欧氏距离和正则化的前世今生
  • 厚街婚礼策划哪家值得推荐:秒杀婚礼策划梦幻缔造 - 13425704091
  • IGFBP-3:出生后血液中关键IGF结合蛋白的生物学功能与临床应用价值
  • 百度文库文档免费下载终极指南:3步快速获取纯净PDF
  • 大连导游依依:七年零投诉背后的职业坚守
  • 如何免费重置JetBrains IDE试用期:完整操作指南
  • FanControl.HWInfo插件终极指南:如何实现精准硬件温度监控与风扇控制
  • AutoDock-Vina完全使用手册:从零开始掌握分子对接技术
  • Perplexity接入JSTOR古籍库的5大隐藏限制:92%研究者至今不知的权限陷阱与绕过方案