Code & Transcript :
Best C++ Book :
We cover a lot in this tutorial. We cover Polymorphism, Structs, Friend Classes, Abstract Classes, Override, Final, Virtual and much more.
Tag: polymorphism c++, C++ Tutorial, C++ Polymorphism, Polymorphism
Xem thêm: https://meohay360.com/category/internet
Nguồn: https://meohay360.com
Understanding typecasting and pointer arithmetics could be helpful to fully understand this:
Say that you have 2 classes: class base and class derived. base contains 2 integer variables: a and b, and derived inherits from base and has 2 additional integers: c and d.
Now let's say that we initialize derived: class derived* d = new derived; d->a = 1; d->b = 2; d->c = 3; d->d = 4;
Now let's initialize the base class by not using new but instead by placing it's pointer to the same memory location as d: class base* b = d;
Now what happens if we try to print out the values?
cout << b->a << endl; cout << b->b << endl; we will see these values: 1 and 2.
Now for the pointer arithmetics: if we do: cout << (b+1)->a << endl; cout << (b+1)->b << endl; we get these values: 3 and 4.
This makes sense because we use pointer arithmetics.
But this is also typecasting. When we did "class base* b = d" we actually did: class base* b = (class base*)d; This (class base*) in parenthesis can be done using the g++ compiler for example.
So this is a part of polymorphism that is not talked about so much, maybe. This (b+1)->a should not be used when virtual functions are present though because the virtual table is then present.
Wow. You should instruct at Universities…
The best !!
Thank god you don't have an Indian accent hehe
I see parts of polymorphism as object typecasting. Like for example b = (class baseclass*)d; same as: b = d. It gets trickier to use pointer arithmetic when virtual functions are present but maybe that too works somehow.
if anyone is reading the comments can you tell me what does the arrow operator do ??
I have some problems with inherit classes, and after playing it, I get that. { I dont know why Your version didn't worked for me. 🙁 }
#include <iostream>
#include <cmath>
using namespace std;
class SHAPE{
protected:
double height;
double width;
public:
void s(double lenght){ // changed to function
height = lenght;
width = lenght;
}
void s(double h, double w){ // changed to function
height = h;
width = w;
}
virtual double area(){
return height * width;
}
};
class CIRCLE : public SHAPE{
public:
void c(double w) { // changed to function
s(w); // used as function.
}
double area(){
return 3.14159 * pow((width / 2), 2);
}
};
void showarea(SHAPE& shape){
cout << "Area : " << shape.area() << endl;
}
int main(){
SHAPE square;
square.s(10,5); //after creating object.
CIRCLE circle;
circle.c(10);
showarea(square);
showarea(circle);
return 0;
}
program works great, but after that changes 🙁
NOw I understand! I needed look this one 3 times 11:32. Polymorphism is a way to copy all containers to class circle, that means i don't need create new values of 'width, height, area' because class shape have that already. And I can use structure from that class for new object. Its like #Include file.h, polymorphism works at that rule . I am include structure from another class for new object, and I don't need anymore create this same names.
Do I have right??
If You could do tutorial with more examples about that I will be super HAPPY.
10:51 I dont understand that, Circle is child class? And shape is main class? Somebody else was explaining me oposite way, That Circle who have inherit shape class <- this shape class is child. Not parrent… Now I am confused.
7:24 You forget to mention that friend class have access to private data of main class, and have ability to change private stuff. Its like a back door. Can You make more examples in real world why friend class should be used?
Thank you! This was awesome!
0:24 struct
2:21 inheritance ( how to use or start at struct to understand)
5:20 friend
8:07 polymorphism
13:14 abstract classes
thank you
nice vid but regarding that friend class, what if the base class has more components and more data types ?
Hello Derek,
Thank you for explaining hard concepts in a very clear way. You are very helpful in helping lots of Com Sci student understand the concept thoroughly. I have two questions regarding this video Derek, and they are from the Polymorphism part in this video.
1) In the Shape class, you did not define a default constructor, isn't it like when you decide to explicitly define general constructors then you also have to explicitly define default constructor(with no arguments or with default parameter)?
2) For the void Show Area(Shape& shape), why did you use pass by reference instead of pass by value? I mean you aren't modifying data member from the object so why did you use it?
Thank you so much for your time Derek. God bless.🙏
extremely clean and informative introduction. Thanks for sharing~!
Please explain the Function GetName( Customer& customer)
{ return customer.name;}
I did not understood how it returns the name.
Kolos zdany
Can we use polymorphism with protected inheritance of base class?
Thank derek, keep the good work
Coming from Java and some C knowledge, this is perfect! Thanks!
A little comment: Common convention is since the beginning of time, NEVER to name a member function or a method or operation with a beginning capital letter. The naming conventions are there to enhance readability and increasing the value of code in many ways. The same is for class names, they are not named containing imperative verbs, NEVER. A class is the something that contains data and operations on that data. So it is to be seen as a static real world object. a car, a tree, a shape, a customer etc.. a class named GetCustomerName is wrong. Instead, Customer and CustomerData or CustomerMeintainance or CustomerQueries or off course straight in Customer class, with getCusomterName() as a member function is correct.
Refering to the class with only public function Area, it's like java interfaces?
bruh
Amazing Channel, Thanks a lot !
Literally talking code awesome
Wow. This was explained very clearly. Thank you
Derek, I really can't tell you how much I appreciate your training videos. Just terrific. I'm a technical writer, not a professional programmer. I do need to write short snippets and examples of using new classes and methods my company adds to its very deep code base, and your videos have helped me incredibly. I am so grateful. Much fun to write the code here, play with it to break and fix. It's amazing how deep and powerful C++ is. C++ engineers are geniuses. Thanks for everything. I am a fan!
whats the difference betweem this->height = h and just height = h
I believe you are showing pure virtual when setting the method = 0
hello derek will you ever cover linked lists?
I just found this and 5min in I already love what you do Derek. Thank you so much! Awesome tutorial!
Hi, is this c++ 11? Also I've not seen anything like "virtual double Area () = 0;" before, how can you just set a function declaration equal to an int? Looks interesting! Thanks!
Exceptional tutorials Derek.
At 2:33, you have not used the data access specifier for circle inheriting from struct shape. Can we use protected or private data specifier for the derived struct and in that case, do we have to use specifiers.
At 14:53, why should the compiler be forced to check if virtual function of base class is same as derived class. Due to the law of inheritance, the double Area() inside the circle class should be virtual.
now this is the way I like to learn to code. thank you for this style of video!
<>
Do you do anything on minimax type things?
Thanks for your help xD
your videos are awesome, Thanks for helping humankind .
Do you really answer all the new questions?
why not use M_PI…
Hi Darek, AWSOME tutorials, gladd theres people like you around :). Quick question, why do you have to put the reference operator & in Customers? Thanks!
Thank your very much Derek, You are a awesome man. could you make JavaFx series and how to develope game with javafx?
Can someone explain line 30 to me around 12:18? So line 29 is a constructor for a circle but what is line 30 doing?
Dude, you are really good. Congrats.
I don't really understand the difference between overriding and polymorphism. Could you explain please.
This is all i need to review before i go and get my CPA from pearson-vue… got any recommendations for an extremely devoted learner? ill spend 16 hours a day studying.