C++ Class Member Function Overloading

Description

C++ Class Member Function Overloading

#include <iostream>

using namespace std;

class Cat//  ww  w .  java 2s  .  co 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;
}



PreviousNext

Related