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
Definitely check these two tutorials out before moving on:
Part 19: Object Oriented Programming 1 -- Data Structures
Part 20: Object Oriented Programming 2 -- Classes
Declaring a Derived Class
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();
}
} ;
- remember, protected members can only be accessed by other Employee members, or members of any class that continues Employee
- I will explain position() and print_position() after we look at the next class
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:
- virtual string position() = 0;
- this declares a virtual member called position. This means that is can be redefined in a derived class.
- set it equal to 0 for now
- print_position()
- "this" - a pointer to the object's own address
- "->" - shorthand for dereferencing, then accessing a member
- if you are still confused by "this->", it is just a way to call the following function (in this case, position() )
- print the returned value of position()
Cashier
- redefine position()
- position() returns "cashier"
Calling a Function From a Derived Class
To create an object from a derived class, simply treat it like any other class object:
Cashier randy;
- this creates a cashier object called randy
To call the derived class function, call the function in the parent (Employee) class that calls the derived function:
randy.print_position();
- this calls the Employee member, print_position()
- print_position() calls position() in the Cashier class
- position() returns the string "cashier"
- print_position() prints "cashier"
- print_position() calls position() in the Cashier class
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<iostream>
#include<string>
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;
}
- Animal --> a class that describes an animal
- name --> a protected string that holds the name of an Animal object
- num_legs --> a protected integer that holds the number of legs an Animal object has
- color --> a public string that holds the color of an Animal object
- change_name() --> a public function used to access and change the member name of an Animal object
- change_legs() --> a public function used to access and change the member num_legs of an Animal object
- printA() --> a public function used to print the attributes of an Animal object
- speak() --> a public function to be later defined in derived classes
- talk() --> the public function that will call speak() for each derived class
- call speak() and print the returned value
- change_name() --> an Animal function that changes the member name of an Animal object
- change_legs() --> an Animal function that changes the member num_legs of an Animal object
- printA() --> an Animal function that prints attributes of an Animal object
- Dog --> a class derived from Animal
- speak() is redefined for Dog
- return "Bark bark!\n"
- speak() is redefined for Dog
- Bird --> a class derived from Animal
- speak() is redefined for Bird
- return "Chirp chirp!\n"
- speak() is redefined for Bird
- Spider --> a class derived from Animal
- speak() is redefined for Spider
- return "Spiders don't make noise...\n"
- speak() is redefined for Spider
- main()
- create a Dog object called dog
- create a Bird object called bird
- create a Spider object called spider
- set all of the attributes for each object
- call printA() for each object
- print "Dogs go: "
- dog.talk()
- this calls the Animal function talk()
- talk() calls the speak() function for Dog
- speak() returns "Bark bark!\n"
- talk() prints "Bark bark!\n"
- talk() calls the speak() function for Dog
- this calls the Animal function talk()
- print "Birds go: "
- bird.talk()
- this calls the Animal function talk()
- talk() calls the speak() function for Bird
- speak() returns "Chirp chirp!\n"
- talk() prints "Chirp chirp!\n"
- talk() calls the speak() function for Bird
- this calls the Animal function talk()
- print "Spiders... "
- spider.talk()
- this calls the Animal function talk()
- talk() calls the speak() function for Spider
- speak() returns "Spiders don't make noise...\n"
- talk() prints "Spiders don't make noise...\n"
- talk() calls the speak() function for Spider
- this calls the Animal function talk()
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!