what is the different between using ->out() and sub.out() to call the function out()?
Also, I would like to know why we use -> to call the function out() as well.
thanks!
by the way, the output is "bb" and I know that "virtual" keyword makes the Sup class' out function overridden by its subclass. Please correct me or add more explanations if there is something that I am misunderstanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
class Sup {
public: virtualvoid out() { cout << "p"; }
};
class Sub : public Sup {
public: virtualvoid out() { cout << "b"; }
};
int main()
{
Sub sub;
Sup *sup;
sup = ⊂
sup->out(); // what is the different between using ->out() and sub.out() to call the function out()?
sub.out(); //
return 0;
sup->out() is the same thing as (*sup).out().
It's just syntactical sugar for when you want to call a member function on an object when you just have a pointer to it.