譚浩強(qiáng)《C++程序設(shè)計(jì)(第3版)》第11章習(xí)題9程序驗(yàn)證實(shí)驗(yàn)報(bào)告

實(shí)驗(yàn)題目:第11章習(xí)題9程序驗(yàn)證

一、實(shí)驗(yàn)?zāi)康?/h2>
  1. 上機(jī)驗(yàn)證《C++程序設(shè)計(jì)(第3版)》第11章習(xí)題9。
  2. 復(fù)習(xí)C++繼承與派生相關(guān)知識(shí)。

二、實(shí)驗(yàn)內(nèi)容

詳見(jiàn)一、實(shí)驗(yàn)?zāi)康?/code>。

三、設(shè)計(jì)和編碼

1. 本實(shí)驗(yàn)用到的理論知識(shí)

C++繼承與派生

2. 編碼

程序清單如下

// ex3.cpp
// Encoding: UTF-8

/*
 * 3.分別聲明Teacher(教師)類(lèi)和Cadre(干部)類(lèi),
 *   采用多重繼承方式由這兩個(gè)類(lèi)派生出新類(lèi)Teacher_Cadre(教師兼干部)類(lèi)。要求: 
 * (1) 在兩個(gè)基類(lèi)中都包含姓名、年齡、性別、地址、電話等數(shù)據(jù)成員。
 * (2) 在Teacher類(lèi)中還包含數(shù)據(jù)成員title(職稱(chēng)),在Cadre類(lèi)中還包含數(shù)據(jù)成員post(職務(wù))。
 *     在Teacher_Cadre類(lèi)中還包含數(shù)據(jù)成員wages(工資)。
 * (3) 對(duì)兩個(gè)基類(lèi)中的姓名、年齡、性別、地址、電話等數(shù)據(jù)成員用相同的名字,
 *     在引用這些數(shù)據(jù)成員時(shí),指定作用域。
 * (4) 在類(lèi)體中聲明成員函數(shù),在類(lèi)外定義成員函數(shù)。
 * (5) 在派生類(lèi)Teacher_Cadre的成員函數(shù)show中調(diào)用Teacher類(lèi)中的display函數(shù),
 *     輸出姓名、年齡、性別、職稱(chēng)、地址、電話,然后再用cout語(yǔ)句輸出職務(wù)與工資。
 */

#include <string>
#include<stdio.h>
#include <iostream>
using namespace std;
class Teacher
{
public:
    Teacher(string nam, int a, char s, string tit, string ad, string t);
    void display();

protected:
    string name;
    int age;
    char sex;
    string title;
    string addr;
    string tel;
};

Teacher::Teacher(string nam, int a, char s, string tit, string ad, string t) : name(nam), age(a), sex(s), title(tit), addr(ad), tel(t) {}
void Teacher::display()
{
    cout << "name:" << name << endl;
    cout << "age" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "title:" << title << endl;
    cout << "address:" << addr << endl;
    cout << "tel:" << tel << endl;
}

class Cadre
{
public:
    Cadre(string nam, int a, char s, string p, string ad, string t);
    void display();

protected:
    string name;
    int age;
    char sex;
    string post;
    string addr;
    string tel;
};

Cadre::Cadre(string nam, int a, char s, string p, string ad, string t) : name(nam), age(a), sex(s), post(p), addr(ad), tel(t) {}

void Cadre::display()
{
    cout << "name:" << name << endl;
    cout << "age:" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "post:" << post << endl;
    cout << "address:" << addr << endl;
    cout << "tel:" << tel << endl;
}

class Teacher_Cadre : public Teacher, public Cadre
{
public:
    Teacher_Cadre(string nam, int a, char s, string tit, string p, string ad, string t, float w);
    void show();

private:
    float wage;
};

Teacher_Cadre::Teacher_Cadre(string nam, int a, char s, string t, string p, string ad, string tel, float w) : Teacher(nam, a, s, t, ad, tel), Cadre(nam, a, s, p, ad, tel), wage(w) {}
void Teacher_Cadre::show()
{
    Teacher::display();
    cout << "post:" << Cadre::post << endl;
    cout << "wages:" << wage << endl;
}

int main()
{
    Teacher_Cadre te_ca("Wang-li", 50, 'f', "prof.", "president", "135 Beijing Road,Shanghai", "(021)61234567", 1534.5);
    te_ca.show();
    return 0;
}

四、運(yùn)行與測(cè)試

1. 測(cè)試環(huán)境

運(yùn)行環(huán)境:Windows 20H2, i7-9750H @ 2.60GHz, 16GB RAM

編譯器:gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

編譯命令:-g

運(yùn)行終端:cmd

2. 在調(diào)試程序的過(guò)程中遇到的問(wèn)題與解決方案

暫未發(fā)現(xiàn)異常。

3. 程序的運(yùn)行結(jié)果

運(yùn)行結(jié)果(符合預(yù)期)

D:\OneDrive - mail2.sysu.edu.cn\MyDocuments\code\DSA\week03>ex3
name:Wang-li
age50
sex:f
title:prof.
address:135 Beijing Road,Shanghai
tel:(021)61234567
post:president
wages:1534.5

六、總結(jié)與心得

繼承與派生是C++面向?qū)ο蟪绦蛟O(shè)計(jì)中重要知識(shí),不容小覷。

七、參考資料

  1. 譚浩強(qiáng). C++程序設(shè)計(jì)[M]. 3 版. 清華大學(xué)出版社, 2015.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容