Presentation is loading. Please wait.

Presentation is loading. Please wait.

group work #hifiTeam

Similar presentations


Presentation on theme: "group work #hifiTeam"— Presentation transcript:

1 Constructor What is the use of Constructor ? The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.

2 General Syntax of Constructor Constructor is a special member function that takes the same name as the class name. The syntax generally is as given below: { arguments}; The default constructor for a class X has the form X::X()

3 Cont…… The constructor is automatically called when an object is created. There are several forms in which a constructor can take its shape namely: Default Constructor Parameterized Constructors Copy constructor

4 Some important points about constructor: Automatically called when an object is created. We can define our own constructors A constructor takes the same name as the class name. We can’t define a constructor in the private section.

5 Destructor Destructors are special member functions. The destructor is a method called when the object is destroyed. Release dynamic allocated memory. Destructors are automatically named. Takes the same name of class name.

6 General Syntax of Destructor ~ class name();

7 Some important points about destructor: Take the same name as class name. Defined in the public. Destructors cannot be overloaded. No return type is specified.

8 COMMENT TO EACH LINE OF CODE Example of destructor using namespace std; class City { //within a class public: string cityName; //code to run on object deletion int population; City(String cityName, int population) { this->cityName = cityName; this->population = population; } ~City() { cout << "This city is being destroyed" << endl; }

9 Example of constructor using namespace std; class City { public: string cityName; //fields int population; City() { //constructor if no input parameters are provided this->cityName = "No city name provided"; this->population = 0; }

10 Cont……… City(String cityName) { //constructor if only a city name is provided this->cityName = cityName; //this keyword distinguishes the city Name's this->population = 0; } City(String cityName, int population) { //constructor if both parameters provided this->cityName = cityName; this->population = population; } }

11 Parameterized Constructor: A parameterized constructor is just one that has parameters specified in it. We can pass the arguments to constructor function when object are created. A constructor that can take arguments are called parameterized constructors.

12 Example class Creature { private: int year Of Birth; public: // … Creature(int year) { //Parameterized Constructor year Of Birth = year; } };

13 Copy Constructor: Copy Constructor is used to declare and initialize an object from another object. The process of initializing through a copy constructor is known as copy initialization.


Download ppt "group work #hifiTeam"

Similar presentations


Ads by Google