CSC-335 Data Structures and Algorithms
CSC-335 Data Structures and Algorithms
The content of this power point lecture has been originally created by Christos Kolonis and modified by Dr. Ahmad R. Hadaegh
Chapter Contents
4.1 Procedural vs. Object-Oriented Programming 4.2 Classes 4.3 Example: A First Version of a User-Defined Time Class 4.4 Class Constructors 4.5 Other Class Operators
Chapter Objectives
Contrast OOP with procedural programming Review classes in C++
Study in detail a specific example of how a class is built Show how operators can be overloaded for new types Show how conditional compilation directives are used to avoid redundant declarations Discuss pointers to class objects the this pointer, in particular
Procedural
Action-oriented concentrates on the verbs Programmers: Identify basic tasks to solve problem Implement actions to do tasks as subprograms (procedures / functions / subroutines) Group subprograms into programs / modules / libraries, together make up a complete system for solving the problem
Object-oriented
Focuses on the nouns of problem specification Programmers: Determine objects needed for problem Determine how they should work together to solve the problem. Create types called classes made up of
data members function members to operate on the data.
4.2 Structs and Classes Similarities Essentially the same syntax Both are used to model objects with multiple attributes (characteristics)
represented as data members also called fields or instance or attribute variables).
Structs can have members declared private Class members are private by default Can be specified public
Advantages in C++ (structs and Classes) C++ structs and classes model objects which have:
Attributes represented as data members Operations represented as functions (or methods)
Class Declaration
Syntax
class ClassName { public: Declarations of public members private: Declarations of private members };
Designing a Class
Data members normally placed in private: section of a class Function members usually in public: section
Class Libraries
Class declarations placed in header file
Given .h extension
Implementation file
Same prefix name as header file Given .cpp extension
10
Translating a Library
Fig. 4.1
11
Now we create a Time class Actions done to Time object, done by the object itself
Data members private inaccessible to users of the class Information hiding
12
4.4 Constructors
Constructor definition in Time.cpp example Syntax
ClassName::ClassName (parameter_list) : member_initializer_list { // body of constructor definition }
13
4.4 Constructors
Results of default constructor
14
Overloading Functions
Note existence of multiple functions with the same name:
Time(); Time(unsigned initHours, unsigned initMinutes, char initAMPM); Known as overloading
private: unsigned myHours; myMinutes; char myAMorPM; unsigned myMilTime // military time equivalent
}; Having multiple constructor is a sign of function overloading. Function overloading refers to multiple functions with the same name but different signature (number of parameters may be different. Type of parameters can be different too).
16
Default Arguments
Possible to specify default values for constructor arguments
Time(unsigned initHours = 12, unsigned initMinutes = 0, char initAMPM = 'A');
Consider
Time t1, t2(5), t3(5,30), t4(5,30,'P');
17
Overloading operators
Same symbol can be used more than one way Note declaration for I/O operators << and >> Note definition of overloaded I/O operators
18
Copy Operations
We can assign one object to another. For example suppose object bedTime and midnight have been created. For the following assignments statement, we have : Time t = bedTime
Or
t = midnight;
19
Friend Functions
Note use of two functions used for output
display() and operator<<()
20
Friend Functions
Implementation of the function
ostream & operator<<( ostream & out, const Time & t) { out << t.myHours<<":" <<(t.myMinutes< 10? "0": "") <<t.myMinutes << ' '<<t.myAMorPM<<".M."; return out; } There are also other operations. Take a look at those functions and let me know if you have any issue with them.
21
Redundant Declarations
Note use of #include "Time.h" in
Time.cpp
Client program
22
Access with
timePtr->getMilTime() or (*timePtr).getMilTime()
23
24