1 / 21

CHAPTER 2 – PART 2 Constructors & Destructors

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.

Download Presentation

CHAPTER 2 – PART 2 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. F3031 – OBJECT ORIENTED PROGRAMMING CHAPTER 2 – PART 2Constructors & Destructors

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

  3. 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 constructor They can have default arguments.

  4. Example 1 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 class bird{ private : char *color; char *name; public : bird(); display(); }; Constructor declaration void main(){ bird bird1; bird1.display(); } *Remember, when a constructor is declared for a class, initialization of the class objects becomes mandatory.

  5. Types of Constructor

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

  7. Parameterized Constructors It may necessary to initialized the various attributes of different objects with different values when they are created. The constructor that can take arguments are called parameterized constructors. 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

  8. Example 2 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 class bird{ private : char *color; char *name; public : bird(char *c, char *n); display(); }; Constructor declaration void main(){ bird bird1(“pink”,”Peacock”); bird1.display(); }

  9. Copy Constructor A copy constructor is used to declare and initialize an object from another object. It is a member function which initializes an object using another object of the same class. Syntax to create a copy constructor refer example number 3. 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.

  10. Example 3 class bird{ intnum_legs; intnum_wings; public : bird(int l, int w); bird(bird &num); void display(); }; bird::bird(int le, intwi){ 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 initialized bird bird2(bird1);//object bird 2 is created and the values of object bird1 are copied into object bird 2 bird1.display(); bird2.display(); } Parameterized constructor Copy constructor

  11. Example 4 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(); } class 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; }

  12. Example 5 class rectangle { private: float height; float width; intxpos; intypos; 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; }

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

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

  15. 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; intxpos; intypos; 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 };

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

  17. 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(); } Example 6 class 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; }

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

  19. Characteristics of destructor A destructor has the same name as the class, but it has a tilde (`~’) in front of it A destructor does not return a value and therefore it does not have a type specifier. Unlike a constructor, you cannot pass any argument to a destructor. You cannot have more than one destructor for each class. A destructor is called automatic when an object goes out of scope. That means, you don’t have to invoke it like other functions.

  20. Example 7 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; }

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

More Related