标签:c++
#include<iostream> #include<string.h> using namespace std; class string1 { private: char *str; public: string1(const char *s) { str = new char[strlen(s) + 1]; strcpy(str,s); cout<<"structing:"<<str<<endl; } string1() {}//构造函数没有给name2就不能初始化 void print() { cout<<str<<endl; } ~string1() { delete []str; cout<<"distructing:"<<str<<endl; } }; class girl { private: string1 name; string1 name2; int age; public: girl(const char *st,int ag):name(st)//用参数列表的形式给name赋初值 { age = ag; cout<<"structing girl"<<endl; } void print() { cout<<"姓名:"; name2.print(); cout<<"年龄:"<<age<<endl; } };//容器类 int main() { girl g1("张晓丽",15); g1.print(); return 0; }
标签:c++
原文地址:http://blog.csdn.net/cherry_ermao/article/details/44962343