Top Banner

of 24

CSC335-Chapter4

Jun 04, 2018

Download

Documents

frankjamison
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/13/2019 CSC335-Chapter4

    1/24

    1

    (Chapter 4More about OOP and ADTsClasses)

    CSC-335

    Data Structures and Algorithms

    Instructor:Ahmad Reza Hadaegh

    The content of this power point lecture has been originallycreated by Christos Kolonis and modified by

    Dr. Ahmad R. Hadaegh

  • 8/13/2019 CSC335-Chapter4

    2/24

    2

    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

  • 8/13/2019 CSC335-Chapter4

    3/24

    3

    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 toavoid redundant declarations

    Discuss pointers to class objectsthe thispointer, inparticular

  • 8/13/2019 CSC335-Chapter4

    4/24

    4

    4.1 Contrast Procedural, Object Oriented Paradigms

    ProceduralAction-oriented concentrates

    on the verbs

    Programmers:

    Identify basic tasks to solve

    problem Implement actions to do tasks as

    subprograms (procedures /functions / subroutines)

    Group subprograms intoprograms / modules / libraries,

    together make up a completesystem for solving the problem

    Object-oriented Focuses on the nounsof problem

    specification

    Programmers:

    Determine objects needed for

    problem Determine how they should work

    together to solve the problem.

    Create types called classes madeup of

    data members

    function membersto operate onthe data.

    Instances of a type (class) calledobjects.

  • 8/13/2019 CSC335-Chapter4

    5/24

    5

    4.2 Structs and ClassesSimilarities

    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 non-

    homogeneous data sets.

  • 8/13/2019 CSC335-Chapter4

    6/24

    6

    Structs vs. ClassesDifferences

    No classes in C

    Members public by default

    Can be specified private

    Both structs and classesin C++

    Structs can havemembers declared private

    Class members areprivate by default

    Can be specified public

    Structs (in C) Classes & Classes (in C++)

  • 8/13/2019 CSC335-Chapter4

    7/24

    7

    Advantages in C++(structs and Classes)

    C++ structs and classes model objects whichhave:

    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

  • 8/13/2019 CSC335-Chapter4

    8/24

    8

    Class Declaration

    Syntax

    class ClassName{

    public:

    Declarations of public membersprivate:Declarations of private members

    };

  • 8/13/2019 CSC335-Chapter4

    9/24

    9

    Designing a Class

    Data members normally placed in private: sectionof a class

    Function members usually in public: section

  • 8/13/2019 CSC335-Chapter4

    10/24

    10

    Class Libraries

    Class declarations placed in header file Given .hextension

    Contains data items and prototypes

    Implementation file

    Same prefix name as header file

    Given .cppextension

    Programs which use this class library called client

    programs

  • 8/13/2019 CSC335-Chapter4

    11/24

    11

    Translating a Library

    Fig. 4.1

  • 8/13/2019 CSC335-Chapter4

    12/24

    12

    4.3 Example of User-DefinedTimeClass

    Recall Time struct from previous chapter Actions done to Timeobject required use of Timeparameter in

    the functions

    Now we create a Time class

    Actions done to Timeobject, done by the object itself

    Data members privateinaccessible to users of the class

    Information hiding

  • 8/13/2019 CSC335-Chapter4

    13/24

    13

    4.4 Constructors

    Constructor definition in Time.cpp example

    Syntax

    ClassName::ClassName (parameter_list): member_initializer_list{

    // body of constructor definition}

  • 8/13/2019 CSC335-Chapter4

    14/24

    14

    4.4 Constructors

    Results of defaultconstructor

    Results of explicit-valueconstructor

  • 8/13/2019 CSC335-Chapter4

    15/24

    15

    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 ofarguments of overloaded functions

    Checks the "signature" of the functions

  • 8/13/2019 CSC335-Chapter4

    16/24

    16

    ConstructorFunmction Overloadingrefers 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. Functionoverloading refers to multiple functions with the same name butdifferent signature (number of parameters may be different. Type ofparameters can be different too).

  • 8/13/2019 CSC335-Chapter4

    17/24

    17

    Default Arguments

    Possible to specify default values for constructor

    argumentsTime(unsigned initHours = 12,

    unsigned initMinutes = 0,

    char initAMPM = 'A');

    ConsiderTime t1, t2(5), t3(5,30), t4(5,30,'P');

  • 8/13/2019 CSC335-Chapter4

    18/24

    18

    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 >

    Note definitionof overloaded I/O operators

  • 8/13/2019 CSC335-Chapter4

    19/24

    19

    Copy Operations We can assign one object to another. For example suppose object

    bedTime and midnight have been created. For the followingassignments statement, we have :

    Time t = bedTime

    Or t = midnight;

  • 8/13/2019 CSC335-Chapter4

    20/24

    20

    Friend Functions

    Note use of two functions used for output display() andoperator

  • 8/13/2019 CSC335-Chapter4

    21/24

    21

    Friend Functions

    Implementation of the function

    ostream & operator

  • 8/13/2019 CSC335-Chapter4

    22/24

    22

    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

    http://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htmTime.cpp-http://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#Figure%204.4%20Test%20Driver%20for%20Time%20Classhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#ifndef%20TIMEhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htmto%20t2%20and%20false%20otherwise.http://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htmto%20t2%20and%20false%20otherwise.http://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htmto%20t2%20and%20false%20otherwise.http://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htmto%20t2%20and%20false%20otherwise.http://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#ifndef%20TIMEhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#ifndef%20TIMEhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#ifndef%20TIMEhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#ifndef%20TIMEhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htm#Figure%204.4%20Test%20Driver%20for%20Time%20Classhttp://localhost/var/www/apps/conversion/tmp/scratch_1/CodeSamplesChapter04.htmTime.cpp-
  • 8/13/2019 CSC335-Chapter4

    23/24

    23

    Pointers to Class Objects

    Possible to declare pointers to class objectsTime * timePtr = &t;

    Access withtimePtr->getMilTime() or(*timePtr).getMilTime()

  • 8/13/2019 CSC335-Chapter4

    24/24

    24

    The thisPointer

    Every class has a keyword, this

    a pointer whose value is the address of the object Value of *thiswould be the object itself