第九章:内存模型与名称空间

9.1题:构造函数的调用

#include<iostream>
#include<cstring>
using namespace std;
class Golf
{
    string m_name;
    int m_handicap;
    public:
            Golf():m_name("Sam"),m_handicap(24){};//析构函数的赋值,m_name和m_handicap在进入大括号之前就已经初始化
            void show()
            {
                cout << m_name << ‘,‘ << m_handicap <<endl;
            }
            Golf( string name,int handicap)
            {
                m_name = name;
                m_handicap = handicap;
            }

};
int main()
{
    Golf one;
    Golf two("Tonny",10086);
    one.show();
    two.show();
}

直接用string就好了,前面试着用strcpy赋值很麻烦



上面其实是10.1的答案,以下才是9.1

共有golf.h, golf.cpp,9-1golf.cpp 三个文件

golf.h头文件:

 1 //golf.h
 2 const int Len = 40;
 3 struct golf
 4 {
 5  char fullname[Len];
 6  int handicap;
 7 };
 8 // non-interactive version
 9 // function sets golf structure to provided name, handicap
10 // using values passed as arguments to the function
11 void setgolf(golf & g, const char * name, int hc);
12 // interactive version
13 // function solicits name and handicap from user
14 // and sets the members of g to the values entered
15 // returns 1 if name is entered, 0 if name is empty string
16 int setgolf(golf & g);
17 // function resets handicap to new value
18 void handicap(golf & g, int hc);
19 // function displays contents of golf structure
20 void showgolf(const golf & g);

golf.cpp:

 1 //golf.cpp
 2 #include <iostream>
 3 #include "golf.h"
 4 #include <cstring>
 5 // function solicits name and handicap from user
 6 // returns 1 if name is entered, 0 if name is empty string
 7 int setgolf(golf & g)
 8 {
 9     std::cout << "Please enter golfer‘s full name: ";
10     std::cin.getline(g.fullname, Len);
11      if (g.fullname[0] == ‘\0‘)
12      return 0; // premature termination
13     std::cout << "Please enter handicap for " << g.fullname << ": ";
14      while (!(std::cin >> g.handicap))
15     {
16         std::cin.clear();
17         std::cout << "Please enter an integer: ";
18     }
19      while (std::cin.get() != ‘\n‘)
20      continue;
21      return 1;
22 }
23 // function sets golf structure to provided name, handicap
24 void setgolf(golf & g, const char * name, int hc)
25 {
26     std::strcpy(g.fullname, name);
27     g.handicap = hc;
28 }
29 // function resets handicap to new value
30 void handicap(golf & g, int hc)
31 {
32     g.handicap = hc;
33 }
34 // function displays contents of golf structure
35 void showgolf(const golf & g)
36 {
37     std::cout << "Golfer: " << g.fullname << "\n";
38     std::cout << "Handicap: " << g.handicap << "\n\n";
39 }

9-1golf.cpp:

//9-1golf.cpp
#include <iostream>
#include "golf.h"
// link with golf.cpp
const int Mems = 5;
int main(void)
{
     using namespace std;
     golf team[Mems];
    cout << "Enter up to " << Mems << " golf team members:\n";
     int i;
     for (i = 0; i < Mems; i++)
     if (setgolf(team[i]) == 0)
     break;
     for (int j = 0; j < i; j++)//如果过早终止了,就可以按现有输入的运行。
     showgolf(team[j]);
     setgolf(team[0], "Fred Norman", 5);
     showgolf(team[0]);
     handicap(team[0], 3);
     showgolf(team[0]);
     return 0;
}

三个文件在一个项目里编译。

时间: 2024-08-24 23:25:17

第九章:内存模型与名称空间的相关文章

《C++ Primer Plus 6th》读书笔记 - 第九章 内存模型和名称空间

1. 单独编译 1.1 头文件中常包含的内容: 函数原型 使用#define或const定义的符号常量 结构声明 类声明 模板声明 内联声明 1.2 只需将源代码文件加入到项目中,而不用加入头文件.这是因为用#include管理头文件. 1.3 避免多次包含同一个头文件 1 #ifndef COORDIN_H_ 2 #define COORDIN_H_ 3 // place include file contents here 4 #endif 2. 存储持续性.作用域和链接性 1. 存储方案:

C++ primer plus读书笔记——第9章 内存模型和名称空间

