1 / 14

Constructors And Destructors In C | Constructors In C | C Tutorial

This presentation on Constructors and Destructors in C will cover what are constructor in C and how a constructor is different from the function. You will also learn about the types of constructors and understand destructors in C . You will get an idea about constructor overloading. Finally, we will also do some hands-on examples in this C tutorial for beginners slides.<br>

Simplilearn
Download Presentation

Constructors And Destructors In C | Constructors In C | C Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What’s in it for you ? What is a constructor in C++ ? Declaration of constructor How constructor is different from the function? Types of constructor Default constructor Parameterized constructor Copy constructor Constructor overloading Destructor in C++

  2. What is a constructor in C++? Constructors are special functions of a class that is used to initialize objects of a class. They are invoked automatically whenever an object is created.

  3. Click here to watch the video

  4. V Declaration of constructor {…} Constructors are the member function of a class, and it has the same name as that of the class. It can be defined inside the class and outside the class as well using the scope resolution operator :: Syntax: Class Cons { Public: int n; Cons() { } };

  5. How constructor is different from the function? • There are some differences between Constructor and function: • Constructor doesn’t have a return type, unlike function. • The name of constructor must be of the same name as that of the class. • Whenever an object is created then a constructor is invoked. • Function can be virtual function but it is not so incase of constructor.

  6. Types of constructors • Types of constructor • Default constructor • Parameterized constructor • Copy constructor

  7. Default constructor A default constructor is a constructor that doesn’t have any arguments inside it. It can either be declared explicitly or can be generated automatically. For a class named Cons, the default constructor would be: Cons() { }

  8. Parameterized constructor A parameterized constructor is similar to that of the default constructor; the only difference is we can pass arguments to the parameterized constructor For a class named Cons, the parameterized constructor would be: Cons(int n1, int n2) { }

  9. Copy constructor A copy constructor is a type of constructor which initializes an object using another object of the class. In this constructor properties of one object is copied to another object. For a class named Cons, the copy constructor would be: Cons (const Cons &obj) { }

  10. y Constructor overloading Like functions, constructors can also be overloaded. The overloaded constructor varies in the number of arguments, which helps determine which constructor should be called. Cons( int x, int y ) { // operations } Cons( int x, int y, int z ) { // operations }

  11. y Destructor in C++ A destructor is also a special function which deletes or destructs the object as soon as the object goes out of scope. Its name is the same as that of the class name with a tilde sign (~). Syntax: ~Cons() { }

More Related