Plorg.cpp
#include <iostream> #include <string> using namespace std; //这次把声明和实现都放在cpp里面 class Plorg{ const static int MAX=19; private: char name[MAX]; //string name; int CI; public: Plorg(const char *n="Plorga"):CI(50)//,int c=50)//:name("Plorga")如何设定初始值?如果name为string就可以这样做 { //原来还设定了CI初始值为50? strcpy(name,n); //CI=c; }; /* Plorg(int c)//如此默认初始化?? { strcpy(name,"Plorga"); CI=c; }*/ void setCI(const int c) { CI=c; }; void show() { cout<<" name="<<name<<" , CI="<<CI<<endl; }; };
main107.cpp
#include <iostream> #include "Plorg.cpp" void main107() { //Plorg p1("my",10); Plorg p1("myname"); p1.show(); p1.setCI(20); p1.show(); //Plorg p2=Plorg();//可以这样写 //Plorg p2();不能这样写 Plorg p2;//能这样写 p2.show(); p2.setCI(100); p2.show(); cin.get(); }