Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

C++ Polymorphism

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

C++ Polymorphism

Uploaded by

ramukaka777787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1/26/24, 10:23 PM C++ Polymorphism

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

C++ Polymorphism
❮ Previous Next ❯

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() {
https://www.w3schools.com/cpp/cpp_polymorphism.asp 1/6
1/26/24, 10:23 PM C++ Polymorphism

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


 } Tutorials  Exercises  Services   Sign Up Log in
};
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
// 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";

https://www.w3schools.com/cpp/cpp_polymorphism.asp 2/6
1/26/24, 10:23 PM C++ Polymorphism

}
}; Tutorials  Exercises  Services   Sign Up Log in

HTML CSS
 int main() {JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Animal myAnimal;
Pig myPig;
Dog myDog;

myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
return 0;
}

Try it Yourself »

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.

❮ Previous Log in to track progress Next ❯

https://www.w3schools.com/cpp/cpp_polymorphism.asp 3/6
1/26/24, 10:23 PM C++ Polymorphism

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

COLOR PICKER



 SPACES UPGRADE AD-FREE

NEWSLETTER GET CERTIFIED

REPORT ERROR

Top Tutorials
HTML Tutorial
CSS Tutorial

https://www.w3schools.com/cpp/cpp_polymorphism.asp 4/6
1/26/24, 10:23 PM C++ Polymorphism
JavaScript Tutorial

 How To Tutorial
Tutorials 
SQL Tutorial
Exercises  Services   Sign Up Log in
Python Tutorial
HTML
 CSS W3.CSS Tutorial
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by

https://www.w3schools.com/cpp/cpp_polymorphism.asp 5/6
1/26/24, 10:23 PM C++ Polymorphism
W3.CSS.

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

https://www.w3schools.com/cpp/cpp_polymorphism.asp 6/6

You might also like