第9章 内存模型和名称空间 1. 头文件常包含的内容: 函数原型. 使用#define或const定义的符号常量. 结构声明. 类声明. 模板声明. 内联函数. 2. 如果文件名被包含在尖括号中,则C++编译器将在存储标准头文件的主机系统的文件系统中查找.但如果头文件名包含在双引号中,则编译器将首先查找当前的工作目录或源代码目录(或其他目录,这取决于编译器).如果没有在那里找到头文件,则将在标准位置中查找.因此在包含自己的头文件时,应使用引号而不是尖括号. 3. 链接程序将目标文件代码.库代码和

《C++ Primer Plus》第9章 内存模型和名称空间 学习笔记

C++鼓励程序员在开发程序时使用多个文件.一种有效的组织策略是,使用头文件来定义用户类型,为操纵用户类型的函数提供函数原型,并将函数定义放在一个独立的源代码文件中.头文件和源代码文件一起定义和实现了用户定义的类型及其使用方式.最后,将main()和其他使用这些函数的函数放在第三个文件中.C++的存储方案决定了变量保留在内存中的事件(存储持续性)以及程序的哪一部分可以访问它(作用域和链接性).自动变量在代码块(如函数体或函数体中的代码块)中定义的变量,仅当程序执行到包含定义的代码块时,它们才存在,

《C++ Primer Plus》读书笔记之七—内存模型和名称空间

第九章 内存模型和名称空间 1.不要将函数定义或者变量声明放到头文件中. 2.头文件常包含的内容:函数原型.使用#define或者const定义的常量.结构声明.类声明.模板声明.内联函数. 3.避免多次包含同一个头文件的技术:#ifndef/#endif.仅当以前没有使用预处理器编译指令#define定义一个头文件名称时,才处理#ifndef和#endif之间的语句. 4.链接性描述了名称如何在不同单元间共享.链接性为外部的名称可在文件间共享,链接性为内部的名称只能由一个文件中的函数共享.自动

[C++ Primer Plus] 第9章、内存模型和名称空间——(一)程序清单

程序清单9.11-13(名称空间示例) namesp.h 头文件:常量.结构定义.函数原型 1 //namesp.h 2 #include<string> 3 //creat the pers and debts namespace 4 namespace pers //包含Person结构的定义和两个函数原型 5 { 6 struct Person 7 { 8 std::string fname; 9 std::string lname; 10 }; 11 void getPerson(Pe

[C++ Primer Plus] 第9章、内存模型和名称空间——(二)课后习题

头文件 golf.h 1 //golf.h --for pe9-1.cpp 2 3 const int Len = 40; 4 struct golf 5 { 6 char fullname[Len]; 7 int handicap; 8 }; 9 10 void setgolf(golf &g, const char *name, int hc); 11 12 int setgolf(golf &g); 13 14 void handicap(golf &g, int hc);

《深入理解Java虚拟机》笔记 第十二章 内存模型

主内存与工作内存 ? Java内存模型的主要目标是定义程序中各个变量的访问规则,即在虚拟机中将变量存储到内存和从内存中取出变量值这样的底层细节. ? ? 此处的变量(Variable)与Java编译中所说的变量略有区别,它包括了实例字段,静态字段和构成数组对象的元素,但是不包括局部变量与方法参数,因为后者是线程私有的,不会被共享,自然就不存在竞争的问题. ? ? 为了获得比较好的执行效率,Java内存模型并没有限制执行引擎使用处理器的特定寄存器或缓存来和主内存进行交互,也没有限制即时编译器调整代

[学习笔记—Objective-C]《Objective-C-基础教程 第2版》第九章 内存管理

内存管理: 确保在需要的时候分配内存,在程序运行结束时释放占用的内存 如果只分配内存而不释放内存,则会发生内存泄漏(leak memory),程序的内存占用量不断增加,最终会被耗尽并导致程序崩溃. 不要使用任何刚释放的内存,否则可能误用陈旧的数据,如果内存已经加载了其他数据,将会破坏这些新数据. 9.1 对象生命周期 对象的生命周期: 诞生:通过alloc或new方法实现 生存:接受消息并执行操作 交友:通过复合以及向方法传递函数 死去:被释放掉 9.11 引用计数 关于引用计数的操作: 增加对

C++模板编程 - 第九章 模板中的名称

名称的分类 1. 受限名称:前面有::运算符成员访问运算符.比如 this->var 2. 依赖型名称:以某种方式依赖于模板参数.比如 std::vector<T>::iterator 更详细的分类见P116. 名称查找 1 int x; 2 3 class B 4 { 5 public: 6 int i; 7 }; 8 9 class D: public B 10 { 11 }; 12 13 void f(D* pd) 14 { 15 pd->i=3; //ok 16 D::x=