1.C++类的定义
C++使用class关键字定义一个类:
public:公共的行为或属性(类外可以调用);
private:表示该部分内容是私密的,不能被外部访问或调用,只能在类内调用;
protected:保护成员,和私有成员类似,不过在派生类可以调用;
比如我们建立一个people类
#include <iostream> using namespace std; class people{ public: void a(){ cout<<name; } private: string name="小明"; void b(){ cout<<"dsfsfds"; //不必在意; } }; int main(){ people cao; //实例化 cao.a(); return 0; }
类的传参,比如我们要传入人的身高和体重,可以通过一个函数传入,传给私有变量;
#include <iostream> using namespace std; class people{ public: void a(){ cout<<name<<' '<<height<<' '<<weight; } void chuanru(int x,int y){ //x,y是函数内的的变量,因此需要赋值给整个类的变量,作用域不同 height=x; weight=y; } private: string name="小明"; int height; int weight; }; int main(){ people cao; cao.chuanru(170,130); cao.a(); return 0; }
此外类内的函数或者方法可以放到类外面,需要先在类内声明函数,通过“::”作用域操作符实现例如:
#include <iostream> using namespace std; class people{ public: void a(); void chuanru(int x,int y); //函数声明 private: string name="小明"; int height; int weight; }; void people::a(){ //表面是people的a函数 cout<<name<<' '<<height<<' '<<weight; } void people::chuanru(int x,int y){ height=x; weight=y; } int main(){ people cao; cao.chuanru(170,130); cao.a(); return 0; }
2.初识构造函数和析构函数
先不管那些拷贝构造函数和转换构造函数,我们先学习普通的,
#include <iostream> using namespace std; class people{ public: people(int x=8,int y=9){ //构造函数,可以设置默认值 height=x; weight=y; } void a(){ cout<<name<<' '<<height<<' '<<weight; } private: string name="小明"; int height; int weight; }; int main(){ people cao(170,130); //构造函数传参, cao.a(); return 0; }
同理构造函数也可以放到类外面
#include <iostream> using namespace std; class people{ public: people(int x,int y); void a(); private: string name="小明"; int height; int weight; }; people::people(int x=9,int y=8){ //类内或类外只能有一个地方初始化 height=x; weight=y; } void people::a(){ cout<<name<<' '<<height<<' '<<weight; } int main(){ people cao(170,130); cao.a(); return 0; }
构造函数用来初始化类,析构函数与构造函数相反;在对象生命周期结束后自动调用,用于在对象删除之前的清理工作,清理对象释放内存;
#include <iostream> using namespace std; class people{ public: people(int x,int y); void a(); ~people(); private: string name="小明"; int height; int weight; }; people::people(int x=9,int y=8){ height=x; weight=y; } people::~people(){ } void people::a(){ cout<<name<<' '<<height<<' '<<weight; } int main(){ people cao(170,130); cao.a(); return 0; }
是生命周期结束,举个例子:
#include <iostream> using namespace std; class people{ public: people(string n,int x,int y); void a(); ~people(); private: string name; int height; int weight; }; people::people(string n,int x=9,int y=8){ name=n; height=x; weight=y; cout<<name<<' '<<"调用构造函数"<<endl; } people::~people(){ cout<<name<<' '<<"调用析构函数"<<endl; } void people::a(){ cout<<name<<' '<<height<<' '<<weight<<endl;; } void shiyan(){ people lin("小刚",180,190); } int main(){ people cao("小明",170,130); shiyan(); cout<<"main还没结束!"<<endl; return 0; }
小明是在main函数内,小刚是在shiyan()函数内,调用完shiyan() 函数后小刚生命周期结束,调用小刚的析构函数,而小明是等到main函数结束后调用析构函数;
其实构造函数还有初始化列表形式:
#include <iostream> using namespace std; class people{ public: people(int x,int y); //构造函数 void a(); ~people(); //析构函数 private: string name="小明"; int height; int weight; }; people::people(int x=9,int y=8):height(x),weight(y){} //初始化列表 people::~people(){ } void people::a(){ cout<<name<<' '<<height<<' '<<weight; } int main(){ people cao(170,130); cao.a(); return 0; }