1.c++和c语言实现栈的比较
#include <iostream>
using namespace std;
//c++兼容C,结构用法也可以继续使用
//同时struct也升级成了类
struct Stack
{
//成员函数
void Init()
{
a = nullptr;
top = capacity = 0;
}
//成员变量
int* a;
int top;
int capacity;
};
void StackInit(struct Stack* ps)
{
//
}
int main()
{
struct Stack st1;//c语言
StackInit(&st1);
Stack st2;//C++
st2.Init();
return 0;
}
类中数据和函数都放到结构中,函数不用传参数,直接用。
2.类的定义
class className
{
//类体:由成员函数和成员变量组成
};//一定要注意后面的分号
3.访问限定符
在C++中,访问限定符用于控制类(class)或结构体(struct)中成员的可见性和访问权限。主要的访问限定符有public、private和protected。类里面不受访问限定符的限制,只有类外限制。锁的是外面的人,不锁家里人。
1.public:成员在类外部和类内部都是可见的。这意味着可以从类的外部访问这些成员。
2.private:成员只有在类内部才可见。这意味着类的外部无法直接访问这些成员,只有类的成员函数可以访问它们。
3.protected:与private类似,但派生类中的成员函数也可以访问这些成员。
#include <iostream>
using namespace std;
//c++兼容C,结构用法也可以继续使用
//同时struct也升级成了类
struct Stack
{
//成员函数
void Init()
{
a = nullptr;
top = capacity = 0;
}
void push(int x)
{
if (top = capacity)
{
size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
a = (int *)realloc(a,sizeof(int) * newcapacity);
capacity = newcapacity;
}
a[top++] = x;
}
//成员变量
int* a;
int top;
int capacity;
};
int main()
{
Stack st;
st.Init();
st.push(1);
cout << st.a[st.top] << endl;//C语言中,不知道top指向最后一个元素还是下一个,还得看别人的源代码
return 0;
}
运行结果:随机值
C++:成员变量是私有的,但是可以通过公共的成员函数访问
#include <iostream>
using namespace std;
//c++兼容C,结构用法也可以继续使用
//同时struct也升级成了类
class Stack
{
public:
//成员函数
void Init()
{
a = nullptr;
top = capacity = 0;
}
void push(int x)
{
if (top = capacity)
{
size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
a = (int *)realloc(a,sizeof(int) * newcapacity);
capacity = newcapacity;
}
a[top++] = x;
}
int Top()
{
return a[top--];//函数中我知道top指向的是哪个位置
}
private:
//成员变量
int* a;
int top;
int capacity;
};
int main()
{
Stack st;
st.Init();
st.push(1);
st.push(1);
cout << st.Top() << endl;//C语言中,不知道top指向最后一个元素还是下一个,还得看别人的源代码
return 0;
}
现在来比较class和struct在默认访问限定符上的区别:
1.对于class,默认的访问限定符是private,这意味着在class中定义的成员默认情况下是私有的,除非另行指定为public或protected。
2.对于struct,默认的访问限定符是public,这意味着在struct中定义的成员默认情况下是公有的,除非另行指定为private或protected。因此,主要的区别在于默认的访问限定符不同,对于class来说,默认情况下成员是私有的,而对于struct来说,默认情况下成员是公有的。此外,class和struct在其他方面是相似的,它们都可以包含成员函数、静态成员、构造函数、析构函数等。
4.c++中栈实现类的声明和定义分离
.c文件
#pragma once
#include<stdlib.h>
class Stack
{
public:
//成员函数
void Init();
void push(int x);
int Top();
private:
//成员变量
int* a;
int top;
int capacity;
};
.cpp文件
#include"Func.h"
void Stack::Init()//告诉编译器Init是Stack的成员函数
{
a = nullptr;
top = capacity = 0;
}
void Stack::push(int x)
{
if (top = capacity)
{
int newcapacity = capacity == 0 ? 4 : capacity * 2;
a = (int*)realloc(a, sizeof(int) * newcapacity);
capacity = newcapacity;
}
a[top++] = x;
}
int Stack::Top()
{
return a[top--];//函数中我知道top指向的是哪个位置
}
main函数
#include <iostream>
#include"Func.h"
using namespace std;
int main()
{
Stack st;
st.Init();
st.push(1);
st.push(1);
cout << st.Top() << endl;//C语言中,不知道top指向最后一个元素还是下一个,还得看别人的源代码
return 0;
}
注意:成员函数如果在类中定义,编译器可能将会当成内联函数处理。 如下所示:
#include <iostream>
using namespace std;
//c++兼容C,结构用法也可以继续使用
//同时struct也升级成了类
class Stack
{
public:
//成员函数
void Init()
{
a = nullptr;
top = capacity = 0;
}
void push(int x)
{
if (top = capacity)
{
size_t newcapacity = capacity == 0 ? 4 : capacity * 2;
a = (int *)realloc(a,sizeof(int) * newcapacity);
capacity = newcapacity;
}
a[top++] = x;
}
int Top()
{
return a[top--];//函数中我知道top指向的是哪个位置
}
private:
//成员变量
int* a;
int top;
int capacity;
};
int main()
{
Stack st;
st.Init();
st.push(1);
st.push(1);
cout << st.Top() << endl;//C语言中,不知道top指向最后一个元素还是下一个,还得看别人的源代码
return 0;
}