c++面向对象

c++面向对象

image-20191108170627836

image-20191108171009756

image-20191108171843986

image-20191108172250542

引用是指针常量

如:

1
2
3
int a=10;

int &b=a;

即:

1
int *const b=&a;

image-20191108205932273

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include

using namespace std;
void test(int ,int =10);//占位参数,且可以有默认参数
void test(int a,int b){
return a+b;
}
int fun(int a,int b=10000,int c=100){//若我们自己传入数据,就用自己的数据,如果没有,就用默认值
return a+b+c;
}
//注意事项,若某个位置有了默认参数,那么从这个位置后的参数都必须要有默认值
//如果函数声明和函数实现只能有一个有默认参数
int main(){
cout<10,20)<<endl;
}

函数重载

1 必须在同一个作用域下

2 函数名称相同

3 函数参数类型不同,或者个数不同,或者顺序不同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void fun(){
cout<<"fun"<<endl;
}
void fun(int){
cout<<"fun!"<<endl;
}
//返回值不能作为函数重载的条件
//注意事项

//1 引用作为重载的条件
void func(int &a){
cout<<"引用作为重载"<<endl;
}
void func(const int &a){
cout<<"const int &a作为重载"<<endl;
}
int a=10;
func(a);//调用的是func(int &a)
func(10);//调用的是func(const int &a)
//2 函数重载作为默认参数
void func2(int a,int b=10){
cout<<"func2(int a,int b=10)"<<endl;
}
void func2(int a){
cout<<"func2(int a)"<<endl;
}
//出现歧义
//应该避免这种情况

类和对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//圆类
#include

using namespace std;

const double PI=3.14;

class Circle{
public://权限,访问权限:public protected,private
int r ;
double calculateZC(){
return 2*PI*r;
}
};

int main(){
//创建对象
Circle c1;
c1.r=3;
cout<<"圆的周长:"<endl;

}
//struct和class的区别:
//struct默认权限是public
//class默认权限是private

构造函数

1 构造函数没有返回值,不用写void

2 函数名与类名相同

3 构造函数可以有参数,可以发生重载

4 创建对象的时候,构造函数会自动调用,且只调用一次

1 析构函数 进行清理的操作

2 没有返回值,不写void

3 函数名与类名相同,在名称前加~

4 对象在销毁前会自动调用析构函数,且只会调用一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include

using namespace std;

class Person{

public:
Person(int a){
cout<<"person构造函数的调用"<<endl;
cout<endl;
}
~Person(){
cout<<"析构函数的调用"<<endl;
}
};

void test(){
Person p1(12);//在栈上的数据
}

int main(){
test();
}

拷贝构造

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include

using namespace std;

//构造函数的分类和调用

//
class Person{
public:
Person(){
cout<<"无参"<<endl;
};//无参构造
Person(int a){//有参构造
cout<<"有参"<<endl;
}
//拷贝构造函数
Person(const Person &p){
//将传入的对象的所有属性拷贝到自己身上.
cout<<"拷贝构造"<<endl;
}
~Person(){
cout<<"析构"<<endl;
}
};

int main(){
//括号法
Person p1;
Person p2(12);
//调用拷贝构造函数
Person p3(p1);
//注意事项:
//不能用Person p1()调用,编译器会认为一个函数,不会认为是创建对象
//显示法
Person p4=p1;
Person(10);//匿名对象 ,当前行执行结束,系统会立即回收掉匿名对象
//不要利用拷贝构造函数初始化匿名对象如: Person(p1); 编译器会认为Person (p1)==Person p1 ,对象声明
//隐式转换法
Person p5=10;//等价于 Person p5=Person(10); 有参构造
Person p6=p5;//拷贝
}

拷贝构造函数使用时机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include

using namespace std;

//拷贝构造函数调用时机
//1 使用一个已经创建的对象来初始化一个新对象
//2 值传递的方式给函数传值
//3 值返回局部对象
class Person{
public:
Person(){
cout<<"默认"<<endl;
};
Person(const Person &p){
cout<<"拷贝构造"<<endl;
}
~Person(){
cout<<"析构"<<endl;
}
};

void test1(){
Person p1;
Person p2(p1);
}
void dowork(Person p){

}
void test2(){
Person p3;
dowork(p3);
}
Person dowork1(){
Person p4;
cout<<"p4--"<<(int*)&p4<<endl;
return p4;
}
void test3(){
Person p5=dowork1();
cout<<(int*)&p5<<endl;
}
int main(){
test1();
test2();
test3();

return 0;
}
/*默认
拷贝构造
析构
析构
默认
拷贝构造
析构
析构
默认
p4--0019FE64
拷贝构造
析构
0019FECC
析构
Press any key to continue
*/
//如果写了有参构造,编译器不提供无参
//写了拷贝构造,不提供其他构造函数

