Object Oriented Programming Techniques Constructor • Special method that is invoked automatically at the time of object creation • Initialize the data members for an object of a class automatically, when an object of the same class is created • Same name as the class or structure • Automatically called by the compiler • Invoked at the time of object creation • Constructor do not return value • Do not have a return type • Constructors may not be static. Syntax <class-name>(list-of-parameters) { //constructor definition } Example code class Table { Public: Table() {} }; Example Code Constructor Within The Class Output Example Code Constructor Outside The Class Scope Resolution Operator (::) Example Code Constructor Outside The Class Output Types of Constructor Default Constructor • No arguments are passed • The compiler will give an implicit default constructor if the programmer does not explicitly provide one • Each time an object is created, a constructor is invoked Example Default Constructor Output Example Default Constructor Output Example Explicit Default Constructor Output Parameterized Constructors • Pass arguments to constructors • Arguments help initialize an object when it is created Example Parameterized Constructors Output Example Parameterized Constructors Output Example Parameterized Constructors Output Copy Constructor • Creates an object by initializing it with a previously created object of the same class Example Copy Constructor Example Copy Constructor Output Example Copy Constructor Example Copy Constructor • Notice that the parameter of this constructor • It has the address of an object of the Wall class. Dry Run for Output Output Destructor • Special member function as a constructor • Destructor destroys the class objects created by the constructor • Same name as their class name preceded by a tilde (~) symbol • It is not possible to define more than one destructor • Destructor can-not be overloaded. • Neither requires any argument nor returns any value. • Automatically called when the object goes out of scope • Release memory space occupied by the objects created by the constructor Syntax ~ <class-name>() { } Example Output Example Output Task • Write a C++ program to create a class named Table. The class has constructor and destructor along with another member function named Multiply. Multiply function will print the table up till 10 for the value through main taken from parameterized constructor.