KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Date.h
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 3 Date.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 4 Date.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 5 Employee.h
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 6 Employee.cpp Class Date does not provide a constructor that receives a parameter of type Date. The compiler provides each class with a default copy constructor. Each data member is copied. birthdate=dateofbirth; hiredate=dateofhire; If birthdate and hiredate are not const parameters, then default memberwise assignment can be used. Again no definition of such a constructor by the user
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 7 Employee.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 8 Default Member-wise Assignment Many classes must provide their own assignment operator To intelligently assign one object to another More about assignment of objects to each other when we get to Operator Overloading The '=' operator can be overloaded, just like any other Copy Constructor A constructor that copies another object of the same type Compiler provides a default copy constructor Copies each member of the original object into the corresponding member of the new object i.e., member-wise assignment Enables pass-by-value for objects Used to copy original object s values into new object to be passed to a function or returned from a function Many classes must provide an explicit Copy Constructor To intelligently copy one object to another A Deeper Look at Classes
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 9
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 10 test_employee.cpp
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 11 friend Function of a Class Ordinary Member Functions: 1. Function can access the private members of the class 2. Function is in the scope of the class 3. Function must be invoked on a specific object of the class e.g., ptr -> func() obj.func()
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 12 friend Function of a Class Defined outside that class s scope. Not a member function of that class. Has right to access non-public and public members of that class. Often appropriate when a member function cannot be used for certain operations. Can enhance performance.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 13 friend Functions and friend Classes To declare a function as a friend of a class: Provide the function prototype in the class definition preceded by keyword friend To declare a class as a friend of another class: Place a declaration of the form friend class ClassTwo; in the definition of class ClassOne All member functions of class ClassTwo are friends of class ClassOne Friendship is granted, not taken. For class B to be a friend of class A, class A must explicitly declare that class B is its friend. Friendship relation is neither symmetric nor transitive If class A is a friend of class B, and class B is a friend of class C, cannot infer that class B is a friend of class A, class C is a friend of class B, class A is a friend of class C.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 14 friend Functions and friend Classes friend function declaration (can appear anywhere in the class) friend function can modify Count s private data Calling a friend function; note that we pass the Count object to the function
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 15 this Pointer Member functions know which object s data members to manipulate Every object has access to its own address through a pointer called this (a C++ keyword) An object s this pointer is not part of the object itself The this pointer is passed (by the compiler) as an implicit argument to each of the object s non-static member functions Objects may use the this pointer implicitly or explicitly. this is used implicitly when accessing members directly. It is used explicitly when using keyword this. Type of the this pointer depends on type of the object, and whether member function is declared const.
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 16 this Pointer Implicitly using the this pointer to access member x Explicitly using the this pointer to access member x Using the dereferenced this pointer and the dot operator
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 17 Cascaded member-function calls Multiple functions are invoked in the same statement. Enabled by member functions returning the dereferenced this pointer. Time t; t.sethour(15).setminute( 30 ).setsecond( 22 ); Time& Time::setHour(int hr) { hour=hr; return *this; }