120 likes | 206 Views
Object Oriented Programming. Array of Objects + Friend. Static Data Members and Member Function. A data member function can be declared as static in a class’s declaration. Static data members of a class are shared by all objects of the class .
E N D
Object Oriented Programming Array of Objects + Friend
Static Data Members and Member Function • A data member function can be declared as static in a class’s declaration. • Static data members of a class are shared by all objects of the class. • If an alteration occur in one object, then it will directly alter other objects which under the same class. • The static data members of an uninitialized object are initialized to zero. • Static member functions can only have access to static data members, cannot access non-static data members and cannot call non-static member functions.
class Point { private: static double x, y, z; public: static void Member ( ); }; Void Point :: Member () { cout << x << y << z << endl; } The x, y, and z data members of class Point are declared as static • Static data members can eliminate the use of global variables.
const member functions • Allows a member function access to a class’s data members, but prevents the member function from altering the value of a data member. class Point { private: double x; public: double X() const; }; inline double Point::X() const { x = x + 1; return x; } void main() { Point p; cout << p.X () << endl; } const member function Error: cannot alter the data member
Const and Mutable Data Members • The data members of a class can be declared as const. • A data member declared as const must be initialized in the constructor declarator and cannot be initialized via an assignment statement in the body of the constructor. • The mutable keyword is used to allow a member of an object to cast away const. • Const class data member or const object cannot be altered. • However, if a data member is declared mutable then the data member of a const object is not const and can therefore be modified.
class X { public: int data; const int c_data; mutable int m_data; mutable const int mc_data; mutable static int mc_data; X ():data (0), c_data (0), m_data (0) {} }; void main() { X x; const X cx; x.data = 1; x.c_data = 2; cx.data = 3; cx.c_data =4; cx.m_data = 5; } Const data member Mutable data member Error: Mutable const Error: Mutable static Non-const object Const object Ok: non-const object, non-const member Error: non-const object, const member Error: const object, non-const member Error: const object, const member Ok: mutable member of const object can modified cx.m_data = 5.
Arrays of Objects class Point { private: double x; public: Point (); //constructor Point (double x_arg); double X(); }; inline Point::Point():x(0.0){} inline Point::Point(double x_arg):x(x_arg){} double Point::X() {return x;} void main( ) { Point tri_vertices[3]; tri_vertices[0] = Point(1.0); tri_vertices[1] = Point(2.0); tri_vertices[2] = Point(3.0); for (int i=0; i<3; i++) { cout << "vertices " << i << " " << tri_vertices[i].X() << endl;} } Define array Initialize array objects
Introduction to friend • Both classes and functions can be made friends. • A friend has access to a class’s private data members, although it is not a member of the class. • A function which is made a friend of a class is allowed direct access to a class’s private data members. • It is irrelevant whether declaration is placed in private or public section. • General syntax: friend ReturnType FunctionName (parameter list);
friend Functions class Circle{ private: double radius; public: Circle( ): radius(0.0){ } Circle(double r ): radius(r){ } friend double Area (const Circle &c); }; double Area(const Circle &c){ return 3.142*c.radius*c.radius; } void main( ){ Circle circle(5.0); Double area = Area(circle); Cout << area<<endl; } friend. No (::) scope resolution operator. Area( ) not a member of class Circle. Member access operator (.) is not required for function call Output: 78.55
Why Use friend Functions? • Friend functions can be useful for overloading operators and for giving a more general application to overloaded operators. • Friends are important when there is aneed to provide a function with access to members of more than one class • Disadvantages: • Friend contradict completely with C++ philosophy of private data member. • Friend function should be used with caution and reserved for special cases.
class B; class C; class A{ private: int a; public: A(int a_arg) {a = a_arg;} friend void Display(A aDisp, B bDisp, C cDisp ); }; class B{ private: int b; public: B(int b_arg) {b = b_arg;} friend void Display(A aDisp, B bDisp, C cDisp ); }; class C{ private: int c; public: C(int c_arg) {c = c_arg;} friend void Display(A aDisp, B bDisp, C cDisp ); }; Within the declaration of class A the friend function Display( ) references classes B and C before they are declared. Continues…..
void Display (A aDisp, B bDisp, C cDisp ){ cout << aDisp.a << bDisp.b << cDisp.c << endl; } Void main( ){ A a_main(1); B b_main(2); C c_main(3); Display (a_main, b_main, c_main); } A single function is called to access the data members of class A, B and C. Output: 1 2 3