1、基本定义与相关判断:
一个类可以没有默认构造函数,有别的构造函数也是可以的
2、
3、析构函数
3、实例程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include <iostream>
#include <string>
#include<stdlib.h>
using
namespace
std;
/**
* 定义类:Student
* 数据成员:m_strName
* 无参构造函数:Student()
* 有参构造函数:Student(string _name)
* 拷贝构造函数:Student(const Student& stu)
* 析构函数:~Student()
* 数据成员函数:setName(string _name)、getName()
*/
class
Student{
public
:
Student(){
m_strName=
""
;
}
Student (string _name){
m_strName= _name;
}
Student(
const
Student & stu){
};
//构造函数
~Student(){};
//析构函数
void
setName(string);
string getName();
private
:
string m_strName;
};
void
Student::setName(string _name){
m_strName=_name;
}
void
Student::getName(){
return
m_strName;
}
int
main(
void
)
{
Student *stu=
new
Student;
stu->setName(
"你好"
);
cout<<stu->getName()<<endl;
delete
stu;
stu=NULL;
system
(
"pause"
);
return
0;
}
|
本文转自 lillian_trip 51CTO博客,原文链接:http://blog.51cto.com/xiaoqiaoya/1962699,如需转载请自行联系原作者