# 1 CSC241: Object Oriented Programming Lecture No 21.

## Presentation on theme: "1 CSC241: Object Oriented Programming Lecture No 21."— Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 21

2 Previous Lecture Polymorphism Example program – person class Virtual destructor – base class destructor is virtual, derived class destructor also become virtual Friend functions – can access private member of a class

3 Today’s Lecture Friend function – example program: Distance class – Friend function for functional notation Friend classes static functions

4 Distance Example class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “ : ” << inches ; } Distance operator + ( Distance ) const; }; Distance Distance::operator + ( Distance d2 ) const { int f = feet + d2.feet; float i = inches + d2.inches; if(i >= 12.0) { i -= 12.0; f++; } return Distance(f,i); } When such constructor exists, following statements are allowed d3 = 15.5; d3 = d1 + 10.0;

5 Cont. d3 = 10.5; – 10.5 is passed to one argument constructor, – 10.5 is converted into Distance object – d3 = nameless object of Distance; d3 = d1 + 9.75; – 9.75 is converted into Distance object by calling one argument constructor – Then operator + function is invoked by d1 object d3 = 6.5 + d1; d3 = Distance(6.5) + d1;

6 Note When a float value pass as argument to function – Definition: void function (Distance d1) { … } – Call : function (10.75); – One argument constructor is called Distance(float) – Convert float value i.e. 10.75 into Distance object – Call : function (Distance (10.75)) – function (Nameless Distance object) d3 = 10.75 + d1; – Neither float value 10.75 invoke operator + function – Nor operator knows to convert float to Distance

7 Distance Example – Friend function class Distance {. friend Distance operator + ( Distance, Distance ) const; }; Distance Distance::operator + (Distance d1, Distance d2) const { int f = d1.feet + d2.feet; float i = d1.inches + d2.inches; if(i >= 12.0) i -= 12.0; f return Distance(f,i); } d3 = 10.0 + d1; friend function Overloaded () operator took two argument

8 friends for Functional Notation class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “ : ” << inches ; } float square(); }; float Distance::square() { float fltfeet = feet + inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd; } main() { Distance dist(3, 6.0); float sqft; sqft = dist.square(); cout << “\nDistance = “; dist.showdist(); cout << “\nSquare = “ << sqft; } sqft = square(dist);

9 Cont. class Distance { private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() const { cout << feet << “:” << inches; } friend float square(Distance); }; float square(Distance d) { float fltfeet; fltfeet = d.feet + d.inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd; } In general, the friend version of a function requires one more argument than when the function is a member. Go to program

10 Friend classes Same as a function can be friend of a class – Friend function can access private data member A class can be friend of another class – All functions of friend class become friend function of that class

11 Cont. class alpha { private: int data1; public: alpha() : data1(99) { } friend class beta; }; main() { alpha a; beta b; b.func1(a); b.func2(a); cout << endl; } class beta { public: void func1(alpha a) { cout << “\ndata1=” << a.data1; } void func2(alpha a) { cout << “\ndata1=” << a.data1; } }; In class alpha the entire class beta is proclaimed a friend. Now all the member functions of beta can access the private data of alpha

12 Static function A static data member is not duplicated for each object; rather a single data item is shared by all objects of a class Example: a class that keep track of how many objects of itself have created Function can also be static A static function can only access static data member of class

13 Example program class gamma { private: static int total; int id; public: gamma() { total++; id = total; } ~gamma() { total--; cout << “Destroying ID “ << id << endl; } static void showtotal() { cout << “Total is “ << total; } void showid() { cout << “ID number is “ << id; } }; int gamma::total = 0;

14 Static vs. non static functions Static function can only access static data member of class – className::static_function(); – Object_of_class.static function(); Non static member function can access static and non static data member of class – Object_of_class.Non_static_function(); Go to program

15 Assignment and Copy Initialization C++ compiler do many things on programmer behalf. For example: – Assignment operator – Copy constructor Assignment a2 = a1; Copy constructor alpha a2(a1); cause the compiler to copy the data from a1, member by member, into a2. compiler creates an object, a2, copies the data from a1, member by member, into a2

16 Cont. Both of these default activities are provided, free of charge, by the compiler If member-by member copying is required, then default activities are enough If assignment or initialization has to do something more complex, then they must be override First, separate example to override default assignment and copy constructor String class in more efficient way by overriding these two activities

17 Overloading the Assignment Operator class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } void display() { cout << data; } alpha operator = (alpha& a){ data = a.data; cout << “Assignment operator invoked”; return alpha(data); } }; main() { alpha a1(37); alpha a2; a2 = a1; cout << “\na2=”; a2.display(); alpha a3 = a2; cout << “\na3=”; a3.display(); } Program Output Assignment operator invoked a2=37 a3=37 Go to program

18 Note Initialization Is Not Assignment – alpha a3 = a2; // copy initialization is not an assignment but an initialization, with the same effect as – alpha a3(a2); Passing by Reference – Save memory space – Avoid creation of unwanted objects Returning a Value -Cannot do return by reference

19 Assignment operator – inheritance The assignment operator is unique among operators in that it is not inherited If you overload the assignment operator in a base class, you can’t use this same function in any derived classes

20 The Copy Constructor An object can be define and initialize with values of another object of same class – alpha a3(a2); // copy initialization – alpha a3 = a2; // copy initialization, alternate syntax Both invoke a copy constructor: – That creates a new object and copies its argument into it Default copy construct perform member by member copy Distance d2 = d1; 7 6.5 feet inches d1 feet inches d2 7 6.5

21 Cont. It is similar to what assignment operator (d2 = d1) does The difference is that copy initialization also creates a new object

22 Example program – assignment and copy constructor class alpha { private: int data; public: alpha() { } alpha(int d) { data = d; } alpha(alpha& a) { data = a.data; cout << “\nCopy constructor invoked”; } void display() { cout << data; } void operator = (alpha& a) { data = a.data; cout << “\nAssignment operator invoked”; } }; a2 = a1; alpha a3(a1); Program Output Assignment operator invoked Copy constructor invoked

23