Nineth Chapter: Memory model and namespaces

Source: Internet
Author: User

9.1 topic: Call to Constructors

#include <iostream>#include<cstring>using namespacestd;classgolf{stringM_name; intM_handicap;  Public: Golf (): M_name ("Sam"), M_handicap ( -){};//assignment of destructors, M_name and M_handicap are initialized before entering curly braces            voidShow () {cout<< M_name <<','<< M_handicap <<Endl; } Golf (stringNameintHandicap) {M_name=name; M_handicap=Handicap; }            };intMain () {Golf one; Golf ("Tonny",10086);    One.show (); Two.show ();}

Just use string and try to use strcpy to assign a lot of trouble.

Above is actually the answer of 10.1, the following is 9.1

Total Golf.h, golf.cpp,9-1golf.cpp three files

Golf.h header file:

1 //golf.h2 Const intLen = +;3 structGolf4 {5  CharFullname[len];6  intHandicap;7 };8 //non-interactive version9 //function sets golf structure to provided name, HandicapTen //using values passed as arguments to the function One voidSetgolf (Golf & G,Const Char* Name,inthc); A //Interactive version - //function solicits name and handicap from user - //and sets the members of the G to the values entered the //returns 1 if name is entered, 0 if name is empty string - intSetgolf (Golf &g); - //function resets handicap to new value - voidHandicap (Golf & G,inthc); + //function displays contents of golf structure - voidShowgolf (ConstGolf & G);

Golf.cpp:

1 //Golf.cpp2#include <iostream>3#include"golf.h"4#include <cstring>5 //function solicits name and handicap from user6 //returns 1 if name is entered, 0 if name is empty string7 intSetgolf (Golf &g)8 {9Std::cout <<"Please enter golfer ' s full name:";Ten std::cin.getline (G.fullname, Len); One      if(g.fullname[0] ==' /') A      return 0;//Premature termination -Std::cout <<"Please enter Handicap for"<< G.fullname <<": "; -       while(! (Std::cin >>g.handicap)) the     { - std::cin.clear (); -Std::cout <<"Please enter an integer:"; -     } +       while(Std::cin.Get() !='\ n') -      Continue; +      return 1; A } at //function sets golf structure to provided name, Handicap - voidSetgolf (Golf & G,Const Char* Name,intHC) - { - std::strcpy (g.fullname, name); -G.handicap =HC; - } in //function resets handicap to new value - voidHandicap (Golf & G,intHC) to { +G.handicap =HC; - } the //function displays contents of golf structure * voidShowgolf (ConstGolf &g) $ {Panax NotoginsengStd::cout <<"golfer:"<< G.fullname <<"\ n"; -Std::cout <<"Handicap:"<< G.handicap <<"\ n"; the}

9-1golf.cpp:

//9-1golf.cpp#include <iostream>#include"golf.h"//link with golf.cppConst intMems =5;intMainvoid){     using namespacestd;    Golf Team[mems]; cout<<"Enter up to"<< Mems <<"Golf Team members:\n"; inti;  for(i =0; i < Mems; i++)     if(Setgolf (team[i]) = =0)      Break;  for(intj =0; J < I; J + +)//If it terminates prematurely, it can be run as an existing input. Showgolf (Team[j]); Setgolf (team[0],"Fred Norman",5); Showgolf (team[0]); Handicap (team[0],3); Showgolf (team[0]); return 0;}

Three files are compiled in a project.

Nineth Chapter: Memory model and namespaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.