前言
面向过程和面向对象的初步认识
C语言是面向过程的,关注的是过程,分析出求解问题的步骤通过函数调用来逐步解决问题
C++是基于面向对象的,关注的是对象,将一件事情拆分成不同的对象,靠对象之间的交互完成
类的引入
C语言结构体只能定义变量,在C++中结构体不仅可以定义变量还能定义函数
C++兼容C语言struct的所有用法,struct同时升级成了类,类名就是类型
C语言定义一个栈
//C
struct Stack
{
int* a;
int top;
int capacity;
};
void StackInit(struct Stack* ps);
void StackPush(struct Stack* ps,int x);
//...
int mian()
{
struct Stack s1;//C的
return 0;
}
C++定义一个栈
调用类里面的成员函数也很方便
//C++
struct Stack
{
int* a;
int top;
int capacity;
void Init()
{
a = 0;
top = 0;
capacity = 0;
}
void Push(int x)
{
//...
}
//...
};
int mian()
{
//struct Stack s1;//C的
//Stack s2;//C++的 类名就是类型
Stack s1;
s1.Init();
s1.Push(1);
s1.Push(2);
s1.Push(3);
//C语言
//struct Stack s1;
//StackInit(&s1);
//StackPush(&s1,1);
//StackPush(&s1, 2);
//StackPush(&s1, 3);
return 0;
}
上面结构体的定义,在C++更喜欢用class来代替
类的定义
class className
{
//类体:由成员函数和成员变量组成
};
//1.声明和定义都在类体内
class Date
{
//成员变量
int _year;
int _month;
int _day;
//成员函数
void Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
//......
};
2.声明和定义分离:
类声明放在.h文件中,成员函数定义放在.cpp文件中,注意:成员函数名前需要加类名::
.h
#pragma once
#include<iostream>
using namespace std;
class Date1
{
//成员变量
int _year;
int _month;
int _day;
void Print();
};
.cpp
#include"date.h"
void Date1::Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
类的访问限定符及封装
访问限定符
C++实现封装的方式:
用类将对象的属性和方法绑定在一块,让对象更加完善,通过访问权限的选择性的将其接口提供给外部用户使用
访问限定符
public(公有) 类里类外都能访问
protected(保护)与private(私有) 只有类里能访问,类外不行 protected和private是类似的
struct和class还有一个区别
struct不写访问限定符默认是公有的
class不写访问限定符默认是私有的
注意:访问限定符只在编译时有用,当数据映射到内存后,没有任何访问限定符的区别
封装
面向对象的三大特性:封装、继承、多态
封装:将数据和操作数据的方法进行有机结合,隐藏对象的属性和实现细节,仅对外公开接口来和对象进行交互
封装本质上是一种管理,让用户更加方便使用类
在C++语言中实现封装,可以通过类将数据以及操作数据的方法进行有机结合,通过访问权限来隐藏对象内部实现细节,控制哪些方法(成员函数)可以在类的外部直接使用
类的作用域
类定义了一个新的作用域
{}就是类的作用域
类的所有成员都在类的作用域中
如果向我们上面所说的,声明和定义分离该怎么办呢?
在类外定义成员是我们需要用域作用限定符来指明该成员属于哪个类域
class Date1
{
private:
//成员变量
int _year;
int _month;
int _day;
public:
void Print();
};
#include"date.h"
void Date1::Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
类的实例化
用类的类型创建对象的过程就叫类的实例化
如果把类比作一份设计图,那么对象就是基于这份设计图建造出来的房子
而基于设计图建造出房子的过程就是类的实例化
类和对象的关系是一对多的关系 一个类可以定义多个对象(C++更喜欢把变量称为对象)
类对象模型
如何计算类对象的大小
class d1
{
int a;
char b;
int c;
/*void Print();*/
};
int main()
{
cout << sizeof(d1) << endl;
return 0;
}
这说明类也要考虑内存对齐
class d1
{
int a;
char b;
int c;
void Print()
{
cout << a << " " << b << " " << c << endl;
}
};
int main()
{
cout << sizeof(d1) << endl;
return 0;
}
通过上述代码可以说明类的大小进行计算的时候只计算成员变量,成员函数不在对象里面
类对象的存储方式猜测
class Date1
{
private:
//成员变量
int _year;
int _month;
int _day;
public:
void Print();
void Init(int year,int month,int day);
};
#include"date.h"
void Date1::Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
void Date1::Init(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
}
#include"date.h"
int main()
{
Date1 d1;
Date1 d2;
d1.Init(2023, 10, 26);
d2.Init(2022, 10, 26);
return 0;
}
d1与d2调用的是同一个函数
我们可以猜测类对象的存储方式为
this指针
引入
#include<iostream>
using namespace std;
class Date1
{
private:
//成员变量
int _year;
int _month;
int _day;
public:
void Print();
void Init(int year,int month,int day);
};
#include"date.h"
void Date1::Print()
{
cout << _year << " " << _month << " " << _day << endl;
}
void Date1::Init(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
}
#include"date.h"
int main()
{
Date1 d1;
Date1 d2;
d1.Init(2023, 10, 26);
d2.Init(2022, 10, 26);
d1.Print();
d2.Print();
return 0;
}
this指针的特性
1.this指针的类型,类的类型*const,即成员函数中,不能给this指针赋值
2.只能在"成员函数" 的内部使用
3.this指针本质上是成员函数的形参,当对象调用成员函数时,将对象的地址作为实参传递给this形参,所以对象中不存储this指针
4.this指针是"成员函数"第一个隐含的指针形参,一般情况由编译器通过ecx寄存器自动传递,不需要用户传递