Hey guys! This tutorial is about polymorphism. It is the third tutorial I have done about object oriented programming. Object oriented programming is a great quality of C++ (and Java). I also have a number of other C++ tutorials. Check them out if you want:
Part 11: Colored Text in your Terminal
Part 16: Binary, Bitwise Operators, and 2^n
Part 19: Object Oriented Programming 1 -- Data Structures
Part 20: Object Oriented Programming 2 -- Classes
In my other tutorial, we learned how to create a class. Polymorphism is the concept of classes that build upon other existing classes.
Take this class:
class Employee
{
protected:
string name;
int id;
public:
void change_name(string);
void change_id(int);
virtual string position() = 0;
void print_position(void)
{
cout << this->position();
}
} ;
We can create another class that continues Employee by placing ": public Employee" after the class declaration:
class Cashier: public Employee
{
string position(void)
{
return "cashier";
}
}
Employee:
Cashier
To create an object from a derived class, simply treat it like any other class object:
Cashier randy;
To call the derived class function, call the function in the parent (Employee) class that calls the derived function:
randy.print_position();
Note: Only Employee and Cashier members can access the members name and id. They were declared protected.
Here is a program I wrote based off of the two programs I made in the previous object oriented programming tutorials:
#include
#include
using namespace std;
class Animal
{
protected:
string name;
int num_legs;
public:
string color;
void change_name(string);
void change_legs(int);
void printA(void);
virtual string speak() = 0;
void talk(void)
{
cout << this->speak();
}
};
void Animal::change_name(string new_name)
{
name = new_name;
}
void Animal::change_legs(int new_num_legs)
{
num_legs = new_num_legs;
}
void Animal::printA()
{
cout << name
<< "s are "
<< color
<< ", and have "
<< num_legs
<< " legs.\n";
}
class Dog: public Animal
{
string speak(void)
{
return "Bark bark!\n";
}
};
class Bird: public Animal
{
string speak(void)
{
return "Chirp chirp!\n";
}
};
class Spider: public Animal
{
string speak(void)
{
return "Spiders don't make noise...\n";
}
};
int main()
{
Dog dog;
Bird bird;
Spider spider;
dog.change_name("Dog");
dog.color = "brown";
dog.change_legs(4);
bird.change_name("Bird");
bird.color = "grey";
bird.change_legs(2);
spider.change_name("Spider");
spider.color = "black";
spider.change_legs(8);
dog.printA();
bird.printA();
spider.printA();
cout << endl;
cout << "Dogs go: "; dog.talk();
cout << "Birds go: "; bird.talk();
cout << "Spiders... "; spider.talk();
return 0;
}
Here is the output:
[cmw4026@omega test]$ g++ poly.cpp
[cmw4026@omega test]$ ./a.out
Dogs are brown, and have 4 legs.
Birds are grey, and have 2 legs.
Spiders are black, and have 8 legs.
Dogs go: Bark bark!
Birds go: Chirp chirp!
Spiders... Spiders don't make noise...
[cmw4026@omega test]$
Note: All Animal objects can access the Animal functions. This includes Dog objects, Bird objects, and Spider objects. We cannot create a regular Animal object; g++ will give an error.
Very large programs will use OOP. When programming large programs in this style, it is good practice to use header files instead of writing the whole program in one file.
I hope this helped! Leave any suggestions in the comments!