DOC PREVIEW
UT Dallas CS 4337 - Chapter14 Class2 - static member, friend, copy object, overloading

This preview shows page 1-2-3-22-23-24-44-45-46 out of 46 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 46 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

PowerPoint Presentation14.1Instance and Static Membersstatic member variableSlide 5Three Instances of the Tree Class, But Only One objectCount Variablestatic member functionSlide 814.2Friends of Classesfriend Function Declarationsfriend Class Declarations14.3Memberwise AssignmentSlide 15Slide 1614.4Copy ConstructorsSlide 19Slide 20Programmer-Defined Copy ConstructorSlide 22Slide 23Slide 24Slide 25Slide 2614.5Operator OverloadingSlide 29Invoking an Overloaded OperatorReturning a ValueSlide 32The this Pointerthis Pointer ExampleNotes on Overloaded OperatorsSlide 36Overloading Types of OperatorsSlide 38Slide 39Overloaded [] Operator14.6Object Conversion14.7AggregationSlide 45See the Instructor, TextBook, and Course classes in Chapter 14.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Chapter 14:More About ClassesCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.14.1Instance and Static MembersCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Instance and Static Members•instance variable: a member variable in a class. Each object has its own copy.•static variable: one variable shared among all objects of a class•static member function: can be used to access static member variable; can be called before any objects are definedCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.static member variableContents of Tree.h 1 // Tree class 2 class Tree 3 { 4 private: 5 static int objectCount; // Static member variable. 6 public: 7 // Constructor 8 Tree() 9 { objectCount++; }10 11 // Accessor function for objectCount12 int getObjectCount() const13 { return objectCount; }14 };15 16 // Definition of the static member variable, written17 // outside the class.18 int Tree::objectCount = 0;Static member declared here.Static member defined here.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Three Instances of the Tree Class, But Only One objectCount VariableCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.static member function•Declared with static before return type:static int getObjectCount() const{ return objectCount; }•Static member functions can only access static member data•Can be called independent of objects:int num = Tree::getObjectCount();Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Modified Version of Tree.h 1 // Tree class 2 class Tree 3 { 4 private: 5 static int objectCount; // Static member variable. 6 public: 7 // Constructor 8 Tree() 9 { objectCount++; }10 11 // Accessor function for objectCount12 static int getObjectCount() const13 { return objectCount; }14 };15 16 // Definition of the static member variable, written17 // outside the class.18 int Tree::objectCount = 0;Now we can call the function like this:cout << "There are " << Tree::getObjectCount() << " objects.\n";See Budget Version 2Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.14.2Friends of ClassesCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Friends of Classes•Friend: a function or class that is not a member of a class, but has access to private members of the class•A friend function can be a stand-alone function or a member function of another class•It is declared a friend of a class with friend keyword in the function prototypeCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc. friend Function Declarations•Stand-alone function:friend void setAVal(intVal&, int);// declares setAVal function to be// a friend of this class•Member function of another class:friend void SomeClass::setNum(int num)// setNum function from SomeClass // class is a friend of this classSee Budget Version 3Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc. friend Class Declarations•Class as a friend of a class:class FriendClass{...};class NewClass{public: friend class FriendClass; // declares// entire class FriendClass as a friend// of this class…};Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.14.3Memberwise AssignmentCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Memberwise Assignment•Can use = to assign one object to another, or to initialize an object with an object’s data•Copies member to member. e.g.,instance2 = instance1; means copy all member values from instance1 and assign to the corresponding member variables of instance2•Use at initialization:Rectangle r2 = r1;Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.14.4Copy ConstructorsCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copy Constructors•Special constructor used when a newly created object is initialized to the data of another object of same class•Default copy constructor (memberwise assignment) copies field-to-field•Default copy constructor works fine in many casesCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copy ConstructorsProblem: what if object contains a pointer?class SomeClass{private: int *value; public: SomeClass(int val){value=new int; *value = val;} int getVal(); void setVal(int); }Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Copy ConstructorsWhat we get using memberwise copy with objects containing dynamic memory:SomeClass object1(5);SomeClass object2 = object1;object2.setVal(13);cout << object1.getVal(); // also 13object1object2value value13Copyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Programmer-Defined Copy Constructor•Allows us to solve problem with objects containing pointers:SomeClass::SomeClass(const SomeClass &obj){ value = new int; *value = obj.value;}•Copy constructor takes a reference parameter to an object of the classCopyright © 2012 Pearson Education, Inc.Copyright © 2012 Pearson Education, Inc.Programmer-Defined Copy Constructor•Each object now points to


View Full Document

UT Dallas CS 4337 - Chapter14 Class2 - static member, friend, copy object, overloading

Download Chapter14 Class2 - static member, friend, copy object, overloading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter14 Class2 - static member, friend, copy object, overloading and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter14 Class2 - static member, friend, copy object, overloading 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?