目录
1.类的定义
1.1类定义的格式
1.class为定义类的关键字,Stack为类的名字,{}中为类的主体,注意类定义结束时后面的分号不能省略。这个和我们在C语言中的结构体比较类似,下面会讲到类和结构体的区别。类主体中内容称为类的成员:类中的变量称为类的属性或成员变量;类中的函数称为类的方法或者成员函数。
2.为了区分成员变量,一般习惯会在成员变量之前加个特殊标识,比如在成员变量前面加_或者m进行区分。这个不是强制的,主要看自己的需求。
3.C++中的struct也可以定义类,C++兼容c语言的struct的用法,同时把strcut也升级成了类,也就是说在c++里面strcut既可以当结构体用,也可以当作类来使用。
#include<iostream>
using namespace std;
//C++兼容c语言中的struct用法
typedef struct ListNode
{
struct ListNode* next;
int data;
}LTNode;
//C++中的用法
//不需要typedef,ListNodeCPP可以表示类型
struct ListNodeCPP
{
//成员函数
void Init(int x) {
next = nullptr;
data = x;
}
//成员变量
ListNodeCPP* next;
int data;
};
4.定义在类里面的函数默认为内联函数。
#include<iostream>
#include<assert.h>
using namespace std;
typedef int STDateType;
class Stack
{
public:
//成员函数
Stack(int n = 4)
{
_arr = (STDateType*)malloc(sizeof(STDateType) * n);
if (_arr == nullptr)
{
perror("malloc fail!");
exit(1);
}
_capacity = n;
_top = 0;
}
void Push(int x)
{
//扩容
if (_capacity == _top)
{
STDateType newcapacity = 2 * _capacity;
STDateType* tmp = (STDateType*)realloc(_arr, sizeof(STDateType) * newcapacity);
if (tmp == nullptr)
{
perror("realloc fail!");
exit(1);
}
_arr = tmp;
_capacity = newcapacity;
}
_arr[_top++] = x;
}
int Top()
{
assert(_top > 0);
return _arr[_top - 1];
}
~Stack()
{
free(_arr);
_arr = nullptr;
_top = _capacity = 0;
}
private:
//成员变量
STDateType* _arr;
int _capacity;
int _top;
};//分号不能省略
int main()
{
Stack st;
st.Push(1);
st.Push(2);
st.Push(3);
cout << st.Top() << endl;
return 0;
}
1.2访问限定符
1.在c语言中方法和数据都是分开的,但是在c++中把数据和方法整合到一起了:C++一般实现封装的方式,用类将对象的属性与方法结合在一块,让对象更加完善,通过访问权限选择性的将其接口提供给外部的用户使用。
2.public正如它的含义一样修饰的成员在类外可以直接被访问;protected和private修饰的成员在类外不能被直接访问。
访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止,如果后面没有访问限定符,作用域就到}即类结束。
class定义成员没有被访问限定符修饰时默认为private,struct默认为public。
一般成员变量都会被限制为private/protected,需要给别人使用的成员函数都会放为public。
1.3类域
1.类定义了一个新的作用域,类的所有成员都在类的作用域中,在类体外定义成员时,需要使用::作用域操作符指明成员属于哪个类域。
类域影响的是编译的查找规则,下面程序中Init如果不指定类域Date,那么编译器就会把Init当成全局函数,那么编译时,就找不到_year等成员的声明/定义在哪里,指定类域Date,就是直到Init是成员函数,当前域找不到_year等成员,就会 到类域中去查找。
#include<iostream>
using namespace std;
class Date
{
public:
void Init(int year = 1, int month = 1, int day = 1);
private:
int _year;
int _month;
int _day;
};
void Date::Init(int year , int month , int day )
{
_year = year;
_month = month;
_day = day;
}
int main()
{
Date d;
d.Init(2003, 5, 6);
return 0;
}
2.实例化
2.1实例化概念
1.用类类型在物理内存中创建对象的过程,称为类实例化出对象 。
2.类是对象进行一种抽象的描述,是一个模型一样的东西,限定了类有哪些成员变量,这些成员变量只是声明,没有分配空间,用类实例化对象时,才会分配空间。
#include<iostream>
using namespace std;
class Date
{
public:
void Init(int year = 1, int month = 1, int day = 1);
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
void Date::Init(int year , int month , int day )
{
_year = year;
_month = month;
_day = day;
}
int main()
{
Date d1;
Date d2;
d1.Init(2003, 5, 6);
d2.Init(20023, 5, 6);
d1.Print();
d2.Print();
return 0;
}
2.2对象的大小
Date实例化出了两个对象分别为d1,d2;d1,d2分别存储各自的独立的成员变量,但是它们两个的Init和Print函数是一样的,存储在对象中就浪费了,函数是不需要存储的,函数指针就是一个地址,调用函数在编译时会被编译成汇编指令call这个函数的地址,在编译链接时就要找到这个函数的地址而不是在运行的时候找。计算大小时只需要计算成员变量就可以了
和C语言的结构体一样需要内存对齐:
#include<iostream>
using namespace std;
class A
{
public:
void Print()
{
cout << "void Print()" << endl;
}
private:
char _ch;
int _i;
};
class B
{
public:
void Print()
{
cout << "void Print()" << endl;
}
};
class C
{
};
int main()
{
A a;
B b;
C c;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
cout << sizeof(c) << endl;
return 0;
}
3.this指针
Date类中有Init和Print两个成员函数,函数体中没有关于不同对象的区分,那么d1调用Init和Print两个成员函数时,是如何正确识别d1和d2的呢?C++通过this指针解决了这个问题。
• 编译器编译后,类的成员函数默认都会在形参第⼀个位置,增加⼀个当前类类型的指针,叫做this 指针。⽐如Date类的Init的真实原型为, void Init(Date* const this, int year, int month, int day)
#include<iostream>
using namespace std;
class Date
{
public:
//void Init(Date* const this int year, int month, int day)
void Init(int year, int month, int day)
{
_year = year;
this->_month = month;
this->_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2;
//d1.Init(&d1,2003, 5, 6);
d1.Init(2003, 5, 6);
d2.Init(20023, 5, 6);
d1.Print();
d2.Print();
return 0;
}
4.C++和C语言实现Stack对比
⾯向对象三⼤特性:封装、继承、多态,下⾯的对⽐我们可以初步了解⼀下封装。通过下⾯两份代码对⽐,我们发现C++实现Stack形态上还是发⽣了挺多的变化,底层和逻辑上没啥变化。• C++中数据和函数都放到了类⾥⾯,通过访问限定符进⾏了限制,不能再随意通过对象直接修改数 据,这是C++封装的⼀种体现,这个是最重要的变化。这⾥的封装的本质是⼀种更严格规范的管 理,避免出现乱访问修改的问题。当然封装不仅仅是这样的,我们后⾯还需要不断的去学习。• C++中有⼀些相对⽅便的语法,⽐如Init给的缺省参数会⽅便很多,成员函数每次不需要传对象地址,因为this指针隐含的传递了,⽅便了很多,使⽤类型不再需要typedef⽤类名就很⽅便• 在我们这个C++⼊⻔阶段实现的Stack看起来变了很多,但是实质上变化不⼤。等着我们后⾯看STL 中的⽤适配器实现的Stack,⼤家再感受C++的魅⼒。
C实现栈的代码:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
//初始化
void STInite(ST* ps)
{
assert(ps);
ps->data = NULL;
ps->capacity = ps->top = 0;
}
//销毁
void STDestroy(ST* ps)
{
assert(ps);
if (ps->data)
{
free(ps->data);
}
ps->data = NULL;
ps->capacity = ps->top = 0;
}
//入栈
void STPush(ST* ps, STDatatype x)
{
assert(ps);
//判断一下空间是否充足
if (ps->capacity == ps->top)
{
//扩容
int newcapacity = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
STDatatype* tmp = (STDatatype*)realloc(ps->data, newcapacity * sizeof(STDatatype));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1);
}
ps->data = tmp;
ps->capacity = newcapacity;
}
//插入数据
ps->data[ps->top++] = x;
}
//判断栈是否为空
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
//出栈
void STPop(ST* ps)
{
assert(ps);
//判断栈是否为空
assert(!STEmpty(ps));
--ps->top;
}
//取栈顶元素
STDatatype STTop(ST* ps)
{
assert(ps);
//判断栈是否为空
assert(!STEmpty(ps));
return ps->data[ps->top - 1 ];
}
//获取栈顶有效元素的个数
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
#include"stack.h"
void STtest01()
{
//初始化
ST st;
STInite(&st);
//入栈
STPush(&st, 1);
STPush(&st, 2);
STPush(&st, 3);
STPush(&st, 4);
STPush(&st, 5);
STPush(&st, 6);
STPush(&st, 7);
printf("size:%d\n", STSize(&st));
//循环出栈
while (!STEmpty(&st))
{
//取出栈
STDatatype ret= STTop(&st);
printf("%d ", ret);
//出栈
STPop(&st);
}
printf("\n");
printf("size:%d\n", STSize(&st));
//销毁
STDestroy(&st);
}
int main()
{
STtest01();
return 0;
}
C++实现Stack:
#include<iostream>
#include<assert.h>
using namespace std;
typedef int STDateType;
class Stack
{
public:
//成员函数
Stack(int n = 4)
{
_arr = (STDateType*)malloc(sizeof(STDateType) * n);
if (_arr == nullptr)
{
perror("malloc fail!");
exit(1);
}
_capacity = n;
_top = 0;
}
void Push(int x)
{
//扩容
if (_capacity == _top)
{
STDateType newcapacity = 2 * _capacity;
STDateType* tmp = (STDateType*)realloc(_arr, sizeof(STDateType) * newcapacity);
if (tmp == nullptr)
{
perror("realloc fail!");
exit(1);
}
_arr = tmp;
_capacity = newcapacity;
}
_arr[_top++] = x;
}
void Pop()
{
assert(_top > 0);
--_top;
}
bool Empty()
{
return _top == 0;
}
//取栈顶元素
int Top()
{
assert(_top > 0);
return _arr[_top - 1];
}
//析构
~Stack()
{
free(_arr);
_arr = nullptr;
_top = _capacity = 0;
}
private:
//成员变量
STDateType* _arr;
int _capacity;
int _top;
};//分号不能省略
int main()
{
Stack st;
st.Push(1);
st.Push(2);
st.Push(3);
while (!st.Empty())
{
//取栈顶
cout << st.Top() << endl;
//出栈
st.Pop();
}
return 0;
}
明显比c语言代码量少很多。