一.类对象作为类成员
代码:
#include <iostream>
using namespace std;
class phone {
public:
string shouji;
phone(string shouji1) :shouji(shouji1) {
cout << "phone的构造函数调用" << endl;
}
~phone() {
cout << "phone的析构函数调用" << endl;
}
};
class person {
public:
int age;
string name;
phone shouji;
//隐式转换相当于shouji=phone(shouji1)
person(int a, string name1, string shouji1) :age(a), name(name1), shouji(shouji1) {
cout << "person的构造函数调用" << endl;
}
~person() {
cout << "person的析构函数调用" << endl;
}
};
void fun() {
person p(23, "小明", "苹果");
}
int main() {
fun();
return 0;
}
注意:
二.静态成员
2.1静态成员变量
代码:
#include <iostream>
using namespace std;
class person {
public:
static int age;
};
int person::age = 10;
void fun() {
person p;
p.age = 90;
person p1;
cout << p1.age << endl;
}
int main() {
fun();
return 0;
}
代码:
#include <iostream>
using namespace std;
class person {
public:
static int age;
};
int person::age = 10;
void fun() {
person p;
p.age = 90;
cout << person::age << endl;
}
int main() {
fun();
return 0;
}
2.2静态成员函数
代码:
#include <iostream>
using namespace std;
class person {
public:
static int age;
static void fun(int a) {
age = a; //只能调用静态成员变量
cout << "静态函数fun调用" << endl;
}
};
int person::age = 100;
void dioayong() {
person p;
p.fun(99); //对象调用
person::fun(66); //类名调用
cout << p.age << endl;
}
int main() {
dioayong();
return 0;
}
三.this指针
3.1成员变量和函数的存储
代码:
#include <iostream>
using namespace std;
class person {
};
void fun() {
person p;
cout<<sizeof(p);
}
int main() {
fun();
return 0;
}
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
char b;
};
void fun() {
person p;
cout<<sizeof(p);
}
int main() {
fun();
return 0;
}
代码:
#include <iostream>
using namespace std;
class person {
public:
int a;
static int b;
void fun() {
cout << "fun函数调用" << endl;
}
static void fun1() {
cout << "fun1函数调用" << endl;
}
};
int person::b = 10;
void fun() {
person p;
cout<<sizeof(p);
}
int main() {
fun();
return 0;
}
3.2this指针使用
代码:
#include <iostream>
using namespace std;
class people {
public:
int age;
people(int age) {
this->age = age;
}
};
void zhixing() {
people p(10);
cout << p.age << endl;
}
int main() {
zhixing();
return 0;
}
#include <iostream>
using namespace std;
class people {
public:
int age;
people(int age) {
this->age = age;
}
people& add(const people& p) {
this->age += p.age;
return *this;
}
};
void zhixing() {
people p1(10);
people p2(20);
p2.add(p1).add(p1).add(p1).add(p1);
cout << p2.age << endl;
}
int main() {
zhixing();
return 0;
}
四.空指针访问成员函数
代码:
#include <iostream>
using namespace std;
class person {
public:
int age;
void fun1() {
cout << "fun1调用" << endl;
}
void fun2() {
cout << "fun2调用" << age << endl;
}
};
void diaoyong() {
person* p = NULL;
p->fun1();
p->age = 10;
p->fun2();
}
int main() {
diaoyong();
return 0;
}
代码:
#include <iostream>
using namespace std;
class person {
public:
int age;
void fun1() {
cout << "fun1调用" << endl;
}
void fun2() {
if (this == NULL) {
return;
}
cout << "fun2调用" << age << endl;
}
};
void diaoyong() {
person* p = NULL;
p->fun1();
p->fun2();
}
int main() {
diaoyong();
return 0;
}
五.const修饰成员函数
5.1常函数
代码:
#include <iostream>
using namespace std;
class person {
public:
int age;
void showage(int m) const{
// age = 89; //报错
m = 100;
cout << "age=" << age <<" m=" << m << endl;
}
};
void fun() {
person p;
p.age = 10;
p.showage(7);
}
int main() {
fun();
return 0;
}
#include <iostream>
using namespace std;
class person {
public:
mutable int age;
void showage(int m) const{ //常函数
age = 89; //报错
m = 100;
cout << "age=" << age <<" m=" << m << endl;
}
};
void fun() {
person p;
p.age = 10;
p.showage(7);
}
int main() {
fun();
return 0;
}
5.2 常对象
代码:
#include <iostream>
using namespace std;
class person {
public:
mutable int age;
void showage1(int m) const{
//age = 89; //报错
m = 100;
cout << "age=" << age <<" m=" << m << endl;
}
void showage2(int m) {
age = 89;
m = 100;
cout << "age=" << age << " m=" << m << endl;
}
};
void fun() {
const person p;
p.age = 10;
p.showage1(7);
//p.showage2(7); 报错
}
int main() {
fun();
return 0;
}
六.友元
友元关键字:friend
6.1全局函数做友元
代码:
#include <iostream>
using namespace std;
class home {
friend void func(home& p);
public:
home(string keting, string woshi, string cuosuo):keting(keting),woshi(woshi),cuosuo(cuosuo) {
}
string keting;
private:
string woshi;
protected:
string cuosuo;
};
void func(home &p) {
cout << p.keting << ' ' << p.woshi << ' ' << p.cuosuo << endl;
}
void fun() {
home p("客厅", "卧室", "厕所");
func(p);
}
int main() {
fun();
//cout << p.keting << ' ' << p.woshi << ' ' << p.cuosuo << endl;
//保护和私有属性的不能访问
return 0;
}
6.2类做友元
#include <iostream>
using namespace std;
class building {
friend class goodgay; //声明友元类
public:
building();
string keting;
private:
string cesuo;
protected:
string woshi;
};
building::building() {
keting = "客厅";
cesuo = "厕所";
woshi = "卧室";
}
class goodgay {
public:
goodgay();
void show();
private:
building* p;
};
void goodgay::show() {
cout << this->p->keting << ' ' << this->p->cesuo << ' ' << this->p->woshi << endl;
}
goodgay::goodgay() {
p = new building;
}
void f() {
goodgay a;
a.show();
}
int main() {
f();
return 0;
}
6.3成员函数做友元
代码:
#include <iostream>
using namespace std;
class building;
class goodgay {
public:
goodgay();
void show1(building& p);
private:
building* p;
};
class building {
friend void goodgay::show1(building& p); //声明友元类
public:
building();
string keting;
private:
string cesuo;
protected:
string woshi;
};
building::building() {
keting = "客厅";
cesuo = "厕所";
woshi = "卧室";
}
void goodgay::show1(building& p1) {
cout <<p1.keting << ' ' << p1.cesuo << ' ' << p1.woshi << endl;
}
//void goodgay::show2() {
// cout << this->p->ketipng << ' ' << this->p->cesuo << ' ' << this->p->woshi << endl;
//}
//无权限
goodgay::goodgay() {
p = new building;
}
void f() {
goodgay a;
building b;
a.show1(b);
}
int main() {
f();
return 0;
}