egr 2261 unit 11 classes and data abstraction read malik, chapter 10. homework #11 and lab #11 due...

44
EGR 2261 Unit 11 Classes and Data Abstraction Read Malik, Chapter 10. Homework #11 and Lab #11 due next week. Quiz next week.

Upload: vincent-dawson

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

EGR 2261 Unit 11Classes and Data Abstraction

Read Malik, Chapter 10. Homework #11 and Lab #11 due next

week. Quiz next week.

Page 2: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Review: What is a struct?

• Recall that a struct is a collection of a fixed number of components (called members), accessed by name.– The members may be of different types.

• structs are very useful because they let us design complex data structures that represent real-world objects.– Recall example (on next slide) of houseType.

2C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 3: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Example of a struct

3C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 4: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Data Members versus Function Members

• In our examples so far, structs have contained data members but not function members. – In other words, the structs in our examples have contained variables, but not functions.

4C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 5: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Limitations of structs

• In the C language, structs could only contain data members.

• C programmers came to see this as a limitation, and realized that it would be nice to be able to define structs that contain function members as well as data members.

• This realization, along with other ideas about how to improve structs, led to the development of C++.

5C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 6: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Classes and Object-Oriented Design

• The main new idea in C++ is the class, which is a generalization of the struct.

• Hand-in-hand with the introduction of classes was a whole new way of thinking about a program—as a collection of interacting objects. This new way of thinking is called object-oriented design (OOD) or object-oriented programming (OOP).

6C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 7: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Defining a Class

• A class definition looks a lot like a struct definition.

• Syntax:

• Just as with struct definitions, a class definition defines a new data type; but no memory is allocated until we declare a variable of this type.

7C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 8: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Defining a Class (cont’d.)

• Each class member in classMemberList can be a variable (data member) or a function (function member).– Data members are like the members we’ve used in structs.

– For a function member, usually just the function prototype is listed is listed in the class definition. The function definition is given elsewhere in the program.

8C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 9: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Defining a Class: Example

Note the new keywords public and private, which are called member access specifiers.

Seven function

members

Three data

members

Page 10: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Member Access Specifiers

• Each class member falls into one of the following categories, as designated by the access specifiers:– public

• The member can be accessed by any code outside the class.

– private (This is the default.)• The member cannot be accessed by any code outside the

class.

– protected• "In between" public and private. We'll discuss in a

later chapter.

10C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 11: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Member Access Specifiers: Example

Since hr isprivate, it can be accessed by code within the function members, but no other code can access it.

On the other hand, since setTime() is public, any code can call it.

