1 hpp 2 #pragma once 3 #include<iostream> 4 using namespace std; 5 class MachinePets { 6 private: 7 string nickname; 8 public: 9 MachinePets(const string &s); 10 virtual string talk() const = 0; 11 string get_nickname() const; 12 }; 13 MachinePets::MachinePets(const string& s) :nickname{ s }{} 14 string MachinePets::get_nickname()const { 15 return nickname; 16 } 17 class PetCats :public MachinePets { 18 public: 19 PetCats(const string& s); 20 string talk()const override; 21 }; 22 PetCats::PetCats(const string& s) :MachinePets{ s } { 23 } 24 string PetCats::talk()const { 25 return "miao wu~"; 26 } 27 class PetDogs :public MachinePets { 28 public: 29 PetDogs(const string& s); 30 string talk()const override; 31 }; 32 PetDogs::PetDogs(const string &s):MachinePets{s}{} 33 string PetDogs::talk()const { 34 return "wang wang ~"; 35 } 36 37 38 39 cpp 40 #include <iostream> 41 #include <vector> 42 #include "pets.hpp" 43 void test() { 44 using namespace std; 45 vector<MachinePets*> pets; 46 pets.push_back(new PetCats("miku")); 47 pets.push_back(new PetDogs("da huang")); 48 for (auto& ptr : pets) 49 cout << ptr->get_nickname() << " says " << ptr->talk() << endl; 50 } 51 int main() { 52 test(); 53 }
1 hpp 2 #pragma once 3 #include<iostream> 4 #include<iomanip> 5 using namespace std; 6 class film { 7 private: 8 string name, director, area, time; 9 public: 10 film(const string& name="", const string& director="", const string& area="", const string& time=""); 11 friend ostream& operator<<(ostream& out, const film& f); 12 friend istream& operator>>(istream& in, film& f); 13 string get_year()const; 14 }; 15 film::film(const string& name, const string& director, const string& area, const string& time) { 16 this->name = name; 17 this->director = director; 18 this->area = area; 19 this->time = time; 20 } 21 string film::get_year()const { 22 return time; 23 } 24 ostream& operator<<(ostream& out, const film& f) { 25 out << f.name<< setw(15) << f.director <<setw(10) << f.area << setw(10) << f.time << endl; 26 return out; 27 } 28 istream& operator>>(istream& in,film& f) { 29 in >> f.name >> f.director >> f.area >> f.time; 30 return in; 31 } 32 bool compare_by_year(const film& f1, const film& f2) { 33 return f1.get_year() < f2.get_year(); 34 } 35 36 37 38 cpp 39 #include "film.hpp" 40 #include <iostream> 41 #include <string> 42 #include <vector> 43 #include <algorithm> 44 void test() { 45 using namespace std; 46 47 int n; 48 cout << "輸入電影數目: "; 49 cin >> n; 50 cout << "錄入" << n << "部影片資訊" << endl; 51 vector<film> film_lst; 52 for (int i = 0; i < n; ++i) { 53 film f; 54 cout << string(20, '-') << "第" << i + 1 << "部影片錄入" << string(20, 55 '-') << endl; 56 cin >> f; 57 film_lst.push_back(f); 58 } 59 // 按發行年份升序排序 60 sort(film_lst.begin(), film_lst.end(), compare_by_year); 61 cout << string(20, '=') + "電影資訊(按發行年份)" + string(20, '=') << endl; 62 for (auto& f : film_lst) 63 cout << f << endl; 64 } 65 int main() { 66 test(); 67 }
1 hpp 2 #pragma once 3 #include <iostream> 4 using namespace std; 5 template <typename T> 6 class Complex { 7 public: 8 Complex(const T& r = 0, const T& i = 0) : real{ r }, imag{ i } {} 9 Complex operator+=(const Complex& c) { 10 real += c.real; 11 imag += c.imag; 12 return *this; 13 } 14 friend Complex operator+(const Complex& c1, const Complex& c2) { 15 return Complex<T>(c1.real + c2.real, c1.imag + c2.imag); 16 } 17 friend bool operator==(const Complex& c1, const Complex& c2) { 18 return c1.real == c2.real && c1.imag == c2.imag; 19 } 20 friend ostream& operator<<(ostream& out, const Complex& c) { 21 if (c.imag >= 0) 22 out << c.real << "+" << c.imag << "i"; 23 else 24 out << c.real << "-" << -c.imag << "i"; 25 return out; 26 } 27 friend istream& operator>>(istream& in, Complex& c) { 28 in >> c.real >> c.imag; 29 return in; 30 } 31 T get_real() const { 32 return real; 33 } 34 T get_imag() const { 35 return imag; 36 } 37 38 private: 39 T real; 40 T imag; 41 }; 42 43 44 45 cpp 46 #include "Complex.hpp" 47 #include <iostream> 48 using std::cin; 49 using std::cout; 50 using std::endl; 51 using std::boolalpha; 52 void test1() { 53 Complex<int> c1(2, -5), c2(c1); 54 cout << "c1 = " << c1 << endl; 55 cout << "c2 = " << c2 << endl; 56 cout << "c1 + c2 = " << c1 + c2 << endl; 57 58 c1 += c2; 59 cout << "c1 = " << c1 << endl; 60 cout << boolalpha << (c1 == c2) << endl; 61 } 62 void test2() { 63 Complex<double> c1, c2; 64 cout << "Enter c1 and c2: "; 65 cin >> c1 >> c2; 66 cout << "c1 = " << c1 << endl; 67 cout << "c2 = " << c2 << endl; 68 cout << "c1.real = " << c1.get_real() << endl; 69 cout << "c1.imag = " << c1.get_imag() << endl; 70 } 71 int main() { 72 cout << "自定義類别範本Complex測試1: " << endl; 73 test1(); 74 cout << endl; 75 cout << "自定義類别範本Complex測試2: " << endl; 76 test2(); 77 }
1 date.h 2 #pragma once 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 public: 10 Date(int year, int month, int day); 11 int getYear()const { return year; } 12 int getMonth()const { return month; } 13 int getDay()const { return day; } 14 int getMaxDay()const; 15 bool isLeapYear()const { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show()const; 19 int operator-(const Date& date)const { 20 return totalDays - date.totalDays; 21 } 22 }; 23 24 25 accmulator.h 26 #pragma once 27 #include"date.h" 28 class Accumulator { 29 private: 30 Date lastDate; 31 double value; 32 double sum; 33 public: 34 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {} 35 double getSum(const Date& date) const{ 36 return sum + value * (date - lastDate); 37 } 38 void change(const Date& date, double value) { 39 sum = getSum(date); 40 lastDate = date; 41 this->value = value; 42 } 43 void reset(const Date& date, double value) { 44 lastDate = date; 45 this->value; 46 sum = 0; 47 } 48 }; 49 50 account.h 51 52 #pragma once 53 #include"date.h" 54 #include"accumulator.h" 55 #include<string> 56 using namespace std; 57 class Account { 58 private: 59 string id; 60 double balance; 61 static double total; 62 protected: 63 Account(const Date& date, const string &id); 64 void record(const Date& date, double amoount, const string& desc); 65 void error(const string& msg) const; 66 public:const string& getId() { return id; } 67 double getBalance()const { return balance; } 68 static double getTotal() { return total; } 69 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 70 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 71 virtual void settle(const Date& date) = 0; 72 virtual void show()const; 73 }; 74 class SavingsAccount :public Account { 75 private: 76 Accumulator acc; 77 double rate; 78 public: 79 SavingsAccount(const Date& date, const string& id, double rate); 80 double getRate() const { return rate; } 81 void deposit(const Date& date, double amount, const string& desc); 82 void withdraw(const Date& date, double amount, const string& desc); 83 void settle(const Date& date); 84 }; 85 class CreditAccount :public Account { 86 private: 87 Accumulator acc; 88 double credit; 89 double rate; 90 double fee; 91 double getDebt()const { 92 double balance = getBalance(); 93 return(balance < 0 ? balance : 0); 94 } 95 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 96 double getCredit()const { return credit; } 97 double getRate()const { return rate; } 98 double getFee() const { return fee; } 99 double getAvailableCredit()const { 100 if (getBalance() < 0)return credit + getBalance(); 101 else return credit; 102 } 103 void deposit(const Date& date, double amount, const string& desc); 104 void withdraw(const Date& date, double amount, const string& desc); 105 void settle(const Date& date); 106 void show()const; 107 }; 108 109 110 date.cpp 111 #include"date.h" 112 #include<iostream> 113 #include<cstdlib> 114 using namespace std; 115 namespace { 116 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 117 } 118 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 119 if (day <= 0 || day > getMaxDay()) { 120 cout << "Invalid date: "; 121 show(); 122 cout << endl; 123 exit(1); 124 } 125 int years = year - 1; 126 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 127 if (isLeapYear() && month > 2) totalDays++; 128 } 129 int Date::getMaxDay()const { 130 if (isLeapYear() && month == 2) 131 return 29; 132 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 133 } 134 void Date::show()const { 135 cout << getYear() << "-" << getMonth() << "-" << getDay(); 136 } 137 138 139 account.cpp 140 #include "account.h" 141 #include <cmath> 142 #include<iostream> 143 using namespace std; 144 double Account::total = 0; 145 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 146 date.show(); 147 cout << "\t#" << id << "created" << endl; 148 } 149 void Account::record(const Date& date, double amount, const string& desc) { 150 amount = floor(amount * 100 + 0.5) / 100; 151 balance += amount; 152 total += amount; 153 date.show(); 154 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 155 } 156 void Account::show()const { cout << id << "\tBalance:" << balance; } 157 void Account::error(const string& msg)const { 158 cout << "Error(#" << id << "):" << msg << endl; 159 } 160 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 161 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 162 record(date, amount, desc); 163 acc.change(date, getBalance()); 164 } 165 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 166 if (amount > getBalance()) { 167 error("not enough money"); 168 } 169 else { 170 record(date, -amount, desc); 171 acc.change(date, getBalance()); 172 } 173 } 174 void SavingsAccount::settle(const Date& date) { 175 double interest = acc.getSum(date) * rate / (date-Date(date.getYear() - 1, 1, 1)); 176 if (interest != 0)record(date, interest, "interest"); 177 acc.reset(date, getBalance()); 178 } 179 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) {} 180 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 181 record(date, amount, desc); 182 acc.change(date, getDebt()); 183 } 184 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 185 if (amount - getBalance() > credit) { 186 error("not enouogh credit"); 187 } 188 else { 189 record(date, -amount, desc); 190 acc.change(date, getDebt()); 191 } 192 } 193 void CreditAccount::settle(const Date& date) { 194 double interest = acc.getSum(date) * rate; 195 if (interest != 0) record(date, interest, "interest"); 196 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 197 acc.reset(date, getDebt()); 198 } 199 void CreditAccount::show()const { 200 Account::show(); 201 cout << "\tAvailable credit:" << getAvailableCredit(); 202 } 203 204 task6.cpp 205 #include"account.h" 206 #include<iostream> 207 using namespace std; 208 int main() { 209 Date date(2008, 11, 1); 210 SavingsAccount sa1(date, "S3755217", 0.015); 211 SavingsAccount sa2(date, "02342342", 0.015); 212 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 213 Account* accounts[] = { &sa1,&sa2,&ca }; 214 const int n = sizeof(accounts) / sizeof(Account*); 215 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 216 char cmd; 217 do { 218 date.show(); 219 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 220 int index, day; 221 double amount; 222 string desc; 223 cin >> cmd; 224 switch (cmd) { 225 case 'd': 226 cin >> index >> amount; 227 getline(cin, desc); 228 accounts[index]->deposit(date, amount, desc); 229 break; 230 231 case 'w': 232 cin >> index >> amount; 233 getline(cin, desc); 234 accounts[index]->withdraw(date, amount, desc); 235 break; 236 case 's': 237 for (int i = 0; i < n; i++) { 238 cout << "[" << i << "]"; 239 accounts[i]->show(); 240 cout << endl; 241 } 242 break; 243 244 case 'c': 245 cin >> day; 246 if (day < date.getDay()) { 247 cout << "You cannot specify a previous day"; 248 } 249 else if (day > date.getMaxDay()) 250 cout << "Invalid day"; 251 else date = Date(date.getYear(), date.getMonth(), day); 252 break; 253 case 'n': 254 if (date.getMonth() == 12) 255 date = Date(date.getYear() + 1, 1, 1); 256 else date = Date(date.getYear(), date.getMonth() + 1, 1); 257 for (int i = 0; i < n; i++) { 258 accounts[i]->settle(date); 259 } 260 break; 261 } 262 } while (cmd != 'e'); 263 return 0; 264 }
在基類賬戶中將公共操作皆宣告為虛擬函式,因此可以透過基類指標來執行各種操作,因而各種型別的賬戶物件都可以透過一個基類指標的陣列來訪問,這提供了極大的便利,使我們可以透過統一的方式來操作各個賬戶,因而從cin輸入所執行的操作就變得非常方便了。
在本例的主程式中我們必須將幾個賬戶的構造在程式中寫死,而不允許使用者隨時動態的新增新的賬戶。