More on constructors, destructors - PowerPoint PPT Presentation

benjamin
more on constructors destructors l.
Skip this Video
Loading SlideShow in 5 Seconds..
More on constructors, destructors PowerPoint Presentation
Download Presentation
More on constructors, destructors

play fullscreen
1 / 9
Download Presentation
More on constructors, destructors
849 Views
Download Presentation

More on constructors, destructors

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. More on constructors, destructors • All the examples that follow will be based on this simple class definition:class Point { private: int x; int y; public: Point(); ~Point(); Point(const Point&);};

  2. More on constructors, destructors • Constructor • A constructor is called automatically when a class object is created. • Examples: • In the following cases, the default constructor is called: • Point center; • Point *pcenter = new Point; • Point *pcenter = new Point();

  3. More on constructors, destructors • Destructor • The destructor is called automatically when a class object goes out of scope or is deallocated. • Examples: • In the following cases, the default constructor is called: • { Point center; // destructor is called here} • Point *pcenter = new Point;delete pcenter; // destructor is called here.

  4. More on constructors, destructors • Destructor • It is possible for an object to kill itself, though this is very dangerous. • See http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15 for additional information. • One way to ensure that the object will not be statically allocated is by making the destructor private. We'll discuss this after class.

  5. More on constructors, destructors • Copy Constructor • The copy constructor is called when • An object is created explicitly as a copy of an existing one • Example 1: • Point obj1;Point obj2(obj1); • Example 2: • Point *obj1 = new Point;Point obj2(*obj1);Point *obj3 = new Point(*obj1); • An object is passed by value. • An object is returned by value.

  6. More on constructors, destructors • Initialization lists • The constructor may have a special initialization list to initializes data members, instead of using assignment statements in its body. • Example: • Point::Point() : x(0), y(0) { // empty} • This initializes x and y to 0. • This is done before the body of the constructor is executed. • The initialization list is generally more efficient than the use of assignments. • The only way to initialize const data members is through an initialization list.

  7. static data members • In some applications, we want all objects to be able to share a data member. • For example, we may want to keep track of how many objects have been created. We want to have a counter data member that gets incremented whenever a new object is created, and whose values can be examined. • This can be done by using static data members: • Declare the member as static. • Initialize the data member outside the class. • Only one copy of this data member exists

  8. static data members • Example: // countobj.cpp #include"countobj.h" int CountObj::counter = 0; CountObj::CountObj() { ++counter; } CountObj::~CountObj() { --counter; } int CountObj::getCount() { return counter; } // countobj.h #ifndef _COUNT #define _COUNT class CountObj { private: static int counter; public: CountObj(); ~CountObj(); int getCount(); }; #endif #include"count.h" #include<iostream> int main () { CountObj obj1, obj2; std::cout << obj2.getCount() << ": should be 2"; return 0; }

  9. Common bug • Never allow a public method to return a reference to a private data member, as this will allow the caller to modify the data. • Example: class A { int a; public: A() : a(1) {}; int& func() {return a;} void print() { cout << a << endl; } }; int main() { A obj; (obj.func())++; obj.print(); return 0; } This will print out 11!