Page 12: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Member Access Specifiers: Example (cont'd.)

In this example,all of the functionmembers are public and all of the data members are private. This is fairly common, but it's not always the case.

Page 13: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Implementation of Member Functions

• As seen above, usually only the prototypes of a class's function members are included in the class definition itself.

• Your program must also contain the code for these functions. This code is called the function's implementation.

• Note the syntax on the next slide, which uses the scope resolution operator ::

13C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 14: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Implementation of Member Functions: Example

This code is outside of the class definition, so you must precede the function's name with clockType:: to indicate that this is a member function of the clockType class, rather than just a regular function.

Class name followed by scope resolution

operator

Page 15: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Separating the Definition from the Implementation

Though not required, in a program that uses classes we usually separate the code into the following files:1. A header file that holds the class definition.2. An implementation file that holds the

implementations of the class’s member functions.

3. A file that holds the client program, which uses the class defined in the other two files. Here is where you’ll put your program’s main() function.

For a program that uses more than one class, files 1 and 2 will be repeated for each class.

Page 16: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Separating the Definition from the Implementation: Example

Page 17: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

1. Create a new project named clockPractice and start a new source-code file named clockPractice.cpp.

2. Download the files named clockType.h and clockType.cpp from the website and place them in your project’s folder that contains clockPractice.cpp.

3. In your project’s Solution Explorer, right-click Source Files and select Add > Existing Item…. Then select clockType.cpp and click Add.

4. In clockPractice.cpp, type the code shown on the next slide.

Getting Started with a Program that Uses ClockType

Page 18: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

5. Run the program.6. Modify main to call myClock.incrementSeconds()

and then myClock.printTime() again.

Getting Started with a Program that Uses ClockType (cont’d.)

Page 19: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Variable (Object) Declaration

• Once a class has been defined and implemented, you can declare variables of that class type:

clockType myClock;

• A class variable is called a class object or class instance, or simply an object.

• Each object has its own copy of the data members (hr, min, and sec).

19C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 20: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Accessing Class Members From Outside

• Code outside of the member functions can access the object's public members, using the syntax:

– The dot (.) is the member access operator (same as for structs).

• But code outside of the member functions cannot access the object's private members.

20C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 21: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Accessing Class Members From Outside: Example

This code is in the program's main(), so it is outside of the class's member functions. Of course, the class and its member functions

are defined elsewhere in this program.

Accessing myClock's public members: okay!

Trying to access myClock's private

members: no good!

Page 22: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Accessing Class Members Within the Class Code

• Code within a member function of the class can access the object's public and private members directly by name (no dot operator needed).

22C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 23: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Accessing Class Members Within the Class Code: Example

This code is in the member function printTime(), so it can access public and private members. Note that we don’t need to precede the

member name with the object's name.

Accessing an object's private members: okay!

Page 24: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Constructors

• A constructor is a special member function that runs automatically when a new class object is created. Its purpose is to initialize the object’s data members.

• A constructor’s name is always the same as the name of the class.

• A constructor has no type (not even void).• Unlike other functions, constructors cannot be

explicitly called in user-written code.24C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 25: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Constructors: Example

In the class’s header file:

In the class’s implementation file:

Page 26: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Constructors (cont’d.)

• A class can have more than one constructor.– This is an example of function overloading, which we

looked at briefly in Unit 7.– Recall from Unit 7 that each overloaded function

must have a different formal parameter list.• Example of overloaded constructor functions:clockType();clockType(int hours, int minutes, int seconds);

26C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 27: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Multiple Constructors: Example

In the class’s header file:

In the class’s implementation file:

Page 28: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Invoking a Constructor

• A constructor is automatically executed when a class object is declared.

• Which of the constructors is executed depends on whether you specify parameters in parentheses after the class object’s name when you declare it.

• Examples:clockType yourClock;clockType myClock(6, 30, 56);

28C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 29: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Invoking the Default Constructor

• To invoke the default constructor, don’t specify any parameters after the class object’s name when you declare it:

• Example:clockType yourClock;

29C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 30: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Invoking a Constructor with Parameters

• Syntax:• Example:

clockType myClock(6, 30, 56);

• The number and type of arguments should match the formal parameters (in the order given) of one of the constructors.– Otherwise, C++ uses type conversion and looks for

the best match.– Any ambiguity causes a compile-time error.

30C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 31: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Constructors and Default Parameters

• A constructor can have default parameters.– The rules for declaring formal parameters are the

same as for declaring default formal parameters in any function.

– Actual parameters are passed according to same rules for any functions.

• Default constructor: a constructor with no parameters or with all default parameters.

31C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 32: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Built-in Operations on Classes

• As with structs, aggregate arithmetic, relational, and input/output operations are not allowed on class objects, but aggregate assignment is allowed:clockType myClock, yourClock;

myClock = yourClock + 1; //Error

if (myClock >= yourClock) //Error

cout << myClock; //Error

cin >> myClock; //Error

myClock = yourClock; //Okay!!32C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 33: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Assignment Operator and Classes

33C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 34: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Class Objects and Functions

• Objects can be passed as parameters to functions by value or by reference.

• If an object is passed by value, a copy must be made of the object’s data members. For large objects, this may require a lot of time and memory, degrading performance.

• If an object is passed by reference, no copy is made—may result in better performance.

34C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 35: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Class Objects and Functions (cont’d.)

• Passing by reference is an efficient way to pass an object as a parameter.– Potential Problem: when passing by reference, the

actual parameter is changed if the formal parameter is changed.

– Solution: use const in the formal parameter list. This prevents the function from changing the parameter’s value. Example from clockType:

35C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 36: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Another Use of const

• The previous slide showed how to use const to prevent a function from changing the value of a reference parameter.

• Another use of const is to prevent a function from changing the object on which the function is invoked. Example from clockType:

36C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 37: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Two Uses of const: Example

This is an error because of this const.

This is an error because of this const.

Page 38: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Accessor and Mutator Functions

• Accessor function: member function that only accesses the values of member variables.

• Mutator function: member function that modifies the values of member variables.

• Constant function:– Member function that cannot modify member variables.– Use const at the end of the function heading, as we saw

previously:

38C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 39: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Arrays of Class Objects (Variables) and Constructors

39C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 40: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Unified Modeling Language Class Diagrams

• Unified Modeling Language (UML) notation is widely used by programmers to graphically describe a class and its members. UML has many special symbols, including: + for public members - for private members # for protected members

40C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 41: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Unified Modeling Language Class Diagrams (cont’d.)

41C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 42: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Data Abstraction, Classes, and Abstract Data Types

• Abstraction– Separating design details from usage– Separating the logical properties from the

implementation details• Abstraction can also be applied to data• Abstract data type (ADT): data type that

separates the logical properties from the implementation details

42C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 43: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Information Hiding

• Information hiding: hiding the details of the operations on the data.

• Interface (header) file: contains the specification details.• File extension is .h

• Implementation file: contains the implementation details.• File extension is .cpp

• In header file, include function prototypes and comments that briefly describe the functions.

43C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Page 44: EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week

Information Hiding (cont’d.)

• Implementation file must include header file via include statement

• In include statement:– User-defined header files are enclosed in double

quotes – System-provided header files are enclosed

between angular brackets

44C++ Programming: From Problem Analysis to Program Design, Seventh Edition