Polymorphism
class MathOperations { public: int Add(int a, int b) { return a + b; } double Add(double a, double b) { return a + b; } };class Complex { public: Complex operator+(const Complex& other) { Complex result; result.real = this->real + other.real; result.imaginary = this->imaginary + other.imaginary; return result; } };class Animal { public: virtual void MakeSound() { std::cout << "Some generic animal sound." << std::endl; } }; class Dog : public Animal { public: void MakeSound() override { std::cout << "Woof! Woof!" << std::endl; } }; class Cat : public Animal { public: void MakeSound() override { std::cout << "Meow!" << std::endl; } };Animal* myAnimal; myAnimal = new Dog(); myAnimal->MakeSound(); // Calls Dog's MakeSound method. delete myAnimal; myAnimal = new Cat(); myAnimal->MakeSound(); // Calls Cat's MakeSound method. delete myAnimal;
Last updated