1 / 14

Lecture 3: Constructors and Destructors

Lecture 3: Constructors and Destructors. Bushra Riaz. Initializing Objects with Constructors. A constructor can be used to automatically initialize an object of the class when the object is created.

analu
Télécharger la présentation

Lecture 3: Constructors and 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. Lecture 3: Constructors and Destructors Bushra Riaz

  2. Initializing Objects with Constructors • A constructor can be used to automatically initialize an object of the class when the object is created. • A constructor (abbreviated ctor) is a special member function that must be defined with the same name as the class • Cannot return a value, cannot specify a return type (not even void)

  3. Initializing Objects with Constructors • Constructor is called implicitly when an object is created. • If a class does not explicitly include a ctor, the compiler provides a default constructor; i.e. a ctor with no arguments Remember: An implicit default constructor is built into the program automatically by the compiler if you don’t provide one.

  4. Counter example • // counter.cpp // object represents a counter variable #include <iostream> using namespace std; /////////////////////////////////////////////////////////////// class Counter { private: unsigned int count; //count public: Counter() : count(0) //constructor { /*empty body*/ } void inc_count() //increment count { count++; } intget_count() //return count { return count; } }; ////////////////////////////////////////////////////////////////

  5. int main() { Counter c1, c2; //define and initialize cout << “\nc1=” << c1.get_count(); //display cout << “\nc2=” << c2.get_count(); c1.inc_count(); //increment c1 c2.inc_count(); //increment c2 c2.inc_count(); //increment c2 cout << “\nc1=” << c1.get_count(); //display again cout << “\nc2=” << c2.get_count(); cout << endl; return 0; }

  6. Format of the constructor • Same name as the class • No return type • Initializer list count() { count = 0; } count() : count(0) { }

  7. Overloaded constructors • Types of Constructors • Compiler Generated Constructor • Simple Constructor • Parameterized/overloaded constructors Default constructors • Default constructor • No arguments • May be implicit or explicit • Overloaded constructor • Has arguments

  8. Constructors with Default Arguments //time.h class Time { private: int hour, minute, second; public: Time(int=0, int=0, int=0); //default ctor … … … };

  9. Constructors with Default Arguments //time.cpp #include “time.h” Time::Time(int hr, int min, int sec) { hour=hr; minute=min; second=sec; } … … …

  10. Constructors with Default Arguments //TestTime.cpp … #include “time.h” void main() { Time t1; Time t2(2); Time t3(21, 34); Time t4(12, 25, 42); … } • Note: In C++ any function can use default arguments • A constructor that defaults all its arguments is also a default constructor...that is , a constructor that can be invoked with no arguments

  11. Copy Constructors • It is a member function which initializes an object using another object of the same class. • Invoked (implicitly) in following situations: ClassType obj1; ClassType obj2 = obj1; // Note: NOT assignment ClassTypeobj3(obj1); • Other situations that invoke copy ctor: • An object is passed by value to a function • An object is returned as the value of a function, as in return myGradeBook; • Takes one argument, a reference to class type: ClassName(const ClassName & T);

  12. Default Copy Constructor • In the absence of a programmer-provided copy constructor, the C++ compiler builds a default copy constructor for each class which performs a member-wise copy between objects. • Default copy constructors work fine unless the class contains pointer data members (more on this later)

  13. DESTRUCTORS class Foo{ private: int data; public: Foo(): data(0) {} ~Foo(){} }; • No return values • Same name as the class preceded by tilde (~) • No arguments • The most common use of destructors is to de-allocate memory that was allocated for the object (more later)

  14. Destructors • Destructor does house-keeping when object is destroyed • Triggered by scope change or delete • Required when user does dynamic allocation; default is generated • destructor has form: ClassName::~ClassName() {…} • No return type or arguments: no overloading • Should delete allocated memory • Called immediately before object is destroyed

More Related