C++ OOP - What is polymorphism in programming? (simple example)

preview_player
Показать описание
📚 Learn how to solve problems and build projects with these Free E-Books ⬇️

Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

The word "polymorphism" means to have many forms.
Polymorphism usually occurs when we have multiple classes that are related by inheritance. Because of this, two derived classes can define a method that has the same name, but the different implementation (behavior)
In this video, I'm providing a simple explanation and example of what I mentioned above.

This is a very understandable and simple explanation, that I found on the internet, and I want to share it with you:
If you ask a question like "What is a set?" in the forum topic "Tennis", you'll get one answer and if the forum topic is "Data structures" you'll get an entirely different answer. That's because the people who read these two topics process the same question in different ways. This is basically what polymorphism does!

C++ Object-Oriented Programming playlist:

C++ functions course:

Follow me on other platforms:

*******Initial code is available in COMMENTS and on TrueCodeBeauty GIT*******
Рекомендации по теме
Комментарии
Автор

📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.

#include <iostream>
#include <list>
using namespace std;

class YouTubeChannel {
private:
string Name;
int SubscribersCount;
list<string> PublishedVideoTitles;
protected:
string OwnerName;
public:
YouTubeChannel(string name, string ownerName) {
Name = name;
OwnerName = ownerName;
SubscribersCount = 0;
}
void GetInfo() {
cout << "Name: " << Name << endl;
cout << "OwnerName: " << OwnerName << endl;
cout << "SubscribersCount: " << SubscribersCount << endl;
cout << "Videos: " << endl;
for (string videoTitle : PublishedVideoTitles) {
cout << videoTitle << endl;
}
}
void Subscribe() {
SubscribersCount++;
}
void Unsubscribe() {
if (SubscribersCount > 0)
SubscribersCount--;
}
void PublishVideo(string title) {

}
};

class CookingYouTubeChannel : public YouTubeChannel {
public:
CookingYouTubeChannel(string name, string ownerName):YouTubeChannel(name, ownerName) {

}
void Practice() {
cout << OwnerName << " is practicing cooking, learning new recipes, experimenting with spices..." << endl;
}
};

int main()
{
CookingYouTubeChannel cookingYouTubeChannel("Amy's Kitchen", "Amy");

system("pause>0");
}

CodeBeauty
Автор

That is not polymorphism.

I mean `CookingYouTubeChannel` has a single function named `Practice`, so their is no need to invoke polymorphism to call
Same thing for `SingersYouTubeChannel`.
So in the end you end up with 2 functions that does not have the same full name: and
It is not polymorphism, it is just scoping.

Casting a pointer of a derived class to a base class is not polymorphism.
Calling a function a the base class (CheckAnanlytics) through pointer to the base class is still not polymorphism, even if the pointer points to an instance of the derived class.

To demonstrate polymorphism, you need examples where you are missing a piece of information at the call site.
You could have done:
1. Have multiple functions with same name (and scope) but different parameters. This could be done by introducing effort)` and then when calling you effectively use polymorphism because the compiler has to decide if you are calling effort)` or

2. Make Practice() a virtual function *and* call it through pointer to the base class.
class YouTubeChannel {
...
virtual void Practice() = 0;
...
}

class CookingYouTubeChannel {
...
void Practice() override;
...
}

YouTubeChannel *channel = new CookingYouTubeChannel;
channel->Practice();

3. Use templates and demonstrate polymorphism without inheritance.

template<typename T>
void call_practice(T &channel) {
channel.Practice();
}

gehngis
Автор

For people who didn't understand why Saldina used the pointer at the end of the video, well, I will explain the difference.
The only limitation in using *yt1 and *yt2 instead of cookingYt and singersYt is that both *yt1 and *yt2 are of type YoutubeChannel* and therefore we can only use these pointers to refer to the members that cookingYT and singersYT inherit from YoutubeChannel. so if we were to call the private function from these derived class (The Function made in these classes), we would use "cookingYT or SingersYT" instead of *yt1 and *yt2.
thx 😗

humanRk
Автор

This is honestly the best conetnt on YT for learning c++. My professor is completely useless and makes everything harder. Sometimes I spend 3 hours trying to understand a concept, then I find one fo your videos and I understand it instantly.
P.S. When I watch your videos I never skip the ads but I let them run for as long as they run. I know that adsense pays youtubers more when ads are watched in full (or something like that :D ), anyway, THANK YOU!

olivertwist
Автор

I'm a student's software engineer, and your videos are very helpfully to begin in C++ OPP, thanks for this content, I´m sure You help more than one person.

Greetings from Mexico !!

jesusguerrero
Автор

It's a good demonstration of polymorphism but you can't demonstrate true polymorphism without using the virtual keyword for dynamic binding. A better example would be to have the Practice function defined in the YouTubeChannel class as virtual or pure virtual, then override the Practice function in the child classes. Now, you can use Liskov Substitution to store the child classes as their parent, and still invoke the Practice method with polymorphism due to Practice being defined as virtual or pure virtual. I love your C++ series and thought I'd give some feedback on this one. I forward my students to your page all the time!

aw
Автор

Like the step-by-step breakdown this is a tough subject and you made it quite easy. Well done Saldina!

mytechnotalent
Автор

great series, I have been doing C development for a long time and have always been intimidated by C++ but am loving it now!

connelly
Автор

Making the pointer type a base Class type, enables the pointer to only access the functions in that base class only, but making the pointer type a derived class type, enables the pointer to access functions in both classes, base class & that derived class..

kwayssa
Автор

please keep making videos!

A brighter future waits you!

legolas
Автор

Wouldn't it be polymorphism if the base class had a .Practice method, but the derived classes implemented them differently?

jamesperih
Автор

Hello Saldina. Your videos are very well done, and informative. Would you make a video explaining the 'Copy Constructor' when, and why it is used and needed? Thanks in advance.

dalerobinson
Автор

Great Saldina !! Thx you so much for your effort.
Just a little question about your example. To be honest, I'm not understanding rightly the reason of the pointer used there.
I mean that same result could be get using derived class method directly. The derived classes are already instanced and yt1->CheckAnalytics() =
Am I wrong ? What's the rule ?

giovannimagni
Автор

why do you use pointers to get analyitics? shouln't be the same to use a method? thanks Saldina

roros
Автор

If you have watched previous videos, click 2:57

arjunans
Автор

Hello! Why do we use pointers instead of for example since it's public already. I might've missed something.

Cheers for the course!

TheDarkLordSpaniard
Автор

"I'm going to copy that, so that I don't make a typo"
Introduces a typo with the copy :))

Nice and informative videos, btw :)

scalemodelsworld
Автор

At 13:20 why did you use pointers to call a method from base class if you can do it directly from derived class?

grosucristi
Автор

why did we use pointers instead of just using

?
why create a *yt1 for it?

ronitakhariya
Автор

Very much appreciate your time and energy you put into your video’s I’m still an absolute beginner, could you please do a tutorial on complete beginners for just C programming language, I love 💓 the way you teach you have the best videos on c++ that I’ve seen so far thank you x

trueascendant