Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors & Destructors, Proxy Classes, Friend Function and example of static member.

Similar presentations


Presentation on theme: "Constructors & Destructors, Proxy Classes, Friend Function and example of static member."— Presentation transcript:

1 Constructors & Destructors, Proxy Classes, Friend Function and example of static member.

2 A proxy class A proxy class is a stand-in for another class. Let's suppose you have a class that has a method that takes 60 seconds to complete. That means everytime you call that method, your program waits. But let's also assume you rarely call that method. Let's further assume this method is named Load() and the class is MyClass

3 The proxy class would look like: [code=cpp] class MyClassProxy { MyClass* theObject; public: MyClassProxy(); : theObject(0) {} MyClass* operator->(); MyClass& operator*(); }; So when you create a MyClassProxy object, the MyClass* inside is set to zero. Now you use MyClassProxy objects instead of MyClass objects. If someone needs the MyClass object, they use the operator-> overload of MyClassProxy. This function just returns the MyClass* if the MyClass object exists otherwise is creates it and calls Load().

4 So not until you use the proxy object with the -> operator do you see the 60 second delay.

5 Friend Functions It is possible to grant a nonmember function access to the private members of a class by using a friend. A friend function has access to all private and protected members of the class for which it is a friend. To declare a friend function, include its prototype within the class, preceding it with the keyword friend.

6 #include using namespace std; class myclass { int a, b; public: friend int sum(myclass x); void set_ab(int i, int j); }; void myclass::set_ab(int i, int j) { a = i; b = j; } // Note: sum() is not a member function of any class. int sum(myclass x) { /* Because sum() is a friend of myclass, it can directly access a and b. */ return x.a + x.b; } int main() { myclass n; n.set_ab(3, 4); cout << sum(n); return 0; }

7 Constructors & Destructors

8 What is a constructor? It is a member function which initializes a class. A constructor has: (i) the same name as the class itself (ii) no return type

9 class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; rectangle::rectangle(float h, float w) { height = h; width = w; xpos = 0; ypos = 0; }

10 Comments on constructors A constructor is called automatically whenever a new instance of a class is created. You must supply the arguments to the constructor when a new instance is created. If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body).

11 Overloading constructors You can have more than one constructor in a class, as long as each has a different list of arguments. class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(); // another constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

12 Overloading constructors (cont.) rectangle::rectangle() { height = 10; width = 10; xpos = 0; ypos = 0; } void main() { rectangle rc1(3.0, 2.0); rectangle rc2(); rc1.draw(); rc2.draw(); }

13 Composition: objects as members of classes A class may have objects of other classes as members. class properties { private: int color; int line; public: properties(int, int); // constructor }; properties::properties(int c, int l) { color = c; line = l; }

14 class rectangle { private: float height; float width; int xpos; int ypos; properties pr; // another object public: rectangle(float, float, int, int ); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; Composition: objects as members of classes (cont.)

15 rectangle::rectangle(float h, float w, int c, int l):pr(c, l) { height = h; width = w; xpos = 0; ypos = 0; }; void main() { rectangle rc(3.0, 2.0, 1, 3); C++ statements; }

16 What is a destructor? It is a member function which deletes an object. A destructor function is called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing temporary variables ends (4) a delete operator is called A destructor has: (i) the same name as the class but is preceded by a tilde (~) (ii) no arguments and return no values

17 class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor }; string::string(char *c) { size = strlen(c); s = new char[size+1]; strcpy(s,c); } string::~string() { delete []s; }

18 Comments on destructors If you do not specify a destructor, the compiler generates a default destructor for you. When a class contains a pointer to memory you allocate, it is your responsibility to release the memory before the class instance is destroyed.

19 What is a copy constructor? It is a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype: class_name (const class_name&);

20 class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(const rectangle&); // copy constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

21 rectangle::rectangle(const rectangle& old_rc) { height = old_rc.height; width = old_rc.width; xpos = old_rc.xpos; ypos = old_rc.ypos; } void main() { rectangle rc1(3.0, 2.0); // use constructor rectangle rc2(rc1); // use copy constructor rectangle rc3 = rc1; // alternative syntax for // copy constructor C++ statements; }

22 Defining copy constructors is very important In the absence of a copy constructor, the C++ compiler builds a default copy constructor for each class which is doing a memberwise copy between objects. Default copy constructors work fine unless the class contains pointer data members... why???

23 #include class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor void print(); void copy(char *); }; void string::print() { cout << s << endl; }

24 void string::copy(char *c) { strcpy(s, c); } void main() { string str1("George"); string str2 = str1; // default copy constructor str1.print(); // what is printed ? str2.print(); str2.copy("Mary"); str1.print(); // what is printed now ? str2.print(); }

25 Defining a copy constructor for the above example: class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor string(const string&); // copy constructor void print(); void copy(char *); };

26 string::string(const string& old_str) { size = old_str.size; s = new char[size+1]; strcpy(s,old_str.s); } void main() { string str1("George"); string str2 = str1; str1.print(); // what is printed ? str2.print(); str2.copy("Mary"); str1.print(); // what is printed now ? str2.print(); } Note: same results can be obtained by overloading the assignment operator.

27 Static Data Members #include using namespace std; class shared { static int a; int b; public: void set(int i, int j) {a=i; b=j;} void show(); } ; int shared::a; // define a void shared::show() { cout << "This is static a: " << a; cout << "\nThis is non-static b: " << b; cout << "\n"; } int main() { shared x, y; x.set(1, 1); // set a to 1 x.show(); y.set(2, 2); // change a to 2 y.show(); x.show(); /* Here, a has been changed for both x and y because a is shared by both objects. */ return 0; }

28 This program displays the following output when run. This is static a: 1 This is non-static b: 1 This is static a: 2 This is non-static b: 2 This is static a: 2 This is non-static b: 1


Download ppt "Constructors & Destructors, Proxy Classes, Friend Function and example of static member."

Similar presentations


Ads by Google