实验5 继承和多态

实验任务1

源代码:

#pragma once

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

// 发行/出版物类:Publisher (抽象类)
class Publisher {
public:
    Publisher(const string &s = "");            // 构造函数

public:
    virtual void publish() = 0;                 // 纯虚函数,作为接口继承

protected:
    string name;    // 发行/出版物名称
};

Publisher::Publisher(const string &s): name {s} {
}


// 图书类: Book
class Book: public Publisher {
public:
    Book(const string &s = "", const string &a = "");  // 构造函数

public:
    void publish() override;        // 接口
    void read();                    // 派生类新增接口

private:
    string author;          // 作者
};

Book::Book(const string &s, const string &a): Publisher{s}, author{a} {
}

void Book::publish() {
    cout << "Publishing book: " << name << " by " << author << endl;
}

void Book::read() {
    cout << "Reading book: " << name << " by " << author << endl;
}


// 电影类: Film
class Film: public Publisher {
public:
    Film(const string &s = "", const string &d = "");   // 构造函数

public:
    void publish() override;    // 接口
    void watch();               // 派生类新增接口

private:
    string director;        // 导演
};

Film::Film(const string &s, const string &d): Publisher{s}, director{s} {
}

void Film::publish() {
    cout << "Publishing film: " << name << " directed by " << director << endl;
}

void Film::watch() {
    cout << "Watching film: " << name << " directed by " << director << endl;
}


// 音乐类:Music
class Music: public Publisher {
public:
    Music(const string &s = "", const string &a = "");

public:
    void publish() override;        // 接口
    void listen();                  // 派生类新增接口

private:
    string artist;      // 音乐艺术家名称
};

Music::Music(const string &s, const string &a): Publisher{s}, artist{a} {
}

void Music::publish() {
    cout << "Publishing music " << name << " by " << artist << endl;
}

void Music::listen() {
    cout << "Listening to music: " << name << " by " << artist << endl;
}
publisher.hpp
#include "publisher.hpp"
#include <vector>
#include <typeinfo>

using std::vector;

void func(Publisher *ptr) {
    cout << "pointer type: " << typeid(ptr).name() << endl;
    cout << "RTTI type: " << typeid(*ptr).name() << endl;
    
    ptr->publish();
}

void test() {
    Publisher *ptr;

    cout << "测试1: \n";
    Book book{"Harry Potter", "J.K. Rowling"};
    ptr = &book;
    func(ptr);
    book.read();

    cout << "\n测试2: \n";
    Film film{"The Godfather", "Francis Ford Coppola"};
    ptr = &film;
    func(ptr);
    film.watch();

    cout << "\n测试3: \n";
    Music music{"Blowing in the wind", "Bob Dylan"};
    ptr = &music;
    func(ptr);
    music.listen();
}

int main() {
    test();
}
task1.cpp

运行结果:

 

实验任务2

源代码:

#pragma once

#include <iostream>

class Complex {
public:
    Complex(double r = 0, double i = 0): real{r}, imag{i} {}
    Complex(const Complex &c): real{c.real}, imag{c.imag} {}
    ~Complex() = default;

public:
    double get_real() const { return real; }
    double get_imag() const { return imag; }

    Complex& operator+=(const Complex &c);

    friend Complex operator+(const Complex &c1, const Complex &c2);
    friend bool operator==(const Complex &c1, const Complex &c2);
    
    friend std::ostream& operator<<(std::ostream &os, const Complex &c);
    friend std::istream& operator>>(std::istream &is, Complex &c);

private:
    double real, imag;
};

//=====================成员函数=================
// 重载运算符+=为成员函数, 支持c1 += c2这样的操作
Complex& Complex::operator+=(const Complex &c) {
    real += c.real;
    imag += c.imag;

    return *this;
}

// =====================普通函数=====================
// 重载运算符+为普通函数,支持c1 + c2这样的操作
Complex operator+(const Complex &c1, const Complex &c2) {
    return Complex(c1.get_real() + c2.get_real(),
                   c1.get_imag() + c2.get_imag());
}

// 重载运算符==为普通函数,支持c1 == c2这样的操作
bool operator==(const Complex &c1, const Complex &c2) {
    return (c1.get_real() == c2.get_real() && c1.get_imag() == c2.get_imag());
}

// =====================友元函数=====================
// 重载插入运算符<<为友元函数,支持类似cout << c1 << c2这样的操作
std::ostream& operator<<(std::ostream &os, const Complex &c) {
    if(c.imag >= 0)
        os << c.real << " + " << c.imag << "i";
    else
        os << c.real << " - " << -c.imag << "i";

    return os;
} 

// 重载提取运算符>>为友元函数,支持类似cin >> c1 >> c2这样的操作
std::istream& operator>>(std::istream &is, Complex &c) {
    is >> c.real >> c.imag;

    return is;
}
complex.hpp
#include <iostream>
#include "complex.hpp"

