`
fdyang
  • 浏览: 79688 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Return to the Basic - 类 (class )

 
阅读更多

创建一个队列类(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.

 

分享到:
评论

相关推荐

    servlet2.4doc

    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....

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - 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 ...

    jQuery完全实例.rar

    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 ...

    Bochs - The cross platform IA-32 (x86) emulator

    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'...

    Java邮件开发Fundamentals of the JavaMail API

    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 Art of Assembly Language Programming

    The 80x86 MOV Instruction 4.8 - Some Final Comments on the MOV Instructions &lt;br&gt;4.9 Laboratory Exercises 4.9.1 The UCR Standard Library for 80x86 Assembly Language Programmers 4.9.2 ...

    google api php client

    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 $...

    es6-class-mixin:ES6类的Mixins

    npm i -S es6-class-mixin 原料药 mixin通过具有功能或属性的对象扩展给定的类。 mixin ( ParentClass , ... mixins ) // return new MixedClass extended by Mixins 例子 // Basic const mixin = require ( 'es6-...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    Sakemail

    ).- 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 ...

    react-basic-3

    视觉斐波那契 使用JavaScript的斐波那契数列的视觉表示。 网站 用法 向上/向下滚动以更改序列长度。 定义 在数学中,斐波那契数通常... return this . sequence [ n ] ; } if ( n &lt; 2 ) { this . sequence [ n ]

    JSP Simple Examples

    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 ...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + 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 ...

    Programming in Objective-C 4th Edition

    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 ...

    Chinese Entity Linking Comprehensive

    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...

    restful restful所需要的jar包

    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 ...

    微软内部资料-SQL性能优化2

    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 ...

    微软内部资料-SQL性能优化3

    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 ...

    Hibernate Reference Documentation3.1

    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 ...

Global site tag (gtag.js) - Google Analytics