More on constructors destructors l.jpg
Advertisement
This presentation is the property of its rightful owner.
1 / 9

More on constructors, destructors PowerPoint PPT Presentation

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&); }; More on constructors, destructors Constructor

Download Presentation

More on constructors, destructors

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


More on constructors destructors l.jpg

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&);};


More on constructors destructors2 l.jpg

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();


More on constructors destructors3 l.jpg

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.


More on constructors destructors4 l.jpg

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.


More on constructors destructors5 l.jpg

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.


More on constructors destructors6 l.jpg

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.


Static data members l.jpg

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


Static data members8 l.jpg

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;

}


Common bug l.jpg

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!


  • Login