#include <iostream>
#include <vector>
#include <assert.h>
//#define NDEBUG
using namespace std;
void printVec(vector<int >const & vec, vector<int >::iterator & it) {
if (it != vec.end()) {
#ifndef NDEBUG
cerr << __func__ << endl;
printVec(vec, ++it);
#endif // !NDEBUG
}
return;
}
int main() {
vector<int > vec = { 0,1,2,3,4,5,6,7,8,9 };
printVec(vec, vec.begin());
for (;;);
return 0;
}
6.48
个人认为挺合理的,判断输入是否有效,并且不等于sought,如果cin为无效输入则跳出错误
6.49
候选函数具备两个特征: 一是与被调用的函数同名,二是其声明在调用点可见
可行函数:一是其形参数量与本次调用提供的实参数量相等,二是每个实参的类型与对应的形参类型相同
6.50
void f(){}
void f(int){}
void f(int,int){}
void f(double,double = 3.14){}
int main() {
f(2.56, 42);
f(42);
f(42,0);
f(2.56, 3.14);
return 0;
}
(a). 二义性, f(int,int),f(double,double)
(b).f(int)
(c).f(int,int)
(d).f(double,double)
6.51
6.52
(a).算术类型转换
(b).算术类型转换
6.53
(a) 传入的int引用
传入int的常量引用
(b) 传入char类型的引用
传入char类型的常量引用
(c)不合法,char* const为顶层const与char*无区别,所以重复声明不合法
6.54,6.55,6.56
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int add(int a,int b){
return a + b;
}
int sub(int a, int b) {
return a-b;
}
int multi(int a , int b ) {
return a*b;
}
int divide(int a, int b) {
return b ? a/b:0;
}
int main() {
vector<int(*)(int, int)> vecA;
vecA.push_back(add);
vecA.push_back(sub);
vecA.push_back(multi);
vecA.push_back(divide);
for(vector<int(*)(int,int)>::iterator it = vecA.begin(); it != vecA.end();it++){
cout << (*it)(1,2)<<endl;
}
for(;;);
return 0;
}
7.1
struct Sales_data {
string bookNo;
int units_sold = 0;
double revenue = 0;
};
int main() {
Sales_data itema, itemb;
cin >> itema.bookNo >> itemb.bookNo >> itema.units_sold>> itemb.units_sold >> itema.revenue >> itemb.revenue;
if(itema.bookNo == itemb.bookNo){
cout << "itema.bookNo" << itema.bookNo << "units_sold" << itema.units_sold + itemb.units_sold << "revenue"<< itema.revenue + itemb.revenue <<endl;
return 0;
}else{
cout << "not find same book No" << endl;
return -1;
}
for(;;);
return 0;
}
7.2
using namespace std;
struct Sales_data {
string isBn()const { return bookNo;}
Sales_data& combine(Sales_data& theData);
string bookNo;
int units_sold = 0;
double revenue = 0;
};
Sales_data& Sales_data::combine(Sales_data& theData){
this->units_sold += theData.units_sold;
this->revenue += theData.revenue;
return *this;
}
int main() {
for(;;);
return 0;
}
7.3:???
7.4:
struct Person {
string name;
string address;
string getName() const { return name; }
string getAddress() const { return address; }
};
7.5:应该是const的,函数只负责返回地址和名字,可以起到防止程序编写时误编写的作用
7.6:????
7.7:略
7.8,read需要对成员变量进行修改,print不需要
7.9
struct Person {
string name;
string address;
string getName() const { return name; }
string getAddress() const { return address; }
};
istream& read(istream& is, Person& per){
cin >> per.name ;
return is;
}
ostream& print(ostream& os, const Person& per){
os << "name" << per.name << "address" << per.address;
return os;
}
7.10
判断两个data是否进行了正确的输入操作
7.11: 略
7.12: 略
7.13略
7.14:
struct Sales_data {
Sales_data():
bookNo(this->bookNo),
units_sold(this->units_sold),
revenue(this->revenue)
{
cout << "init ok" << endl;
}
string bookNo;
int units_sold = 0;
double revenue = 0;
};
7.15:
struct Person {
Person(){
}
Person(string name):name(name) {
}
Person(string name, string address) :name(name), address(address){
}
string name;
string address;
string getName() const { return name; }
string getAddress() const { return address; }
};
7.16: 访问说明符的位置和次数没有限定,class为默认private, struct 为默认public,希望对外公开的放在public下面,希望对外隐藏 的放在private下面
7.17唯一区别就是默认的访问权限
7.18 封装可以让用户只使用提供的接口,不能直接去访问成员变量,可以强壮程序,使用户的使用符合设计者的初衷
7.19
struct Person {
Person(){
}
Person(string name):name(name) {
}
Person(string name, string address) :name(name), address(address){
}
private:
string name;
string address;
public:
string getName() const { return name; }
string getAddress() const { return address; }
};
让用户只通过getName和getAddress获取成员变量,防止用户可以直接获取成员变量发生误修改操作
7.20 优点是在不破坏原有封装性的情况下,可以扩展更多的功能,缺点暂未想到,可能会导致结构混乱?
7.21,略
7.22,同7.19