10-1
#pragma once #include <string> #ifndef ACCOUNT_H_ #define ACCOUNT_H_ class Account { private: char names[20]; char accounts[20]; double deposits; public: Account(char namec[],char accountc[],double depositc); ~Account(); void display(); void store(double n); void getout(double n); }; #endif // !ACCOUNT_H_ #include "pch.h" #include "account.h" #include <iostream> #include <cstring> using namespace std; Account::Account(char namec[],char accountc[],double depositc) { strcpy_s(names, namec); strcpy_s(accounts, accountc); deposits = depositc; } Account::~Account() { } void Account::display() { cout << names << ": " << accounts << "---" << deposits << endl; } void Account::store(double n) { deposits += n; } void Account::getout(double n) { deposits -= n; } #include "pch.h" #include <iostream> #include "account.h" using namespace std; int main(void) { char namec[20]; char accountc[20]; double depositc; cin >> namec; cin >> accountc; cin >> depositc; Account lls(namec, accountc,depositc); lls.display(); lls.store(100.0); lls.display(); lls.getout(20.0); lls.display(); return 0; }
10-2
#pragma once #ifndef Person_H_ #define Person_H_ using namespace std; class Person { private: static const int LIMIT = 25; string lname; char fname[LIMIT]; public: Person() { lname = ""; fname[0] = '\0'; } Person(const string &ln, const char *fn = "Heyyou"); void Show() const; void FormalShow() const; }; #endif // !1 #include "pch.h" #include <iostream> #include <string> #include "account.h" Person::Person(const string &ln, const char *fn) { lname = ln; strcpy_s(fname,fn); } void Person::Show() const { cout << fname << " " << lname << endl; } void Person::FormalShow() const { cout << lname << " " << fname << endl; } #include "pch.h" #include <iostream> #include <cstdlib> #include <string> #include <cstring> #include <ctime> #include <array> #include <iomanip> #include <algorithm> #include "account.h" using namespace std; int main(void) { Person one; Person two("Smythecraft"); Person three("Dimwiddy", "Sam"); one.Show(); two.FormalShow(); three.Show(); return 0; }
10-6
#pragma once #ifndef Person_H_ #define Person_H_ using namespace std; class Move { private: double x; double y; public: Move(double a = 0, double b = 0); void showmove() const; Move add(const Move & m) const; void reset(double a = 0, double b = 0); }; #endif #include "pch.h" #include <iostream> #include <string> #include "account.h" using namespace std; Move::Move(double a, double b) { x = a; y = b; } void Move::showmove() const{ cout << "Move toward x: " << x << "\nMove toward y: " << y << endl; } Move Move::add(const Move & m) const { Move newm; newm.x = x + m.x; newm.y = y + m.y; return newm; } void Move::reset(double a, double b) { x = a; y = b; } #include "pch.h" #include <iostream> #include "account.h" using namespace std; int main(void) { Move move1(1.1, 2.2); move1.showmove(); Move move2(3.3, 4.4); move2.showmove(); move2 = move2.add(move1); move2.showmove(); move2.reset(0.1, 0.2); move2.showmove(); return 0; }
10-7
#pragma once #ifndef Plorg_H_ #define Plorg_H_ using namespace std; class Plorg { private: enum {LEN=20}; char names[LEN]; int CI; public: Plorg(const char* name="Plorga", int CI = 50); void setCI(int n); void show() const; }; #endif #include "pch.h" #include <iostream> #include <string> #include "account.h" using namespace std; Plorg::Plorg(const char* name, int CI) { strcpy_s(names, name); this->CI = CI; } void Plorg::setCI(int n) { CI = n; } void Plorg::show() const { cout << names << ": " << CI << endl; } int main(void) { Plorg lls1; lls1.show(); Plorg aa("lls", 20); aa.show(); aa.setCI(30); aa.show(); return 0; }
10-8
#pragma once class List { public: typedef int Item; private: enum {LIMIT=5}; Item items[LIMIT]; Item top; public: List(const Item[]=nullptr, int m = 0) { top = 0; } bool push(const Item &); bool isempty()const; bool isfull()const; void visit(void(*fun)(Item &m)); }; void show(List::Item &m); void change(List::Item &m); List::Item getin(); #include "pch.h" #include "list.h" #include <iostream> using namespace std; bool List::push(const Item &m) { if (!isfull()) { items[top++] = m; return true; } else { cout << "插入失败。" << endl; return false; } } bool List::isempty()const { if (!top) return true; else return false; } bool List::isfull()const { if (top == LIMIT) return true; else return false; } void List::visit(void(*fun)(Item &m)) { for (int i = 0; i < top; i++) fun(items[i]); } void show(List::Item &m) { cout << m << ", "; } void change(List::Item &m) { cout << "请输入要替换的内容:"; List::Item a; cin >> a; m = a; cout << "替换成功,新的值为" << m << endl; } List::Item getin() { List::Item a; cout << "请输入你要插入的内容: "; cin >> a; while (!cin) { cin.clear(); cout << "输入错误,请重新输入: "; cin >> a; } return a; } using namespace std; int main(void) { List m; if (m.isempty()) cout << "是空的!" << endl; else cout << "不是空的" << endl; while (!m.isfull()) { List::Item a = getin(); if (m.push(a)) cout << "插入成功" << endl; else cout << "插入失败" << endl; } m.visit(show); m.visit(change); m.visit(show); system("pause"); return 0; }