Download presentation
Presentation is loading. Please wait.
1
More on Classes Programming in C++ Fall 2008
Dr. David A. Gaitros
2
Class Access Functions
One of the important aspects of OOP is information hiding and protection. We do not let users of the classes have direct access to either look at or change private data items. For this reason, most classes that wish to give access to users have a series of “gets” and “sets” for each private data type that is exposed.
3
Class Access Functions
A “get” function would enable the client to read data May also provide formatting or restrictions. Can return the data in a format other than raw data which is used by the class. Can limit the view of the data. Example ( Only give the last four of the SSN). Usually the word “get” is placed in front of the name of the field. (See Time class example later).
4
Class Access Functions
A “set” function would allow a client to set private data items. The set function should ensure the value is set properly to maintain the integrity of the private data. Should inform the client when an attempt is made to assign an invalid number. “set” functions almost always return a status. Allows the client the opportunity to correct situation or pursue some other course of action. Can perform other operations such as translation of data into proper values. (Example: Standard to metric).
5
Class Access Functions
//someclass.h #ifndef TIME_H #define TIME_H class Time{ public: Time(); Time(int h, int m, int s); ~Time(); int setHour(int h); int setMinute(int h); int setSecond(int h); int getHour(); int getMinute(); int getSecond(); void printMilitary(); void printStandard(); private: int hour; int minute; int second; }; #endif
6
Friends and Members Motivation: Suppose we want to write a function that needs to have direct access to private data in another class. Under normal circumstances, this is prohibited. There are numerous instances where a programmer will need access to private data written by another person developing a different class/package.
7
Friends and Members The Solution: A “friend” function
Declared outside the class’s scope, has the right to access private members of the class. A function or an entire class may be declared to be a friend of another class. Purpose – To allow trusted functions to have access to internal data and member functions of a class.
8
Friends and Members Used to enhance performance of a class by avoid unnecessary overhead. To declare a function as a friend of a class, proceed the function prototype in the class declaration with the keyword friend. Friendship is granted, not taken or asked for. By convention, friend declaration appears first in the class declaration.
9
Friends and Members #include <iostream.h> class Count{ friend void setX(Count &, int); public: Count() {x=0;} void print() const const {cout << x<< endl;} private: int x; }; void setX(Count &c, int val) { c.x = val; } // Note: Works as if place in the class itself.
10
Friends and Members The keyword “friend” allows a class to grant full access to an outside entity. The “friend” can access all class’ members including the private section. An outside entity can be a function or even another class. To grant “friend” status, the declaration of a “friend” is made inside the class definition block. A “friend” is neither public nor private. As we have seen, the “friend” declaration goes right after the class definition.
11
Friends and Members // Non Friend Example Fraction F1(1,2); // 1/2 Fraction F2(2,4); // 2/4 If( Equals(F1,F2)) cout << “Fractions are equal”; Bool Equals (Fraction x, Fraction y) { if (x.GetNumerator() * y.GetDemoninator() == x.GetDemoninator() * Y.GetNumerator()) return true; else return false; }
12
Friends and Members // FRAC.H class Fraction { friend bool Equals(Fraction x, Fraction y); friend Fraction Add(Fraction x, Fraction y); public: Fraction(); Fraction(int n, int d=1); void Input(); void Show(); GetNumerator(); int GetDenominator(); bool SetValue(int n, int d); double Evaluate(); private: int numerator; int denominator; }; // Note Equals and Add are friends. The actual code // sits in the FRAC.cpp file.
13
Friends and Members cout << "\nf1 = "; f1.Show(); cout << "\nf2 = "; f2.Show(); cout << "\n\n"; if (Equals(f1, f2)) cout << "f1 and f2 are equal\n"; else cout << "f1 and f2 are NOT equal\n"; f3 = Add(f1, f2); cout << "f1 + f2 = "; f3.Show(); cout << '\n'; // NOTE: The Dot (.) operator is not // needed with the friend functions.
14
Friends and Members Friend functions are useful when you have to write a member function that works with more than one object of a class. Remember that in order to use a member function of a class it must be in the form: Object.memberfunction(parameters) Friends functions do not have this restriction for the reason because we may wish to work with a different object.
15
Equal as Member Function
Bool Fraction:equals(Franction f) { if (numerator * f.denominator == f.numerator * denominator) return true; else return false; } // Here is what the call would look // Like if (F1.Equals(F2)) cout << “They are equal”<< endl; else cout << “They are not “<< endl;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.