15. Polymorphism-Virtual-Functions-Abstracts-Classes
15 Sep 2019 | C++
Virtual
Exercise
Main.cpp
#include <iostream>
#include "farm.h"
using namespace std;
/* polymorphism, virtual functions, abstract class */
void voiceOfAnimal(Animal *);
void voiceOfAnimal(Animal &);
int main()
{
Dog dog;
Cat cat;
Cow cow;
/*
cout << animal.getVoice() << endl;
cout << dog.getVoice() << endl;
cout << cat.getVoice() << endl;
cout << cow.getVoice() << endl;
*/
voiceOfAnimal(&cow); // why we use & because it is for class usually. so it took as reference
return 0;
}
void voiceOfAnimal(Animal *p)
{
cout << p->getVoice() << endl;
}
void voiceOfAnimal(Animal &p)
{
cout << p.getVoice() << endl;
}
farm.h
#ifndef FARM_H_INCLUDED
#define FARM_H_INCLUDED
#include <iostream>
using namespace std;
class Animal
{
protected:
string voice;
public:
Animal();
~Animal();
virtual string getVoice() = 0; // 가상함수로 선언하여 유도 클래스 Getvoice를 쓸 수 있게 함
};
class Dog : public Animal
{
string sign;
public:
Dog();
~Dog();
string getVoice() { return voice + " " + sign; }
};
class Cat : public Animal
{
string sign;
public:
Cat();
~Cat();
string getVoice() { return voice + " " + sign; }
};
class Cow : public Animal
{
string sign;
public:
Cow();
~Cow();
string getVoice() { return voice + " " + sign; }
};
#endif // FARM_H_INCLUDED
farm.cpp
#include "farm.h"
Animal::~Animal()
{
}
Dog::Dog()
{
sign = "D";
voice = "hau";
}
Dog::~Dog()
{
}
Cat::Cat()
{
sign = "C";
voice = "meow";
}
Cat::~Cat()
{
}
Cow::Cow()
{
sign = "CO";
voice = "moooo";
}
Cow::~Cow()
{
}
Virtual destructor
- First of all, it call Dog Class as sub-class within Dynamic
- then it call Animal Class as Base-Class within pointer. because of it instance called by dynamic
main.cpp
#include <iostream>
#include "farm.h"
using namespace std;
/* virtual destructor */
void voiceOfAnimal(Animal *);
void voiceOfAnimal(Animal &);
int main()
{
/*
Dog dog;
Cat cat;
Cow cow;
cout << animal.getVoice() << endl;
cout << dog.getVoice() << endl;
cout << cat.getVoice() << endl;
cout << cow.getVoice() << endl;
voiceOfAnimal(&cow);
*/
Animal *dog = new Dog;
delete dog;
return 0;
}
void voiceOfAnimal(Animal *p)
{
cout << p->getVoice() << endl;
}
void voiceOfAnimal(Animal &p)
{
cout << p.getVoice() << endl;
}
farm.h
#ifndef FARM_H_INCLUDED
#define FARM_H_INCLUDED
#include <iostream>
using namespace std;
class Animal
{
protected:
string voice;
public:
Animal();
virtual ~Animal();
virtual string getVoice() = 0;
};
class Dog : public Animal
{
string sign;
public:
Dog();
~Dog();
string getVoice() { return voice + " " + sign; }
};
class Cat : public Animal
{
string sign;
public:
Cat();
~Cat();
string getVoice() { return voice + " " + sign; }
};
class Cow : public Animal
{
string sign;
public:
Cow();
~Cow();
string getVoice() { return voice + " " + sign; }
};
#endif // FARM_H_INCLUDED
farm.cpp
#include "farm.h"
Animal::Animal()
{
}
Animal::~Animal()
{
cout << "im from the base class " << endl;
}
Dog::Dog()
{
sign = "D";
voice = "hau";
}
Dog::~Dog()
{
cout << "im from the sub-class " << endl;
}
Cat::Cat()
{
sign = "C";
voice = "meow";
}
Cat::~Cat()
{
cout << "im from the sub-class " << endl;
}
Cow::Cow()
{
sign = "CO";
voice = "moooo";
}
Cow::~Cow()
{
cout << "im from the sub-class " << endl;
}
Virtual
Exercise
Main.cpp
#include <iostream>
#include "farm.h"
using namespace std;
/* polymorphism, virtual functions, abstract class */
void voiceOfAnimal(Animal *);
void voiceOfAnimal(Animal &);
int main()
{
Dog dog;
Cat cat;
Cow cow;
/*
cout << animal.getVoice() << endl;
cout << dog.getVoice() << endl;
cout << cat.getVoice() << endl;
cout << cow.getVoice() << endl;
*/
voiceOfAnimal(&cow); // why we use & because it is for class usually. so it took as reference
return 0;
}
void voiceOfAnimal(Animal *p)
{
cout << p->getVoice() << endl;
}
void voiceOfAnimal(Animal &p)
{
cout << p.getVoice() << endl;
}
farm.h
#ifndef FARM_H_INCLUDED
#define FARM_H_INCLUDED
#include <iostream>
using namespace std;
class Animal
{
protected:
string voice;
public:
Animal();
~Animal();
virtual string getVoice() = 0; // 가상함수로 선언하여 유도 클래스 Getvoice를 쓸 수 있게 함
};
class Dog : public Animal
{
string sign;
public:
Dog();
~Dog();
string getVoice() { return voice + " " + sign; }
};
class Cat : public Animal
{
string sign;
public:
Cat();
~Cat();
string getVoice() { return voice + " " + sign; }
};
class Cow : public Animal
{
string sign;
public:
Cow();
~Cow();
string getVoice() { return voice + " " + sign; }
};
#endif // FARM_H_INCLUDED
farm.cpp
#include "farm.h"
Animal::~Animal()
{
}
Dog::Dog()
{
sign = "D";
voice = "hau";
}
Dog::~Dog()
{
}
Cat::Cat()
{
sign = "C";
voice = "meow";
}
Cat::~Cat()
{
}
Cow::Cow()
{
sign = "CO";
voice = "moooo";
}
Cow::~Cow()
{
}
Virtual destructor
- First of all, it call Dog Class as sub-class within Dynamic
- then it call Animal Class as Base-Class within pointer. because of it instance called by dynamic
main.cpp
#include <iostream>
#include "farm.h"
using namespace std;
/* virtual destructor */
void voiceOfAnimal(Animal *);
void voiceOfAnimal(Animal &);
int main()
{
/*
Dog dog;
Cat cat;
Cow cow;
cout << animal.getVoice() << endl;
cout << dog.getVoice() << endl;
cout << cat.getVoice() << endl;
cout << cow.getVoice() << endl;
voiceOfAnimal(&cow);
*/
Animal *dog = new Dog;
delete dog;
return 0;
}
void voiceOfAnimal(Animal *p)
{
cout << p->getVoice() << endl;
}
void voiceOfAnimal(Animal &p)
{
cout << p.getVoice() << endl;
}
farm.h
#ifndef FARM_H_INCLUDED
#define FARM_H_INCLUDED
#include <iostream>
using namespace std;
class Animal
{
protected:
string voice;
public:
Animal();
virtual ~Animal();
virtual string getVoice() = 0;
};
class Dog : public Animal
{
string sign;
public:
Dog();
~Dog();
string getVoice() { return voice + " " + sign; }
};
class Cat : public Animal
{
string sign;
public:
Cat();
~Cat();
string getVoice() { return voice + " " + sign; }
};
class Cow : public Animal
{
string sign;
public:
Cow();
~Cow();
string getVoice() { return voice + " " + sign; }
};
#endif // FARM_H_INCLUDED
farm.cpp
#include "farm.h"
Animal::Animal()
{
}
Animal::~Animal()
{
cout << "im from the base class " << endl;
}
Dog::Dog()
{
sign = "D";
voice = "hau";
}
Dog::~Dog()
{
cout << "im from the sub-class " << endl;
}
Cat::Cat()
{
sign = "C";
voice = "meow";
}
Cat::~Cat()
{
cout << "im from the sub-class " << endl;
}
Cow::Cow()
{
sign = "CO";
voice = "moooo";
}
Cow::~Cow()
{
cout << "im from the sub-class " << endl;
}
Comments