Runtime polymorphism in C++ is a concept that allows a function to behave
differently based on the object that invokes it. It is achieved through inheritance
and function overriding, and it is implemented using pointers or references to base
class objects. This is a key feature of object-oriented programming that enables
dynamic method dispatch.
Key Components of Runtime Polymorphism:
Inheritance: A base class and one or more derived classes.
Virtual Functions: Functions in the base class that are declared with the virtual
keyword and overridden in derived classes.
Base Class Pointer/Reference: Used to call the overridden function at runtime.
Example Code:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
virtual void sound() { // Virtual function
cout << "Animal makes a sound" << endl;
}
virtual ~Animal() {} // Virtual destructor for proper cleanup
};
// Derived class 1
class Dog : public Animal {
public:
void sound() override { // Overriding the base class function
cout << "Dog barks" << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void sound() override { // Overriding the base class function
cout << "Cat meows" << endl;
}
};
int main() {
Animal* animal; // Base class pointer
Dog dog;
Cat cat;
animal = &dog;
animal->sound(); // Calls Dog's sound() at runtime
animal = &cat;
animal->sound(); // Calls Cat's sound() at runtime
return 0;
}
Explanation:
Virtual Function: The sound() function in the Animal class is declared as virtual.
This allows derived classes to override it.
Dynamic Dispatch: When the base class pointer (animal) points to a derived class
object (dog or cat), the appropriate overridden function is called at runtime.
Polymorphism: The same function call (animal->sound()) behaves differently
depending on the actual object type.
Output:
Dog barks
Cat meows
Benefits of Runtime Polymorphism:
Flexibility: Enables writing more generic and reusable code.
Extensibility: New derived classes can be added without modifying existing code.
Dynamic Behavior: Decisions are made at runtime, allowing for more dynamic and
adaptable programs.
This is a powerful feature in C++ that supports the principles of abstraction and
encapsulation in object-oriented programming.
In C++, a base class is a class that serves as a foundation for other classes,
known as derived classes. It allows you to implement inheritance, enabling code
reuse and the creation of hierarchical relationships between classes.
Here’s a concise explanation and example:
Key Points:
Definition: A base class provides common functionality that derived classes can
inherit and extend.
Access Specifiers: Members of the base class can be accessed in derived classes
based on their access specifiers:
Public: Accessible in derived classes and outside the class.
Protected: Accessible in derived classes but not outside the class.
Private: Not accessible in derived classes.
Inheritance Syntax: class Derived : access_specifier Base { ... };
Example Code:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from the base class
myDog.bark(); // Defined in the derived class
return 0;
}
Output:
This animal eats food.
The dog barks.
Notes:
Polymorphism: If the base class has virtual functions, derived classes can override
them to provide specific behavior.
Multiple Inheritance: C++ allows a class to inherit from multiple base classes, but
it should be used carefully to avoid ambiguity.
Let me know if you'd like further clarification or examples! 😊
#include <iostream.h>
#include <conio.h>
// Function with default arguments
int add(int a, int b = 10, int c = 20) {
return a + b + c;
}
void main() {
clrscr(); // Clear screen (specific to Turbo C++)
cout << "Case 1: Calling add(5): " << add(5) << endl; // Only one argument
provided
cout << "Case 2: Calling add(5, 15): " << add(5, 15) << endl; // Two arguments
provided
cout << "Case 3: Calling add(5, 15, 25): " << add(5, 15, 25) << endl; // All
arguments provided
getch(); // Wait for user input (specific to Turbo C++)
}
In C++, default arguments allow you to specify default values for function
parameters. If a caller omits these arguments when calling the function, the
default values are used. This feature makes functions more flexible and reduces the
need for function overloading in some cases.
Key Points:
Default arguments are specified in the function declaration or prototype.
They must be provided from right to left (i.e., trailing parameters can have
defaults, but leading ones cannot unless all subsequent parameters also have
defaults).
If a default argument is provided in both the declaration and definition, the one
in the declaration is used.
return_type name (p1= v1, p2= v2, ...);
In C++, We can have more than one constructor in a class with same name, as long as
each has a different list of arguments.This concept is known as Constructor
Overloading and is quite similar to function overloading.
Overloaded constructors essentially have the same name (exact name of the class)
and different by number and type of arguments.
A constructor is called depending upon the number and type of arguments passed.
While creating the object, arguments must be passed to let compiler know, which
constructor needs to be called.
// C++ program to illustrate
// Constructor overloading
#include <iostream>
using namespace std;
class construct
{
public:
float area;
// Constructor with no parameters
construct()
{
area = 0;
}
// Constructor with two parameters
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
// of class name
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 1;
c58b2bca47e211c57a39b277000f1342 leo
e82f527fa2ee67f694f473dc050043f2 jn
n0 pass nithish222J 537413d2c572a51935ea412211bbb931
nith pass nithish222J 722d23228f60f15c5cd9abe65592fe6f