Presentation is loading. Please wait.

Presentation is loading. Please wait.

《Visual C++程序设计》 48113 软件与通信工程学院 李 刚 QQ: 上课时间:周二567节 上课地点:荟庐W101

Similar presentations


Presentation on theme: "《Visual C++程序设计》 48113 软件与通信工程学院 李 刚 QQ: 上课时间:周二567节 上课地点:荟庐W101"— Presentation transcript:

1 软件与通信工程学院 李 刚 teacherlg@163.com 13133808285 QQ:4263697
《Visual C++程序设计》 48113 软件与通信工程学院 李 刚 QQ: 上课时间:周二567节 上课地点:荟庐W101 课程特点:理论实践结合 英汉双语教学

2 Object-Based Programming
Chapter 4 Object-Based Programming

3 Ch4 Object-Based Programming
4.1 How to Implement a Class 4.2 What Are Class Constructors and the Class Destructor? 4.3 What Are 'mutable' and 'const'? 4.4 What Is the 'this' Pointer? 4.5 Static Class Members 4.6 Building an Iterator Class * 4.7 Collaboration Sometimes Requires Friendship 4.8 Implementing a Copy Assignment Operator 4.9 Implementing a Function Object 4.10 Providing Class Instances of the iostream Operators 4.11 Pointers to Class Member Functions*

4 We have already used the following classes before: string, vector, etc.
#include <vector> #include <string> string dummy( "dummy" ); vector< string > svec1( 4 ); vector< string > svec2( 4, dummy ); vector< string > svec3( pooh, pooh+4 );

5 The class is a fundamental OOP concept in C++.
The class is identical to the struct keyword in every way except one: class defaults to private, whereas struct defaults to public.

6 User defined class name
A class类 is an user-defined data type which encapsulates data members and function members, like: Use keyword class // People.h: interface for the People class. // ////////////////////////////////////////////////////////////////////// #include<string> using std::string; class People { public: People(); virtual ~People(); private: string _name; int _age; bool _gender; }; User defined class name Member functions成员函数 Member variables成员变量

7 Each class provides a set of operations ( interfaces接口 ) we can apply to objects of the class.
Named functions, such as size() and empty(), and Overloaded instances of the predefined operators, such as inequality and assignment: if ( svec2 != svec3 && ! svec3.empty() ) svec2 = svec3; if ( svec2.size() == 4 ) // all is well ...

8 member function definitions and any data associated with the class.
A class consists of two parts: a public set of operations and operators, and a private implementation. member functions member function definitions and any data associated with the class. The Only public interfaces for class users. implementation details are usually of no concern to the user of the class, but important to class designers

9 4.1 How to Implement a Class
class Stack { public: // ... public interface private: // ... private implementation }; The public and private keywords within the class body control access to the members declared within each section.

10 4.1 How to Implement a Class
Public members can be accessed from anywhere within the program. Private members can be accessed only by the member functions and friends of the class

11 4.1 How to Implement a Class
There are two reasons for controlling access to members: The first is to keep the client programmer’s hands off tools they should touch. The second reason for access control is to allow the library designer to change the internal workings of the class without worrying about how it will affect the client programmer.

12 4.1 How to Implement a Class
class Stack { public: // each operation returns true if able to be carried out // pop and peek place the string value within elem bool push( const string& ); bool pop( string &elem ); bool peek( string &elem ); bool empty(); bool full(); // definition of size() is placed within class // other members are simply declared ... int size() { return _stack.size(); } private: vector<string> _stack; }; 堆栈类的声明实例 成员函数可以在类里只声明 也可以在类里给出定义

13 4.1 How to Implement a Class
Use a stack object through its public interface: void fill_stack( Stack &stack, istream &is = cin ) { string str; while ( is >> str && ! stack.full() ) stack.push( str ); cout << "Read in " << stack.size() << " elements\n"; } Public member functions

14 4.1 How to Implement a Class
成员函数的实现: inline function definition must be placed with the class definition inline bool Stack::empty() { return _stack.empty(); } bool Stack::pop( string &elem ) { if ( empty() ) return false; elem = _stack.back(); _stack.pop_back(); return true; } Classname:: tell compiler where pop is in class scope operator

15 4.1 How to Implement a Class
Remaining Stack member function definitions inline bool Stack::full() { return _stack.size() == _stack.max_size(); } compares the current size of the underlying vector with max_size(), the largest possible size of the vector.

16 4.1 How to Implement a Class
Remaining Stack member function definitions bool Stack::peek( string &elem ) { if ( empty() ) return false; elem = _stack.back(); return true; } Read the last element from the stack.

17 4.1 How to Implement a Class
Remaining Stack member function definitions bool Stack::push( const string &elem ) { if ( full() ) return false; _stack.push_back( elem ); return true; } Put one element to the end of the stack.

18 4.1 How to Implement a Class
Exercise 4.1 Create a Stack.h and a Stack.suffix, where suffix is whatever convention your compiler or project follows. Write a main() function to exercise the full public interface, and compile and execute it. Both the program text file and main() must include Stack.h: #include "Stack.h"

19 4.1 How to Implement a Class
Exercise 4.2 Extend the Stack class to support both a find() and a count() operation. find() returns true or false depending on whether the value is found. count() returns the number of occurrences of the string. Reimplement the main() of Exercise 4.1 to invoke both functions.

20 点此继续学习


Download ppt "《Visual C++程序设计》 48113 软件与通信工程学院 李 刚 QQ: 上课时间:周二567节 上课地点:荟庐W101"

Similar presentations


Ads by Google