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

实验5 继承和多态

时间:2023-12-03 21:24:46浏览次数:24  
标签:std const string 继承 double 多态 Date 实验 date

实验任务3

pets.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using std::cout;
 7 using std::endl;
 8 using std::string;
 9 
10 class MachinePets{
11 protected:
12     string nickname;
13 public:
14     MachinePets(const string s);
15 public:
16     virtual string talk() = 0;
17     string get_nickname() const;
18 };
19 
20 MachinePets::MachinePets(const string s): nickname{s} {
21 }
22 
23 string MachinePets::get_nickname() const {
24     return nickname;
25 }
26 
27 class PetCats: public MachinePets{
28 public:
29     PetCats(const string s);
30 public:
31     string talk();    
32 };
33 
34 PetCats::PetCats(const string s): MachinePets{s} {
35 }
36 
37 string PetCats::talk() {
38     return "miao wu~";
39 }
40 
41 class PetDogs: public MachinePets{
42 public:
43     PetDogs(const string s);
44 public:
45     string talk();    
46 };
47 
48 PetDogs::PetDogs(const string s): MachinePets{s} {
49 }
50 
51 string PetDogs::talk() {
52     return "wang wang~";
53 }
View Code

task3.cpp

 1 #include <iostream>
 2 #include "pets.hpp"
 3 
 4 void play(MachinePets &obj) {
 5     std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
 6 }
 7 
 8 void test() {
 9     PetCats cat("miku");
10     PetDogs dog("da huang");
11 
12     play( cat );
13     play( dog );
14 }
15 
16 int main() {
17     test();
18 }
View Code

运行测试结果

 实验任务4

Person.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 class Person{
 9 private:
10     string name,telephone,email; 
11 public:
12     Person();
13     Person(const string &name, const string &telephone, const string &email);
14     Person(const Person &p);
15 
16     void update_telephone();
17     void update_email();
18 
19     friend std::ostream& operator<<(std::ostream &os, const Person &p);
20     friend std::istream& operator>>(std::istream &is, Person &p);
21     friend bool operator==(const Person &p1, const Person &p2);
22 };
23 
24 Person::Person() {
25 }
26 
27 Person::Person(const string &name, const string &telephone, const string &email): name{name}, telephone{telephone}, email{email} {
28 }
29 
30 Person::Person(const Person &p): name{p.name}, telephone{p.telephone}, email{p.email} {
31 }
32 
33 std::ostream& operator<<(std::ostream &os, const Person &p) {
34     os << p.name << endl;
35     os << p.telephone << endl;
36     os << p.email << endl;
37     return os;
38 }
39 
40 std::istream& operator>>(std::istream &is, Person &p) {
41     is >> p.name >> p.telephone >> p.email;
42     cout << endl;
43     return is;
44 }
45 
46 bool operator==(const Person &p1, const Person &p2) {
47     if(p1.name == p2.name && p1.telephone == p2.telephone && p1.email == p2.email)
48         return 1;
49     else 
50         return 0;
51 }
52 
53 void Person::update_telephone() {
54     cin.clear();
55     cout << "输入电话号码:";
56     string t;
57     getline(cin,t);
58     telephone = t;
59     cout << "电话号码已更新..." << endl;
60 }
61 
62 void Person::update_email() {
63     cin.clear();
64     cout << "输入email地址:";
65     string e;
66     getline(cin,e);
67     email = e;
68     cout << "email地址已更新..." << endl;
69 }
View Code

task4.cpp

 1 #include <iostream>
 2 #include <vector>
 3 #include "Person.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<Person> phone_book;
 9     Person p;
10 
11     cout << "输入一组联系人的联系方式,E直至按下Ctrl+Z终止\n";
12     while(cin >> p) 
13         phone_book.push_back(p);
14     
15     cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n";
16     phone_book.at(0).update_telephone();
17     phone_book.at(0).update_email();
18 
19     cout << "\n测试两个联系人是否是同一个:\n";
20     cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
21 }
22 
23 int main() {
24     test();
25 }
View Code

运行测试结果

 实验任务5

 date.h

 1 class Date {
 2 private:
 3     int year;
 4     int month;
 5     int day;
 6     int totalDays;
 7 public:
 8     Date(int year, int month, int day);
 9     int getYear() const { return year; }
10     int getMonth() const { return month; }
11     int getDay() const { return day; }
12     int getMaxDay() const;
13     bool isLeapYear() const {
14         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
15     }
16     void show() const;
17     int operator-(const Date& date) const {
18         return totalDays - date.totalDays;
19     }
20 };
View Code

date.cpp

 1 #include"date.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 
 5 using namespace std;
 6 
 7 namespace
 8 {
 9     const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
10 }
11 
12 Date::Date(int year, int month, int day) :year(year), month(month), day(day)
13 {
14     if (day <= 0 || day > getMaxDay())
15     {
16         cout << "INvalid date:";
17         show();
18         cout << endl;
19         exit(1);
20     }
21     int years = year - 1;
22     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
23     if (isLeapYear() && month > 2) totalDays++;
24 }
25 
26 int Date::getMaxDay() const
27 {
28     if (isLeapYear() && month == 2)
29         return 29;
30     else
31         return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
32 }
33 
34 void Date::show() const
35 {
36     cout << getYear() << "-" << getMonth() << "-" << getDay();
37 }
View Code

accumulator.h

 1 #include"date.h"
 2 class Accumulator
 3 {
 4 private:
 5     Date lastDate;
 6     double value;
 7     double sum;
 8 public:
 9     Accumulator(const Date& date, double value) :lastDate(date), value(value), sum(0) {}
10     
11     double getSum(const Date& date) const {
12         return sum + value * (date - lastDate);
13     }
14     
15     void change(const Date& date, double value) {
16         sum = getSum(date);
17         lastDate = date; this->value = value; sum = 0;
18     }
19     
20     void reset(const Date& date, double value) {
21         lastDate = date; this->value = value;
22     }
23 };
View Code

account.h

 1 #include"date.h"
 2 #include"accumulator.h"
 3 #include<string>
 4 
 5 class Account
 6 {
 7 private:
 8     std::string id;
 9     double balance;
10     static double total;
11 protected:
12     Account(const Date& date, const std::string& id);
13     void record(const Date& date, double amount, const std::string& desc);
14     void error(const std::string& msg) const;
15 public:
16     const std::string& getId() const { return id; }
17     double getBalance() const { return balance; }
18     static double getToal() { return total; }
19     virtual void deposit(const Date& date, double amount, const std::string& desc) = 0;
20     virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0;
21     virtual void settle(const Date& date);
22     virtual void show() const;
23 };
24 
25 class SavingsAccount :public Account
26 {
27 private:
28     Accumulator acc;
29     double rate;
30 public:
31     SavingsAccount(const Date& date, const std::string& id, double rate);
32     double getRate() const { return rate; }
33     void deposit(const Date& date, double amount, const std::string& desc);
34     void withdraw(const Date& date, double amount, const std::string& desc);
35     void settle(const Date& date);
36 };
37 
38 class CreditAccount :public Account
39 {
40 private:
41     Accumulator acc;
42     double credit;
43     double rate;
44     double fee;
45     double getDebt() const
46     {
47         double balance = getBalance();
48         return(balance < 0 ? balance : 0);
49     }
50 public:
51     CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee);
52     double getCredit() const { return credit; }
53     double getRate() const { return rate; }
54     double getFee() const { return fee; }
55     double getAvailableCredit() const {
56         if (getBalance() < 0)
57             return credit + getBalance();
58         else
59             return credit;
60     }
61     void deposit(const Date& date, double amount, const std::string& desc);
62     void withdraw(const Date& date, double amount, const std::string& desc);
63     void settle(const Date& date);
64     void show() const;
65 };
View Code

account.cpp

 1 #include"account.h"
 2 #include<cmath>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 double Account::total = 0;
 8 
 9 Account::Account(const Date& date, const std::string& id) :id(id), balance(0)
10 {
11     date.show(); cout << "\t#" << id << "created" << endl;
12 }
13 void Account::record(const Date& date, double amount, const string& desc)
14 {
15     amount = floor(amount * 100 + 0.5) / 100;
16     balance += amount; total += amount;
17     date.show();
18     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
19 }
20 void Account::show() const { cout << id << "\tBalance:" << balance; }
21 void Account::error(const string& msg) const
22 {
23     cout << "Error(#" << id << "):" << msg << endl;
24 }
25 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {
26 }
27 void SavingsAccount::deposit(const Date& date, double amount, const string& desc)
28 {
29     record(date, amount, desc);
30     acc.change(date, getBalance());
31 }
32 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc)
33 {
34     if (amount > getBalance())
35     {
36         error("not enough money");
37     }
38     else
39     {
40         record(date, -amount, desc);
41         acc.change(date, getBalance());
42     }
43 }
44 void SavingsAccount::settle(const Date& date)
45 {
46     if (date.getMonth() == 1)
47     {
48         double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1));
49         if (interest != 0) record(date, interest, "interest");
50         acc.reset(date, getBalance());
51     }
52 }
53 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) :Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {
54 }
55 void CreditAccount::deposit(const Date& date, double amount, const string& desc)
56 {
57     record(date, amount, desc);
58     acc.change(date, getDebt());
59 }
60 void CreditAccount::withdraw(const Date& date, double amount, const string& desc)
61 {
62     if (amount - getBalance() > credit)
63     {
64         error("not enough credit");
65     }
66     else
67     {
68         record(date, -amount, desc);
69         acc.change(date, getDebt());
70     }
71 }
72 void CreditAccount::settle(const Date& date)
73 {
74     double interest = acc.getSum(date) * rate;
75     if (interest != 0) record(date, interest, "interest");
76     if (date.getMonth() == 1) record(date, -fee, "annual fee");
77     acc.reset(date, getDebt());
78 }
79 void CreditAccount::show() const
80 {
81     Account::show();
82     cout << "\tAvailable credit:" << getAvailableCredit();
83 }
View Code

8.8.cpp

 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main()
 5 {
 6     Date date(2008, 11, 1);
 7     SavingsAccount sa1(date, "s3755217", 0.015);
 8     SavingsAccount sa2(date, "02342342", 0.015);
 9     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
10     Account* accounts[] = { &sa1,&sa2,&ca };
11     const int n = sizeof(accounts) / sizeof(Account*);
12     cout << "(d)deposit(w)withdraw(s)show(c)change day(n) next month(e) exit" << endl;
13     char cmd;
14     do {
15         date.show();
16         cout << "\tTotal:" << Account::getToal() << "\tcommand";
17         int index, day;
18         double amount; string desc;
19         cin >> cmd;
20         switch (cmd)
21         {
22         case'd':
23             cin >> index >> amount;
24             getline(cin, desc);
25             accounts[index]->deposit(date, amount, desc);
26             break;
27         case'w':
28             cin >> index >> amount;
29             getline(cin, desc);
30             accounts[index]->withdraw(date, amount, desc);
31             break;
32         case's':
33             for (int i = 0; i < n; i++)
34             {
35                 cout << "[" << i << "]";
36                 accounts[i]->show();
37                 cout << endl;
38             }
39             break;
40         case'c':
41             cin >> day;
42             if (day << date.getDay())
43                 cout << "You cannot specify a previous day";
44             else if (day > date.getMaxDay())
45                 cout << "Invalid day";
46             else
47                 date = Date(date.getYear(), date.getMonth(), day);
48             break;
49         case'n':
50             if (date.getMonth() == 12)
51                 date = Date(date.getYear() + 1, 1, 1);
52             else
53                 date = Date(date.getYear(), date.getMonth() + 1, 1);
54             for (int i = 0; i < n; i++)
55                 accounts[i]->settle(date);
56             break;
57         }
58     } while (cmd != 'e');
59     return 0;
60 }
View Code

运行测试结果

 

标签:std,const,string,继承,double,多态,Date,实验,date
From: https://www.cnblogs.com/rssn/p/17858958.html

相关文章

  • 实验5 继承和多态
    任务31#include<iostream>2#include<string>3usingnamespacestd;45classMachinePets{6public:7MachinePets(conststring&s);8MachinePets();9stringget_nickname()const;10virtualstringtalk()=0;......
  • 实验五 继承和多态
    实验任务3:1.代码:pets.hpp:1pragmaonce23#include<iostream>4#include<string>5usingstd::cout;6usingstd::cin;7usingstd::endl;8usingstd::string;910classMachinePets{11private:12stringnickname;13......
  • 实验5 继承和多态
    实验任务3pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}conststringget_nickname(){returnnickname;}virtu......
  • 实验5 继承和多态
    实验任务1publisher.hpp#pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;classPublisher{public:Publisher(conststring&s="");public:virtualvoidpublish()=......
  • 实验五-继承和多态
    pets.hpp1#ifndefPETS_HPP2#definePETS_HPP34#include<iostream>5#include<string>67classMachinePets{8protected:9std::stringnickname;10public:11MachinePets(conststd::strings):nickname(s){}12......
  • 实验5 ———
    1.1找到最大值和最小值数组x的第一个元素x[0]的值1.2最大值所在元素的地址不,地址不能交换 2.11__23   sizeof(s1)计算的是数组s1在内存中所占用的字节数strlen(s1)统计的是字符串s1中的字符数2__不能  未分配地址3__能2.21__s1不再是一个数组,而......
  • 实验5
    任务5:1#ifndef__DATE_H__2#define__DATE_H__3classDate{4private:5intyear;6intmonth;7intday;8inttotalDays;9public:10Date(intyear,intmonth,intyear);11......
  • 实验5 继承和多态
    四、实验结论实验任务3pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststring&s="");stringget_nickname()const;public:virtualstringtalk()......
  • 实验5
    #include<iostream>#include<string>usingnamespacestd;classMachinePets{private:stringnickname;public:MachinePets(conststrings):nickname{s}{}stringget_nickname()const{returnnickname;}virtualstringtalk()......
  • 实验五
     找出最大最小值 指向x【0】的地址 返回最大值的地址  sizeof计算数据类型所占据的空间,比如字节长度,而strlen计算字符串的长度,从第一个字符遇到结束符停止。sizeof包含“\0”,strlen则不计算。     #defineN80voidencoder(char*str);//函数声......