码迷,mamicode.com
首页 > 编程语言 > 详细

[c++]容器类

时间:2015-04-09 17:23:38      阅读:132      评论:0      收藏:0      [点我收藏+]

标签: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++]容器类

标签:c++

原文地址:http://blog.csdn.net/cherry_ermao/article/details/44962343

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!