C++中的靜態(tài)成員與this指針
一晶疼、靜態(tài)成員
1酒贬、static 關(guān)鍵字
- 靜態(tài)屬性只能在全局范圍內(nèi)進(jìn)行初始化賦值
- 靜態(tài)方法可以直接通過類名進(jìn)行訪問又憨,也可以通過對(duì)象名進(jìn)行訪問
class Teacher {
private:
char* name;
public:
//計(jì)數(shù)器
static int total;
public:
Teacher(char* name) {
this->name = name;
cout << "Teacher有參構(gòu)造函數(shù)" << endl;
}
~Teacher() {
cout << "Teacher析構(gòu)函數(shù)" << endl;
}
void setName(char* name) {
this->name = name;
}
char* getName() {
return this->name;
}
//計(jì)數(shù)的靜態(tài)函數(shù)
static void count(){
total++;
cout << "total:" << total << endl;
}
};
//靜態(tài)屬性初始化賦值
int Teacher::total = 10;
void main() {
Teacher::total++;
cout << Teacher::total << endl;
//直接通過類名訪問
Teacher::count();
cout << Teacher::total << endl;
//通過對(duì)象名訪問
Teacher t1((char*)"JACK");
t1.count();
cout << Teacher::total << endl;
getchar();
}
二翠霍、類的大小
C/C++ 內(nèi)存分區(qū):棧、堆蠢莺、全局(靜態(tài)寒匙、全局)、常量區(qū)(字符串)躏将、程序代碼區(qū)
普通屬性與結(jié)構(gòu)體具有相同的內(nèi)存布局
class A {
public:
int i;
int j;
int k;
};
class B {
public:
int i;
int j;
int k;
void myprint() {
cout << "打印" << endl;
}
};
void main() {
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
getchar();
}
三锄弱、this 指針
1、this指針
this 是當(dāng)前對(duì)象的指針
因?yàn)楹瘮?shù)是共享的祸憋,所有使用this指針用來標(biāo)識(shí)當(dāng)前對(duì)象
class Teacher {
private:
char* name;
int age;
public:
Teacher(char* name,int age) {
this->name = name;
this->age = age;
cout << "Teacher有參構(gòu)造函數(shù)" << endl;
}
~Teacher() {
cout << "Teacher析構(gòu)函數(shù)" << endl;
}
void setName(char* name) {
this->name = name;
}
char* getName() {
return this->name;
}
void setAge(int age) {
this->age = age;
}
int getAge() {
return this->age;
}
void myprint() {
cout << this->getName() << "," << this->getAge() << endl;
}
};
void main() {
Teacher t1((char*)"Jack", 20);
Teacher t2((char*)"Rose", 18);
t1.myprint();
t2.myprint();
getchar();
}
2会宪、常函數(shù)(const修飾函數(shù))
常函數(shù),修飾的是this蚯窥;既不能改變指針的值掸鹅,也不能改變指針指向的內(nèi)容
常量對(duì)象只能調(diào)用常量函數(shù),不能調(diào)用非常量函數(shù)
常函數(shù)當(dāng)前對(duì)象不能被修改,防止數(shù)據(jù)成員被非法訪問
class Teacher {
private:
char* name;
int age;
public:
Teacher(char* name,int age) {
this->name = name;
this->age = age;
cout << "Teacher有參構(gòu)造函數(shù)" << endl;
}
~Teacher() {
cout << "Teacher析構(gòu)函數(shù)" << endl;
}
void setName(char* name) {
this->name = name;
}
char* getName() {
return this->name;
}
void setAge(int age) {
this->age = age;
}
int getAge() {
return this->age;
}
void myprint() const {
printf("%#x\n",this);
//不能改變屬性的值
//this->name = "Jason";
//不能改變this指針的值
//this = (Teacher*)0x000090;
cout << this->getName() << "," << this->getAge() << endl;
}
};
四拦赠、友元函數(shù)與友元類
1巍沙、友元函數(shù)
在友元函數(shù)中可以訪問私有的屬性
class A {
private:
int i;
public:
A(int i) {
this->i = i;
}
void myprint() {
cout << i << endl;
}
//友元函數(shù)
friend void modify_i(A*p, int a);
};
//友元函數(shù)的實(shí)現(xiàn),在友元函數(shù)中可以訪問私有的屬性
void modify_i(A*p, int a) {
p->i = a;
}
void main() {
A* a = new A(10);
a->myprint();
modify_i(a, 20);
a->myprint();
getchar();
}
2、友元類
友元類可以訪問引用友元類的類的任何成員
class A {
//友元類
friend class B;
private:
int i;
public:
A(int i) {
this->i = i;
}
void myprint() {
cout << i << endl;
}
//友元函數(shù)
friend void modify_i(A*p, int a);
};
//友元函數(shù)的實(shí)現(xiàn),在友元函數(shù)中可以訪問私有的屬性
void modify_i(A*p, int a) {
p->i = a;
}
class B {
private:
A a;
public:
B(int a_i):a(a_i) {
this->a = a;
}
//B這個(gè)友元類可以訪問A類的任何成員
void accessAny() {
a.i = 30;
}
A getA() {
return a;
}
};
void main() {
B* b = new B(10);
A a = b->getA();
a.myprint();
b->accessAny();
a = b->getA();
a.myprint();
getchar();
}
五荷鼠、運(yùn)算符重載
運(yùn)算符的重載句携,本質(zhì)上還是函數(shù)的調(diào)用
1、單獨(dú)進(jìn)行運(yùn)算符重載
class Point {
public:
int x;
int y;
public:
Point(int x = 0,int y = 0) {
this->x = x;
this->y = y;
}
void myprint() {
cout << x << "," << y << endl;
}
};
//重載+號(hào)
Point operator+(Point &p1, Point &p2) {
Point tmp(p1.x + p2.x, p1.y + p2.y);
return tmp;
}
//重載-號(hào)
Point operator-(Point &p1, Point &p2) {
Point tmp(p1.x - p2.x, p1.y - p2.y);
return tmp;
}
void main() {
Point p1(10,20);
Point p2(20,10);
Point p3 = p1 + p2;
p3.myprint();
getchar();
}
2允乐、類成員函數(shù)運(yùn)算符重載
class Point {
public:
int x;
int y;
public:
Point(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
void myprint() {
cout << x << "," << y << endl;
}
//成員函數(shù)運(yùn)算符重載+號(hào)
Point operator+(Point &p2) {
Point tmp(this->x + p2.x, this->y + p2.y);
return tmp;
}
//成員函數(shù)運(yùn)算符重載-號(hào)
Point operator-(Point &p2) {
Point tmp(this->x - p2.x, this->y - p2.y);
return tmp;
}
};
void main() {
Point p1(10, 20);
Point p2(20, 10);
//p1.operator+(p1);
Point p3 = p1 + p2;
p3.myprint();
getchar();
}
3矮嫉、友元函數(shù)運(yùn)算符重載
當(dāng)屬性私有時(shí),通過友元函數(shù)完成運(yùn)算符重載
class Point {
friend Point operator+(Point &p1, Point &p2);
friend Point operator-(Point &p1, Point &p2);
private:
int x;
int y;
public:
Point(int x = 0, int y = 0) {
this->x = x;
this->y = y;
}
void myprint() {
cout << x << "," << y << endl;
}
};
//友元函數(shù)運(yùn)算符重載+號(hào)
Point operator+(Point &p1, Point &p2) {
Point tmp(p1.x + p2.x, p1.y + p2.y);
return tmp;
}
//友元函數(shù)運(yùn)算符重載-號(hào)
Point operator-(Point &p1, Point &p2) {
Point tmp(p1.x - p2.x, p1.y - p2.y);
return tmp;
}
void main() {
Point p1(10, 20);
Point p2(20, 10);
Point p3 = p1 + p2;
p3.myprint();
getchar();
}