How Constructors Work With Inheritance | C++ Tutorial

  Рет қаралды 18,631

Portfolio Courses

Portfolio Courses

2 жыл бұрын

An overview of how constructors work with inheritance in C++ including demonstrations of the relationship between base class and derived class constructors, as well as inheritance of constructors from the base class by the derived class. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 38
@ArM-wo6wd
@ArM-wo6wd Жыл бұрын
Easily the most concise explanation on this topic on YT. You answered every single one of my questions. Thanks a lot and I hope you get more recognition for your awesome work!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re very welcome, I’m happy to hear the video answered your questions! :-)
@nugget7579
@nugget7579 Жыл бұрын
Insanely underrated video lectures you are giving out for free, I am going to college soon and this has been a great help, will share with my friends(if i make any)👍
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm really glad to hear you're enjoying the video lectures, and thank you so much for sharing them with people, that's very much appreciated! :-D
@victorsuarez4333
@victorsuarez4333 10 ай бұрын
This was exactly what I was looking for, thank you so much!
@shovnikpaul3297
@shovnikpaul3297 Жыл бұрын
Very good explanation. Addressed all the nuances that I was struggling with. Thanks!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm so glad to hear that Shovnik, and you're welcome! :-)
@itsme-oc4vl
@itsme-oc4vl Жыл бұрын
Great explanation! I hope your channel grows!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you very much for the encouragement! :-)
@Sergio-sd8fh
@Sergio-sd8fh Жыл бұрын
Clear explanation, well done
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you very much Sergio! 🙂
@shanewalsch
@shanewalsch 7 ай бұрын
very good explanation, thanks!
@David-mk8kh
@David-mk8kh Жыл бұрын
Thankyou so munch man You made it clear
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome David, I'm glad that it made things clear! :-)
@behzad.tabari
@behzad.tabari Жыл бұрын
Hi, thanks it was a plain and good explanation, yet I have this question, is it possible to instantiate the derived and base class param constructor in run time, for example at the end you instantiated the base class at run time by giving it a string is there any notation which I could give the string and the ounces at the same time?
@and_then_I_whispered
@and_then_I_whispered 2 жыл бұрын
Thanks man!!!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@destroylevi
@destroylevi Жыл бұрын
thank you!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@arafathdhroba8897
@arafathdhroba8897 Жыл бұрын
thank you very much
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Arafath! :-)
@mowafkmha4505
@mowafkmha4505 Жыл бұрын
dude you are a legend I have been stuck with that error with the parametrized constructor and no default one but actually, the using base_class::base_class Methode didn't work with me I don't know why
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm really glad to hear this video helped you out Mowafk! :-) I'm not sure why that wouldn't work for you, if you post your code here in a comment I could take a look at it (or maybe someone else may be able to help you out too if I can't).
@deadapprox2267
@deadapprox2267 Жыл бұрын
Hello, thank you for this video, however I have faced a problem working on a project, what if the based param constructor was protected and not public, how can we use it from the derived constructor ?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Can you maybe share your code in a comment so I can see what you're trying to do? :-) In general we cannot make something "more public" in a derived class... if it's protected in the base class it should be protected in the derived class too. So I'm just wondering what your code looks like and what you mean by "use it".
@deadapprox2267
@deadapprox2267 Жыл бұрын
@@PortfolioCourses Thank you for your reply, here is the part of the code : ----- header file ---- class Piece : public CObject { private: CString model; // type of the piece protected: CString reference; // reference of the piece Piece(CString); // constructor, the parameter gives the piece type ; the reference will be composed of the three first letters of the type, all defined in cpp file below. public: Piece(); // default constructor, the type of the piece will be "piece" and the reference to "NR" in cpp file. void display(); //function that will display the model and reference }; class Engine : public Piece { private: int cptref; // counter public: Engine(); // default constructor, the type of the piece will be "engine" and the reference will be composed of the 3 first letters of the type followed by a underdash "_" then the number of the piece. void display(); }; ------- cpp file ------- Piece::Piece() { model = _T("piece"); reference = _T("NR"); } Piece::Piece(CString type) { modele = type; reference=modele.Left(3); } void Piece::display() { cout
@mymo_in_Bb
@mymo_in_Bb Жыл бұрын
Okay that's cool, but what if I need to call the base class constructor with some parameters (not hard-coded into the derived class) and then also the derived class constructor with more parameters? For example, making an instance of Drink with the name "hot chocolate" and a volume of 7 ounces? Would it look something like Drink( string name, double ounces ) : MenuItem( name ) ? Or is it something different?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That should work yes. :-)
@mymo_in_Bb
@mymo_in_Bb Жыл бұрын
@@PortfolioCourses Thank you very much! :D
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome! :-)
@gavinthecrafter
@gavinthecrafter Жыл бұрын
One question: What if my base class has only a constructor with parameters, and my derived class has only a seperate constructor with parameters? Is there a way to put in parameters for both constructors at the same time?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Like when you create the object, you want to provide arguments for both constructors, one in the base class and one in the derived class? The only way to do that, that I'm aware of, is to make a derived class constructor that accepts whatever parameters are needed for itself and whatever the base class constructor would need. And then that construct can "do some work" and then call the base class constructor as well. So you still only use one constructor when making the object, but the derived class constructor uses the base class constructor to have it do work too.
@gavinthecrafter
@gavinthecrafter Жыл бұрын
@@PortfolioCourses Thanks!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@gavinthecrafter You're welcome! 🙂
@muhammadluqman3452
@muhammadluqman3452 Жыл бұрын
hello sir great video , how can we stop the parent class constructor to be used in the child class ? thanks
@PortfolioCourses
@PortfolioCourses Жыл бұрын
So in general that is not something we want to do, the idea is that the parent class constructor sets up any member variables that the child class uses too. It may be possible though, there is an answer here for example: stackoverflow.com/questions/4065109/not-calling-base-class-constructor-from-derived-class
@Aashutosh_Bajaj
@Aashutosh_Bajaj 4 ай бұрын
Which is the IDE that you are using?
Introduction To Destructors In Inheritance  | C++ Tutorial
4:52
Portfolio Courses
Рет қаралды 3,1 М.
Multiple Inheritance Deep Dive | C++ Tutorial
14:53
Portfolio Courses
Рет қаралды 9 М.
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 16 МЛН
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
Schoolboy - Часть 2
00:12
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 2,5 МЛН
Define A Copy Constructor To Create A Deep Copy Of An Object | C++ Tutorial
17:06
C++ Inheritance: constructors and destructors in base and derived classes  [3]
8:23
new & delete Operators For Dynamic Memory Allocation | C++ Tutorial
15:52
Portfolio Courses
Рет қаралды 27 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
C++ OOP - What is inheritance in programming?
16:32
CodeBeauty
Рет қаралды 138 М.
Constructor Basics | C++ Tutorial
7:08
Portfolio Courses
Рет қаралды 21 М.
Member Initializer Lists | C++ Tutorial
23:10
Portfolio Courses
Рет қаралды 8 М.
Object Oriented Programming (OOP) in C++ Course
1:30:26
freeCodeCamp.org
Рет қаралды 2,5 МЛН
Что делать если забыл ОЧКИ??? #моястихия #swimming #юмор #fun
0:23
МОЯ СТИХИЯ | ПЛАВАНИЕ | МОСКВА
Рет қаралды 5 МЛН
Small Act of Kindness is also a charity
1:00
PainCare _Trust
Рет қаралды 49 МЛН
UNO!
0:18
БРУНО
Рет қаралды 2,2 МЛН
She realized that true happiness was not near this guy 😇🦄❤️‍🔥
0:12
iPolina Queen of the Cringe 👑
Рет қаралды 76 МЛН