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

Slides:



Advertisements
Similar presentations
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Advertisements

Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 为了更好的揭示随机现象的规律性并 利用数学工具描述其规律, 有必要引入随 机变量来描述随机试验的不同结果 例 电话总机某段时间内接到的电话次数, 可用一个变量 X 来描述 例 检测一件产品可能出现的两个结果, 也可以用一个变量来描述 第五章 随机变量及其分布函数.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
UML 对象设计与编程 主 讲 : 董兰芳 副教授 Dept. of Computer Science,USTC
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Class ~ The essence of Object Oriented Programming.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Introduction to Object Oriented Programming Chapter 10.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
SME.USTB Human Factors 人机工程学 By Wei Dong Department of Industry Design, SME, USTB.
Session 03 - Templates Outline 03.1Introduction 03.2Function Templates 03.3Overloading Template Functions 03.4Class Templates 03.5Class Templates and Non-type.
C++ Lesson 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 22 - C++ Templates
Classes (Part 1) Lecture 3
Abstract Data Types and Encapsulation Concepts
A First C++ Class – a Circle
Review: Two Programming Paradigms
11.1 The Concept of Abstraction
Chapter 5 Classes.
STACKS AND QUEUES UNIT 2 DS THROUGH C++.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Introduction to Classes
Abstract Data Types and Encapsulation Concepts
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 8: Class Relationships
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lists CMSC 202, Version 4/02.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
11.1 The Concept of Abstraction
Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

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

Object-Based Programming Chapter 4 Object-Based Programming

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*

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

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.

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成员变量

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

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

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.

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

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.

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; }; 堆栈类的声明实例 成员函数可以在类里只声明 也可以在类里给出定义

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

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

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.

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.

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.

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"

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.

点此继续学习