深拷贝浅拷贝

浅拷贝:简单的赋值拷贝操作

深拷贝:在堆区重新申请空间,进行拷贝操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include

using namespace std;

//深拷贝,浅拷贝
class Person{
public:
Person(){
}
~Person(){
//将堆区开辟的数据释放
if(P_height!=NULL){
delete P_height;
P_height=NULL;
}
}
Person(int age,int height){
P_height=new int(height);
P_age=age;
}
Person(const Person &p){
P_age=p.P_age;
//p_height=p.P_height;编译器执行的是这种
//用深拷贝解决
P_height=new int(*p.P_height);
}

int P_age;
int *P_height;
};

void Test1(){
Person p(12,150);
Person p1(p);
cout<endl;
cout<endl;
}
int main(){
Test1();
return 0;
}

初始化列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include

using namespace std;
class Person{
public:
Person(int a,int b,int c):a(a),b(b),c(c){//初始化列表
}
int a;
int b;
int c;
};
int main(){
Person p(1,2,3);
cout<" "<endl;

}

对象成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include
#include
using namespace std;

class Phone{
public:
Phone(string Phone_name):name(Phone_name){
cout<<"Phone的构造";
}
string name;
};
class Person{
public:
Person(string name,int id):phone(name),P_id(id){
cout<<"Person的构造";
}
Phone phone;
int P_id;
};

void test(){
Person p("apple",12);
cout<endl;
}
int main(){
test();

return 0;
}
//Phone的构造 Person的构造 apple
//先初始化Phone再初始化Person

static成员

静态成员函数:所有对象共享,只能访问静态变量

静态成员变量:所有对象共有,类内声明,类外定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include

using namespace std;

class Person{
public:
static void fun(){
cout<<"静态成员函数"<<endl;
cout<endl;
}
static int a;

};
int Person::a=0;
int main(){
//对象访问
Person p;
p.fun();
Person::fun();//类访问
}

只有非静态成员变量属于对象

空对象的内存占用为1个字节

this指针: this指针指向被调用的成员函数所属的对象,this指针是指针常量其指向不可以修改

1 可以解决名称冲突

2 返回此对象 return *this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include

using namespace std;

class Person{
public:
int age;
Person(int age):age(age){
}
Person& addPersonAge(Person &p){
this->age+=p.age;
return *this;
}

};

int main(){
Person p1(10);
Person p2(10);
p2.addPersonAge(p1).addPersonAge(p1).addPersonAge(p1);
cout<endl;
cout<endl;

}

常函数 :不能修改指针指向和值.

常对象:只能调用常函数

image-20191110010838064

image-20191110011503286

友元函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include
using namespace std;

class Building{
friend void visit(Building &b);//友元函数//友元类 firend class GoodGay;
public:
string sittingRoom;
Building(){
sittingRoom="客厅";
bedRoom="卧室";
}
private:
string bedRoom;
};

void visit(Building &b){
cout<endl;
cout<endl;
}

int main(){
Building b;
visit(b);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include
using namespace std;

class Building{
friend class GoodGay;
public:
string sittingRoom;
Building();
private:
string bedRoom;
};
Building::Building(){
sittingRoom="客厅";
bedRoom="卧室";
}

class GoodGay{
public:
GoodGay();
void visit();
Building *building;
};
GoodGay::GoodGay(){
building=new Building;
}
void GoodGay::visit(){
cout<sittingRoom<<endl;
cout<bedRoom<<endl;
}


int main(){
GoodGay gay;
gay.visit();
}

成员函数做友元

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include
//此处若要将哪个类的成员函数做右元,其类应放在前面,此乃大坑
using namespace std;
class Building;
class GoodGay{
public:
GoodGay();
Building *building;
void visit();
};
class Building{
friend void GoodGay::visit();//将GoodGay类的visit作为BUilding的友元,GoodGay应该先定义.注意与友元类的区别
public:
Building();
string sitRoom;
private:
string bedRoom;

};
Building::Building(){
sitRoom="客厅";
bedRoom="卧室";
}
GoodGay::GoodGay(){
building=new Building;
}
void GoodGay::visit(){
cout<sitRoom<<endl;
cout<bedRoom<<endl;
}
int main(){
GoodGay gay;
gay.visit();
}

本文标题:c++面向对象

文章作者:philxling

发布时间:2020年04月02日 - 23:05

最后更新:2020年10月15日 - 12:26

原始链接:http://philxling.club/2020/04/02/c++/2-c-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

philxling wechat
ex. subscribe to my blog by scanning my public wechat account
-------------本文结束感谢您的阅读-------------
谢谢你给我糖吃!!!
0%