一、类的简介
C语言使用者应该熟悉结构(struct)这一概念,类可以理解为一个加强版的struct,不仅拥有内部成员,还包括对内部成员的操作方法。
二、类的定义方法
类的定义代码如下:
class stack
{
private:
//定义在私有部分中的对象
int max;
int stack[MAX];
//定义在私有部分中的方法
void acquire_max();
public:
//定义在公共部分中的对象
int top;
//定义在公有部分中的方法
void init();
void pop();
void push();
};
下面将给出具体的解释:
1--定义的框架:定义了一个名称为stack的类,其中包括对私有部分和公有部分的定义,分别用关键字private和public引出。定义的方式和struct的定义很相像!
class stack
{
private:
statement;
public:
statement;
};
由于类(class)中的对象默认是私有成员对象,所以private关键字甚至可以省略。
class Stack
{
int top;
int stack[1000];
public:
int max;
void pop();
void push();
}
2--私有部分成员对象:也即定义在private中的成员对象,只有通过同一类型的私有成员函数或者公有成员函数(或友元函数)才可以访问这些对象(具体的在后面给出)。以下就是私有部分的定义示例,这里定义了一个栈,栈顶指针和栈数组作为私有成员对象。
private:
int top;
int stack[1000];
privite_function;
3--公有部分成员对象:即定义在public中的成员对象,这些成员对象可以像结构中的变量一样被访问。
public:
int top;
int max;
public_function;
4--公有部分成员函数(类方法):即定义在public中的成员函数,这些函数可以在类的定义外,比方说主函数中被使用,也即类的使用者而不仅仅是类的编写者都可以调用这些函数。
下面展示了两种定义方法:
class Stack
{
private:
int top;
int stack[1000];
public:
void push(int num);
void pop(void)
{
top--;
}
void init(void)
{
top=-1;
}
}
void Stack::push(int num)
{
stack[++top]=num;
}
第一种是直接在public里定义函数,如pop和init函数;
第二种是在public里写自定义函数的声明,并在class的定义之外写这个函数,如push函数;class内的函数声明是push,但在外部定义函数是,要加上作用域解析运算符::来标识所属的类。
5--私有部分成员函数:定义在private中的函数,这部分函数只能在类成员函数的定义中使用,不可以在主函数和其他自定义函数中使用。你可以理解为这些函数只能让类的编写者使用,不能让类的使用者使用!
下面给出两种定义方法:
class Stack
{
private:
int top;
int stack[1000];
void show_top_element(void)
{
std::cout<<stack[top]<<std::endl;
}
void show_top_num(void);
public:
void push(int num);
void pop(void)
{
top--;
}
void init(void)
{
top=-1;
}
}
void Stack::push(int num)
{
stack[++top]=num;
}
inline Stack::show_top_num()
{
std::cout<<top<<std::endl;
}
第一种方法还是直接在private中定义函数
第二种方法是在private中写函数声明,然后在外部定义该函数。注意,此时除了要加上作用域解析运算符::,还要加上作用域限定符inline
三、使用类方法
按照第二节给出的规范,我已经定义好了Stack类如下,那么应该如何使用这个类呢?
#include <iostream>
class Stack
{
private:
int top;
int stack[1000];
void showstack()
{
int i;
for (i=top;i>=0;i--)
{
std::cout<<stack[i]<<std::endl;
}
std::cout<<std::endl;
}
void showtop();
public:
int is_true;
void init(void);
void push(int num);
void pop(void);
void show(void);
void topshow(void);
};
void Stack::init(void)
{
top=-1;
}
void Stack::pop(void)
{
top--;
}
void Stack::show(void)
{
showstack();
}
void Stack::push(int num)
{
stack[++top]=num;
}
void Stack::topshow()
{
showtop();
}
inline void Stack::showtop()
{
std::cout<<"top="<<top<<std::endl;
}
1--类就好像一种变量的类型,需要创建一个实例,也就是创建一个对象。创建方法和声明一个int类型的变量很相像!
int main(void)
{
Stack stack1;
}
这里就创建了一个名称为stack1的Stack类对象。
2--对于公有部分的成员对象
和struct中的成员变量一样,使用(.)运算符可以实现访问
int main(void)
{
Stack stack1;
stack1.is_true=1;
}
和结构一样的访问方式
3--对于公有部分的函数
使用公有函数的时候,不能直接写入函数名称,必须先写出其所作用的对象的名称,然后在写出成员函数名,中间用(.)运算符连接。
int main(void)
{
Stack stack1;
stack1.init();
stack1.push(0);
}
4--对于私有部分的函数
类的使用者不可以调用这些函数,但类的编写者可以。如编写Stack::topshow()这个函数的时候,类的编写者就调用了这个函数。调用时,也不用声明作用的对象了。
四、类的应用实例
在写C++程序的时候,我们写队列这一数据结构的时候,总需要自己动手。但是,有了类这个东西,我们就可以更好地把它封装起来。下面给出了一个示例:
#include <iostream>
class Queue
{
private:
int count;
//用于记录在队列的元素个数
int *line;
//数组的指针
int rear;
int head;
int scale;
//数组规模
int check(void);
void warning1(void);
//队满警告
void warning2(void);
//队空警告
public:
void init(int num);
void push(int num);
int pop(void);
};
void Queue::init(int num)
{
scale=num;
line=new int[scale];
rear=-1;
head=0;
count=0;
}
void Queue::push(int num)
{
if (check()==1)
{
warning1();
}
else
{
rear=(rear+1)%scale;
line[rear]=num;
count++;
}
}
int Queue::pop(void)
{
int tmp;
if (check()==2)
{
warning2();
return -1;
}
else
{
tmp=line[head];
head=(head+1)%scale;
count--;
}
return tmp;
}
inline int Queue::check(void)
{
if (count==0)
{
return 2;
}
else if (count==scale)
{
return 1;
}
else
{
return 0;
}
}
inline void Queue::warning1(void)
{
std::cout<<"the queue overflow!"<<std::endl;
}
inline void Queue::warning2(void)
{
std::cout<<"the queue empty!"<<std::endl;
}
int main(void)
{
Queue queue1;
queue1.init(5);
int input;
do
{
std::cin>>input;
if (input==-1)
{
std::cout<<queue1.pop()<<std::endl;
}
if (input>=0)
{
queue1.push(input);
}
}
while(input!=-2);
}