C++Primer 中文版 第五版 第七章课后习题答案

来源:互联网 发布:c语言正弦函数怎么表示 编辑:程序博客网 时间:2024/04/29 05:36

//7.1

#include<iostream>

#include<string>

using namespace std;

struct Sales_data

{

string bookNo;

unsigned units_sold;

double revenue;

};

int main()

{

double price=0;

Sales_data total;

Sales_data trans;

if(cin>>total.bookNo>>price>>total.units_sold)

    {

total.revenue=total.units_sold*price;

while(cin>>trans.bookNo>>price>>trans.units_sold)

{

trans.revenue=trans.units_sold*price;

if(total.bookNo==trans.bookNo)

{

total.revenue+=trans.revenue;

total.units_sold+=trans.units_sold;

}

else

{

cout<<total.bookNo<<" "<<total.revenue<<" "<<total.units_sold;

total.bookNo=trans.bookNo;

total.revenue=trans.revenue;

total.units_sold=trans.units_sold;

}

}

cout<<total.bookNo<<" "<<total.revenue<<" "<<total.units_sold;

}

}

//7.2

Sales_data &conbine(const Sales_data &rhs)

{

Units_sold+=rhs.units_sole;

Revenue+=rhs.revenue;

Return *this;

}

String isbn()

{

Return bookNo;

}

//7.3

//7.4

 

#include<iostream>

#include<string>

using namespace std;

class Person

{

private:

 

string name;

string adr;

public:

Person(string na,string ad):name(na),adr(ad){}

};

//7.5

需要,因为我们不希望他改变

 

#include<iostream>

#include<string>

using namespace std;

class Person

{

private:

 

string name;

string adr;

public:

Person(string na,string ad):name(na),adr(ad){}

string get_name()

{return name;

}

string get_adr()

{

return adr;

}

 

};

//7.6

Sales_data add(Sales_add &lhs,Sales_add & rhs)

{

Sales_data sum=lhs;

sum.combine(rhs);

return sum;

}

istream &read(istream &is,Sales_data &item)

{

double price=0;

is>>item.bookNo>>item.units_sold>>price;

item.revenue=price*item.units_sold;

return is;

}

ostream &print(ostream &os,Sales_data &item)

{

os<<item.isbn()<<" "<<item.units_sold<<" "<<item.revenue;

return os;

}

//7.7

//7.8

输入我们要改变,但输出我们不需要改变值

//7.9

 

#include<iostream>

#include<string>

using namespace std;

class Person

{

private:

 

string name;

string adr;

public:

Person(string na,string ad):name(na),adr(ad){}

istream &read(istream &is,Person &data)

{

is>>data.name>>data.adr;

return is;

}

ostream &print(ostream &os,Person &data)

{

cout<<data.name<<" "<<data.adr;

return os;

}

 

string get_name()

{return name;

}

string get_adr()

{

return adr;

}

};

7.10

连续输入两次数据;

//7.14

Sales_data()::bookNo(0),units_sole(0),revenue(0.0)

//7.15

7.4上加一个person()=default

//7.16

没有明确限定

Publicprivate区别就是他的答案

//7.17

Struct 默认publlic

Class默认private

//7.18

隐藏细节,防止用户无意的破坏

//7.19

Nameaddrprivate的,我们不希望外界使用他

//7.20

非成员函数想访问私有成员时

//7.21

//7.22

 

#include<iostream>

#include<string>

using namespace std;

class Person

{

private:

 

string name;

string adr;

public:

Person()=default;

Person(string na,string ad):name(na),adr(ad){}

istream &read(istream &is,Person &data)

{

is>>data.name>>data.adr;

return is;

}

ostream &print(ostream &os,Person &data)

{

cout<<data.name<<" "<<data.adr;

return os;

}

 

string get_name()

{return name;

}

string get_adr()

{

return adr;

}

};

////7.23

#include<iostream>

#include<string>

using namespace std;

class Screen

{

public:

typedef string::size_type pos;

Screen()=default;

Screen():cursor(0),height(0),width(0){}

Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c){}

    char get()const

{

return contents[cursos];

}

inline char get(pos ht,pos wd);

Screen &move(pos r,pos c);

private:

pos cursos;

pos height,width;

string contents;

};

//7.24

#include<iostream>

#include<string>

using namespace std;

class Screen

{

public:

typedef string::size_type pos;

Screen()=default;

Screen():cursor(0),height(0),width(0){}

Screen(pos ht=0,pos wd=0):cursor(0),height(ht),width(wd),contents(ht*wd,' '){}

Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c){}

    char get()const

{

return contents[cursos];

}

inline char get(pos ht,pos wd);

Screen &move(pos r,pos c);

private:

pos cursos;

pos height,width;

string contents;

};

//7.25

不能,因为有些需要在内部进行初始化

//7.26

加上inline就行

//7.27

不会- -

//7.28

调用函数操作完后,没有变

//7.29

调试一下即可

//7.30

This代表着首地址,对代码消耗维护代价大

//7.31

Class Y;

Class x

{

     Y *p;

};

Class y

{

   X xob;

};

//7.32

//7.33

我们不知道pos是哪一个的

//7.34

编译错误

//7.35

错误了 type定义了两次

//7.36

顺序错了

//7.37

默认构造

提供一个参数的

//7.38

Salse_data(istream &is)

{

Read(is,*this);

}

//7.39

不合法,出现二义性

//7.40

B:年月日

//7.41

//7.42

没有必要,不做了

//7.43

#include<iostream>

using namespace std;

class NoDefault

{

public:

Nodefault(int i);

};

class c

{

public:

c():i(0),Ndf(i){}

private:

int i;

NoDefault Ndf;

};

//7.44

不合法,没有默认构造函数

//7.45

合法。

//7.46

A:错,可以自动合成

B:错,可以带参数

C:

D对

//7.47

可以,防止紫铜发生类型转换

//7.48

只说如果是ex

如果是,第三个不通过,因为没有定义

//7.49

Ab隐性类型转换

C可以

//7.50

没有

//7.51

Vector:传入一个值,我们不能确定他会被转化成一个vector对象还是整型变量42.

//7.52

去掉={},换成()

//7.53

#include<iostream>

#include<string>

using namespace std;

class Debug

{

public:

constexpr Debug(bool b=true):hw(b),io(b),other(b){}

constexpr Debug(bool h,bool i,bool o):hw(h),io(i),other(o){}

    constexpr Debug bool any(){return hw||io||other}

constexpr Debug bool hardware(){return hw||io}

constexpr Debug bool app(){return other}

    void set_io(bool b){io=b;}

void set_hw(bool b){hw=b;}

void set_other(bool b){hw=b;}

private:

  bool hw;

  bool io;

  bool other;

};

//7.54

不能,constexper唯一可执行语句即使返回语句

//7.55

不是,因为他的成员函数不是

//7.56

//7.57

#include<iostream>

#include<string>

using namespace std;

class Account

{

private:

string owner;

double amount;

static double interestRate;

static double initRate()

{

return 0.0225;

}

static string accountType;

double daily_tbl[period];

public:

Account():amount(0.0){}

Account(string &s,double amt):owner(s),amount(amt){}

void calculate()

{

amount+=amount*interestRate;

}

double balance()

{

return amount;

}

static double rate()

{

return interestRate;

}

static void rate(double);

};

//7.58

A:

Rate不能赋值

Vec后面去掉

B:

Double Example::rate=6.5;

Vector<double>Example::vec(vecSize);

0 0
原创粉丝点击