C++ Tutorial:  Multiple Inheritance // Giving your classes multiple parents

C++ Tutorial: Multiple Inheritance // Giving your classes multiple parents

Professor Hank Stalica

3 года назад

2,911 Просмотров

I'll show you how to use multiple inheritance to make a C++ class inherit from multiple parents. In C++, we're not limited to a class having a single parent class. You can have 1, 2, 3, 100...doesn't matter. You just need to know the syntax.

This is nice because you can flatten your class hierarchy and gives you a bit more flexibility in how you design your programs without making things overly complicated.

This C++ tutorial for beginners will give you a better understanding of object oriented programming (oop) in C++.

It's my hope this will help you to learn c++.

2:46 ⇒ Code Begins

// Learn More //
Check out my complete C++ videos playlist:
https://www.youtube.com/playlist?list=PLaatXkJEXKyIK736SvHJBY6cc8azGaxXo

Check out my C++ Classes playlist:
https://www.youtube.com/playlist?list=PLaatXkJEXKyKxZrRaut4hSIhx6xmwAnZh

Тэги:

#C++_Tutorial #Professor_Hank_Stalica #C++_Tutorial_Multiple_Inheritance #cpp #visual_studio #c++_classes #classes #programming #object_oriented_programming #inheritance #cpp_tutorials #c++_tutorial_for_beginners #program_examples #oop #learn_to_program #Professor_Hank #multiple_inheritance #inheritence #learn_c++ #c++_lecture #lecture #c++_programming #c++_inheritance #c++_multiple_inheritance
Ссылки и html тэги не поддерживаются


Комментарии:

@anton_98
@anton_98 - 15.07.2022 19:13

Thanks!
You have left some int in the float class though)

Ответить
@watcher8582
@watcher8582 - 03.12.2021 02:06

Thanks, that helped.

Ответить
@gabic8320
@gabic8320 - 06.04.2021 20:00

This tutorial rules. I was having trouble with my homework and this helped me so much because I was having issues with ambiguity.

Ответить
@maxim25o2
@maxim25o2 - 04.12.2020 17:20

Now I figure out why previous post don't work.
struct GAME : public MAP, public PIKA {
void t(char c){
PIKA::turning_side = c;
}
void test() {
PIKA::testi();
}
};
void main(){
GAME game;
game.t('L');
}
Turns out that Creating new parent GAME creates also new child PIKA, and this is separate from main loop. It means that I can't use separately in main PIKA, full it with data and then use in GAME, but I need initiate GAME, and from GAME level initiate all values of PIKA.

Ответить
@maxim25o2
@maxim25o2 - 04.12.2020 16:37

I try use it, but I have error.
struct PIKA{
int x;
int y;
char turning_side;
pair <int, int> pos;
void update_pos(int _x, int _y){
x = _x;
y = _y;
}
void update_pos(pair<int,int> _pos){
x = _pos.first;
y = _pos.second;
}
void update_side(char _c){
turning_side = _c; // this line get char ' L '
}
void testi(){
cerr << turning_side << endl;
}
};

struct GAME : public PIKA {
void test() {
PIKA::testi(); // This line cant display char ' L ' but displays ' ` ' an error.
}
};
And rather get character ' L ' I get ' ` ' Its like Parent class GAME using that function from child, that child don't have that character, but when I call that funcion from PIKA, value is corect. I do something wrong?

Ответить