Learning Objectives Pointers as dada members Friend functions, friend classes Pointers and classes, the this pointer Operator overloading The BIG THREE Destructors Assignment operator Copy constructor
Topic 1: Pointers as Data Members In constructor, use new keyword to allocate space for the member To access pointer member, we need to dereference the pointer
Pointer as a Data Member Example Class G { private: int x; Int * y; public: G(int, int); void Reset_Y(int); } G::G(int xx, int yy) { x = xx; y = new int; *y = yy; } void G::Reset_Y(int w) *y = w;
Topic 2: Friend Functions / Classes Friend function of a class Not a member function Has direct access to private members Just as member functions do Use keyword friend in front of function declaration friend return_type function_name(parameters) Specified in class definition (public field) But they’re NOT member functions!
Friend Function Example Class G { private: int x; public: G(int); Int get_x(); friend void F(int , int); } This means: function F is a friend of class G and F can directly access the private member of G
Friend Classes Entire classes can be friends Syntax: friend class F Example: class F is friend of class C All class F member functions are friends of C NOT reciprocated Friendship granted, not taken Syntax: friend class F Goes inside class definition of "authorizing" class
Friend Class Example Class G { private: int x; public: get_x(); friend class F; } This means: class F is a friend of class G and functions in F can directly access the private member of G
Topic 3: Pointer and Classes The -> operator Shorthand notation Combines dereference operator, *, and dot operator Specifies member of class "pointed to" by given pointer Example: MyClass *p; p = new MyClass; p->grade = "A"; Equivalent to: (*p).grade = "A";
The this Pointer Member function definitions might need to refer to calling object Use predefined this pointer Note: this is not the name of the calling object, but is the name of a pointer that points to the calling object. Why do we need this pointer To overcome the ambiguities of member variable name and parameter name
Topic 4: Operator Overloading Recall: Function overlaoding Operators +, -, %, ==, etc. Really just functions! Simply "called" with different syntax: x + 7 Think of it as: +(x, 7) "+" is the function name x, 7 are the arguments Function "+" returns "sum" of it’s arguments
Overloading Basics Overloading operator Operator itself is "name" of function NOT a member function Example: + operator overloading declaration: const Money operator +(const Money& a1, const Money& a2); Function name: operator + Uses reference parameters for efficiency Use const for parameters to avoid changes of objects Both qualifiers are optional Returned value is type Money
Overloading Basics Example: + operator overloading declaration: const Money operator +(const Money& a1, const Money& a2); Use const for return value to avoid code like this: MyClass a, b, c; ... (a + b) = c; // What does this mean? This statement would basically do nothing, but if the + operator returns a non-const value, it will compile! So, we want to return a const instance, so that such code will not even be allowed to compile.
Overloaded "==" Equality operator, == Enables comparison of Money objects Declaration: bool operator ==(const Money& a1, const Money& a2); Function name: operator == Returns bool type for true/false equality Uses reference parameters for efficiency Use const for parameters to avoid changes of objects Again, it’s a non-member function
Topic 5: The BIG THREE Dynamically-allocated variables Do not go away until "deleted" If member data are pointers They dynamically allocate memory (using new keyword) in constructors Must have means to "de-allocate" when object is destroyed Answer: destructor!
Destructors Opposite of constructor Automatically called when object is out-of-scope Default version only removes ordinary variables, not dynamic variables Defined like constructor, just add ~ MyClass::~MyClass() { //Perform clean-up duties for dynamic variables }
Destructors The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated
Assignment operator (=) Shallow copy Only copy value to value; Does not copy dynamically allocated field Works fine for classes that have no dynamic variables Has problems with classes that have dynamic variables
Operator Overloading (=) Deep Copy (solution to the problems of shallow copy) Assignment operator overloading Using copy constructor
Operator (=) Deep Copy (Assignment Operator Overloading) class_name & operator=(const class_name &) Pass by reference is for efficiency const for parameters to avoid changes of object The reason operator= returns a reference is so that you can concatenate multiple assignments in one statement MyClass a, b, c; a = b = c; It is a member function Why return value is not const? To allow code like this: (a = b) = c;
Operator (=) Deep Copy (copy constructor overloading) 2. Using copy constructor class_name(class_name&) It is a member function
Shallow copy and Deep copy Shallow copy is called when assignment operator and copy constructor are not overloaded Deep copy is called when assignment operator and copy constructor are overloaded Assignment operator is called when = operator is used between two initialized objects Copy constructor is called when one declared object is initialized by another object A function returns a value of the class type A function has call-by-value class type parameters
The BIG THREE Destructor, assignment operator overloading, copy constructor should be used together When to use The data members of a class are pointers