
一:类和对象
- 类和对象在内存中的关系 this指针
- 类的静态成员:static数据成员和成员函数
- 类对象的定义:指针,普通对象,对象数组
- 类对象作为函数参数传递方式:按引用
- const修饰的对象的使用,mutable的存取权限
二: 每个对象成员属性各不相同,但成员方法共用
this指针:每个对象都有一个指向自身的this指针
this隐藏参数:标识每一个对象的不同
this指针隐藏参数:区别每一个调用公共方法(函数)的对象
类-数据类型 对象-变量
要求用到数据成员的都要this也就是:this->数据成员(所有的)
- 每个对象拥有一份属性,属性值不同,成员函数是所有对象共用的
- 一个类的不同对象响应相同的信息时,调用的是同一个函数 this指针 隐式传参 show()
- 当数据成员与形参名称相同时 this指针区分
- this指针指代自身,可以作为返回值
三:类的静态成员static
类的数据成员和成员函数都可以声明为static
静态成员解决数据共享的问题。
之前引用全局变量,现在不用 全局变量有局限性,破坏类的完整性
类的非静态成员函数可以访问类的静态成员
1.static 修饰数据成员 实现数据共享
CStaff.h
#ifndef CSTAFF_H
#define CSTAFF_H
class Staff
{
public:
Staff();
Staff(int id,char *name,char *pwd,int prole);
~Staff();
static int num;//静态数据成员 放在公有的
private:
int ID;
char name[20];
char pwd[20];
int role;
protected:
};
#endif
CStaff.cpp
#include"CStaff.h"
#include
using namespace std;
//不存在对象当中 是所有对象都能够公共访问的
//独立于对象 存在在类当中的
int Staff::num = 1000;//静态成员的初始化
Staff::Staff()
{
Staff::num++;
}
Staff::Staff(int id,char *name,char *pwd,int prole)
{
this->ID = id;
strcpy(this->name,name);
strcpy(this->pwd,pwd);
this->role = prole;
Staff::num++;
}
Staff::~Staff()
{
}
main.cpp
#include
using namespace std;
#include"CStaff.h"
int main()
{
//静态成员放在public声明
//静态成员所有对象共享,属于类
//静态成员不属于任何一个对象
Staff s1(1001,"lily","123456",1);
// 类名::静态成员变量名
cout<<"num = "<
2.static修饰成员函数 独立于对象 属于类
类::静态函数名() --- Ctools(不用对象 直接使用类名 域运算符 函数名就能访问)
#ifndef CSTAFF_H
#define CSTAFF_H
class Staff
{
public:
Staff();
Staff(int id,char *name,char *pwd,int prole);
~Staff();
static int num;//静态数据成员 放在公有的
static int getNum();//静态成员函数
private:
int ID;
char name[20];
char pwd[20];
int role;
protected:
};
#endif
//只能获取num
int Staff::getNum()
{
return num;
}
#include
using namespace std;
#include"CStaff.h"
int main()
{
//静态成员放在public声明
//静态成员所有对象共享,属于类
//静态成员不属于任何一个对象
Staff s1(1001,"lily","123456",1);
cout<<"num = "<
补充:
static char * getName(); //static获取不到非静态,只能获取静态
char * Staff::getName()
{
return name;// 静态函数中不能访问非静态成员
//静态函数中没有this指针
}
四:为什么对象不使用按值传递
因为在函数内部需要创建临时变量,走拷贝构造和析构,内存消耗过大,不建议 所以要按引用传参。
五:类对象中的const的使用
const修饰:
1.修饰类对象,对象不能修改 不能访问非const成员方法
2.修饰成员变量 不能被修改 初始化列表
构造函数:const成员变量名1(值1),const成员变量名2(值2)
3. 修饰成员方法 只能访问数据成员 不能修改 mutable 修饰想要修改的数据成员
int a=10,b=20;
const int *p =NULL;//const修饰
p = &a;
cout<<*p<
1.const修饰类对象
//报错
const CLabel title(35,7,10,2,"点菜系统");
//const修饰对象 不能访问普通成员函数
title.show();
//const修饰对象 不能访问普通成员函数
//这里不修饰对象(static修饰成员属性,成员方法,独立于对象) 就可以访问了
const Staff s1(1001,"lily","123456",1);
cout<<"num = "<
2.const修饰成员变量
#ifndef CSTAFF_H
#define CSTAFF_H
class Staff
{
public:
Staff();
Staff(int id,char *name,char *pwd,int prole);
~Staff();
static int num;//静态数据成员 放在公有的
static int getNum();
void getId(int num); //成员函数
private:
int ID;
char name[20];
char pwd[20];
int role;
const int k;//const修饰成员变量
protected:
};
#endif
初始化列表
#include"CStaff.h"
#include
using namespace std;
int Staff::num = 1000;//静态成员的初始化
Staff::Staff():k(5)//const成员初始化 初始化列表
{
Staff::num++;
}
Staff::Staff(int id,char *name,char *pwd,int prole):k(5)//const成员初始化 初始化列表
{
this->ID = id;
strcpy(this->name,name);
strcpy(this->pwd,pwd);
this->role = prole;
Staff::num++;
}
Staff::~Staff()
{
}
//只能获取num
int Staff::getNum()
{
return num;
}
void Staff::getId(int num)
{
// cout<ID<ID = num; //不能修改
// k = num; //不可以修改
cout<
void getId(); //成员函数
void Staff::getId()
{
cout<
#include
using namespace std;
#include"CStaff.h"
int main()
{
Staff s1(1001,"lily","123456",1);
cout<<"num = "<
3.const修饰成员方法
修饰成员方法 只能访问数据成员 不能修改 mutable 修饰想要修改的数据成员
#ifndef CSTAFF_H
#define CSTAFF_H
class Staff
{
public:
Staff();
Staff(int id,char *name,char *pwd,int prole);
~Staff();
static int num; //静态数据成员
// static char * getName(); //static获取不到非静态,只能获取静态
static int getNum(); //静态成员函数
// void getId(int num); //成员函数
void getId(int num) const;//const修饰成员方法(函数)
private:
mutable int ID;//真的想要改变值,使用mutable
char name[20];
char pwd[20];
int role;
const int k;//成员变量
protected:
};
#endif
void Staff::getId(int num) const//const修饰成员方法
{
// cout<ID<ID = num; //不能修改
// k = num; //不可以修改
cout<
若一定要修改就使用mutable
mutable int ID;//真的想要改变值,使用mutable
mutable可以修改但最好不使用,会破坏封装性。
小结:const修饰成员变量,成员函数,不可以修改,只能访问。const修饰对象不可以访问普通成员(变量)函数,可以访问静态成员变量,静态成员函数。
六:对象数组和对象指针数组
1.对象数组
CButton btns[3] = {
CButton(25,18,6,3,"登录"),
CButton(45,18,6,3,"退出")
};
for(int i=0;i<2;i++)
{
btns[i].show();
}
return 0;
2.对象指针数组
//对象指针数组 访问
CButton *btns[3] = {
new CButton(25,18,6,3,"登录"),
new CButton(45,18,6,3,"退出")
};
for(int i=0;i<2;i++)
{
btns[i]->show();
}
for(i=0;i<2;i++) //指针 手动删除
{
delete btns[i];
}
return 0;
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)