网站怎么更新内容,济宁城乡住房建设网站,网页设置与制作,网站建设及空间C(七)封装
封装#xff0c;可以达到#xff0c;对外提供接口#xff0c;屏蔽数据#xff0c;对内开放数据。
权限控制
struct 中所有行为和属性都是 public 的(默认)#xff0c;此举也是为了 C兼容 C 语言#xff0c; 因为 C 语言中没有权限的概念。
C中的 class 可以…C(七)封装
封装可以达到对外提供接口屏蔽数据对内开放数据。
权限控制
struct 中所有行为和属性都是 public 的(默认)此举也是为了 C兼容 C 语言 因为 C 语言中没有权限的概念。
C中的 class 可以指定行为和属性的访问方式默认为 private此举要求你必 须指定权限不然就没有办法外部访问。
访问属性属性对象内部对象外部public公有可访问可访问private私有可访问不可访问
初步使用封装:
#include iostream
#include string.h
using namespace std;class Stack{
public:void init();bool isEmpty();bool isFull();void push(int data);int pop();
private:int space[1024];int top;
};void Stack::init()
{memset(space,0,sizeof(space));top 0;
}
bool Stack::isEmpty()
{return top 0;
}
bool Stack::isFull()
{return top 1024;
}
void Stack::push(int data)
{space[top] data;
}
int Stack::pop()
{return space[--top];
}int main(){Stack s;s.init();if(!s.isFull())s.push(10);if(!s.isFull())s.push(20);if(!s.isFull())s.push(30);if(!s.isFull())s.push(40);while(!s.isEmpty())couts.pop()endl;return 0;
}class
构造器constructor
构造器是类的初始化函数当对象被创建时系统自动调用构造器进行初始化。
无返回值 可以有参数
构造器可以有默认参数,可以被重载
系统提供的默认构造器当没有自定义构造器时系统会自动生成一个默认构造器。
无论重载还是默认参数,都应该将无参的空体构造器包含进来
生成的无参的对象,是一种比较常见的现象,对象数组;
#include iostream
#include string.h
using namespace std;class Stack{
public:Stack(){top 0;size 1024;space new int[1024];memset(space, 0, sizeof(int)*1024);}Stack(int size){top 0;this-size size;space new int[size];memset(space, 0, sizeof(int)*size);}//默认参数会在现在的代码中冲突
// Stack(int size1024){
// top 0;
// this-size size;
// space new int[size];
// memset(space, 0, sizeof(int)*size);
// }bool isEmpty();bool isFull();void push(int data);int pop();
private:int *space;int top;int size;
};bool Stack::isEmpty()
{return top 0;
}
bool Stack::isFull()
{return top size;
}
void Stack::push(int data)
{space[top] data;
}
int Stack::pop()
{return space[--top];
}int main(){Stack s(2);if(!s.isFull())s.push(10);if(!s.isFull())s.push(20);if(!s.isFull())s.push(30);if(!s.isFull())s.push(40);while(!s.isEmpty())couts.pop()endl;return 0;
}initial list 初始化列表
C11 引入了初始化列表可以用来初始化类成员变量。
*** 注意: 初始化的顺序与成员变量的声明顺序相同。与列表中赋值顺序无关。*** 不能使用列表中初始化的成员,去初始化其他成员,做法很容易引发错误
必须用此格式来初始化引用数据。
必须用此格式来初始化非静态 const 数据成员(C98)。
//列表只能初始化类成员变量不能初始化局部变量Stack(int size): top(0), size(size), space(new int[size]){memset(space, 0, sizeof(int)*size);}Stack(int size): top(0), size(size), space(new int[size]{0}){}析构器destructor
析构器是类的析构函数
~开头与类名同的函数在类对象销毁时(栈/堆对象)自动调用
无返回值 不能有参数
析构器不能被重载
系统提供的默认析构器当没有自定义析构器时系统会自动生成一个默认析构器。 #include iostream
#include string.h
using namespace std;class Stack{
public:Stack(){top 0;size 1024;space new int[1024];memset(space, 0, sizeof(int)*1024);}~Stack(){delete[] space;}private:int *space;int top;int size;
};
层次管理 #include iostream
#include string.h
using namespace std;
class Student
{
public:Student(int a, char* n ){coutConstructor calledendl;_age a;_name new char[strlen(n)];strcpy(_name,n);}~Student(){coutDestructor calledendl;delete []_name;}private:char *_name;int _age;
};
int main()
{Student s(10,hello);Student *ps new Student(10,hello);delete ps;return 0;
}分文件编程
myStack.h
//
// Created by gophe on 24-7-28.
//#ifndef CDEMO_MYSTACK_H
#define CDEMO_MYSTACK_Hclass myStack {
public:myStack();//myStack(int size);myStack(int size10);//参数默认值只能在这定义,初始化列表在.cpp中定义~myStack();bool isEmpty();bool isFull();void push(int data);int pop();
private:int *space;int top;int size;
};#endif //CDEMO_MYSTACK_H
myStack.cpp
//
// Created by gophe on 24-7-28.
//#include Headers/myStack.hmyStack::myStack(){top 0;size 100;space new int[100];
}
// 上下两种均可
myStack::myStack():size(10),top(0),space(new int[10]){}myStack::myStack(int s)
{top 0;size s;space new int[s];
}
// 上下两种均可
//初始化列表在.cpp中定义
myStack::myStack(int s):size(s),top(0),space(new int[s])
{
}myStack::~myStack()
{delete[] space;
}bool myStack::isEmpty()
{return top 0;
}
bool myStack::isFull()
{return top size;
}
void myStack::push(int data)
{space[top] data;
}
int myStack::pop()
{return space[--top];
}
main.cpp
#include iostream
#include Headers/myStack.husing namespace std;int main(){myStack s(2);if(!s.isFull())s.push(10);if(!s.isFull())s.push(20);if(!s.isFull())s.push(30);if(!s.isFull())s.push(40);while(!s.isEmpty())couts.pop()endl;return 0;
}