Class 8  >  C++ Programming for Beginners  >  C++ Polymorphism

C++ Polymorphism - Notes | Study C++ Programming for Beginners - Class 8

Table of contents
Polymorphism
1 Crore+ students have signed up on EduRev. Have you?

Polymorphism

Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
For example, think of a base class called Animal that has a method called animalSound(). Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):
Example

// Base class

class Animal {

  public:

    void animalSound() {

    cout << "The animal makes a sound \n" ;

  }

};


// Derived class

class Pig : public Animal {

  public:

    void animalSound() {

    cout << "The pig says: wee wee \n" ;

  }

};


// Derived class

class Dog : public Animal {

  public:

    void animalSound() {

    cout << "The dog says: bow wow \n" ;

  }

};

Remember from the Inheritance chapter that we use the : symbol to inherit from a class.
Now we can create Pig and Dog objects and override the animalSound() method:
Example

// Base class

class Animal {

  public:

    void animalSound() {

    cout << "The animal makes a sound \n" ;

  }

};


// Derived class

class Pig : public Animal {

  public:

    void animalSound() {

    cout << "The pig says: wee wee \n" ;

   }

};


// Derived class

class Dog : public Animal {

  public:

    void animalSound() {

    cout << "The dog says: bow wow \n" ;

  }

};


int main() {

  Animal myAnimal;

  Pig myPig;

  Dog myDog;


  myAnimal.animalSound();

  myPig.animalSound();

  myDog.animalSound();

  return 0;

}

Why And When To Use "Inheritance" and "Polymorphism"?
It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

The document C++ Polymorphism - Notes | Study C++ Programming for Beginners - Class 8 is a part of the Class 8 Course C++ Programming for Beginners.
All you need of Class 8 at this link: Class 8
15 videos|20 docs|13 tests
Download as PDF

Download free EduRev App

Track your progress, build streaks, highlight & save important lessons and more!

Related Searches

Objective type Questions

,

practice quizzes

,

Previous Year Questions with Solutions

,

MCQs

,

shortcuts and tricks

,

mock tests for examination

,

Viva Questions

,

C++ Polymorphism - Notes | Study C++ Programming for Beginners - Class 8

,

past year papers

,

Semester Notes

,

pdf

,

C++ Polymorphism - Notes | Study C++ Programming for Beginners - Class 8

,

Summary

,

ppt

,

Sample Paper

,

Extra Questions

,

Exam

,

video lectures

,

study material

,

Free

,

C++ Polymorphism - Notes | Study C++ Programming for Beginners - Class 8

,

Important questions

;