Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Pathway Constructor Constructor Examples Overloading Constructor (with arguments) Function Overloading vs. Function overriding Copy Constructor Destructor Constructor vs. Destructor
Constructor Constructor is special function that is called automatically when you create an object of the class. Constructor has the same name as class name It does not have any return type. There is no way that we call constructor manually Constructor are normally use to initialize the properties (attributes) of the class
Count Example Return 0; }
Another Example Class Human { Private: string name; int age; public: Human() { name= “null”; age=0; } void getdata() { cout<<“enter name”<<endl; getline(cin, name); cout<<“enter age”<<endl; cin>>age; } void showdata() { cout<<“name is”<<name<<“age is ”<<age<<endl; } };
Int main() { Human h1, h2; h1.getdata(); h1.showdata(); h2.getdata(); h2.showdata(); return 0; }
Overloading Class Constructor It’s convenient to be able to give variables of type Distance a value when they are first created. That is, we would like to use definitions like Distance width(5, 6.25); which defines an object, width, and simultaneously initializes it to a value of 5 for feet and 6.25 for inches. To do this we write a constructor like this: Distance(int ft, float in) : feet(ft), inches(in) { }
Example Class Distance { private: int feet; float inches; public: Distance() { feet=0; inches= 0;} Distance(int ft, float in) { feet=ft; inches=in; } void getdata() { cout<<“enter feet”<<endl; cin>>feet; cout<<“enter inches”<<endl; cin>>inches; } void showdata() { cout<<“the distance is: ”<<feet, inches<<endl; } };
Int main() { Distance d1; Distance d2(11, 6.5); d1.getdata(); d1.showdata(); d2.showdata(); }
Example:
Function Overloading Function overloading is a feature that allows us to have same function more than once in a program. Overloaded functions have same name but their signature must be different. Here we have the same function sum declared four times with different signatures. Based on the parameters we pass, while calling function sum, decides which method is to be called. This happens during compilation, which is why it is also known as compile time polymorphism. – float sum(int, int); – float sum(float, float); – float sum(int, float); – float sum(float, int);
Function Overriding Function overriding is a feature of OOPs Programming that allows us to override a function of parent class in child class. Function overridingOOPs Programming Function Overriding is happens in the child class when child class overrides parent class function. In function overriding the signature of both the functions (overriding function and overridden function) should be same. overriding happens at run time which is why it is known as run time polymorphism.
#include using namespace std; //Parent class or super class or base class class A { public: void disp() { cout<<"Parent Class disp() function"<<endl; } }; Class B : public A { public: void disp() { // definition can be different } };
Copy Constructor Two types of constructors have already discussed: 1.Constructor with no arguments and data members are initialized with constant values. 2.Constructor with arguments where data members are initializes to values passed as arguments. There’s another type of constructor which is called Copy Constructor. Special type of constructor, where the values of objects are initialized with another object of the same type. (data members of one objects are initialized with the data members of same type of another objects). This constructor is called copy constructor. Copy Constructor is one argument constructor, where the argument is another object of same type whose values are going to be copied
Destructor We’ve seen that a special member function—the constructor—is called automatically when an object is first created. You might guess that another function is called automatically when an object is destroyed. This is indeed the case. Such a function is called a destructor. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde: class Foo { private: int data; public: Foo() : data(0) //constructor (same name as class) { } ~Foo() //destructor (same name with tilde) { } };
Destructor It is the same name as constructor but with tild sign (~) Destructor has no arguments Destructor has no return value The major purpose of destructor to de-allocate the memory which was allocated for the objects by the constructor