1 / 12

Object Oriented Programming

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 .

edan-fuller
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Array of Objects + Friend

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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.

  7. 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

  8. 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);

  9. 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

  10. 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.

  11. 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…..

  12. 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

More Related