0% found this document useful (0 votes)
54 views

CSC-335 Data Structures and Algorithms

This document summarizes a lecture on data structures and algorithms that covers object-oriented programming concepts like classes, class constructors, operator overloading, and friend functions. The key topics include contrasting procedural and object-oriented programming, building a sample Time class with private data members and public access functions, and using techniques like default arguments, copy operations, and conditional compilation to avoid redundant declarations when working with classes.

Uploaded by

frankjamison
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

CSC-335 Data Structures and Algorithms

This document summarizes a lecture on data structures and algorithms that covers object-oriented programming concepts like classes, class constructors, operator overloading, and friend functions. The key topics include contrasting procedural and object-oriented programming, building a sample Time class with private data members and public access functions, and using techniques like default arguments, copy operations, and conditional compilation to avoid redundant declarations when working with classes.

Uploaded by

frankjamison
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CSC-335 Data Structures and Algorithms (Chapter 4 More about OOP and ADTs Classes)

Instructor: Ahmad Reza Hadaegh

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

4.1 Contrast Procedural, Object Oriented Paradigms

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.

Instances of a type (class) called objects.

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).

Thus, both are used to process nonhomogeneous data sets.

Structs vs. Classes Differences


Structs (in C) No classes in C Members public by default Classes & Classes (in C++) Both structs and classes in C++

Can be specified private

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)

Leads to object oriented programming


Objects are self contained "I can do it myself" mentality They do not pass a parameter to an external function

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

Contains data items and prototypes

Implementation file
Same prefix name as header file Given .cpp extension

Programs which use this class library called client programs

10

Translating a Library
Fig. 4.1

11

4.3 Example of User-Defined Time Class


Recall Time struct from previous chapter Actions done to Time object required use of Time parameter in
the functions

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

Results of explicit-value constructor

14

Overloading Functions
Note existence of multiple functions with the same name:
Time(); Time(unsigned initHours, unsigned initMinutes, char initAMPM); Known as overloading

Compiler compares numbers and types of arguments of overloaded functions


Checks the "signature" of the functions
15

Constructor Funmction Overloading refers to Figure 4.7 in the book


Class Time { public: Time(); Time(unsigned initHours, unsigned initMinutes, char initAMPM void Display(ostream& out) const; void read(istream& in);

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

Section 4.5 - Other Class Operations


Accessors and Mutators
See get and set functions

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<<()

Possible to specify operator<<() as a "friend" function


Thus given "permission" to access private data elements

Declaration in .h file (but not inside class)


friend ostream & operator<<(ostream & out, const Time & t)

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

Causes "redeclaration" errors at compile time Solution is to use conditional compilation


Use #ifndef and #define and #endif compiler directives

22

Pointers to Class Objects


Possible to declare pointers to class objects
Time * timePtr = &t;

Access with
timePtr->getMilTime() or (*timePtr).getMilTime()
23

The this Pointer


Every class has a keyword, this
a pointer whose value is the address of the object Value of *this would be the object itself

24

You might also like