L22 - friends and reuse

L22 - friends and reuse - Getting Friendly Up till now,...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Getting Friendly • Up till now, we’ve implemented class behaviors as member functions of the class – they could be public, and when they are public they can be invoked on objects of the class from code outside the class as examples, consider the ToString() method or the setName method of the class MemberInfo sum.ToString() invokes the ToString method on a numeric object and returns a string – it can be called from outside the class say in main m1 -> setName(“Susan”) sets the name attribute of the object pointed to by m1 • Data members of a class were usually private to that class and accessible only by methods of that class
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why Get Friendly There are situations in which we might want to access the private data members of a class from outside a member function – as an example, suppose that we are building an application that is designing a personal training program for members of the health club. – this application makes use of a class called ExerciseProgram that has member functions strengthTraining() aerobicActivities() – although this class is a separate from MemberInfo, it makes use of MemberInfo objects and member functions for ExerciseProgram need access to data in the MemberInfo objects – The ExerciseProgram member functions can always make use of the MemberInfo functions getName and getAge, but it would be more efficient if the data members could be accessed directly – This motivates friend functions of a class
Background image of page 2
Friend Functions • A friend function is a function that is not a member of a class but has access to the class’s private and protected members – this improves performance because it is not necessary to call an accessor method to access a private data member • Friend functions are not class members – they are normal functions external to a class that are given special access privileges • How to declare friend functions: – use the keyword friend preceding the function prototype in the class definition of the class that is granting friend status to the function
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Remember the definition of the class Complex (the header file) #pragma once using namespace System; using namespace std; class Complex { friend void SetComplex (Complex & c, double r, double i); public: Complex (); Complex (double r, double i); Complex operator+ (Complex &other); Complex & Complex::operator = (Complex &n); void display(); private: double re,im; }; declaration of the friend function
Background image of page 4
The class implementation remains unchanged: Complex::Complex (){ // default constructor re = 0.0; im = 0.0; } Complex::Complex(double r, double i){ // another constructor re = r; im = i; } Complex Complex::operator + (Complex &other){ // overloaded addition return Complex (re + other.re, im + other.im); } // overloading of = re = n.re; im = n.im; return *this; } void Complex::display(){ cout << "real part = " << re << " , imaginary part = " << im << endl; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

L22 - friends and reuse - Getting Friendly Up till now,...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online