Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四
来源:互联网 发布:开淘宝网店需要的软件 编辑:程序博客网 时间:2024/06/05 20:49
Chapter 2 Abstract Data Types and C++ Classes
2.1 CLASSES AND MEMBERS
A class is a new kind of data type. Each class that you define is a collection of data, and has the ability to include member functions. Member functions are incorporated into the class's definition and are designed specifically to manipulate the class.
PROGRAMMING EXAMPLE: The Throttle Class
class throttle{ public: // MODIFICATION MEMBER FUNCTIONS void shut_off (); void shift (int amount); // CONSTANT MEMBER FUNCTIONS double flow() const; bool is_on() const; private: int position;};A class has some sections as follows :
The class head. The head of the definition consists of the C++ keyword class, followed by the name of the new class.
The member list.
The public section.
modification functions.
constant member functions:may examine the status of its object, but it is forbidden from changing the object.
The private section.
Using a Class
declare objects of a class:
throttle my_throttle;
throttle control;
Every throttle object contains the private member variable position, but there is no way for a program to access this component (组件)directly, because it is a private member. The only way that a program can use its throttle objects is by using the public member functions. such as: control.shut_off(); control.shift(3);
calling a member function always involves the following four steps:
- Start with the name of the object that you are manipulating.
- After the object name, place a single period.
- Next, write the name of the member function.
- Finally, list the arguments for the function call.
A Small Demonstration Program for the Throttle Class
// FILE: demol.cxx// This small demonstration shows how the throttle class is used.#include <iostream>#include <cstdlib>using namespace std;class throttle{ public: // MODIFICATION MEMBER FUNCTIONS void shut_off (); void shift (int amount); // CONSTANT MEMBER FUNCTIONS double flow() const; bool is_on () const; private: int position;};int main(){ throttle sample; int user_input; // Set the sample throttle to a position indicated by the user. cout << "I have a throttle with 6 positions." << endl; cout << "Where would you like to set the throttle? " << endl; cout << "Please type a number from 0 to 6: "; cin >> user_input; sample.shut_off(); sample.shift (user_input); // shift the throttle down to zero, printing the flow along the way. while (sample.is_on()) { cout << "The flow is now " << sample.flow() << endl; sample.shift(-1); } cout << "The folw is now off" << endl; return EXIT_SUCCESS;}
Implementing Member Functions
void throttle::shut_off()// precondition: None// Postcondition: The throttle has been turned off.{ position = 0;}
void throttle::shift (int amount)// Precondition: shut_off has been called at least once to initialize the throttle.// Postcondition: The throttle's position has been moved by amount{ position += amount; if (position < 0) position = 0; else if (position > 6) position = 6;}
double throttle::flow() const// Precondition: shut_off has been called at least once to initialize the throttle.// Postcondition: The value returned is the current flow as a proportion of the max flow.{ return position / 6.0;}
bool throttle::is_on() const // Precondition: shut_off has been called at least once to initialize the throttle.// Postcondition: If the throttle's flow is above 0, // then the function returns true; otherwise, it returns false.{ return (flow() > 0);}
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记五
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记一
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记二
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记三
- Objects and Data Structures
- DATA STRUCTURES AND ALGORITHMS USING C#.
- #学习笔记#Princeton Algorithms and Data Structures
- •data structures using c: 1.1: structure: data and program-(2013/11/20)
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(3)——Basic Data Structures
- Data Structures and Algorithm Analysis in C 学习之List
- Python Cookbook 学习笔记 第一章Data Structures and Algorithms
- chapter 11 Data Structures
- Chapter 2 Classes and Objects
- Using and Understanding Java Data Objects
- •data structures using c: 3.3: expanding and contracting lists-(2013/11/20)
- chapter 7 recursion(递归) and linked data structures(链接数据结构)
- Data Structures and Algorithms Using Visual Basic.NET
- [Bug]ArcSDE分区表报:ORA-01000 maximum open cursors exceeded
- 如何通过APPLESCRIPT来狂发imessage(最简单的流程)
- VxWorks中POSIX标准定时机制
- recv函数返回值说明
- VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四
- Unicode、UTF-8 和 ISO8859-1有什么区别?
- win7系统远程桌面无法最大化的解决办法
- Tomcat VS. Apache:您钟情于哪一款?
- jquery博客
- 【Futures】&【Laws】
- Ubuntu hostname修改
- 2013 sap 泛微项目感想
- 用条件运算符?代替if语句