《C++ primer 5》 chapter 1.5 1.6
来源:互联网 发布:淘宝杰胜体育 编辑:程序博客网 时间:2024/06/07 01:16
note
In C++ we define our own data structures by defining aclass.
A class defines a type along with a collection ofoperations that are related to that type.
Header file names are derived from the name of a class defined in that header.
Header files that we write usually have a suffix of .h, but some programmers use .H, .hpp, or .hxx.
The standard library headers typically haveno suffix at all.
This program starts with two #include directives, one of which uses a new form. Headers from the standard library are enclosed in angle brackets (< >). Those that are not part of the library are enclosed in double quotes (" ").
File redirect
$ addItems outfile Assuming
$ is the system prompt and our addition program has been compiled into an executable file named addItems.exe (or addItems on UNIX systems).
This command will read transactions from a file named infile and write its output to a file named outfile in the current directory.
Member functions are sometimes referred to as methods.
The dot operator applies only to objects of class type. The left-hand operand must be an object of class type, and the right-hand operand must name a member of that type. The result of the dot operator is the member named by the right-hand operand.
We call a function using the call operator (the () operator).
exercise
exercise 1.20
#include<iostream>#include"Sales_item.h"int main(){Sales_item s;while(std::cin>>s){std::cout<<s;}system("pause");return 0;}其中用到了头文件中的
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;}
exercise 1.21
#include<iostream>#include"Sales_item.h"int main(){Sales_item a,b;std::cin>>a>>b;if(compareIsbn(a,b)){std::cout<<a+b<<std::endl;}else{std::cerr<<"not the same ISBN"<<std::endl;}system("pause");return 0;}
用到头文件的输入输出重载符以及
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) { return lhs.isbn() == rhs.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}
exercise 1.22
#include<iostream>#include"Sales_item.h"int main(){Sales_item tran,tran_curr,sum;std::cin>>tran;while(std::cin>>tran_curr){if(compareIsbn(tran,tran_curr)){sum = tran + tran_curr;tran = sum;}else{std::cerr<<"must be the same ISBN"<<std::endl;}}std::cout<<sum<<std::endl;system("pause");return 0;}
exercise 1.23
#include<iostream>#include"Sales_item.h"int main(){Sales_item item1,item2,item3,newItem;std::cin>>item1;while(std::cin>>newItem){if(newItem.isbn() == item1.isbn()){item1 += newItem;}else if(newItem.isbn() == item2.isbn()){item2 += newItem;}else if(newItem.isbn() == item3.isbn()){item3 += newItem;} else{if (item2.isbn() == ""){item2 = newItem;}else{item3 = newItem;}}}std::cout<<item1<<std::endl<<item2<<std::endl<<item3<<std::endl;system("pause");return 0;}
exercise 1.24
exercise 1.25
- 《C++ primer 5》 chapter 1.5 1.6
- C++Primer-Function chapter
- C primer plus -- Chapter 2
- [primer]Chapter-5 Expressions
- C++ Primer Chapter 5
- 《C++ primer 5》 chapter 1.2
- 《C++ primer 5》 chapter 1.3
- 《C++ primer 5》 chapter 1.4
- 《C++ primer 5》 chapter 2.1
- 《C++ primer 5》 chapter 2.2
- C++ Primer Chapter 10-5
- C++ Primer Chapter 13-5
- C++ Primer Chapter 14-5
- C++ Primer Chapter 15-5
- c primer plus Chapter 14 p383
- 《C primer plus》笔记--Chapter 1
- C++Primer Chapter One学习笔记
- 加减乘除计算器——C primer plus 5edith chapter 8
- ACM-快速排序
- Uncaught TypeError: Cannot read property 'msie' of undefined问题解决方案
- 58同城陈小华:互联网新浪潮就是O2O
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException
- 23岁的一些迷惘
- 《C++ primer 5》 chapter 1.5 1.6
- PHP网页中嵌入天气
- 【POJ】3659 Cell Phone Network 树上的极小支配集——树型DP
- [Android]API包、目录结构、AndroidManifest.xml
- C语言复习总结
- 盛世3r淼
- 只使用I/O的PrintDigit函数,编写一个过程以输出任意实数
- Reverse Integer
- POJ3187 Backward Digit Sums 枚举