Overloading Functions in a Class - C++ Class

C++ examples for Class:Member Function

Description

Overloading Functions in a Class

Demo Code

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

class Cat/*from w w  w  .ja va2 s .  c  o m*/
{
public:
    string name;
};

class Dog
{
public:
    string name;
};

class Human
{
public:
    string name;
};

class Door
{
private:
    int count;

public:
    void Start();
    void print(Cat *acat);
    void print(Dog *adog);
    void print(Human *ahuman);
};

void Door::Start()
{
    count = 0;
}

void Door::print(Cat *v)
{
    cout << "Welcome, " << v->name << endl;
    cout << "A cat just entered!" << endl;
    count++;
}

void Door::print(Dog *v)
{
    cout << "Welcome, " << v->name << endl;
    cout << "A dog just entered!" << endl;
    count++;
}

void Door::print(Human *v)
{
    cout << "Welcome, " << v->name << endl;
    cout << "A human just entered!" << endl;
    count++;
}

int main()
{
    Door entrance;
    entrance.Start();

    Cat *c1 = new Cat;
    c1->name = "cat";

    Dog *d1 = new Dog;
    d1->name = "dig";

    Human *me = new Human;
    me->name = "man";

    entrance.print(c1);
    entrance.print(d1);
    entrance.print(me);

    delete c1;
    delete d1;
    delete me;

    return 0;
}

Result


Related Tutorials