构造函数和析构函数
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct human {
char name[20] = { NULL
};
unsigned char age = NULL;
//专业的名字叫做:构造函数.
//构造函数的特点.
//1.没有返回值.
2.和类名一致
3.类创建的时候会自动调用.
//构造函数一般是做一些初始化的工作,比如说申请内存,初始化成员的一些默认.
//构造函数是可以有参数的.
//构造函数可以创建多个.
//自动重载
human() {
strcpy(this->name,
"NULL");
this->age = NULL;
}
human(char * p_name) {
strcpy(this->name,
p_name);
this->age = NULL;
}
human(unsigned char p_age)
{
strcpy(this->name, "NULL");
this->age =
p_age;
}
human(char * p_name, unsigned char p_age)
{
strcpy(this->name, p_name);
this->age =
p_age;
}
void output() {
printf("My name is %s
My
age is %d years old.
", this->name, this->age);
}
void test(){
human l_geek;
l_geek.output;
}
};
void main() {
//更灵活了.
test;
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct man{
char name[20] = { NULL };
unsigned int age = NULL;
man( unsigned int p_age) {
this->age = p_age;
strcpy(this->name, "NULL");
}
man(char*p_name) {
this->age = NULL;
strcpy(this->name, p_name);
}
man() {
this->age = NULL;
strcpy(this->name, "NULL");
}
void out() {
printf("%d
%s
", this->age, this->name);
}
};
void main() {
man p(20);
p.out();
system("pause");
}