21
CHAPTER 2 – PART 2 Constructors & Destructors

CHAPTER 2 – PART 2 Constructors & Destructors

Embed Size (px)

DESCRIPTION

F3031 – OBJECT ORIENTED PROGRAMMING. CHAPTER 2 – PART 2 Constructors & Destructors. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: CHAPTER 2 – PART 2 Constructors & Destructors

CHAPTER 2 – PART 2

Constructors & Destructors

Page 2: CHAPTER 2 – PART 2 Constructors & Destructors

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

• Constructor is a special function whose task is to initialize the objects of its class.

• Constructor is invoked whenever an object of its associated class is created.

• It is called constructor because its construct the values of attributes of the class.

Page 3: CHAPTER 2 – PART 2 Constructors & Destructors

The constructor functions have some special characteristics. These are :

They should be declared in the public section.They are invoked automatically when the objects are created.They do not have return types, not even void and therefore, and they cannot return values.They cannot be inherited, though a derived class can call the base class constructorThey can have default arguments.

Page 4: CHAPTER 2 – PART 2 Constructors & Destructors

Example 1class bird{private :

char *color;char *name;public:

bird;)(display;)(

;}

Constructor declaration

bird::bird(){

color=“black”;

name=“jackjaw”;

}

void bird::display(){

cout<<“The bird color is”<<color<<endl;

cout<<“The bird name is”<<name<<endl;

}

Constructor defined

void main(){

bird bird1;

bird1.display();

}

*Remember, when a constructor is declared for a class, initialization of the class objects becomes mandatory.

Page 5: CHAPTER 2 – PART 2 Constructors & Destructors

Types of Constructor

Page 6: CHAPTER 2 – PART 2 Constructors & Destructors

The default constructor • if no constructor was declared, then compiler

generates the default constructor (constructor with no arguments) of an empty body, for T class:

T::T(){}

• if we declare any constructor (even constructor with args) then compiler does not generate the default constructor.

Page 7: CHAPTER 2 – PART 2 Constructors & Destructors

Parameterized Constructorsi. It may necessary to initialized the various attributes of different objects

with different values when they are created. ii. The constructor that can take arguments are called parameterized

constructors.iii. When a constructor has been parameterized, the object declaration

statement such as :bird bird1;may not work. We must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways :

By calling constructor explicitly

example :

bird bird1 = bird(“black”,”Jackjaw”); By calling constructor implicitly

bird bird1(“”black,”Jackjaw”);Is used very often as it is

shorter, looks better and is easy to implement

Page 8: CHAPTER 2 – PART 2 Constructors & Destructors

Example 2class bird{private :

char *color;char *name;public:

bird(char *c, char *n);display;)(

;}

Constructor declaration

bird::bird(char *a, char *n){

color=a;

name=n;

}

void bird::display(){

cout<<“The bird color is”<<color<<endl;

cout<<“The bird name is”<<name<<endl;

}

Constructor defined

void main(){

bird bird1(“pink”,”Peacock”);

bird1.display();

}

Page 9: CHAPTER 2 – PART 2 Constructors & Destructors

Copy Constructori. A copy constructor is used to declare and initialize an object from another

object.

ii. It is a member function which initializes an object using another object of the same class.

iii. Syntax to create a copy constructor refer example number 3.

iv. Another syntax to copy constructor is

bird bird1 = bird2;

v. But the syntax as below :

bird1=bird2

will not invoke the copy constructor. However if bird1 and bird2 are objects, this statement is legal and simply assigns the values of bird1 to bird2, member-by-member.

Page 10: CHAPTER 2 – PART 2 Constructors & Destructors

Example 3class bird{

int num_legs;int num_wings;

public :bird(int l, int w);bird(bird &num);void display();

}; bird::bird(int le, int wi){

num_legs=le;num_wings=wi;

}bird::bird(bird &br){num_legs=br.num_legs;num_wings=br.num_wings;}void display(){cout<<“The number of legs are”<<num_legs<<endl;cout<<“The number of wings are”<<num_wings<<endl;}void main(){

bird bird1(2, 2); //object bird1 is created and initializedbird bird2(bird1);//object bird 2 is created and the values of object bird1 are copied into object bird 2bird1.display();bird2.display();

}

Copy constructorParameterized constructor

Page 11: CHAPTER 2 – PART 2 Constructors & Destructors

Example 4class student{

char *hair_color;char *skin_color;student();student(char hc,char sc);student(student &std);void display();

}; student::student(){}student::student(char hcl, char sck){

hair_color=hcl;skin_color=sck;

}student::student(student &h){

hair_color=h.hair_color;skin_color=h.skin_color;

}

student::display(){cout<<“The hair color

is”<<hair_color<<endl;cout<<“The skin color

is”<<skin_color<<endl;}void main(){student student0;student student1(“Brown”,”White”);student student2(student1);student student3 = student0;student0.display();student1.display();student2.display();student3.display();}

Page 12: CHAPTER 2 – PART 2 Constructors & Destructors

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

Example 5

Page 13: CHAPTER 2 – PART 2 Constructors & Destructors

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

Page 14: CHAPTER 2 – PART 2 Constructors & Destructors

Comments on constructors (cont.) void main(){ rectangle rc(3.0, 2.0);  rc.posn(100, 100); rc.draw(); rc.move(50, 50); rc.draw();} 

• Warning: attempting to initialize a data member of a class explicitly in the class definition is a syntax error.

Page 15: CHAPTER 2 – PART 2 Constructors & Destructors

Overloading constructors • You can have more than one constructor in a class, as

long as each has a different list of arguments.• This is called overloaded constructors.

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

Page 16: CHAPTER 2 – PART 2 Constructors & Destructors

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

Page 17: CHAPTER 2 – PART 2 Constructors & Destructors

Example 6class bird{private :

char *color;char *name;

public :bird();bird(char *c, char *n);display();

};

bird::bird(){color=“Purple”;name=“Peacock”;} bird::bird(char *a, char *n){color=a;name=n;}

void bird::display(){

cout<<“The bird color is”<<color<<endl;

cout<<“The bird name is”<<name<<endl;

}

void main(){

bird bird1;

bird bird2(“peach”,”peguin”);

bird1.display();

bird2.display();

}

Page 18: CHAPTER 2 – PART 2 Constructors & Destructors

What is a destructor?• It is a member function which deletes an object.• The destructor is a function that is called automatically when an object

is destroyed or goes out of scope.• The destructor reclaims the memory that is allocated to the 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

Page 19: CHAPTER 2 – PART 2 Constructors & Destructors

i. A destructor has the same name as the class, but it has a tilde (`~’) in front of it

ii. A destructor does not return a value and therefore it does not have a type specifier.

iii.Unlike a constructor, you cannot pass any argument to a destructor.

iv.You cannot have more than one destructor for each class.

v. A destructor is called automatic when an object goes out of scope. That means, you don’t have to invoke it like other functions.

Characteristics of destructor

Page 20: CHAPTER 2 – PART 2 Constructors & Destructors

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

Example 7

Page 21: CHAPTER 2 – PART 2 Constructors & Destructors

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.