C++Primer中文版(第四版)题解整理(20)
时间:2025-04-20
时间:2025-04-20
C++Primer题解
//...对象上的操作
private:
std::stringname;
Addressaddr;
Tel_numbertel;
};
(d)某大学的学生
classStudent{
public:
//...对象上的操作
private:
std::stringID;
std::stringname;
charsex;
std::stringdept;//所在系
std::stringmajor;
Addresshome_addr;
Tel_numbertel;
};
注意,在不同的具体应用中,类的设计会有所不同,这里给出的只是一般性的简单例子。
习题2.31
判别下列语句哪些是声明,哪些是定义,请解释原因。
(a)externintix=1024;
(b)intiy;
(c)externintiz;
(d)externconstint&ri;
【解答】
(a)是定义,因为extern声明进行了初始化。
(b)是定义,变量定义的常规形式。
(c)是声明,extern声明的常规形式。
(d)是声明,声明了一个const引用。
习题2.32
下列声明和定义哪些应该放在头文件中?哪些应该放在源文件中?请解释原因。
(a)intvar;
(b)constdoublepi=3.1416;
(c)externinttotal=255;
(d)constdoublesq2=squt(2.0);