Chapter Structured Types, Data Abstraction and Classes

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
Inheritance and Composition (aka containment) Textbook Chapter –
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
1 Chapter 6 Object-Oriented Software Development.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 11: Classes and Data Abstraction
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
1 Structured Types, Data Abstraction and Classes.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
1 Structural Types, Data Abstractions and Classes.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 Review: C++ class represents an ADT 2 kinds of class members: data members and function members Class members are private by default Data members are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
1 Structured Types, Data Abstraction and Classes.
Chapter 12 Classes and Abstraction
Classes C++ representation of an object
What Is? function predefined, programmer-defined
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Dale/Weems/Headington
C++ Data Types and Data Abstractions
A First C++ Class – a Circle
Review: Two Programming Paradigms
About the Presentations
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
C++ Data Types and Data Abstractions
Introduction to Structured Data Types and Classes
CS212: Object Oriented Analysis and Design
Chapter 9 Scope, Lifetime, and More on Functions
Separate Compilation and Namespaces
Introduction to Classes
Classes and Data Abstraction
Classes: A Deeper Look Outline
Chapter 9 Classes: A Deeper Look, Part 1
Introduction to Classes and Objects
Chapter 14 Object-Oriented Software Development
Classes C++ representation of an object
Review: C++ class represents an ADT
What Is? function predefined, programmer-defined
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Chapter 11- 3 Structured Types, Data Abstraction and Classes Dale/Weems

class type Declaration The class declaration creates a data type and names the members of the class It does not allocate memory for any variables of that type! Client code still needs to declare class variables

Remember … Two kinds of class members: data members and function members Class members are private by default Data members are generally private Function members are generally declared public Private class members can be accessed only by the class member functions (and friend functions), not by client code

Class Scope Every function member can access any other members (function or data) no matter in what order the members are placed in the class body block Declaration-before-use rule does not apply to class members

Aggregate class Operations Built-in operations valid on class objects are Member selection using dot (.) operator , Assignment to another class variable using (=), Pass to a function as argument (by value or by reference), Return as value of a function Other operations can be defined as class member functions

Separate Specification and Implementation // Specification file “time.h” // Specifies the data and function members class Time { public: . . . private: }; // Implementation file “time.cpp” // Implements the Time member functions }

Implementation File for Time // Implementation file “time.cpp” // Implements the Time member functions. #include “ time.h” // Also must appear in client code #include <iostream> . . . bool Time::Equal(/* in */ Time otherTime) const // Postcondition: Return value == true, // if this time equals otherTime, // otherwise == false { return ((hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs)); }

Should be familiar … The member selection operator (.) selects either data members or function members Header files iostream and fstream declare the istream, ostream,and ifstream, ofstream I/O classes Both cin and cout are class objects and get and ignore are function members cin.get (someChar); cin.ignore (100, ‘\n’); These statements declare myInfile as an instance of class ifstream and invoke function member open ifstream myInfile; myInfile.open (“mydata.dat”);

Information Hiding Information hiding - Class implementation details are hidden from the client’s view Public functions of a class provide the interface between the client code and the class objects client code abstraction barrier specification implementation

Selection and Resolution C++ programs typically use several class types Different classes can have member functions with the same identifier, like Write() Member selection operator is used to determine the object to whom member function Write() is applied currentTime.Write(); // Class Time numberZ.Write(); // Class ComplexNumber In the implementation file, the scope resolution operator is used in the heading before the function member’s name to specify its class void Time::Write () const { . . . }

Time Class Instance Diagrams currentTime endTime Set Set Private data: hrs mins secs Private data: hrs mins secs Increment Increment 18 30 17 58 2 Write Write LessThan LessThan Equal Equal

Use of const with Member Functions When a member function does not modify the private data members, use const in both the function prototype (in specification file) and the heading of the function definition (in implementation file)

Example Using const with a Member Function void Time::Write () const // Postcondition: Time has been output in form // HH:MM:SS { if (hrs < 10) cout << ‘0’; cout << hrs << ‘:’; if (mins < 10) cout << mins << ‘:’; if (secs < 10) cout << secs; } 13

Separate Compilation and Linking of Files specification file main program time.h implementation file client.cpp time.cpp #include “time.h” Compiler Compiler client.obj time.obj Linker client.exe

Avoiding Multiple Inclusion of Header Files Often several program files use the same header file containing typedef statements, constants, or class type declarations--but, it is a compile-time error to define the same identifier twice within the same namespace This preprocessor directive syntax is used to avoid the compilation error that would otherwise occur from multiple uses of #include for the same header file #ifndef Preprocessor_Identifier #define Preprocessor_Identifier . #endif

Example Using Preprocessor Directive #ifndef // time .h For compilation the class declaration in // Specification file File time.h will be included only once #ifndef TIME_H #define TIME_H // time .cpp // client.cpp // IMPLEMENTATION FILE // Appointment program class Time { #include “time.h” #include “time.h” public: . . . . . . int main (void) { private: . . . . . . } }; #endif

Class Constructors A class constructor is a member function whose purpose is to initialize the private data members of a class object The name of a constructor is always the name of the class, and there is no return type for the constructor A class may have several constructors with different parameter lists A constructor with no parameters is the default constructor A constructor is implicitly invoked when a class object is declared--if there are parameters, their values are listed in parentheses in the declaration

Specification of Time Class Constructors class Time // Time.h { public : // 7 function members void Set(int hours, int minutes, int seconds); void Increment(); void Write() const; bool Equal(Time otherTime) const; bool LessThan(Time otherTime) const; // Parameterized constructor Time (int initHrs, int initMins, int initSecs); // Default constructor Time(); private : // 3 data members int hrs; int mins; int secs; }; 18

Implementation of Time Default Constructor Time::Time () // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { hrs = 0; mins = 0; secs = 0; } 19

Parameterized Constructor Time::Time( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs) // Constructor // Precondition: // 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 // Postcondition: // hrs == initHrs && mins == initMins // && secs == initSecs { hrs = initHrs; mins = initMins; secs = initSecs; } 20

Automatic invocation of constructors occurs Time departureTime; // Default constructor invoked Time movieTime (19, 30, 0);// Parameterized constructor departureTime movieTime Set Set Private data: hrs mins secs Increment Private data: hrs mins secs Increment 19 30 Write Write LessThan LessThan Equal Equal

The End of Chapter 11 Part 3