UT Dallas CS 4337 - Chapter14 Class2 - static member, friend, copy object, overloading (46 pages)

Previewing pages 1, 2, 3, 22, 23, 24, 44, 45, 46 of 46 page document View the full content.
View Full Document

Chapter14 Class2 - static member, friend, copy object, overloading



Previewing pages 1, 2, 3, 22, 23, 24, 44, 45, 46 of actual document.

View the full content.
View Full Document
View Full Document

Chapter14 Class2 - static member, friend, copy object, overloading

27 views


Pages:
46
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 14 More About Classes Copyright 2012 Pearson Education Inc 14 1 Instance and Static Members 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 defined Copyright 2012 Pearson Education Inc static member variable Contents of Tree h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Static member declared here Tree class class Tree private static int objectCount Static member variable public Constructor Tree objectCount Accessor function for objectCount int getObjectCount const return objectCount Static member defined here Definition of the static member variable written outside the class int Tree objectCount 0 Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Three Instances of the Tree Class But Only One objectCount Variable 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 Modified Version of Tree h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Tree class class Tree private static int objectCount public Constructor Tree objectCount Static member variable Accessor function for objectCount static int getObjectCount const return objectCount Definition of the static member variable written outside the class int Tree objectCount 0 Now we can call the function like this cout There are Tree getObjectCount objects n See Budget Version 2 Copyright 2012 Pearson Education Inc 14 2 Friends of Classes 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 prototype 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 class See Budget Version 3 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 14 3 Memberwise Assignment 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 14 4 Copy Constructors 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 cases Copyright 2012 Pearson Education Inc Copy Constructors Problem 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 Copy Constructors What we get using memberwise copy with objects containing dynamic memory SomeClass object1 5 SomeClass object2 object1 object2 setVal 13 cout object1 getVal also 13 object1 value Copyright 2012 Pearson Education Inc 13 object2 value 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 class Copyright 2012 Pearson Education Inc Programmer Defined Copy Constructor Each object now points to separate dynamic memory SomeClass object1 5 SomeClass object2 object1 object2 setVal 13 cout object1 getVal still 5 object1 value Copyright 2012 Pearson Education Inc 5 13 object2 value Programmer Defined Copy Constructor Since copy constructor has a reference to the object it is copying from SomeClass SomeClass SomeClass obj it can modify that object To prevent this from happening make the object parameter const SomeClass SomeClass const SomeClass obj Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc Copyright 2012 Pearson Education Inc 14 5 Operator Overloading Copyright 2012 Pearson Education Inc Operator Overloading Operators such as and others can be redefined when used with objects of a class The name of the function for the overloaded operator is operator followed by the operator symbol e g operator to overload the operator and operator to overload the operator Prototype for the overloaded operator goes in the declaration of the class that is overloading it Overloaded operator function definition goes with other member functions Example Class Date which holds the month day and year member variables and a member function add Date today today add 5 to add 5 days to the date stored in today Equivalently today 5 Copyright 2012 Pearson Education Inc Operator Overloading Prototype void operator const SomeClass rval return type function name parameter for object on right side of operator Operator is called via object on left side Copyright 2012 Pearson Education Inc Invoking an Overloaded Operator Operator can be invoked as a member function object1 operator object2 It can also be used in more conventional manner object1 object2 See StudentTestScores Version 3 Copyright 2012 Pearson Education Inc Returning a Value Overloaded operator can return a value class Point2d public double operator const point2d right return sqrt pow x right x 2 pow y right y 2 private int x y Point2d point1 2 2 point2 4 4 Compute and display distance between 2 points cout point2 point1 endl displays 2 82843 Copyright 2012 Pearson Education Inc


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

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