练习13.18
Define an Employee class that contains an employee name and a unique employee identifier. Give the class a default constructor and a constructor that takes a string representing the employee’s name. Each constructor should generate a unique ID by incrementing a static data member.
定义一个名为Employee的类别,它包括了员工的名字和一个唯一的员工识别证号码。请给这个类别定义一个预设建构器,还有一个建构器是带了一个string型别的参数用来表示员工的名字。这二个建构器都应该要能够藉由递增一个静态的资料成员来产生一个唯一的识别号码。【为什么要用静态?因为静态成员的生命週期是在一经配置后就会保留到整个应用程式结束时才会结束;因此只要我们不会去归零它,它的值就永远不会归零;但要注意若在下次启动应用程式时,一定要有一个读取最后值的机制来赋予给它,否则就会从零开始递增,那就必然会有重複值了】
忘了录的部分脸书直播第535集约10分钟前后
#include<iostream>using namespace std;struct Employee {//public: Employee() :ID(++myID),employeeName("十方三世佛,共同一法身,一心一智慧,力无畏亦然"){}; Employee(const string& employeeName) :ID(++myID),employeeName(employeeName) {}; const string employeeName; const unsigned ID;private: static unsigned myID;//此类似宣告-配置资源(宣告类别内的静态成员)};unsigned Employee::myID = 0;/*此类似定义-建构实例,初始化已经宣告的类别静态成员如何初始化静态资料成员 https://openhome.cc/Gossip/CppGossip/staticMember.html static 资料成员属于类别,而非个别实例,想在类别内初始 static 资料成员的话,必须是个 constexpr,也就是必须是编译时期常数,若否,必须在类别外指定,例如: class Math { public: static double PI; }; double Math::PI = 3.14159; */void f(Employee s) { cout << s.employeeName << "'s ID is :"<< s.ID << endl; }int main() { Employee a,b("孙守真"),c("阿弥陀佛"); f(a);f(b); f(c);}
https://github.com/oscarsun72/prog1-C-Primer-5th-Edition-s-Exercises/blob/exercise13_18/prog1/prog1.cpp
C++自修入门实境秀 535 重新译撰 《C++ Primer 5th》
13.1.6. 制断拷贝 练习13.18~
上一集:
https://www.facebook.com/100003034306665/videos/2641474629296988
下一集:
全部:http://bit.ly/2NoA2ID 原档下载:http://bit.ly/2Ixe2Vc
课文: http://bit.ly/2FIHV57
http://bit.ly/2mttmfa(第二篇)
第10-11章: http://bit.ly/2MuPmiZ
章12: http://bit.ly/2Rw53sH
重译12章:http://bit.ly/2V8UgZ7
http://bit.ly/2G2fPSg (docx)
重译11章:http://bit.ly/39P7HRU
第三篇:http://bit.ly/2UaFbDY
第三篇13章:http://bit.ly/33mh49y
https://play.google.com/books/reader?id=J1HMLyxqJfgC&pg=GBS.PT940.w.4.0.73
讲义下载:
http://bit.ly/2khF8Ic (全部)
程式码:
https://github.com/oscarsun72/prog1
缘起:http://bit.ly/2XwHOUH