C++类的分离式写法介绍示例
介绍
类的分离式写法,使得代码更加规范,增强了阅读性。
分离式写法的规则:
1.类的变量:写在类的里面
2.成员函数:类中写函数的声明,函数的定义写在类体外。
3.写在类外函数定义时,类名前加限定(Object::),其中,::理解为表示范围的符号。
代码演示
头文件:Object.h
#ifndef_OBJECT_H_
#define_OBJECT_H_
classStudent
{
private:
charname[32];
intage;
public:
voidSetName(char*pName);
voidSetAge(intage);
voidPrintf_Stu();
};
#endif
源文件:Object.cpp
#include#include #include"Object.h" voidStudent::SetName(char*pName) { strcpy(this->name,pName); } voidStudent::SetAge(intage) { this->age=age; } voidStudent::Printf_Stu() { printf("name=%s,age=%d\n",this->name,this->age); } intmain() { Studentstu; stu.SetName("aaaa"); stu.SetAge(30); stu.Printf_Stu(); return1; }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下