创建一个队列类(Queue, FIFO)
声明:
class queue{
int q[20];
int front,rear;
public:
void init();
void qput(int i);
int qget();
};
成员变量:q[], front , rear
- 默认的情况下是私有的(private): 只能被queue类的成员访问.
成员函数: init(), qput(); qget();
- public: 可以被程序中的其他函数访问.
- 要实现成员函数,需要使用运算符:: (作用域解析运算符)告诉成员函数是属于该类的。
void queue::qput(int i){
if(rear==20){
cout<<"Queue is full.\n";
return;
}
rear++;
q[rear]=i; //在队尾进行数据插入
}
创建对象: queue Q1,Q2;
成员函数调用: 只能通过相应的对象来调用(对象名和.运算符 )
queue object1,boject2;
object1.init();
//完整的代码:
#include <iostream>
using namespace std;
//创建类queue
class queue{
int q[20];
int front,rear;
public:
void init();
void qput(int i);
int qget();
};
//初始化类queque
void queue::init(){
front=rear=0;
}
//在队尾插入一个整数
void queue::qput(int i){
if(rear==20){
cout<<"Queue is full.\n";
return;
}
rear++;
q[rear]=i;
}
//在队首取出一个整数
int queue::qget(){
if(front==rear){
cout<<"Queue underflow.\n";
return 0;
}
front++;
return q[front];
}
int main(){
queue q1,q2; //创建2个queue 对象(object)
q1.init();
q2.init();
q1.qput(15);
q2.qput(18);
q1.qput(25);
q2.qput(28);
cout<<"Queue q1:";
cout<<q1.qget()<<" ";
cout<<q1.qget()<<"\n";
return 0;
}
输出结果:
Queue q1:15 25
类的通用形式:
class class_name{
private data and functions
public:
public data and functions
} object-list;
Q. What is the difference between C struct and C++ class?
A. The default access level assigned to members of struct is public while the default access level assigned to a class is private.
分享到:
相关推荐
The default behavior of this method is to return addHeader(String name, String value) on the wrapped response object. addHeader(String, String) - Method in interface javax.servlet....
- FIX: The TFlexPanel.FPaintCache field moved in the protected class section. Added rcPaint field in FPaintCache that represents drawing rectangle. - ADD: In the text prcise mode (TFlexText.Precise=...
The cursor must be able to return at least one bit of additional state (via a but¬ton, touching a digitizing surface, etc.). Devices may have multiple cursor types that have different physical ...
The most basic use of this function is to pass in an expression (usually consisting of CSS), which then finds all matching elements. By default, if no context is specified, $() looks for DOM ...
In addition to the default Bochs method using the CTRL key and the middle mouse button there are now the choices: - CTRL+F10 (like DOSBox) - CTRL+ALT (like QEMU) - F12 (replaces win32 'legacyF12'...
take advantage of protocols for which Sun does not provide out-of-the-box support. You'll find support for NNTP (Network News Transport Protocol) [newsgroups], S/MIME (Secure Multipurpose Internet ...
The 80x86 MOV Instruction 4.8 - Some Final Comments on the MOV Instructions <br>4.9 Laboratory Exercises 4.9.1 The UCR Standard Library for 80x86 Assembly Language Programmers 4.9.2 ...
1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject: ```php $...
npm i -S es6-class-mixin 原料药 mixin通过具有功能或属性的对象扩展给定的类。 mixin ( ParentClass , ... mixins ) // return new MixedClass extended by Mixins 例子 // Basic const mixin = require ( 'es6-...
For example, if your header file uses the File class in ways that do not require access to the declaration of the File class, your header file can just forward declare class File; instead of having ...
).- Added the property FileStream to the class TAtachedFile and the procedure SaveToStream, this was done by Brian Sheperd- The address separator (in the TO: field) is ‘,‘ and ‘;‘ now (before it ...
视觉斐波那契 使用JavaScript的斐波那契数列的视觉表示。 网站 用法 向上/向下滚动以更改序列长度。 定义 在数学中,斐波那契数通常... return this . sequence [ n ] ; } if ( n < 2 ) { this . sequence [ n ]
To use the class inside the jsp page we need to create an object of the class by using the new operator. At last use the instance of the class to access the methods of the java file. Setting Colors ...
+ added a property TruncateLongTexts to the XLS OLE export that allows to disable truncating texts longer than a specified limit + added option EmbedProt which allows to disable embedding fonts into ...
Basic File Operations: NSFileHandle 390 The NSURL Class 395 The NSBundle Class 396 Exercises 397 17 Memory Management and Automatic Reference Counting 399 Automatic Garbage Collection 401 Manual ...
The goal of Entity Linking is to determine whether or not the entity referred to in each query has a matching entity node in the reference Knowledge Base (KB) (LDC2014T16). If there is a matching node...
In addition, it is able to return directory listings. * Client CLAP connector to access to the Classloader resources. * Client RIAP connector to access to the Restlet internal resources, directly ...
Application memory tuning provides more of the computer's virtual memory to applications by providing less virtual memory to the operating system. Although a system having less than 2 GB of physical ...
Prior to delivering this material, test the class to see if they fully understand the different isolation levels. If the class is not confident in their understanding, review appendix A04_Locking and ...
1.2.1. The first class 1.2.2. The mapping file 1.2.3. Hibernate configuration 1.2.4. Building with Ant 1.2.5. Startup and helpers 1.2.6. Loading and storing objects 1.3. Part 2 - Mapping associations ...