码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-20 23:29:33      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:

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.cpp9-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;
}

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

 

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

标签:

原文地址:http://www.cnblogs.com/fudianheg/p/4237512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!