Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual Function #14

Closed
jimin-kiim opened this issue Dec 15, 2022 · 4 comments
Closed

Virtual Function #14

jimin-kiim opened this issue Dec 15, 2022 · 4 comments

Comments

@jimin-kiim
Copy link
Owner

jimin-kiim commented Dec 15, 2022

@jimin-kiim
Copy link
Owner Author

  • the behavior of virtual function is defined within an inheriting class by a function with the same signature
  • without virtual :
    • member function call depends on the class type of the pointer
  • with virtual :
    • member function call depends on the actual object to which it points to

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Dec 15, 2022

#include <iostream> 

class Animal { 
  public: 
    virtual void eat() 
    { 
      std::cout << "I eat like a generic Animal.\n"; 
    } 
}; 

class Wolf : public Animal { 
  public: 
    void eat() { std::cout << "I eat like a wolf!\n"; } 
}; 

class Fish : public Animal { 
  public: 
    void eat() { std::cout << "I eat like a fish!\n"; } 
}; 

class OtherAnimal : public Animal { 
}; 

int main() 
{ 
  Animal *anAnimal[4]; 
  // dynamic binding
  anAnimal[0] = new Animal(); 
  anAnimal[1] = new Wolf(); 
  anAnimal[2] = new Fish(); 
  anAnimal[3] = new OtherAnimal(); 

  for(int i = 0; i < 4; i++) anAnimal[i]->eat();
  return 0; 
} 
  1. member function of the base class is virtual function
  • virtual void eat()
  1. the variable * type of the base class
  • Animal *anAnimal[4];

-> Dynamic Binding works

@jimin-kiim
Copy link
Owner Author

#include <iostream>
using namespace std;

class B
{
public:
    virtual int get_val() { return 0; }; 
};

class D1 : public B
{
public:
    D1(int x_val) : x(x_val){};
    virtual int get_val() { return x; };

private:
    int x;
};

class D2 : public B
{
public:
    D2(int y_val) : y(y_val){};
    virtual int get_val() { return y * y; };

private:
    int y;
};

int main()
{
    const int MAX = 5;
    D1 Zero(0), One(1), Two(2);
    D2 Three(3), Four(4);

    B *B_Array[MAX];
    B_Array[0] = &Zero;
    B_Array[1] = &One;
    B_Array[2] = &Two;
    B_Array[3] = &Three;
    B_Array[4] = &Four;

    for (int i = 0; i < MAX; ++i)
    {
        cout << i << ": " << B_Array[i]->get_val() << endl;
    }

    return 0;
}
  1. member function of the base class is virtual function
    • virtual int get_val() { return 0; };
  2. the variable * type of the base class
    • B *B_Array[MAX];

-> Dynamic Binding works

@jimin-kiim
Copy link
Owner Author

jimin-kiim commented Dec 15, 2022

Pure Virtual Function

  • if a class holds at least one pure virtual function -> abstract class
  • declared with "=0"
  • no implementation in self
  • always defined in a derived class
  • cannot be used as the member function
  • making derived class define and use it is the purpose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant