任务3
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class MachinePets { 6 public: 7 MachinePets(const string& s); 8 MachinePets(); 9 string get_nickname() const; 10 virtual string talk() = 0; 11 12 protected: 13 string nickname; 14 }; 15 16 MachinePets::MachinePets(const string& s) : nickname(s) {} 17 18 MachinePets::MachinePets() : nickname("") {} 19 20 string MachinePets::get_nickname() const { 21 return nickname; 22 } 23 24 class PetCats : public MachinePets { 25 public: 26 PetCats(const string& s); 27 string talk() override; 28 }; 29 30 PetCats::PetCats(const string& s) : MachinePets(s) {} 31 32 string PetCats::talk() { 33 return "miao wu~"; 34 } 35 36 class PetDogs : public MachinePets { 37 public: 38 PetDogs(const string& s); 39 string talk() override; 40 }; 41 42 PetDogs::PetDogs(const string& s) : MachinePets(s) {} 43 44 string PetDogs::talk() { 45 return "wang wang~"; 46 }MachinePets.hpp
1 #include <iostream> 2 #include"machinepets.hpp" 3 void play(MachinePets & obj) { 4 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 5 6 } 7 8 void test() { 9 PetCats cat("miku"); 10 PetDogs dog("da huang"); 11 play(cat); 12 play(dog); 13 14 } 15 int main() { 16 test(); 17 18 }task3.cpp
运行结果:
任务4
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 6 class Person { 7 public: 8 Person(); 9 Person(string n, string t, string e = ""); 10 Person(const Person& p); 11 void update_telephone(); 12 void update_email(); 13 friend istream& operator>>(istream& is, Person& p); 14 friend ostream& operator<<(ostream& os, const Person& p); 15 friend bool operator==(const Person& a, const Person& b); 16 17 private: 18 string name; 19 string telephone; 20 string email; 21 }; 22 23 Person::Person() : name(""), telephone(""), email("") {} 24 25 Person::Person(string n, string t, string e) : name(n), telephone(t), email(e) {} 26 27 Person::Person(const Person& p) : name(p.name), telephone(p.telephone), email(p.email) {} 28 29 void Person::update_telephone() { 30 cout << "输入电话号码:"; 31 cin >> telephone; 32 cout << "电话号码已更新" << endl; 33 } 34 35 void Person::update_email() { 36 cout << "输入email地址:"; 37 cin >> email; 38 cout << "email地址已更新" << endl; 39 } 40 41 istream& operator>>(istream& is, Person& p) { 42 is >> p.name >> p.telephone >> p.email; 43 return is; 44 } 45 46 ostream& operator<<(ostream& os, const Person& p) { 47 os << p.name << " " << p.telephone << " " << p.email; 48 return os; 49 } 50 51 bool operator==(const Person& a, const Person& b) { 52 return (a.name == b.name); 53 }Person.hpp
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 }task4.cpp
运行结果:
任务5
1 #include"date.h" 2 #include"accumulator.h" 3 #include<string> 4 class Account 5 { 6 private: 7 std::string id; 8 double balance; 9 static double total; 10 protected: 11 Account(const Date& date, const std::string& id); 12 void record(const Date& date, double amount, const std::string& desc); 13 void error(const std::string& msg) const; 14 public: 15 const std::string& getId() const { return id; } 16 double getBalance() const { return balance; } 17 static double getToal() { return total; } 18 virtual void deposit(const Date& date, double amount, const std::string& desc) = 0; 19 virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0; 20 virtual void settle(const Date& date); 21 virtual void show() const; 22 }; 23 class SavingsAccount :public Account 24 { 25 private: 26 Accumulator acc; 27 double rate; 28 public: 29 SavingsAccount(const Date& date, const std::string& id, double rate); 30 double getRate() const { return rate; } 31 void deposit(const Date& date, double amount, const std::string& desc); 32 void withdraw(const Date& date, double amount, const std::string& desc); 33 void settle(const Date& date); 34 }; 35 class CreditAccount :public Account 36 { 37 private: 38 Accumulator acc; 39 double credit; 40 double rate; 41 double fee; 42 double getDebt() const 43 { 44 double balance = getBalance(); 45 return(balance < 0 ? balance : 0); 46 } 47 public: 48 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 49 double getCredit() const { return credit; } 50 double getRate() const { return rate; } 51 double getFee() const { return fee; } 52 double getAvailableCredit() const { 53 if (getBalance() < 0) 54 return credit + getBalance(); 55 else 56 return credit; 57 } 58 void deposit(const Date& date, double amount, const std::string& desc); 59 void withdraw(const Date& date, double amount, const std::string& desc); 60 void settle(const Date& date); 61 void show() const; 62 };account.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 double getSum(const Date& date) const 11 { 12 return sum + value * (date - lastDate); 13 } 14 void change(const Date& date, double value) 15 { 16 sum = getSum(date); 17 lastDate = date; this->value = value; sum = 0; 18 } 19 void reset(const Date& date, double value) 20 { 21 lastDate = date; this->value = value; 22 } 23 };Accumulator.h
1 class Date 2 { 3 private: 4 int year; 5 int month; 6 int day; 7 int totalDays; 8 public: 9 Date(int year, int month, int day); 10 int getYear() const { return year; } 11 int getMonth() const { return month; } 12 int getDay() const { return day; } 13 int getMaxDay() const; 14 bool isLeapYear() const 15 { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show() const; 19 int operator-(const Date& date) const 20 { 21 return totalDays - date.totalDays; 22 } 23 };date.h
include"account.h" #include<cmath> #include<iostream> using namespace std; double Account::total = 0; Account::Account(const Date& date, const std::string& id) :id(id), balance(0) { date.show(); cout << "\t#" << id << "created" << endl; } void Account::record(const Date& date, double amount, const string& desc) { amount = floor(amount * 100 + 0.5) / 100; balance += amount; total += amount; date.show(); cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; } void Account::show() const { cout << id << "\tBalance:" << balance; } void Account::error(const string& msg) const { cout << "Error(#" << id << "):" << msg << endl; } SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) { } void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { record(date, amount, desc); acc.change(date, getBalance()); } void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { if (amount > getBalance()) { error("not enough money"); } else { record(date, -amount, desc); acc.change(date, getBalance()); } } void SavingsAccount::settle(const Date& date) { if (date.getMonth() == 1) { double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); if (interest != 0) record(date, interest, "interest"); acc.reset(date, getBalance()); } } 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) { } void CreditAccount::deposit(const Date& date, double amount, const string& desc) { record(date, amount, desc); acc.change(date, getDebt()); } void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { if (amount - getBalance() > credit) { error("not enough credit"); } else { record(date, -amount, desc); acc.change(date, getDebt()); } } void CreditAccount::settle(const Date& date) { double interest = acc.getSum(date) * rate; if (interest != 0) record(date, interest, "interest"); if (date.getMonth() == 1) record(date, -fee, "annual fee"); acc.reset(date, getDebt()); } void CreditAccount::show() const { Account::show(); cout << "\tAvailable credit:" << getAvailableCredit(); }account.cpp
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace 6 { 7 const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 }; 8 } 9 Date::Date(int year, int month, int day) :year(year), month(month), day(day) 10 { 11 if (day <= 0 || day > getMaxDay()) 12 { 13 cout << "INvalid date:"; 14 show(); 15 cout << endl; 16 exit(1); 17 } 18 int years = year - 1; 19 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 20 if (isLeapYear() && month > 2) totalDays++; 21 } 22 int Date::getMaxDay() const 23 { 24 if (isLeapYear() && month == 2) 25 return 29; 26 else 27 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 28 } 29 void Date::show() const 30 { 31 cout << getYear() << "-" << getMonth() << "-" << getDay(); 32 }date.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 }main.cpp
运行结果:
标签:const,string,继承,double,void,多态,Date,实验,date From: https://www.cnblogs.com/8716ydc/p/17873737.html