Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Constructors and Destructors

Similar presentations


Presentation on theme: "Lecture Constructors and Destructors"— Presentation transcript:

1 Lecture 15-17 Constructors and Destructors

2 Contents Constructor Overloading Dynamic Initialization of objects
Dynamic Constructors Destructor

3 Constructor Overloading
Constructor can be overloaded in similar way as function overloading. . Overloaded constructors have the same name (name of the class) but different number (or types or both) of arguments. Depending upon the number and type of arguments passed, specific constructor is called.

4 Example of Constructor Overloading
#include<iostream> using namespace std; class example { private: int a,b; public: example(); //Default Constructor example(int, int); //Parameterized Constructor example(example &); //Copy Constructor void display(); }; example::example() cout<<“Constructor is called”; } example::example(int x, int y) { a=x; b=y; } example::example(example &p) a=p.a; b=p.b; void example::display() cout<<a<<endl<<b; int main() example e1; example e2(4, 5); example e3(e2); e3.display(); return 0;

5 Dynamic Initialization of Objects
Class objects can be initialized dynamically also i.e. the initial value of an object can be provided during run time. Advantage: Various initialization formats can be provided using overloaded constructors. Provides the flexibility of using different format of data at run time depending upon the situation.

6 Example #include<iostream> using namespace std; class factorial
/*Program to find factorial of a number using constructor*/ #include<iostream> using namespace std; class factorial { int n; public: factorial(int); void display(); }; factorial::factorial(int number) n=number; } void factorial::display() { int fact=1; if(n==0) cout<<"\n factorial=1"; else for(int i=1; i<=n; i++) fact=fact *i; } cout<<"\n factorial="<<fact; int main() int x; cout<<"\n enter the number to find its factorial"; cin>>x; factorial obj(x); //dynamic initialization of object obj.display(); return 0;

7 Dynamic Constructor The constructor can also be used to allocate memory while creating objects. This enables the system to allocate the right amount of memory for each object when the objects are not of the same size. Allocation of memory to objects at time of their construction is known as dynamic construction of objects. The memory is allocated with the help of new operator.

8 Example 1 OUTPUT: The value of object's pointer is:100
#include<iostream> using namespace std; class test { int *ptr; public: test(); test(int); void display(); }; test::test() ptr=new int; *ptr=100; } test::test(int t) *ptr=t; void test::display() { cout<<"The value of object's pointer is:"<<*ptr<<endl; } int main() test obj; test obj1(40); obj.display(); obj1.display(); return 0; OUTPUT: The value of object's pointer is:100 The value of object's pointer is:40

9 Example 2 OUTPUT: Welcome to C++ World void example::display() {
#include<iostream> using namespace std; class example { char *name; int length; public: example(); example(char *); void display(); }; example::example() length = 0; name = new char[length+1]; } example::example(char *e) length = strlen(e); name = new char[ length+1]; strcpy( name,e); void example::display() { cout<<name<<endl; } int main() char *a= “Welcome to"; example e1(a), e2(“C++"), e3(“World"); e1.display(); e2.display(); e3.display(); return 0; OUTPUT: Welcome to C++ World

10 Introduction to Destructors
A destructor is a special member function of class which is used to destroy the objects that have been created by a constructor. The destructor is called automatically by the compiler when the object goes out of scope. Like a constructor, the destructor is a member function whose name is the same as class name but is preceded by a tilde ~ sign. class A { public: ~A(); //Destructor declaration };

11 Introduction to Destructors (Cont..)
Destructor never takes any argument. Destructors does not return any value. Destructor cannot be overloaded. Whenever new is used to allocate memory in the constructors, delete should be used to free that memory. Note: Objects are destroyed in reverse order of their creation.

12 Example OUTPUT: a=10 Object is destroyed #include<iostream>
using namespace std; class ABC { int a; public: ABC(int); //Constructor void display(); ~ABC(); //Destructor }; ABC::ABC(int x) a=x; } void ABC::display() cout<<"a="<<a; ABC::~ABC() //Definition of Destructor { cout<<"\nObject is destroyed"; } int main() ABC obj1(10); obj1.display(); return 0; Destructor is automatically called by the compiler once the object goes out of scope. OUTPUT: a=10 Object is destroyed

13 Thank You


Download ppt "Lecture Constructors and Destructors"

Similar presentations


Ads by Google