C++ Function Overriding

We'll look into function overriding in the C++ programming language in this post. Polymorphism at runtime is supported in C++. ...


We'll look into function overriding in the C++ programming language in this post. Polymorphism at runtime is supported in C++.

When a child class redefines a function that is already defined in its parent class, this is known as function overriding.

It allows the programmer to add functionality to derived classes (even though it is defined in the base class).

Now we'll look at a few examples to help us comprehend the notion of function overriding in C++.

Example-1

In the example code below, I've defined two classes: a base class and a derived class. The base class includes a member function, disp (). The "Derived Class" is a subclass of the "Base Class." The "disp()" function appears in "Base Class" and is then redefined in "Derived Class."

An object of Derived Class, i.e., "d," is created in the main() function. Then we call the disp() function on the derived class object, which invokes the function in the derived class. The base class version is ignored in this case. The output is shown below as – "disp() function in the derived class."

[#include using namespace std; //base class class Base_Class { public: void disp() { cout << "disp() function in base class." << endl; } }; //derived class class Derived_Class:public Base_Class { public: void disp() { cout << "disp() function in derived class" << endl; } }; int main() { Derived_Class d; d.disp(); //disp() function on derived class object return 0; }]

Example-2

In this example, we'll see how to use the derived class object to call the base class version of a function. We use the scope resolution operator [::] in the derived class function definition to call the base class version of the function.

[#include using namespace std; class Base_Class { public: void disp() { cout << "disp() function in base class." << endl; } }; class Derived_Class:public Base_Class { public: void disp() { Base_Class::disp(); //Call the base class version of disp() } }; int main() { Derived_Class d; d.disp(); return 0; }]

Example-3

This is yet another instance of function overriding. In this example, we've created a parent class called Animal, as well as two derived classes called Duck and Dog. The base class, Animal, has a member function called sound ().

We redefined the same function, sound(), in the derived class/child class, Dog, to override its base class definition. Similarly, we have redefined the same function, i.e., sound, in the other derived class, Duck ().

We created the "dog" object of "Dog" and the "duck" object of "Duck" in the main() function. The derived class version of the sound() function will now be called when we call the sound() function for dog and duck. The program's output can be seen in the image below. As a result, we may use function overriding to call the derived class-specific function.

[#include using namespace std; class Animal { public: void sound() { cout << "Animal sound!" << endl; } }; //Derived class – Dog class class Dog: public Animal { public: void sound() { cout << "Dog sound - bark." << endl; } }; //Derived class – Duck class class Duck: public Animal { public: void sound() { cout << "Duck sound - quack." << endl; } }; int main() { Dog dog; Duck duck; dog.sound(); //sound() of dog duck.sound(); //sound() of duck return 0; }]

 

Conclusion

I've covered function overriding in C++ in this article. Runtime polymorphism is supported by the C++ programming language. In C++, function overriding aids in achieving run-time polymorphism. This article discussed the notion of function overriding and how to use it to achieve runtime polymorphism.

COMMENTS

Name

2023,2,Ai,2,AlmaLinux 9,3,Amazon Linux,5,Apache Web Server,1,AppImage,1,Arduino IDE,1,Artificial Intelligence,2,BalenaEtcher,1,Bitcoin,1,Blockchain Data,1,Bookworm,2,Bootable USB,1,C++,1,centos,1,CentOS 8,1,CentOS Stream,1,CMake,1,CockroachDB,2,cuDNN,1,Database Security,1,Debian,2,Debian 10,2,Debian 11,2,Debian 12,9,DNS,1,Docker,1,E-commerce,1,Fail2ban,1,Fedora Linux,1,Firefox 118,1,FreeIPA Server,1,Function,1,Game Projects,1,Git,3,Google PageSpeed,1,How to,5,How to Install,9,HTTPS,1,Introduction,1,Iptables,1,ISO Image,1,KVM,1,Laravel,1,Let's Encrypt SSL,1,Linux,4,Linux 6.4,1,Linux Kernel 6.5,1,Linux Mint,1,Linux Server,1,Linux-Based Systems,1,Mageia 9,1,Magento,1,MariaDB,1,Media Server,1,ModSecurity,1,New Features,1,Nextcloud,2,NGINX,2,Nvidia CUDA,1,odoo,1,Oracles,1,Performance,1,PHP Zip Module,1,pip,1,Plex,1,Port Forwarding,1,postgresql,2,Privacy,1,Programming,1,Pylint,1,python,5,Python 3.10,2,Quantum,1,Quantum Computers,1,Remote Branch,1,Renew,1,RHEL,1,Rocky Linux 9,2,Rufus,1,Shadow Password,1,SQLite,1,SSH,1,SSH key,1,SSH Keys,1,Step-by-Step,4,SuiteCRM,1,SUSE Linux,1,Syslog,1,System,1,Testing,1,Top 10,1,Translation,1,Ubuntu,1,Ubuntu 18.04,1,Ubuntu 20.04,5,Ubuntu 22.10,1,Ubuntu 23.04,1,Ubuntu Server,1,Ubuntu Upgrade,1,unsupported,1,Up-to-Date,1,Upgrade,1,Visual Studio Code,1,Vivaldi 6.2,1,Web 3.0,1,Web Hosting Security,1,Web Security,1,Webmin,1,What's New,1,Windows 11,1,
ltr
item
Linux code EDU: C++ Function Overriding
C++ Function Overriding
https://blogger.googleusercontent.com/img/a/AVvXsEjvsv-WzMn8C-EVT8NUuOCDVwoA35vTHr1DjAaNQ-ButDwVkPa-GInfmYYhogmdZTQsOt7Ib1tTZ9p1-90MhbkLYQ0afVqu1xlNxOz3yF7RwG4tP5VbLfJi2nDnZ7dribKk6K_I1rp6-daj4f_BrkDE9F57UI8Zz8upsb-6Uj1OgciuCdw4vNz1wImJyQ=w640-h398
https://blogger.googleusercontent.com/img/a/AVvXsEjvsv-WzMn8C-EVT8NUuOCDVwoA35vTHr1DjAaNQ-ButDwVkPa-GInfmYYhogmdZTQsOt7Ib1tTZ9p1-90MhbkLYQ0afVqu1xlNxOz3yF7RwG4tP5VbLfJi2nDnZ7dribKk6K_I1rp6-daj4f_BrkDE9F57UI8Zz8upsb-6Uj1OgciuCdw4vNz1wImJyQ=s72-w640-c-h398
Linux code EDU
https://linuxcodeedu.blogspot.com/2021/11/c-function-overriding.html
https://linuxcodeedu.blogspot.com/
https://linuxcodeedu.blogspot.com/
https://linuxcodeedu.blogspot.com/2021/11/c-function-overriding.html
true
6096992636254302192
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content