Constructors & Destructors in C++ Programming | Object Oriented Programming Concepts

preview_player
Показать описание
Support Simple Snippets by Donations -
--------------------------------------------------------------------------------------------- 1. What is a Constructor?
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) is created. It is special member function of the class.
2. How constructors are different from a normal member function?
A constructor is different from normal functions in following ways:
Constructor has same name as the class itself
Constructors don’t have return type
A constructor is automatically called when an object is created.
If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).

Types of Constructors?
Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters.
Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.
Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same class.

What is Destructor?
Destructor is a member function which destructs or deletes an object.
When is destructor called?
The function ends.
The program ends.
A block containing local variables ends.
A delete operator is called.
How destructors are different from a normal member function?
Destructors have same name as the class preceded by a tilde (~).
Destructors don’t take any argument and don’t return anything(not even void).

Can there be more than one destructor in a class?
No, there can only one destructor in a class with classname preceded by ~, no parameters and no return type.
When do we need to write a user-defined destructor?
If we do not write our own destructor in class, compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak.

Simple Snippets Official Website -
Simple Snippets on Facebook-
Simple Snippets on Instagram-
Simple Snippets Google Plus Page-
Simple Snippets email ID-

UpSkill is an Ed-Tech Company / Coaching Centre for Information Technology / Computer Science oriented courses and offer coacing for various Degree courses like BSc.IT, BSc.CS, BCA, MSc.IT, MSc.CS, MCA etc.

Contact via email /call / FB /Whatsapp for more info

We also Provide Certification courses like -
Android Development
Web Development
Java Developer Course
.NET Developer Course
Рекомендации по теме
Комментарии
Автор

Hey Guys, if you want more such tech educational videos on this channel then please support me by subscribing to this channel & also share it with your friends as it helps me create more content just for you ✌

SimpleSnippets
Автор

I’ve learned so much from this dude than 5 weeks with my computer programming professor who’s never written a line of code in class 🤦‍♂️

nofx
Автор

The only English speaking channel I ever found.If you are a non-Hindi speaker it helps a lot.

raghuls
Автор

I don’t usually comment but this guy is a total legend. Makes it so easy to understand. Thanks dude!

daniyalkabir
Автор

Your videos on C++ is like NCERT for the PCM students. Brief but Impactful, Consize and Efficient.

shivamshrivastava
Автор

This guy teaches more worthwhile than a professional or a corporate one!! Big time savior, keep up the Good work.

bharatip
Автор

Now I've found a perfect tutor.
Great sir…

wurmhhm
Автор

simple and straight to the point. You have really open me. thank you.

muyambangomukwala
Автор

Bro, you're so much better in teaching object oriented programming than my professor, keep up the good work

saikatsaha
Автор

Wow! I hit a goldmine. You are great at constructors. I was struggling with the concept but you sir have helped me overcome the barrier. Thank you!

subarux
Автор

Man You are great! Beautiful coding. You clear my all concept of Constructor and Destructors.
May you Live long!

sohailirfan
Автор

Okay, this tutorial actually helped a lot and I learned more through this video than my professors lessons and the textbook.
It was especially helpful that you provided notes first and explanations then examples to follow up.
I will say though, it was hard to understand you throughout the video as your accent was pretty strong and you spoke pretty fast.
However, I was able to grasp what you were saying because you were able to provide such awesome material to view and follow.
Thank you!!

TF-ttuf
Автор

Idk why but you explained better than the others I have met so far ..thanks brother ❤️

rohitkumarjat
Автор

That is Amazing . I have been never seen this kind of tutorial on youtube . Thanks sir

zainbhatti
Автор

I usually don't comment, but I found this tutorial really helpful! Really explained well!

likhitanarra
Автор

This channel does deserve more subscribers.. Love from 🇧🇩🇧🇩

fabihahridita
Автор

Thank bro I have back paper for this syllabus. I really gain useful knowledge form this channel and I will surely recommended to my friend cycle.
I will try my best in coming Semester exam👍👍

Riomangang
Автор

Understood it for the first time properly. Thank you sir :)

mandarabattalahalli
Автор

1st time i saw a programming video without wasting a second, nice explanation, all points are valied

mallikrish
Автор

Thank you for this!
You're saving my life rn I have midterms next week lol

isLife-iflz