首页 > 其他分享 >实验5 继承和多态

实验5 继承和多态

时间:2023-12-03 22:44:23浏览次数:25  
标签:std const string 继承 多态 Complex 实验 include cout

实验任务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

 

标签:std,const,string,继承,多态,Complex,实验,include,cout
From: https://www.cnblogs.com/le-sh/p/17858918.html

相关文章

  • 实验5
    实验任务31#pragmaonce2#include<string>3#include<iostream>45classMachinePets{6public:7MachinePets(conststd::string&s);8virtual~MachinePets()=default;910conststd::string&get_nickname()c......
  • 实验五
    实验1  功能是分别找到输入数中的最小值和最大值两者都指向x[0]功能是找到最大数,返回的是最大数的地址可以,因为改变之后返回的也是最大值的地址 实验2  s1大小是24个字节,sizeof以字节为单位返回一个变量,表达式或类型的长度,strlen是计算字符串长度的,不包含结尾......
  • 实验五 继承和多态
    Task3:pets.hpp:#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}conststringget_nickname(){returnnickname;}virtualstringta......
  • 深入理解C++继承(一)
    一、继承的概念及定义1.1继承的概念 继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前我们接触的复......
  • 实验5
    实验任务1:1.1:  1.功能:寻找到输入的多个数据中最大的和最小的数据2.指向该数组中的第一项 1.2:  1.返回的是该数组中最大数据的地址2.不行,算法表达不正确实验任务2:2.1: 问题1: 大小:24 计算:数组s1的大小统计:数组的数据字符串的长度 问题2:不行表达......
  • 实验5 继承和多态
    实验任务3pets.hpp #pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{private:stringnickname;public:MachinePets(conststrings):nickname{s}{}stringget_nickname()const{returnnicknam......
  • 实验五 继承和多态
    任务三pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststring&nickname="");stringgetNickname()const;virtualstringtalk()=0;prot......
  • 实验5 继承和多态
    实验任务3#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings);stringget_nickname()const;virtualstringtalk()=0;private:stringnickname;};......
  • 实验五 继承和多态
    实验任务三#include<iostream>#include<string>#pragmaonceusingnamespacestd;classMachinePets{private:stringnickname;public:MachinePets(conststring&s):nickname{s}{}stringget_nickname()const{returnnickname......
  • 实验报告2
    一、实验目的1.掌握软件开发的基本流程2.掌握常用的软件开发方式和工具。二、实验内容1.设计一个包含登录界面的计算器软件,该软件可以实现第一次作业中的功能,同时可以保存用户的历史计算记录(保存数据最好使用数据库)。三、实验所用环境及工具1.使用DW和IDEA......