🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Polymorphism, a fundamental concept in C++, can be divided into two types: compile-time polymorphism and runtime polymorphism. Compile-time polymorphism is achieved through features like function overloading and templates, while runtime polymorphism is achieved through inheritance and virtual functions.
2. Key Points
1. Compile-time polymorphism is resolved during the compilation of the program.
2. Runtime polymorphism is resolved during the execution of the program.
3. Function overloading and operator overloading are examples of compile-time polymorphism.
4. Virtual functions and dynamic binding are examples of runtime polymorphism.
3. Differences
| Compile-time Polymorphism | Runtime Polymorphism |
|---|---|
| Decided at compile time. | Decided at runtime. |
| Achieved through function overloading and operator overloading. | Achieved through virtual functions and dynamic binding. |
| More efficient, as the decision is made during compilation. | Less efficient compared to compile-time, due to runtime decision making. |
4. Example
#include <iostream>
using namespace std;
// Compile-time polymorphism - Function Overloading
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
// Runtime polymorphism - Virtual Functions
class Base {
public:
virtual void show() { cout << "Base class show" << endl; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived class show" << endl; }
};
int main() {
// Compile-time polymorphism
print(5);
print(5.5);
// Runtime polymorphism
Base *b;
Derived d;
b = &d;
b->show();
return 0;
}
Output:
Printing int: 5 Printing float: 5.5 Derived class show
Explanation:
1. In the function overloading example, print is called with different parameter types, demonstrating compile-time polymorphism.
2. In the virtual functions example, the type of object pointed to by b determines which show method is called, demonstrating runtime polymorphism.
5. When to use?
- Use compile-time polymorphism for efficiency and when the decisions about the executing version of a function can be made at compile time.
- Use runtime polymorphism when you need flexibility and the ability to handle different subclasses dynamically at runtime.
Related C++/CPP Posts:
Difference Between Struct and Class in C++
Difference Between Pointer and Reference in C++
Difference Between null and nullptr in C++
Difference Between Array and Vector in C++
Difference Between const and constexpr in C++
Difference Between List and Vector in C++
Difference Between Function Overloading and Operator Overloading in C++
Difference Between Array and List in C++
Difference Between a While Loop and a Do-While Loop in C++
Difference Between new and malloc C++
Virtual Function vs Pure Virtual Function in C++
Compile Time Polymorphism vs Runtime Polymorphism in C++
Difference Between Shallow Copy and Deep Copy in C++
Difference Between Stack and Heap in C++
Copy Constructor vs Parameterized Constructor in C++
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment