/* -*- C++ -*- */ /************************************************************************* * Copyright(c) 1995~2005 Masaharu Goto (root-cint@cern.ch) * * For the licensing terms see the file COPYING * ************************************************************************/ // // class inheritance test 1 // // constructor, destructor // #ifdef __hpux #include #else #include using namespace std; #endif #include #include #include class String { private: char *p; public: String(const char *set) { cout << "construct String(" << set << ")\n"; p=(char*)malloc(strlen(set)+1); strcpy(p,set); } int len() { return(strlen(p)); } char *s() { return(p); } ~String() { cout << "destruct ~String(" << p << ")\n"; free(p); } }; ostream& operator <<(ostream& ios,String& x) { ios << x.s() ; return(ios); } class gender { private: char *male; String Gender; public: gender(const char *set) : Gender(set) { cout << "construct gender(" << set << ")\n"; male=(char*)malloc(strlen(set)+1); strcpy(male,set); } char *S() { return(male); } ~gender() { cout << "destruct ~gender(" << male<< ")\n"; free(male); } }; class person : public gender { public: String address; String name; int age; person(void); person(const char *adr,const char *nm,int ag,const char *setgender); }; person::person(void) : gender("MALE"),address("SAITAMA"),name("MASAHARU") { age = 33; cout << "construct person(" << age << ")\n"; } person::person(const char *adr,const char *nm,int ag,const char *setgender) : gender(setgender),address(adr) , name(nm) { cout << "construct person(" << ag << ")\n"; age = ag; } int main() { person me=person("Saitama","Masa",32,"male"); cout << " address=" << me.address << " name=" << me.name << " age=" << me.age << " Gender=" << me.S() << "\n"; person *pme; pme = new person; cout << " address=" << pme->address << " name=" << pme->name << " age=" << pme->age << " Gender=" << pme->S() << "\n"; delete pme; return 0; }