class 中 析构函数 构造函数具体应用

the first appilication

#include<iostream>
#include<cstring>     // if use strlen() funtion, plus it
#include<string.h>    // if use strcpy(),plus  it
using namespace std;
class student{
private:
    int id;
    char *name;
    char  sex;
    int  age;
public:
    student(int pid,char *name,char psex,int page);
    void pout();
    ~student();   // ~ the signal of destruct function

};

student::student(int pid,char *pname,char psex,int page)
{
    id=pid;
    name=new char[strlen(pname)+1];//sring is somwhat like array,square bracket instead of round bracket
    strcpy(name,pname);  // easily to forget ,you just assign memory for name but not assign pname a value
    sex=psex;
    age=page;

}
void student::pout()
{
    cout<<"ID:"<<id<<"\t"<<"name:"<<name<<"\t"<<"sex:"<<sex<<"\t"<<"age:"<<age<<endl;
}
student::~student()
{
    delete [] name;
}
int main(void)
{
    student one(001,"mike",'m',20);  //establish a object with constructor function being used
    one.pout();
    one.~student();
    return 0;
}

构造函数的重组及类对象改写

#include<iostream>
#include<cstring>     // if use strlen() funtion, plus #include<cstring>
#include<string.h>
using namespace std;
class student{
private:
    char *id;
    char *name;
    char  sex;
    int   age;
public:
    student(char *pid,char *pname,char psex,char page);
    student();
    void pout();
    void change34(char psex,int page)
    {
        sex=psex;
        age=page;
    }
   void change12(char *pname,char *pid);   
    ~student();   // ~ the signal of destruct function

};

student::student( )
{
    id=new char[10];
    strcpy(id,"00001");
    name=new char[20];//sring is somwhat like array,square bracket instead of round bracket
    strcpy(name,"xxxxxxx");  // easily to forget ,you just assign memory for name but not assign pname a value
    sex=' ';
    age=18;

}
student::student(char *pid,char *pname,char psex,char page)
{
    id=new char[strlen(pid)+1];
    strcpy(id,pid);
    name=new char[strlen(pname)+1];
    strcpy(name,pname);
    sex=psex;
    age=page;
}
 void student::change12(char *pname,char *pid)      //first time forget to add student as singal of action scope
    {
        delete [] id;
        delete [] name;
        name=new char[strlen(pname)+1];
        strcpy(name,pname);
        id=new char[strlen(pid)+1];
        strcpy(id,pid);

    }
void student::pout()
{
    cout<<"ID:"<<id<<"\t"<<"name:"<<name<<"\t"<<"sex:"<<sex<<"\t"<<"age:"<<age<<endl;
}
student::~student()
{
    delete [] name;
    delete [] id;
   // cout<<"the memory of student's name and id released"<<endl;      remind yourself if relief the moemory
}
int main(void)
{
    student one("00001","mike",'m',20);  //establish a object with constructor function being used
    one.pout();
    one.~student();
    student two;
    two.pout();
    cout<<"are you ready to change?"<<endl;
    two.change34('w',26);
    two.change12("balabala","00002");
    two.pout();
    two.~student();

    return 0;
}

look back

  1. name=new char[20]; *name=“llll” strcpy(name,“llll”); 含义不同
  2. #include<string.h> 字符串函数使用 #include 数字符串长度
  3. 在类外完善时,把析构,构造函数定义放一起,返回类型的特殊性,以免写其它函数定义时忘记作用域或返回值类型
  4. 若想表示使用自定义构造函数 student 标识符 ; instead of student 标识符();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值