Interfaces in C++ (Pure Virtual Functions)

  Переглядів 333,262

The Cherno

The Cherno

6 років тому

Twitter ► / thecherno
Instagram ► / thecherno
Patreon ► / thecherno
Slack ► slack.thecherno.com
Series Playlist ► • C++
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K

КОМЕНТАРІ: 292
@danielc4267
@danielc4267 6 років тому
I don't think these videos are for beginners. But since I have some background in programming and C++, I found them very helpful.
@Levelworm
@Levelworm 4 роки тому
@@elonmusk7283 From my understanding an Interface is just a special kind of class that consists of virtual functions ONLY.
@aleksandarfranc1094
@aleksandarfranc1094 4 роки тому
@@Levelworm No ONLY. You can have any function and any data(int, float, char,...) in your class but if you have at least ONE pure virtual f then that class is kind a interface(you cant Instantiate it).
@Levelworm
@Levelworm 4 роки тому
@@aleksandarfranc1094 You are right! I need to brush up my C++...
@martinprochazka3714
@martinprochazka3714 4 роки тому
@Aleksandar Franc I'd actually call that an abstract class not an interface...
@JorenX
@JorenX 4 роки тому
@@martinprochazka3714 It behaves as an interface, but that makes it an abstract class
@user-vc2pm5xv7s
@user-vc2pm5xv7s 4 роки тому
clears a whole lot of uncertainties I don't even know I have deeply in my mind, simple and straight forward. GJ!
@meruhere
@meruhere 3 роки тому
Dude, thanks for these short but to the point videos! I liked the examples too.
@Djzaamir
@Djzaamir 6 років тому
one of my fav series these days on youtube
@HollywoodCameraWork
@HollywoodCameraWork 4 місяці тому
I *LOVE* the clarity of explanation in your videos.
@abdodaood3778
@abdodaood3778 5 місяців тому
I viewed this video five months ago but couldn't grasp its meaning until recently when I faced difficulty understanding senior-level code at my workplace. After struggling with comprehending their code, I revisited your video, and everything became clear. Thank you very much! I recommend every junior developer to examine others' code; it helps in understanding the intricacies of features in C++ and when and why to utilize them. Happy coding to all!
@naserghodsian5545
@naserghodsian5545 2 роки тому
I’m an Android developer but whenever I want to take a break, I watch a few videos of this c++ series. It is helping me deepen my knowledge of programming.
@Mr.PM1993
@Mr.PM1993 Рік тому
dude you need a hobby. it is like having a knife and try to cut yourself here and there.
@karmaindustrie
@karmaindustrie 5 років тому
This video just overrode the thinking method of my brain class.
@ezshroom
@ezshroom 3 роки тому
And it set the body to 'return "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"''
@karmaindustrie
@karmaindustrie 3 роки тому
@@ezshroom why?
@smileynetsmileynet7922
@smileynetsmileynet7922 3 роки тому
I'm sure the thinking method of your brain class is pure virtual and a subclass has it returning: "Im strange."
@karmaindustrie
@karmaindustrie 3 роки тому
@@smileynetsmileynet7922 I am very cool and exemplary. Almost like Jesus.
@liamgw9208
@liamgw9208 3 роки тому
is 'thinking' a pure method? what i mean by that is that 'thinking' is equal to zero.
@gavinw77
@gavinw77 6 років тому
Good episode. Looking forward to many of the vids coming up.
@ricardosendes776
@ricardosendes776 8 місяців тому
This one was a little fast for me, but so far I am loving the series! getting done about 1-2 Videos a day with practice and tasks I set myself. Thank you for creating the best programming tutorial I've found on the Internet thus far!
@aleksandarfranc1094
@aleksandarfranc1094 4 роки тому
Bro you are flying over these topics!! But i am still following you! You are challenging us and put us in uncomfort zone! I like it! Thanks!
@DonnyJ_CountryLife
@DonnyJ_CountryLife 3 місяці тому
Thank you for this series. I'm a PLC programmer by trade but I started traditional programming out of pure interest. I came across Interfaces in TwinCAT structured text and could not figure out why they were useful. Thanks to this video I FINALLY understand that they are simply a contract between classes (Function Blocks and methods in TwinCAT). Thank you again!
@endlesslybrowsing6559
@endlesslybrowsing6559 Рік тому
Dang, this has gotten way over my head 😂. But will continue to keep watching more and more videos out there. Picking up little by little each time I go through videos and books. One day I'll get it all pieces together.
@KushalMaharana
@KushalMaharana 6 років тому
I really like your videos cherno! Keep it up.
@rahalhettiarachchi8967
@rahalhettiarachchi8967 3 роки тому
Thank you very much for making these amazing tutorials!!!
@vignesh5618
@vignesh5618 4 роки тому
Thanks a lot for your c++ series.
@gustavosena7876
@gustavosena7876 8 місяців тому
This video wass sooo good ( as always haha )! Thank you Cherno!
@MarioMartinez_
@MarioMartinez_ 2 роки тому
Hey Cherno, love this series. I'm learning a lot. Thank you just got lost on the last two videos about virtual functions, lol. I hope it get clear later on
@longshot8696
@longshot8696 2 роки тому
same man exact same
@kotaa287
@kotaa287 11 місяців тому
You got it clear now?
@caibai
@caibai 9 місяців тому
the same feeling
@OlaDuda
@OlaDuda 4 роки тому
I had to change the speed of this video on slower, because I couldn't keep up :D However, I have to admit that I found out your voice and style of speaking pretty enjoyable! So grateful for your passion and knowledge which you pass on
@rudrOwO
@rudrOwO 2 роки тому
Much love, brother
@celioreyes2601
@celioreyes2601 3 місяці тому
Thank for the quick explanation!
@funera3385
@funera3385 4 роки тому
it was really helpful , thanks
@h.hristov
@h.hristov 6 років тому
Thanks for this video!
@empty_wallet_nomoney
@empty_wallet_nomoney Рік тому
gotta keep diggin into it, confusing though. thanks for the videos!
@austinfritzke9305
@austinfritzke9305 3 роки тому
It's too rare that I don't need to set the video playback 1.25x or 1.5x
@TheSiprianus
@TheSiprianus 3 роки тому
watch dark docs videos and you won't ever say that again.
@tigermendez6323
@tigermendez6323 2 роки тому
@@TheSiprianus Haha. Not only his voice... but the whole video seems speed up to 1.5x speed.
@bitminer69er
@bitminer69er 2 роки тому
I might recommend to discuss this in terms of the PImpl or Pointer to an Implementation design pattern (see Design Patterns / Gang of 4 book) with a simple class diagram on paper. Then showing how each concrete implementation class that inherits from the interface implement (is forced to) implement the interface. Also another syntax for forcing the user to inherit vs instantiate is to make the interface class constructor and deconstruct member functions protected or private and is just one more indicator to the user that ... yeah you can't instantiate the interface.
@GameStormerGX
@GameStormerGX 2 роки тому
These tutorials have been great so far, but this one gave my brain a run for its money. I was able to piece things together, but this would have been suited for a bit of a slower video in my opinion.
@mathiasmrch9122
@mathiasmrch9122 2 роки тому
Fantastic video
@suhas2go
@suhas2go 6 років тому
Nice. Really well explained :)
@MsJavaWolf
@MsJavaWolf 5 років тому
Because some have asked why pointers were used: you have to use pointers in C++ in order to get polymorphism. Try doing it without pointers and even with the virtual keyword you will not get polymorphism / dynamic dispatch.
@fenjiehuang6475
@fenjiehuang6475 6 років тому
Thanks for your video!
@swaw11
@swaw11 2 роки тому
Thanks man
@fernandoluz891
@fernandoluz891 5 років тому
You are amazing !! :-)
@LucidStew
@LucidStew 6 років тому
Thanks again for the series. A bit of critique on this particular episode: while you're talking and editing you're bouncing between lines rather rapidly and it gets confusing occasionally. If you could be a bit more deliberate in the editor, I think it would help clarify things..
@mc_pavlo3870
@mc_pavlo3870 2 роки тому
thanks a lot!
@1Naif
@1Naif 6 років тому
You are the best.
@tahamagdy4932
@tahamagdy4932 5 років тому
So clear
@MrKrusten
@MrKrusten 5 років тому
This was the first video where i didnt understand a single thing.
@heksogen4788
@heksogen4788 5 років тому
He completely convoluted the topic. Interface are very simple. You just declare a class with pure virtual methods[use virtual keyword and "=0" at the end of function], and then inherit that class/interface, and implement that method.
@Malik-lo7tw
@Malik-lo7tw 4 роки тому
@@heksogen4788 you just saved me 6mins...
@errr-iw4lz
@errr-iw4lz 4 роки тому
It's late, but If you are new to c++, I recomend watching thenewboston's playlist on c++, then coming back and watching these. They elaborate on the topic very well.
@kirizenkirizeto3492
@kirizenkirizeto3492 4 роки тому
right,except =0 to make it pure
@ammgnero
@ammgnero 4 роки тому
@@heksogen4788 still don't get it! why would i use this? i know the usage of virtual keyword but i don't know to why we use the pure virtual!
@adrianoldchannel2494
@adrianoldchannel2494 5 років тому
Just awesome
@Jetshockeyplayer
@Jetshockeyplayer 2 роки тому
I don’t know if you still check comments here, but thank you for carrying me through 3rd year engineering. My profs suck and you’re a life saver
@Special4ce293
@Special4ce293 2 роки тому
The deeper I get into learning C++... the more difficult it becomes to wrap my head around things. This really started to hit me with the Constructors/Destructors videos by you... I will keep looking into it tho!
@ytzzn
@ytzzn Рік тому
Did you finish the series and did you understand? because I'm currently at this episode 28/100 and I'm starting to not understand.
@Special4ce293
@Special4ce293 Рік тому
@@ytzzn I'll be honest, I watched several more tutorials, even repeated a few chapters.. I kinda gave up. I switched to the Godot engine and that has been a blast.
@ytzzn
@ytzzn Рік тому
​@@Special4ce293 Alr ty
@sepehrariaei8123
@sepehrariaei8123 Рік тому
me too. I am not catching up anymore from constructures
@jonispatented
@jonispatented 10 місяців тому
@sepehrariaei8123 you have to stop watching these, go and just write like 30 programs in C++ using only the knowledge you have now, and then come back to watch these videos again. Also, these last two videos have been in a poor position in the playlist. Skip these two for now and come back later.
@JaydenCloudwalker
@JaydenCloudwalker 11 днів тому
A pure virtual function serves as a declaration (it also imposes a requirement on derived classes to provide their own implementation). This declaration is needed when assigning a pointer of a base class type (by base, I'm referring to the abstract class - the class that provides the pure virtual function) to an object of a derived class type and subsequently calling the function. Without a pure virtual function (or just a virtual function if you want to implement the function inside the base class), the base pointer lacks awareness of any of the function's implementation in derived classes. Not every derived class needs to explicitly implement the function. Instead, if a derived class lacks an implementation, the program will search through the inheritance hierarchy to find one, starting from the current derived class and progressing back to the base class. Only if no implementation is found throughout the hierarchy does it result in a compile-time error.
@MultiThetrick
@MultiThetrick 3 роки тому
Thank you.
@yummycandy1703
@yummycandy1703 3 роки тому
superb
@mmaranta785
@mmaranta785 3 роки тому
You’re the best
@l3aIIin23
@l3aIIin23 5 років тому
Thanks!!!
@mickyyang6652
@mickyyang6652 Рік тому
I have no idea why some people was talking about interfaces and abstract classes in comments, but the difference is just sematics. We, who write code, defined these to recognize what they represent. For example, `Entity` is an abstract class, it's the base of all entities in the game; and in the other hand, `PrintableInterface`, which defined some traits that a printable object should have, is an interface.
@brethagen7776
@brethagen7776 5 років тому
Now i'm gonna ace this test.
@RandomGuyyy
@RandomGuyyy 6 років тому
We're getting to the interesting stuff now
@AlwinmathewP
@AlwinmathewP 2 роки тому
In 2019 when i first found the channel i quit learning cpp because it was very hard for me to understand thr concepts behind it now its 2021 I learnt java and am pretty good at it... Now im re-watching this playlist and everything is making more sense to me. My advice to very beginner programmers is to get good at easier languages like python, js, and eventually work your way down to c++ then everything will make much more sense....
@Laevatei1nn
@Laevatei1nn 2 роки тому
tried to learn cpp in 2020, quit, learn python, quit, back to cpp in 2021 and actually understand the concepts idk why lmfao
@xarki768
@xarki768 Рік тому
@@Laevatei1nn i tried to learn cpp in 2018, quit, went back to learning it again at 2020, quit, back to cpp in 2022 but this time i understand it idk why lmaoo
@Laevatei1nn
@Laevatei1nn Рік тому
@@xarki768 same, i went to college this year and learning this definitely pays off
@zainahmed4172
@zainahmed4172 2 роки тому
Welcome 🙏
@ahmopasa
@ahmopasa 3 роки тому
So, in short, can we say for pure visual functions that we just declare the function in the base class and give definitions in the sub-classes?
@bekaryskuralbay3751
@bekaryskuralbay3751 4 роки тому
Really good)
@ivanzvonimirhorvat9744
@ivanzvonimirhorvat9744 4 роки тому
It would be nice if you do "interface" again, and explain vptr(Virtual Pointer) vtbl(Virtual Table), and that size of object increase by 4|8 bytes (depends on architecture), due to VTPR. Thank you in advance.
@battlecrusecruse9155
@battlecrusecruse9155 3 роки тому
for the first time in my life, i played a video at .75X, my brain cells have been ashamed .
@chainonsmanquants1630
@chainonsmanquants1630 3 роки тому
Thanks
@austinfritzke9305
@austinfritzke9305 3 роки тому
Perfect alternative to tutorialspoint
@icebrn
@icebrn 6 років тому
Can you make a video about memory leaks?
@TheCherno
@TheCherno 6 років тому
Yes
@icebrn
@icebrn 6 років тому
NICEEEEEEE!!!!!
@loli42
@loli42 6 років тому
TheChernoProject You should do that next if you don't have another video planned. It seems really interesting!
@HyperManu
@HyperManu 6 років тому
All the information you are giving us is already available on the internet so It would be nice for the "not beginners" to get also a good tutorial on complex stuff such as streams, memory management and whatever you think is more advance. Awesome Tutorials!
@ezioarno15
@ezioarno15 6 років тому
i was just going to ask him
@lemonMemes311
@lemonMemes311 4 роки тому
How do you destruct Printable (assuming that GetClassName actually had destructable arguments)? Would you need a pure virtual destructor or would a regular virtual destructor work?
@tralphstreet
@tralphstreet Рік тому
These are basically what Traits are in Rust. Pretty cool.
@conforzo
@conforzo 6 років тому
I know its queit a long way. But have you thought of making a C++ API serie after this one ? Or should I start the sparky engine series after this one ?
@marutihr
@marutihr 3 роки тому
Nice Video!! Very clear explanation. How do you navigate so fast in code?
@sfafsashfdh6589
@sfafsashfdh6589 Рік тому
magic
@leonardonogueira7815
@leonardonogueira7815 4 роки тому
hey, could you post a .txt file with you code. It would help to understand the video since you scroll a little bit too fast during the video
@mikefochtman7164
@mikefochtman7164 Рік тому
Love the series, working my way through. Question: How can I make sure that 'Player' provides its own implementation of the pure virtual function? You showed that if it doesn't, it ends up using the version inherited from 'Entity', but I want to force all child classes to provide their own implementation. Thanks for the video series, really enjoying it.
@LenninSpirit
@LenninSpirit Рік тому
Let’s say “Entity” has a function named “getAge” that returns an int. “Entity” can either have its own version of the function, or be 'pure' (but not both). for example: version with implementation: virtual int getAge( ) { return 10; } OR “Pure” version: virtual int getAge() = 0; If “ENTITY” declares implementation, then sub-classes (like “Player”) can optionally override or just ignore it. But if “ENTITY” declares as “pure” (virtual int getAge() = 0;) then sub-classes (like “Player”) have the obligation to provide an implementation for themselves.
@Mallchad
@Mallchad 6 місяців тому
First. C++ is stupid as heck when it comes to abstract/pure classes. TL;DR create base class, define constructor and virtual destructor as empty `base() {} virtual ~base() {}` (or just leave a default constructor who cares) Then create the pure functions as virtual without a body `{}` and it will fail to compile when you make a class `void pure(int _unused);` However. C++ is AWFUL at doing inheritance and the error messages are terrible. And you can easily "shadow" or "override" virtual function names with no error. It's particularly bad if you make a mistake. To mitigate it you have a few options - Pure specifier. `virtual void pure(int _unused) = 0` the =0 part is the pure specified and will give an error message if it's unimplimented. - Error messages (mainly just clang) `-Werror=overloaded-virtual` will cause a compile error - Override attribute. Tell the compiler you *intend* to override a virtual function and it will error if the definition differs. This is easily one of my least favourite parts of C++ the fact you have to do it at all is madness. It relies on the writer being smart. `void overriden_pure(int _unused) override {}`
@pb25193
@pb25193 5 років тому
hey, cherno! what do you think of creating quizzes and assignments and making it a full blown course? i know i would pay for such a course, and im sure large companies would really value the qualifications that you endow upon people!
@syzygy8060
@syzygy8060 4 роки тому
Dude.. i am loving the series but this one was entirely too fast.. i had to break out the book to get this point. I do however appreciate you for doing this series.
@mananpandya4493
@mananpandya4493 Рік тому
Very informative video. Just curious to know that why vtable and vptr for pure virtual function is requied. I got some answer over internet but, not satisfied with the explaination. Can you pls make small video on the same if possible
@szinton
@szinton 6 років тому
Why did You use (Printable* obj) instead of just (Printable obj)? Whats the difference?
@Gapo1994
@Gapo1994 6 років тому
Using(Printable obj) makes a copy of that object which can be memory expensive. Thats why (Printable *obj) is a better option since it now points to already existing memory , you could also use (Printable &obj), a reference to Printable obj :D
@szinton
@szinton 6 років тому
Mateo G Thank you for explanation =)
@levanfirtskhalava3836
@levanfirtskhalava3836 6 років тому
difference is that first one is pointer and second is instance. we can't create instance from abstract class, but pointer we can and exactly this is a powerful tool
@oleholgerson3416
@oleholgerson3416 5 років тому
you‘re all missing the most important point: object slicing. In C++ you have to be super careful with polymorphism, it requires a pointer or a reference. Otherwise the copy constructor gets called and slices your subclass
@sprasoon
@sprasoon 3 роки тому
Does "pure virtual function" approach have similar overhead as "virtual function"?
@eyesight2073
@eyesight2073 3 роки тому
@ 5:26 I forgot to put public (that means its private). But still no error. prints "A" correctly. But WHY?
@eyesight2073
@eyesight2073 3 роки тому
How does Print() function call private function of A?
@agnesakne4409
@agnesakne4409 5 років тому
This lad is a very intelligent one.
@AbdulMoiz-ho8rx
@AbdulMoiz-ho8rx 2 роки тому
Great
@yash1152
@yash1152 Рік тому
3:58 > _"i keep calling interface - it's just a class in cpp, unlike othe langs which provide a keyw for it"_ ohkayyy, ohkay. thanks a lot, i was kinda unable to really understand what interface in other langs (kotlin) were. i will give it a second go now after watching this.
@rogansteeven1672
@rogansteeven1672 2 роки тому
After reading "Introducing Go", i feel like the better example would be about class 'shape' and it's derivatives; also we want to write a function for all shape's derivatives. #include #include class Shape { public: virtual double Area() = 0; virtual double Circum() = 0; }; class Circle : public Shape { public: float x, y, r; public: Circle(float X, float Y, float R) : x(X), y(Y), r(R) {} double Area() override { return 3.14 * r * r; } double Circum() override { return 2 * 3.14 * r; } }; class Rectangle : public Shape { public: float x, y, s; public: Rectangle(float X, float Y, float S) : x(X), y(Y), s(S) {} double Area() override { return s * s; } double Circum() override { return 4 * s; } }; void PrintShapes(std::vector shapes) { for (Shape* shape : shapes) { std::cout
@michaelswahla4927
@michaelswahla4927 Рік тому
Thank you so much for this example. You won't believe how much it helped dude, now I get why interfaces are useful! Your amazing!!
@sunilsarode152
@sunilsarode152 4 роки тому
can you please make video on design patterns........Please :) thanks lot
@beachforestmountain4269
@beachforestmountain4269 4 роки тому
Would I be correct to say that pure virtual functions could be very useful for cross-platform code (eg Mac or Windows), where one base class can be called, which leads to sub-classes which have code that correspond to the particular platform being used in the compiler? So the main code can be universal, but the sub-class code can be platform-specific, eg a 'Mac' class, or a 'Windows' class - depending on which platform you were looking to compile at the time. If this is the case, would a 'flag' be set at the start of the program so the correct class (Mac class or Windows class) would be used throughout the program wherever necessary?
@darkengine5931
@darkengine5931 3 роки тому
That's actually one of the worst cases to use virtual functions/interfaces or even inheritance IMHO because the code for Windows typically won't build on Mac and vice versa. So even if you make a subtype for Windows and a subtype for Mac of a particular virtual interface, you'll still have to rely on the preprocessor or the build system to filter out all implementation code except the one for the platform being built against, and at that point virtual/dynamic dispatch becomes useless since there's no varying runtime condition/input to use to select which function to call. All that can and should be resolved at compile time. For cross-platform code, it's often much simpler to just use a concrete class, possibly with a pimpl, like so: // Gui.h #pragma once class Gui { public: ... private: class Impl; std::unique_ptr pimpl; }; Then you might have like a 'GuiWindows.cpp' for Windows, 'GuiOsx.cpp' for OSX, maybe one or more for Linux, and have your build system include the right source file(s) based on the platform. Or you might just have a large 'Gui.cpp' with a bunch of #ifdefs/#ifndefs based on the platform we're targeting. The branching occurs at compile-time, so to speak, rather than runtime. It makes no sense to branch at runtime here. This is a job for your preprocessor or your build system, so to speak, not virtual dispatch. Of course, the virtual dispatch might be useful for event-handling or something. You might still put some virtual functions in the Gui class above and allow people to inherit the base class and override those functions. But it makes no practical sense to me to be using virtual dispatch based on the runtime condition of what operating system/platform is being used... at least until operating systems establish enough of a standard that code designed for other operating systems build on a particular system and we have cross-platform binaries (ex: a single binary executable that can be executed on Windows, OSX, and Linux without requiring separate binaries for each target). An example that's still close but where virtual dispatch might be suitable is to select an underlying rendering target. For example, you might create a virtual interface for rendering which abstracts both the capabilities of OpenGL and DirectX in a single abstract interface, then implement one subtype for OpenGL and another for DirectX. That makes more sense because if the user is running Windows, they have both available and might be able to change which one to use at runtime by changing preferences or a config file. In any case, you should generally reserve the use of virtual dispatch for cases where the actual overridden function that is called is based on a runtime condition/user input. Otherwise, it's just a needless overhead both for the programmer (virtual interfaces and ABCs tend to be more clunky to use) and CPU if the actual functions to call can be resolved at compile-time. If this seems a bit confusing just think about where if/else is appropriate first. Would it make sense to write code like this? if (os == windows) { ... } else if (os == osx) { ... } else if (os == linux) { ... } No, right? Typically you'd need to use the preprocessor here to write code that even builds, and the operating system we're using is not a variable that can be changed at runtime. In those cases, virtual dispatch makes little sense. Meanwhile using if/else for render target might make sense: if (render_target == directx) { ... } else if (render_target == opengl) { ... } And likewise, virtual dispatch makes sense as an alternative to the if/else here.
@beachforestmountain4269
@beachforestmountain4269 3 роки тому
@@darkengine5931 Thanks for the explanation, and thanks for giving an example of when it could be used. I do understand what you said.
@nighma
@nighma 6 років тому
Hi Yan! I've 2 questions : 1/ in C++ is there a difference between Interface and Abstract class like in Java for example? 2/ at 5:40 You said Print(new A()); is a memory leak, could you explain a bit? Thank you
@Gapo1994
@Gapo1994 6 років тому
2/ -> Everytime you use the "new" operator for dinamyc memory allocation you also need to free that memory manualy with "delete" or "delete[].
@nighma
@nighma 6 років тому
Thank you
@nighma
@nighma 6 років тому
In Java the difference between Abstract classes and interfaces is pretty much it : Methods of a Java interface are implicitly abstract and cannot have implementations (pure virtual). A Java abstract class can have instance methods that implements a default behavior.
@zachanderson1196
@zachanderson1196 5 років тому
Could you give a more real world scenario to help wrap my head around this?
@peterarbeitsloser7819
@peterarbeitsloser7819 3 роки тому
Different implementations of Java Sets for example (HashSet, LinkedHashSet,...) that all have to have some functionality. This functionality is guaranteed by implementing a common interface
@smrtfasizmu6161
@smrtfasizmu6161 2 роки тому
I have just had the need to make an interface for the first time ever (when I learned about them I used them but I didn't see why I would use them in those situations, it just seemed unnecessary. I never had a situation where writing my own interface seemed necessary or convenient. I had implemented interfaces that already existed in libraries such as awt and swing and I saw value in those interfaces but I have never had the need to write my own interfaces until now). I was dealing with a Mongo database. I wanted to make a method which will change id of collection members in the database. I want every object in the database to have a unique identification number, I called it pid. So, my method takes as an argument an object which has a pid field and then changes it. How would you write that? How would you tell the compiler that this method receives an object which has a field called pid? By having the method take an instance of the interface as the argument. Interface has getPid and setPid methods. If you don't do that, you will have to write separate methods for every single class that you want to put in your database. I wrote this in Java and I used generics (if you were to do this in C++ you would use templates, they are similar to generics in Java).
@smrtfasizmu6161
@smrtfasizmu6161 2 роки тому
Another example where you can write your own interface is if you write your own Event Object. Event Objects hold references to bunch of objects. Those objects are called event listeners. When some event happens the event object activates a certain method of its event listeners. That's why they are called event listeners, they will be "told" when an event happens, because the event object will activate their method. We would like objects of various classes to be event listeners, so we use an interface. For instance, interface has a method itIs7Am() and all object listeners implement that interface. If you have a class employee, they implement it by going to a job, class student implements it by going to university, class Bird implements it by starting to sing. Event object will activate the method itIs7Am of all of its listeners even though its listeners belong to various different classes and they implement this method in very different ways. Examples for when you use an interface which was already written can also be event listeners, like ActionListener interface in Java awt library which is used a lot. When button is pressed, it activates action Performed method of all of its listeners. All of its listeners have the method actionPerformed because they implement interface ActionListener, even though they implement the method actionPerformed in various different ways.
@smrtfasizmu6161
@smrtfasizmu6161 2 роки тому
The method that I wrote goes like this public void changeId(MongoCollection mgCollection, int pidIncrease){ for(T object : mgCollection.find()){ int pid = Integer.parseInt(object.getPid()); int newPid = pid + pidIncrease; Bson QUERY = Filters.eq("pid", String.valueOf(pid)); Bson UPDATE = Updates.set("pid", String.valueOf(newPid)); mgCollection.updateOne(QUERY, UPDATE); } } I am going through collection and changing pid of members. This method will work for collection of any objects in the database, because I only have collection of objects which implement PidInterface and I have getPid method in the PidInterface. I can have objects of various different classes in the database but they all need to implement PidInterface so that I can do operations on them. As you can see, the method which does operation on those objects in the database (changes their pid in this case) receives a MongoCollection which is made out of objects that implement the PidInterface. If I don't use an interface I would have to write a separate method for each class whose instances are in the database.
@danielfaraday2821
@danielfaraday2821 Рік тому
Am I right that, in the last example if I create Entity e = new Player(); and call e.GetClassName() it will get me "Entity" and not "Player"? And in order to get "Player", the GetClassName() function in Entity should be virtual as well?
@hamzamameche3893
@hamzamameche3893 2 роки тому
Why the GetClassName() method in the Entity class isn't virtual? yet we are still able to override it in the Player class. Does that mean we only need the mother method to be virtual?
@soulimanemammar2909
@soulimanemammar2909 Рік тому
it is virtual although the keyword isn't there ( you can add it for more clarity).
@adamsiepker8902
@adamsiepker8902 4 роки тому
At 1:59, Visual Studio underlines "Entity" because you can't instantiate it due to an error. For me, "Entity" is not underlined although I have the same error. How can I make visual studio underline errors?
@phantomstriker7996
@phantomstriker7996 Рік тому
Pure virtual functions allow us to define a function in a base class which doesn't have an implementation
@spyrex3988
@spyrex3988 Рік тому
Wait den what's da difference between an interface and an abstract class?
@TheAshish8754
@TheAshish8754 4 роки тому
any real life implementation example of interfaces and virtual function please
@ezioarno15
@ezioarno15 6 років тому
please make a Video on Memory Leaks and saving memory and also on functions like malloc
@Matt23488
@Matt23488 5 років тому
I doubt he would do any videos on malloc() and free(), as those are functions in the C Standard Library for dynamic memory allocation. In C++ you would use the 'new' and 'delete' keywords instead, or smart pointers (which he has a video for). That's not to say you can't use malloc() and calloc() and free(), but you probably shouldn't as new and delete keywords are cleaner.
@systematicloop3215
@systematicloop3215 6 років тому
Why doesn't C++ have some sort of interface keyword like other languages do? It doesn't make much sense that you have to set the function to zero.
@meJevin
@meJevin 6 років тому
you're not setting anything to zero, it's just a part of c++ syntax. if you'll get used to it, you'll be able to read c++ code like shown in this video with ease.
@MBBGun14
@MBBGun14 6 років тому
Because it is old language and I think the concept of interface wasn't really clarified at that moment. I don't know if it's in new C++ releases though, if not, then maybe it's for backwards compatibility reasons.
@valrossenOliver
@valrossenOliver 6 років тому
Well, you COULD if you want to define interface yourself with "#define interface struct" and everytime you write interface it will become a struct (class with public variables as standard). As a new-ish person to C++ I have no idea if this would have SOME kind of negative income, but I tried it out and it works. I'd guess maby compiling-time increases **slightly** but meh. /shrug
@josephmellor7641
@josephmellor7641 6 років тому
RadiationAlert N/A Doing it like this allows you to define some methods in the "interface" by not adding an "=0" at the end of some functions.
@meJevin
@meJevin 6 років тому
+valrossenOliver, yes, "#define interface struct" is generally a bad idea, because c-style macros are dangerous, obscuring, and hard to debug. there's tons and tons of articles about how bad it can get when a person uses too many #define macros, look it up. compiling time will not be affected at all (well, actually, preprocessor will have to do more work substituting every occurrence of "interface" to "struct" in your code).
@wesleylim5932
@wesleylim5932 3 роки тому
Can someone explain why override was used for Entity but not A since they are sort of at the same level of branching
@niemand9109
@niemand9109 9 місяців тому
Hardcore Tutorial, this was confusing. I think you really set a standard in explaining code with your series but the last two ones...uff...I got it but it really wasn't easy to grasp. Thanks for explaining but those last Tutorials lacked the easy comprehensabilty. I think it was due to the overloaded cpp-Project. Still grateful.
@chinmaykulkarni3016
@chinmaykulkarni3016 4 роки тому
Should have mentioned of a virtual destructor in base class.
@WayneRiesterer
@WayneRiesterer 5 років тому
What options are there for creating a pure virtual class? Say for example I had a CRUD application and a class called Entity that only had "string guid" as a single member. I don't want to allow Entity to be instantiated, but only serve as a base class for less abstract class definitions. From what I've learned from this series, I'm guessing that hiding the constructor under private scope maybe an option? Also, how come there is a public scope modifier when declaring a class that inherits from another class: class Person : public Entity for example?
@WayneRiesterer
@WayneRiesterer 5 років тому
@PeterolenThanks for your explanation! I overlooked the drawback of having a private constructor. It's obvious now that you mention it. I'll have to check out more on the public/private/protected inheritance as it seems like a topic that would contain a reasonable level of complexity. Thanks again :)
@luismt555
@luismt555 6 років тому
Can you make a video about Multiple Inheritance?
@bhooshandeshpande6982
@bhooshandeshpande6982 3 роки тому
Can you make a video about this concept using some practica examples. I mean player and entity are practical, but their use doesn't exactly highlight the purpose of having interfaces, rather just provides a possible usecase.
@smrtfasizmu6161
@smrtfasizmu6161 2 роки тому
I have just had the need to make an interface for the first time ever (when I learned about them I used them but I didn't see why I would use them in those situations, it just seemed unnecessary. I never had a situation where writing my own interface seemed necessary or convenient. I had implemented interfaces that already existed in libraries such as awt and swing and I saw value in those interfaces but I have never had the need to write my own interfaces until now). I was dealing with a Mongo database. I wanted to make a method which will change id of collection members in the database. I want every object in the database to have a unique identification number, I called it pid. So, my method takes as an argument an object which has a pid field and then changes it. How would you write that? How would you tell the compiler that this method receives an object which has a field called pid? By having the method take an instance of the interface as the argument. Interface has getPid and setPid methods.
@jenniferw8963
@jenniferw8963 Рік тому
I see you use =0 to force an implementation of a method which returns a value. But what about a method that is perhaps "void the_method(int x)". How do you force an implementation of this? Do you put =0 at the end? Doesn't make sense really since the method doesn't return anythingn.
@lysdexic9129
@lysdexic9129 6 років тому
Hi, I'm unsure how you managed to instantiate a Player class without GetClassName as the Player class inherits Entity which inherits Printable, so doesn't that make Player abstract? Also confused as you are using Player* e = new Player and not Entity* = new Player when you print "Entity" for player? I hope you understand what I'm asking.
@lysdexic9129
@lysdexic9129 6 років тому
yes, thanks for that.
@andrejpetrushevski9880
@andrejpetrushevski9880 4 роки тому
i lost you after inheritance
@redox5373
@redox5373 2 роки тому
For people who didn't understand a thing: Pure virtual functions are declared in a parent class, but can't be neither defined nor be instantiated from that class. If you want to use that method, you would have to redeclare and redefine it in a child class. Only then it can be used and instantiated. So basically it's just a blueprint of a function which can be derived by a child class from a parent class.
@koool56
@koool56 2 роки тому
Cheers for helping ppl but vid is 4 years old, doubt he reads comments that much.
@noobdev7013
@noobdev7013 2 роки тому
Also, assuming I understood correctly; if a child's (parent class) inherits an interface, the child class will also be able to implement the interface's methods without having to inherit the interface as well.
@avadhut325
@avadhut325 6 років тому
How are we supposed to know when to use pointer and when not to ?
@ali51717
@ali51717 4 роки тому
So, I guess they are called protocols in Swift
@TW19567
@TW19567 7 місяців тому
Watched this at 80% speed and could actually keep track of what was being talked about. Very good explanation but just too fast for people to follow along coherently.
@HarshVerma-oz5uo
@HarshVerma-oz5uo 3 роки тому
What is the point if anyway i have to create the object of the derived class for base class to access the definition. Then what is the point of pure virtual function
@HarshVerma-oz5uo
@HarshVerma-oz5uo 3 роки тому
@Peterolen first of thank you for the reply and second thing is that you won't believe it but right now I'm watching your c++ series video
@smrtfasizmu6161
@smrtfasizmu6161 2 роки тому
Here is my example for when using an interface is useful. I have just had the need to make an interface for the first time ever (when I learned about them I used them but I didn't see why I would use them in those situations, it just seemed unnecessary. I never had a situation where writing my own interface seemed necessary or convenient. I had implemented interfaces that already existed in libraries such as awt and swing and I saw value in those interfaces but I have never had the need to write my own interfaces until now). I was dealing with a Mongo database. I wanted to make a method which will change id of collection members in the database. I want every object in the database to have a unique identification number, I called it pid. So, my method takes as an argument an object which has a pid field and then changes it. How would you write that? How would you tell the compiler that this method receives an object which has a field called pid? By having the method take an instance of the interface as the argument. Interface has getPid and setPid methods.
Visibility in C++
8:49
The Cherno
Переглядів 157 тис.
😨Новая Война в GTA 5 Online #shorts
00:40
King Dm
Переглядів 1,6 млн
КИРПИЧ ОБ ГОЛОВУ #shorts
00:24
Паша Осадчий
Переглядів 6 млн
Virtual Functions in C++
6:45
The Cherno
Переглядів 483 тис.
I Rewrote This Entire Main File // Code Review
16:08
The Cherno
Переглядів 81 тис.
BEST WAY to make Desktop Applications in C++
26:00
The Cherno
Переглядів 864 тис.
how Google writes gorgeous C++
7:40
Low Level Learning
Переглядів 722 тис.
EVERYTHING takes longer than it seems
22:17
The Cherno
Переглядів 31 тис.
SINGLETONS in C++
19:16
The Cherno
Переглядів 191 тис.
Pure Virtual Function In C++
16:15
CppNuts
Переглядів 13 тис.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Переглядів 1,9 млн
are "smart pointers" actually smart?
9:44
Low Level Learning
Переглядів 67 тис.