参考资料
- 范磊C++(第6课)
- Xcode & VS2015
面向对象的概念
- 抽象
- 封装
- 继承
- 多态
内联函数
参考: 菜鸟驿站.
- 这其实就是个空间代价换时间的i节省。所以内联函数一般都是1-5行的小函数。
- 类结构中所在的类说明内部定义的函数是内联函数。
代码
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
class human //仅仅是声明不占内存
{
public:
void GetStature() { cout << "stature:"<<stature<<endl; }//骆驼命名法
void get_Weight();//下划线命名法
void SetStature(int x) { stature = x; } //声明和定义不建议放在一起 //放在一起就是“内联函数”会增加程序的空间
void SetWeight(int x);
private://类的成员默认私有,建议使用接口函数来访问
int stature;
int weight;
};
//内联函数
inline int print();
int print(){return 1; }
void human::get_Weight()
{
cout << "weight:"<<weight << endl;
}
void human::SetWeight(int x)
{
if (x > 0 && x < 100)
weight = x;
else
cout << "error" << endl;
}
int main(int argc, const char * argv[]) {
// insert code here...
human Mike;
Mike.SetStature(178);
Mike.SetWeight(67);
Mike.GetStature();
Mike.get_Weight();
cout << print();
system("pause");
return 0;
}
总结
数据成员都保存在private,然后通过接口来访问。