` `

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

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

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

move(50. 100). rc. .0). 50).void main() { rectangle rc(3.0. rc.posn(100.draw(). } ` Warning: attempting to initialize a data member of a class explicitly in the class definition is a syntax error. 2. rc. rc.draw().

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

0). } . } void main() { rectangle rc1(3.draw(). xpos = 0. width = 10. rectangle rc2(). ypos = 0. 2.rectangle::rectangle() { height = 10.draw().0. rc1. rc2.

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

int. float width.class rectangle { private: float height. // another object public: rectangle(float. int). int ). // draw member function void posn(int. int ypos. // move member function }. properties pr. int). // constructor void draw(). // position member function void move(int. float. int xpos. .

}. C++ statements.0. l) { height = h. } .0.rectangle::rectangle(float h. float w. 2. 3). void main() { rectangle rc(3. xpos = 0. width = w. ypos = 0. int c. int l):pr(c. 1.

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

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

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

` ` 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&). .

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

height.rectangle::rectangle(const rectangle& old_rc) { height = old_rc. rectangle rc3 = rc1. C++ statements.ypos. xpos = old_rc. 2. rectangle rc2(rc1). } // use constructor // use copy constructor // alternative syntax for // copy constructor . } void main() { rectangle rc1(3. width = old_rc.0.0).width. ypos = old_rc.xpos.

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

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

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

class string { private: char *s. }. // constructor ~string(). // destructor string(const string&). void copy(char *). int size. public: string(char *). // copy constructor void print(). .

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

Sign up to vote on this title
UsefulNot useful