定義一個類:Sales_item
用來表示一本書的總銷售額,售出冊數,平均售價
Sales_item類有以下功能:
● 調用isbn函數,獲取一本書的ISBN書號
● 使用 >> 和 << 操作符可讀寫Sales_item類型對象
● 使用 = 賦值運算符可以將一個Sales_item對象的值賦給另一個Sales_item對象
● 使用 + 運算符將兩個書號相同的Sales_item對象的總銷售額和售出冊數相加,生成一個新的Sales_item對象
● 使用 += 運算符將一個Sales_item對象加到另一個對象上(書號相同的對象)
下面的sales_item.h文件內容是從該書的網站上(http://www.informit.com/title/0321714113)拷貝的,先不在意其內容含義,主要體會如何使用一個自定義的類
sales_item.h
/* * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the * copyright and warranty notices given in that book: * * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo." * * * "The authors and publisher have taken care in the preparation of this book, * but make no expressed or implied warranty of any kind and assume no * responsibility for errors or omissions. No liability is assumed for * incidental or consequential damages in connection with or arising out of the * use of the information or programs contained herein." * * Permission is granted for this code to be used for educational purposes in * association with the book, given proper citation if and when posted or * reproduced.Any commercial use of this code requires the explicit written * permission of the publisher, Addison-Wesley Professional, a division of * Pearson Education, Inc. Send your request for permission, stating clearly * what code you would like to use, and in what specific way, to the following * address: * * Pearson Education, Inc. * Rights and Permissions Department * One Lake Street * Upper Saddle River, NJ 07458 * Fax: (201) 236-3290 */ /* This file defines the Sales_item class used in chapter 1. * The code used in this file will be explained in * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators) * Readers shouldn't try to understand the code in this file * until they have read those chapters. */ #ifndef SALESITEM_H // we're here only if SALESITEM_H has not yet been defined #define SALESITEM_H // Definition of Sales_item class and related functions goes here #include <iostream> #include <string> class Sales_item { // these declarations are explained section 7.2.1, p. 270 // and in chapter 14, pages 557, 558, 561 friend std::istream& operator>>(std::istream&, Sales_item&); friend std::ostream& operator<<(std::ostream&, const Sales_item&); friend bool operator<(const Sales_item&, const Sales_item&); friend bool operator==(const Sales_item&, const Sales_item&); public: // constructors are explained in section 7.1.4, pages 262 - 265 // default constructor needed to initialize members of built-in type Sales_item() = default; Sales_item(const std::string &book): bookNo(book) { } Sales_item(std::istream &is) { is >> *this; } public: // operations on Sales_item objects // member binary operator: left-hand operand bound to implicit this pointer Sales_item& operator+=(const Sales_item&); // operations on Sales_item objects std::string isbn() const { return bookNo; } double avg_price() const; // private members as before private: std::string bookNo; // implicitly initialized to the empty string unsigned units_sold = 0; // explicitly initialized double revenue = 0.0; }; // used in chapter 10 inline bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) { return lhs.isbn() == rhs.isbn(); } // nonmember binary operator: must declare a parameter for each operand Sales_item operator+(const Sales_item&, const Sales_item&); inline bool operator==(const Sales_item &lhs, const Sales_item &rhs) { // must be made a friend of Sales_item return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.isbn() == rhs.isbn(); } inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs) { return !(lhs == rhs); // != defined in terms of operator== } // assumes that both objects refer to the same ISBN Sales_item& Sales_item::operator+=(const Sales_item& rhs) { units_sold += rhs.units_sold; revenue += rhs.revenue; return *this; } // assumes that both objects refer to the same ISBN Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) { Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return ret += rhs; // add in the contents of (|rhs|) return ret; // return (|ret|) by value } std::istream& operator>>(std::istream& in, Sales_item& s) { double price; in >> s.bookNo >> s.units_sold >> price; // check that the inputs succeeded if (in) s.revenue = s.units_sold * price; else s = Sales_item(); // input failed: reset object to default state return in; } std::ostream& operator<<(std::ostream& out, const Sales_item& s) { out << s.isbn() << " " << s.units_sold << " " << s.revenue << " " << s.avg_price(); return out; } double Sales_item::avg_price() const { if (units_sold) return revenue/units_sold; else return 0; } #endif
Sales_item類的使用(編譯需要用到C++11特性,編譯命令:g++ main.cpp -std=c++11 -o test)
讀寫Sales_item
#include <iostream> #include "sales_item.h" // 使用""來引入非標准庫頭文件 int main() { Sales_item book; std::cout << "請輸入:書號 冊數 單價:" << std::endl; std::cin >> book; std::cout << book << std::endl; return 0; }
Sales_item對象的加法
#include <iostream> #include "sales_item.h" int main() { Sales_item item1,item2; std::cout << "請輸入:書號 冊數 單價:" << std::endl; std::cin >> item1 >> item2; std::cout << item1 + item2 << std::endl; return 0; }
練習:讀取兩個書號相同Sales_item對象,輸出它們的和
#include <iostream> #include "sales_item.h" int main() { Sales_item item1,item2; std::cout << "請輸入:書號 冊數 單價:" << std::endl; std::cin >> item1 >> item2; if(item1.isbn() == item2.isbn()){ std::cout << item1 + item2 << std::endl; } else{ std::cout << "書號不同,無法相加" << std::endl; } return 0; }
練習:讀取多個具有相同書號的銷售記錄,輸出所有記錄的和
#include <iostream> #include "sales_item.h" int main() { Sales_item countItem,item; std::cout << "請輸入:書號 冊數 單價:" << std::endl; std::cin >> countItem; while(std::cin >> item){ if(countItem.isbn() == item.isbn()){ countItem += item; } else{ std::cout << "書號不同,結束相加" << std::endl; break; } } std::cout << countItem << std::endl; return 0; }
類的成員函數也叫類的方法,類對象使用.運算符來進行調用,點運算符只能用於類類型的對象
練習:讀取多條銷售記錄,統計相同書號的銷售記錄數(與筆記六類似,沒有將間隔書號相同的銷售記錄累計)
#include <iostream> #include "sales_item.h" int main() { Sales_item currentItem,item; int count; std::cout << "請輸入:書號 冊數 單價:" << std::endl; std::cin >> currentItem; count = 1; while(std::cin >> item){ if(currentItem.isbn() == item.isbn()){ ++count; } else{ std::cout << "書號:" << currentItem.isbn() << "的銷售記錄條數是" << count << std::endl; currentItem = item; count = 1; } } std::cout << "書號:" << currentItem.isbn() << "的銷售記錄條數是" << count << std::endl; return 0; }