实验5
任务3
pets
#include <iostream> #include <string> #pragma once using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string& s) : nickname{ s } {} string get_nickname() const { return nickname; } virtual string talk() = 0; }; class PetCats : public MachinePets { public: PetCats(const string& s) : MachinePets{ s } {} string talk() override { return "miao wu~"; } }; class PetDogs : public MachinePets { public: PetDogs(const string& s) : MachinePets(s) {} string talk() override { return "wang wang~"; } };
test
#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(); }

任务4
person
#include <iostream> #include <string> #include <vector> #include <limits> #pragma once using namespace std; class Person { private: string name; string telephone; string email; public: Person(const string& n, const string& t, const string& e = "") : name{ n }, telephone{ t }, email{ e } {} Person() : name(""), telephone(""), email("") {} Person(const Person& other) : name{ other.name }, telephone{ other.telephone }, email{ other.email } {} void update_telephone() { cin.clear(); cout << "输入电话号码:"; telephone.clear(); string Newtelephone; getline(cin, Newtelephone); this->telephone = Newtelephone; cout << "电话号码已更新..." << endl; } void update_email() { cout << "输入email地址:"; email.clear(); string Newemail; getline(cin, Newemail); this->email = Newemail; } friend ostream& operator<<(ostream& os, const Person& person) { os << "Name: " << person.name << "\nTelephone: " << person.telephone << "\nEmail: " << person.email << endl; return os; } friend istream& operator>>(istream& is, Person& person) { getline(is, person.name); getline(is, person.telephone); getline(is, person.email); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); return is; } friend bool operator==(const Person& P1, const Person& P2) { return (P1.name == P2.name) && (P1.telephone == P2.telephone); } };
test
#include<iostream> #include <iostream> #include <string> #include <vector> #include <limits> #include"person.hpp" using namespace std; void test() { vector<Person> phone_book; Person p; cout << "输入一组联系人的联系方式,直至按下Ctrl+Z终止\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"; if (phone_book.size() > 1) { cout << phone_book[0] << phone_book[1]; cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; } } int main() { test(); return
任务5
date
class Date { private: int year; int month; int day; int totalDays; public: Date(int year, int month, int day); int getYear() const { return year; } 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 { return totalDays - date.totalDays; } };
浙公网安备 33010602011771号