Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.

Similar presentations


Presentation on theme: "Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used."— Presentation transcript:

1 Constructors And Destructors

2 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used for Initialization -- Same Name as the Class Name Does not Return a Value Does not Return a Value Cannot be Virtual and Static Cannot be Virtual and Static Implicitly Invoked when Objects are Created or Copied Implicitly Invoked when Objects are Created or Copied Can have Arguments Can have Arguments Cannot be Explicitly called Cannot be Explicitly called Multiple Constructors are Allowed Multiple Constructors are Allowed Default Constructor -- No Arguments -- Explicit or Implicit Default Constructor -- No Arguments -- Explicit or Implicit Copy Constructor -- Explicit or Implicit Copy Constructor -- Explicit or Implicit

3 3 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; }

4 4 A constructor is called automatically whenever a new instance of a class is created. 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. 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). If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body).

5 5 Cont. void main() { rectangle rc(3.0, 2.0); rectangle rc(3.0, 2.0); rc.posn(100, 100); rc.posn(100, 100); rc.draw(); rc.draw(); rc.move(50, 50); rc.move(50, 50); rc.draw(); rc.draw();} Note: attempting to initialize a data member of a class explicitly in the class definition is a syntax error. Note: attempting to initialize a data member of a class explicitly in the class definition is a syntax error.

6 6 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 };

7 7 Cont. rectangle::rectangle(){ height = 10; height = 10; width = 10; width = 10; xpos = 0; xpos = 0; ypos = 0; ypos = 0;} void main() { rectangle rc1(3.0, 2.0); rectangle rc1(3.0, 2.0); rectangle rc2(); rectangle rc2(); rc1.draw(); rc1.draw(); rc2.draw(); rc2.draw();}

8 8 What is a copy constructor? It is a member function which initializes an object using another object of the same class. 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: A copy constructor has the following general function prototype: class_name (const class_name&);

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

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

11 11 Destructor Member Function used for Clean-up -- Same Name as the Class Name with a ~ One Single Destructor Special Invoked When an Object Goes Out of Scope Can be Explicitly Called -- Unusual Cannot Accept Parameters and Does Not Return A Value Cannot be Declared static, const or volatile Can be virtual

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

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

14 14 // date.h /* A Simple Date Class With a Destructor */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); ~date(); //Destructor }; // date.h /* A Simple Date Class With a Destructor */ #define size 50 class date{ private: int day, month, year; char *string_date; public: date(char *ip_date); ~date(); //Destructor }; // date.cpp #include “date.h” //Constructor date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} //Destructor date::~date() {delete[] (string_date);} // date.cpp #include “date.h” //Constructor date::date(char *ip_date){ day = 0; month = 0; year = 0; string_date = new char [size]; strcpy(string_date, ip_date);} //Destructor date::~date() {delete[] (string_date);} // client.cpp #include “date.h” main(){ // "today" is an object of "date". Constructor is invoked. date today(“ March 10, 2012 "); } // client.cpp #include “date.h” main(){ // "today" is an object of "date". Constructor is invoked. date today(“ March 10, 2012 "); }

15 15


Download ppt "Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used."

Similar presentations


Ads by Google