Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四
2013-10-28 11:15
645 查看
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 intothe 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 1) 学习笔记二
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记三
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记五
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记一
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- Data Structures with C++ Using STL Chapter 2对象设计技术---笔记
- Data Structures with C++ Using STL Chapter 4向量容器---笔记
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- Data Structures with C++ Using STL Chapter 1数据结构入门---笔记
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- Data Structures with C++ Using STL Chapter 5指针和动态内存---笔记
- 《APUE》chapter 6 System Data files and information 学习笔记(加上自己的代码)
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(5)——Sorting
- Data Structures and Algorithm Analysis in c++ 第一章笔记和部分习题
- APUE 学习笔记 - Chapter 6. System Data File and Infomation
- 基于Problem Solving with Algorithms and Data Structures using Python的学习记录(3)——Basic Data Structures
- Python数据结构与算法分析学习记录(2)——基于Problem Solving with Algorithms and Data Structures using Python的学习
- C# 2012 step by step 学习笔记8 CHAPTER 9 Creating Value types with enumerations and Structures
- Data Structures with C++ Using STLChapter 1数据结构入门---作业