160 likes | 345 Views
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.
E N D
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. • 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)
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.
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; } }; ////////////////////////////////////////////////////////////////
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; }
Format of the constructor • Same name as the class • No return type • Initializer list count() { count = 0; } count() : count(0) { }
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
Constructors with Default Arguments //time.h class Time { private: int hour, minute, second; public: Time(int=0, int=0, int=0); //default ctor … … … };
Constructors with Default Arguments //time.cpp #include “time.h” Time::Time(int hr, int min, int sec) { hour=hr; minute=min; second=sec; } … … …
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
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);
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)
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)
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