240 likes | 413 Views
Chapter 11 Classes and Data Abstraction. 不到长城非好汉 He who does not reach the Great Wall is not a true man. Classes. Motivation of OOD: combine data and operations on that data in a single unit A class is a collection of a fixed number of components, which are called members of the class
E N D
Chapter 11 Classes and Data Abstraction 不到长城非好汉 He who does not reach the Great Wall is not a true man.
Classes • Motivation of OOD: combine data and operations on that data in a single unit • A class is a collection of a fixed number of components, which are called members of the class • Members of a class can be • Variables (attributes) • Functions (behaviors) • Other classes • A class is a user-defined data type
Defining classes • Syntaxclass classIdentifier { classMemberList}; • Variable member: declared like any other variable • Function member: declared by function prototype • Function member can access any member of the class (data and functions)
Member Access • Member access specifies whether a member of a class can be accessed from outside the class • public: can be accessed outside the class • private: cannot be accessed outside the class (default) • protected: can be accessed by derived class (chapter 14) • Rules of thumb • Usually data members are private • Usually member functions are public
const member functions cannot modify the data members getTime() could be another const function class clockType {public:void setTime(int, int, int);void getTime(int&, int&, int&);void printTime() const;void incrementSeconds();void incrementMinutes();void incrementHours();bool equalTime(const clockType& otherTime) const; private:int hr;int min;int sec; }; Do not forget this semicolon
UML (Unified Modeling Language) Class name • A + (plus) sign in front of a member name indicates that this member is a public member. • A - (minus) sign indicates that this is a private member. • The symbol # before the member name indicates that the member is a protected member. Data members Member functions
class clockType { private: int hr; int min; int sec; public: void setTime(int h, int m, int s); void displayTime(); void tickByHr(); void tickByMin(); void tickBySec(); }; Visual Studio 2005: View->Class View
Understand The Terms • Difference between class and object Object is an instance of class (“is a” relationship) e.g. Tom “is a” human being salary “is a” double variable cin “is a” istream object myClock “is a”clockType object
Declaring Objects • A class variable is called a class object or class instance. • When an object is declared, memory is allocated for data members of each object. • All objects of the same type share one copy of member functions.
Accessing Class Members • Member access operator .(dot) • All public members can be accessed • No private members can be accessed myClock.setTime(5,2,30);myClock.printTime();yourClock.setTime(x,y,z); if(myClock.equalTime(yourClock)) ... myClock.hr = 10; //illegalmyClock.min = yourClock.min; //illegal
Built-In Operations on Classes • Most of C++ built-in operations do not apply to classes (unless they are overloaded as in Ch.16) • What are allowed • Member access (.) • Assignment (=) Member-wise copy myClock = yourClock;
Class Scope • Scope of an object is similar to the scope of a variable • The scope of a member of a class is local to the class, i.e. accessible by name inside class. • Access to class member outside the class needs the object name and member access operator.
Implementation of Member Functions • Member function definitions (implement-ations) are usually separated from function prototypes (interfaces) • Scope resolution operator (::) needs to be used to refer to the function name in the header void clockType::setTime(int h, int m, int s) { ... }
Constructors • Constructor is a special type of member functions that • Are executed automatically when an object enters into its scope • Do not have a type • Have the same name as the name of the class • Can be overloaded with different parameter list • Are usually used to initialize the data members of an object • Default constructor • Takes no parameters • If not defined, an empty default constructor is generated automatically, which does nothing
Exercise: class Inventory • Define a class called “Inventory” • Data: quantity • Should be able to increase and decrease the quantity • When a new Inventory is created, set the quantity to zero Inventory - quantity: int +inventory() +increaseQty(int): void +decreaseQty(int): void +printQty(): void
Destructors • A member function executed automatically when the object goes out of scope • A class can have only one destructor • Destructor does not take parameters • Name: ~className
Classes and Functions • Objects can be passed to functions and can be returned from functions • Objects can be passed either by value or by reference • const reference parameter is preferred to value parameters • Reference parameter passes the address of the actual parameter to the formal parameter, saving the time of making copy • Using const prevent the function from changing the value of the parameter
Exercise: class Customer • The only information we know about a customer is Id and quantity of products ordered by this customer • Every time a customer is generated, an Id is assigned to this customer • A customer can order from inventory
Information Hiding • Motivation • Make sure the same object is used in the same way by everyone • Hide implementation details from user • Header file • Contains class definition • clock.h • Implementation file • Contains member function definitions • Object code is produced out of implementation file • clock.cpp • #include “clock.h” • User program • #include “clock.h”
Static Class Members • Static data members are shared by all objects of the same classstatic int nbrClocks; • Static data members must be initialized once and only once at file scope • Can be referenced through both class name and object namemyClock.nbrClocksclockType::nbrClocks • Usually needs a public static member function to access a private static data member when there is no existing object
Operator Overloading • Motivation Instead of doing Want to do myClock.printTime(); cout << myClock; myClock.incrementSeconds(); ++myClock; if(myClock.equalTime(yrClock)) if(myClock==yrClock) • Only assignment operator and member access operator can be directly applied to objects • C++ allows extension of definitions of most of the operators to objects • Use operator functions with name operatoroperatorSymbol
friend Functions • friend functions are defined outside the scope of a class but have access to the private data members • friend functions are not members of a class • Example: overloaded << operator is implemented using friend function because the left-hand side is not a clock object
Exercise: Extension • Add id for Inventory • Customer can order different products
Data Abstraction & ADT • Abstraction Separating the design details from its use e.g. we don’t need to know how engine works to drive a car • Abstract Data Types (ADT) A data type that specifies the logical properties (data representation and operation) without the implementation detailse.g. int