void test() {
    using namespace std;

    Complex c1;
    cout << "Enter c1: ";
    cin >> c1;
    cout << "c1 = " << c1 << endl;

    Complex c2{1.5}, c3{1.5, 3}, c4{c3};
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c4 = " << c4 << endl;
    cout << "c3 == c4: " << boolalpha << (c3 == c4) << endl;

    cout << "c1 + c2 = " << c1 + c2 << endl;
    c1 += c2;
    cout << "c1 = " << c1 << endl;
    cout << c1.get_real() << " " << c1.get_imag() << endl;
}

int main() {
    test();
}
task2.cpp

运行结果:

 

实验任务3

源代码:

#include "pets.hpp"

using namespace std;

MachinePets::MachinePets(const string s) : nickname(s) {}

const string MachinePets::get_nickname() const {
    return nickname;
}

PetCats::PetCats(const string s) : MachinePets(s) {}

string PetCats::talk() const {
    return "miao WU~";
}

PetDogs::PetDogs(const string s) : MachinePets(s) {}

string PetDogs::talk() const {
    return "wang wang~";
}
pets.cpp
#ifndef PETS_HPP
#define PETS_HPP

#include <string>

using namespace std;

class MachinePets {
    private:
        string nickname;

    public:
        MachinePets(const string s);
        const string get_nickname() const;
        virtual string talk() const = 0;
};

class PetCats : public MachinePets {
    public:
        PetCats(const string s);
        string talk() const override;
};

class PetDogs : public MachinePets {
    public:
        PetDogs(const string s);
        string talk() const override;
};

#endif
pets.hpp
#include <iostream>
#include "pets.hpp"

void play(MachinePets &obj) {
    std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
}

void test() {
    PetCats cat("miku");
    PetDogs dog("da huang");

    play( cat );
    play( dog );
}

int main() {
    test();
}
task3.cpp

运行结果:

 

实验任务4

源代码:

#include <iostream>
#include <string>

class Person {
private:
    std::string name;
    std::string telephone;
    std::string email;

public:
    Person() : name(""), telephone(""), email("") {}
    Person(const std::string& n, const std::string& t, const std::string& e = "")
        : name(n), telephone(t), email(e) {}
    Person(const Person& other) : name(other.name), telephone(other.telephone), email(other.email) {}

    void update_telephone() {
        std::string new_telephone;
        std::cout << "输入新的手机号码: ";
        std::cin >> new_telephone;
        telephone = new_telephone;  
    }

    void update_email() {
        std::cout << "输入新的邮箱地址: ";
        std::cin >> email;
    }

    friend std::ostream& operator<<(std::ostream& os, const Person& p);
    friend std::istream& operator>>(std::istream& is, Person& p);
    friend bool operator==(const Person& p1, const Person& p2);
};

std::ostream& operator<<(std::ostream& os, const Person& p) {
    os << "姓名: " << p.name << "\n手机号码: " << p.telephone << "\n邮箱: " << p.email;
    return os;
}

std::istream& operator>>(std::istream& is, Person& p) {
    std::cout << "输入姓名: ";
    is >> p.name;
    std::cout << "输入手机号码: ";
    is >> p.telephone;
    std::cout << "输入邮箱地址: ";
    is >> p.email;
    return is;
}

bool operator==(const Person& p1, const Person& p2) {
    return (p1.name == p2.name) && (p1.telephone == p2.telephone);
}
Person.hpp
#include <iostream>
#include <vector>
#include "Person.hpp"

void test() {
    using namespace std;

    vector<Person> phone_book;
    Person p;

    cout << "输入一组联系人的联系方式,输入 'E' 终止\n";
    while (cin >> p)
        phone_book.push_back(p);

    cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n";
    phone_book.at(0).update_telephone();
    phone_book.at(0).update_email();

    cout << "\n测试两个联系人是否是同一个:\n";
    cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;

    cout << "\n联系人信息:\n";
    for (const auto& person : phone_book) {
        cout << person << "\n\n";
    }
}

int main() {
    test();
    return 0;
}
task4.cpp

运行结果:

 

实验任务5

源代码:

#ifndef __DATE_H  〃年
# define DATE_H
class Date {
    private:
        int year;
        int month;
        int day;
        int totalDays;
    public:
        Date (int year, int month, int day);
        int getMonth () const {
            return month;
        }

        int getDay() const {
            return day;
        }
        int getMaxDay() const;
        bool isLeapYear() const {
            return year%4==0 && year%100 != 0 || year%400==0;
        }
        void show() const;
        int operator- (const Date& date) const {
        }
};
#endif
date.h

 

posted @ 2023-12-03 22:38  LE_SH  阅读(13)  评论(0)    收藏  举报