cpp:classexamples:books
國立屏東大學 資訊工程學系 物件導向程式設計
Book書籍類別範例
簡介
Book類別是用以對「真實世界(real world)」中的「書籍」進行「抽象對映(mapping abstraction)」的定義。
Data Members
- [-] title : string
- [-] author : string
- [-] ISBN : string
Member Functions
- [+] setTitle(string t) : void –> setter for title
- [+] setAuthor(string a) : void –> setter for author
- [+] setISBN(string i) : void –> setter for ISBN
- [+] getTitle() : string –> getter for title
- [+] getAuthor() : string –> getter for Author
- [+] getISBN() : string –> getter for ISBN
- [+] showInfo(): void –> print out its information
Constructor and Destructor
- Book() –> set default values of title and author as “unknown”, and set ISBN as “undefined”
- Book(string t, string a, string isbn) –> set title, author and ISBN as t, a and isbn
- ~Book() –> print out [author]'s book has been deleted!
Class Definition
class Book { private: string title; string author; string ISBN; public: void setTitle(string t) {title=t;} void setAuthor(string a) {author=a;} void setISBN(string i) {ISBN=i;} string getTitle() {return title;} string getAuthor() {return author;} string getISBN() {return ISBN;} Book() { title="unknown"; author="unknown"; ISBN="undefined"; } Book(string t, string a, string isbn) { title=t; author=a; ISBN=isbn; } ~Book() { cout << author << "'s book has been deleted!" << endl; } void showInfo() { cout << title << " (" << author << ")" << ", ISBN:" << ISBN << endl; } };
Example
Book b1,b2; b1.setTitle("Programming in C"); b1.setAuthor("Jun Wu"); b1.setISBN("987-123-456-1"); b2=b1; b2.setTitle("Smart Programming"); b2.setISBN("987-111-222-3"); cout << b1.getTitle() << "(" << b1.getAuthor() << "), " << b1.getISBN() << endl; cout << b2.getTitle() << "(" << b2.getAuthor() << "), " << b2.getISBN() << endl;
cpp/classexamples/books.txt · 上一次變更: 2022/04/14 17:05 由 junwu