Top Banner

of 42

9781423902577_PPT_CH07

Oct 14, 2015

Download

Documents

oopnotes
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
  • Object-Oriented Programming Using C++Fourth EditionChapter 7Using Classes

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*ObjectivesCreate classesLearn about encapsulating class componentsImplement functions in a classUnderstand the unusual use of private functions and public dataConsider scope when defining member functionsUse static class membersLearn about the this pointerUnderstand the advantages of polymorphism

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Creating ClassesA class is a category of objects; it is a new data typeClasses provide a description of an objectClasses provide a convenient way to group related data and the functions that use the dataWhen you create an object from the class, you automatically create all the related fieldsYou think about them and manipulate them as real-life classes and objectsAbstract data type (ADT): a type that you define

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Creating Classes (continued)Student aSophomore;aSophomore.idNum = 7645;cout
  • Object-Oriented Programming Using C++, Fourth Edition*Creating Classes (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Encapsulating Class ComponentsTo encapsulate components is to contain themEncapsulation is an example of a black boxAn interface intercedes between you and the inner workings of an object

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Designing ClassesIf you need a class for students, you should ask:What shall we call it?What are its attributes?What methods are needed by Student?Any other methods?In most cases, you declare both fields and functionsDeclare a field using a data type and an identifierDeclare a function by writing its prototype, which serves as the interface to the function

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Designing Classes (continued)To instantiate an object is to declare or create itStudent aSophomore;aSophomore.displayStudentData();A function that uses your class is a class client

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Implementing Functions in a Class

    When you construct a class, you create two parts: Declaration section: contains the class name, variables (attributes), and function prototypesImplementation section: contains the functionsUse both the class name and the scope resolution operator (::) when you implement a member function

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Implementing Functions in a Class (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using Public Functions to Alter Private Data

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using Public Functions to Alter Private Data (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using Public Functions to Alter Private Data (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Unusual Use: Using Private Functions and Public Data

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Unusual Use: Using Private Functions and Public Data (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • *Object-Oriented Programming Using C++, Fourth EditionUnusual Use: Using Private Functions and Public Data (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Considering Scope when Defining Member Functions

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Considering Scope when Defining Member Functions (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using Static Class MembersWhen a class field is static, only one memory location is allocatedAll members of the class share a single storage location for a static data member of that same classWhen you create a non-static variable within a function, a new variable is created every time you call that functionWhen you create a static variable, the variable maintains its memory address and previous value for the life of the program

    Object-Oriented Programming Using C++, Fourth Edition

  • Defining Static Data MembersObject-Oriented Programming Using C++, Fourth Edition*

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Defining Static Data Members (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Defining Static Data Members (continued)Static variables are sometimes called class variables, class fields, or class-wide fields because they dont belong to a specific object; they belong to the class

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Since it is not const, anyone can modify itDefining Static Data Members (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Defining Static Data Members (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Defining Static Data Members (continued)Constant fields are never changeableNon-constant fields are changeableStatic fields hold the same value for every object instantiated from a classNon-static fields can hold different values for every object of a class

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Defining Static Data Members (continued)Non-constant, non-static fields can hold different values for every object and can be changedConstant, non-static fields cannot be changed, but a copy is made for every instantiationNon-constant, static fields hold the same value for every instantiationCan be alteredConstant static fields hold one unchangeable value for every object

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using Static FunctionsA static function can be used without a declared objectNon-static functions can access static variables (provided there is an object)Static functions cannot access non-static variables

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using Static Functions (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding the this Pointer

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding the this Pointer (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding the this Pointer (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding the this Pointer (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding the this Pointer (continued)The this pointer holds the memory address of the current object that is using the functionThe this pointer is automatically supplied when you call a non-static member function of a classExample: clerk.getIdNum(); is actually getIdNum(&clerk); The actual argument list used by the compiler for getIdNum() is getIdNum(Employee *this)The this pointer is a constant pointer

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using the this Pointer Explicitly

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using the Pointer-to-Member Operator

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using the Pointer-to-Member Operator (continued)

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding PolymorphismPolymorphism is the object-oriented program feature that allows the same operation to be carried out differently depending on the objectExample:cout
  • Object-Oriented Programming Using C++, Fourth Edition*You Do It: Creating and Using a Classclass CollegeCourse{ private: string department; int courseNum; int seats; public: void setDepartmentAndCourse(string, int); void setSeats(int); void displayCourseData();};

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Using a static Fieldclass Letter{ private: string title; string recipient; static int count; public: void setRecipient(string, string); void displayGreeting(); static void displayCount();};

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Understanding How static and Non-static Fields are Stored

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*SummaryA class is a category of objectsWhen you create a class, you hide, or encapsulate, the individual componentsWhen you construct a class, you create the declaration section and the implementation sectionWhen you create a class, usually you want to make data items private, and to make functions publicThe scope resolution operator (::) identifies a member function as being in scope within a class

    Object-Oriented Programming Using C++, Fourth Edition

  • Object-Oriented Programming Using C++, Fourth Edition*Summary (continued)Each class object gets its own block of memory for its data membersYou can access a static, class-wide field using a static functionOne copy of each class member function is stored no matter how many objects existWithin any member function, you can explicitly use the this pointer to access the objects data fieldsPolymorphism allows the same operation to be carried out differently depending on the object

    Object-Oriented Programming Using C++, Fourth Edition

    ********************************