Lecture 1 DS Using C++
Lecture 1 DS Using C++
Objectives
Become aware of structured design and objectoriented design programming methodologies Learn about classes Become aware of private, protected, and public members of a class Explore how classes are implemented Become aware of Unified Modeling Language (UML) notation Examine constructors and destructors Become aware of an abstract data type (ADT) Explore how classes are used to implement ADTs
Data Structures Using C++ 2E 2
Classes
OOD first step: identify components (objects) Encapsulation: object combines data and data operations in a single unit Class: collection of a fixed number of components
Class members: class components Class member categories
Private, public, protected
Classes (contd.)
Constructors
Declared variable not automatically initialized With parameters or without parameters (default constructor) Properties
Constructor name equals class name Constructor has no type All class constructors have the same name Multiple constructors: different formal parameter lists Execute automatically: when class object enters its scope Execution: depends on values passed to class object
Data Structures Using C++ 2E 4
Classes (contd.)
Unified Modeling Language diagrams
Graphical notation describing a class and its members Private and public members
Classes (contd.)
Variable (object) declaration
Once class defined
Variable declaration of that type allowed
Class variable
Called class object, class instance, object in C++
A class can have both types of constructors Upon declaring a class object
Default constructor executes or constructor with parameters executes
Classes (contd.)
Accessing class members
When an object of a class is declared
Object can access class members
Classes (contd.)
Implementation of member functions
Reasons function prototype often included for member functions
Function definition can be long, difficult to comprehend Providing function prototypes hides data operation details
Classes (contd.)
Implementation of member functions (contd.)
Example: definition of the function setTime
Classes (contd.)
Implementation of member functions (contd.) Execute statement myClock.setTime(3,48,52);
FIGURE 1-6 Object myClock after the statement myClock.setTime(3, 48, 52); executes
10
Classes (contd.)
Implementation of member functions (contd.)
Example: definition of the function setTime
11
Classes (contd.)
Implementation of member functions (contd.)
Objects of type clockType
myClock and yourClock
12
Classes (contd.)
Implementation of member functions (contd.)
if(myClock.equalTime(yourClock))
Object myClock accesses member function equalTime otherClock is a reference parameter Address of actual parameter yourClock passed to the formal parameter otherClock
Classes (contd.)
Implementation of member functions (contd.)
equalTime execution Variables hr , min , sec in equalTime function body
Instance variables of variable myClock
Client
Program or software using and manipulating class objects
Instance variables
Have own instance of data
Data Structures Using C++ 2E 14
Classes (contd.)
Reference parameters and class objects (variables)
Variable passed by value
Formal parameter copies value of the actual parameter
Variables requiring large amount of memory and needing to pass a variable by value
Corresponding formal parameter receives copy of the data of the variable
15
Classes (contd.)
Reference parameters and class objects (variables) (contd.)
Declaring class object as a value parameter
Declare as a reference parameter using the keyword const
Classes (contd.)
Reference parameters and class objects (variables) (contd.)
Two built-in operations
Member access (.) Assignment (=)
Classes (contd.)
Class scope
Automatic
Created each time control reaches declaration Destroyed when control exits surrounding block
Static
Created once when control reaches declaration Destroyed when program terminates
Can declare an array of class objects: same scope Member of a class: local to the class Access (public) class member outside the class
Use class object name, member access operator (.)
Data Structures Using C++ 2E 18
Classes (contd.)
Functions and classes
Rules
Class objects passed as parameters to functions and returned as function values Class objects passed either by value or reference as parameters to functions Class objects passed by value: instance variables of the actual parameter contents copied into the corresponding formal parameter instance variables
19
Classes (contd.)
Constructors and default parameters
Constructor can have default parameters Rules declaring formal parameters
Same as declaring function default formal parameters
Default constructor
No parameters or all default parameters
20
Classes (contd.)
Destructors
Functions No type Neither value-returning nor void function One destructor per class
No parameters
Name
Tilde character (~) followed by class name
Automatically executes
When class object goes out of scope
Data Structures Using C++ 2E 21
Classes (contd.)
Structs
Special type of classes All struct members public C++ defines structs using the reserved word struct If all members of a class are public, C++ programmers prefer using struct to group the members Defined like a class
22
Data abstraction
Process
Separating logical data properties from implementation details
24
25
Additional Notes
This lectures covers the following material from the textbook:
Chapter 1: pp. 17 - 57
26