๐ Scenario: Imagine you have a base class Animal that can make a sound. Different animals make different sounds. We want to show how a derived class can change the sound by overriding the base class function.
๐ฏ Goal: Build a simple C++ program that demonstrates function overriding by creating a base class Animal with a makeSound() function, and a derived class Dog that overrides makeSound() to show a different sound.
๐ What You'll Learn
Create a base class called Animal with a public function makeSound() that prints "Some generic animal sound".
Create a derived class called Dog that inherits from Animal.
Override the makeSound() function in Dog to print "Woof!".
Create an object of Dog and call its makeSound() function to show the overridden behavior.
๐ก Why This Matters
๐ Real World
Function overriding is used in software to allow different objects to behave differently while sharing the same interface. For example, different animals making different sounds in a zoo simulation.
๐ผ Career
Understanding function overriding is essential for object-oriented programming jobs, especially when working with inheritance and polymorphism in C++.
Progress0 / 4 steps
1
Create the base class Animal with makeSound()
Write a class called Animal with a public function makeSound() that prints exactly "Some generic animal sound" followed by a newline.
C++
Hint
Use class Animal and define void makeSound() inside it. Use std::cout to print the text.
2
Create the derived class Dog that inherits Animal
Add a class called Dog that publicly inherits from Animal. Do not add any functions yet.
C++
Hint
Use class Dog : public Animal to create the derived class.
3
Override makeSound() in Dog to print "Woof!"
Inside the Dog class, override the makeSound() function to print exactly "Woof!" followed by a newline.
C++
Hint
Define void makeSound() inside Dog and print "Woof!" using std::cout.
4
Create Dog object and call makeSound()
In the main() function, create an object called myDog of type Dog and call its makeSound() function.
C++
Hint
Create Dog myDog; and call myDog.makeSound(); inside main().
Practice
(1/5)
1. What is function overriding in C++?
easy
A. A child class provides its own version of a parent class's virtual function.
B. A function with the same name but different parameters in the same class.
C. A function that is called automatically when an object is created.
D. A function that cannot be changed once defined.
Solution
Step 1: Understand function overriding concept
Function overriding means a child class changes the behavior of a function defined in its parent class.
Step 2: Identify correct description
A child class provides its own version of a parent class's virtual function. correctly describes overriding as providing a new version of a parent's virtual function in the child class.
Final Answer:
A child class provides its own version of a parent class's virtual function. -> Option A
Quick Check:
Function overriding = child changes parent's virtual function [OK]
Hint: Overriding means child changes parent's virtual function [OK]
Common Mistakes:
Confusing overriding with overloading
Thinking overriding happens without virtual keyword
Believing overriding applies to non-virtual functions
2. Which of the following is the correct syntax to override a function in C++11 and later?
easy
A. void func() virtual override;
B. override void func();
C. void func() override;
D. virtual void func() override;
Solution
Step 1: Recall override syntax in child class
In C++11+, the child class function uses the same signature and adds the 'override' keyword after the function declaration.
Step 2: Identify correct syntax
virtual void func() override; shows the correct syntax: 'virtual void func() override;'. The 'virtual' keyword can be used in the child class but is optional; however, it is valid and common to include it.
Final Answer:
virtual void func() override; -> Option D
Quick Check:
Child function uses virtual and override keywords correctly [OK]
Hint: Use 'override' after function signature in child class; 'virtual' is optional but allowed [OK]
Common Mistakes:
Adding 'virtual' keyword after function name
Placing 'override' before function name
Omitting 'override' keyword entirely
3. What will be the output of the following code?
class Base {
public:
virtual void show() { std::cout << "Base"; }
};
class Derived : public Base {
public:
void show() override { std::cout << "Derived"; }
};
int main() {
Base* b = new Derived();
b->show();
delete b;
return 0;
}
medium
A. Derived
B. Base
C. Compilation error
D. Runtime error
Solution
Step 1: Understand virtual function behavior
The pointer 'b' is of type Base* but points to a Derived object. Because 'show' is virtual, the Derived version is called.
Hint: Virtual functions call child's version via base pointer [OK]
Common Mistakes:
Expecting base class output
Forgetting to make base function virtual
Confusing pointer type with object type
4. Identify the error in the following code snippet:
class Parent {
public:
void display() { std::cout << "Parent"; }
};
class Child : public Parent {
public:
void display() override { std::cout << "Child"; }
};
medium
A. Child's display() should not use override keyword.
B. Parent's display() must be declared virtual to override.
C. display() must return int, not void.
D. No error, code is correct.
Solution
Step 1: Check virtual keyword in parent
For overriding to work, the base class function must be declared virtual.
Step 2: Analyze override keyword usage
Child uses 'override' but Parent's display() is not virtual, causing a compilation error.
Final Answer:
Parent's display() must be declared virtual to override. -> Option B
Quick Check:
Base function must be virtual for override [OK]
Hint: Base function must be virtual to allow override [OK]
Common Mistakes:
Ignoring virtual keyword in base class
Thinking override works without virtual
Assuming override keyword is optional
5. Given the classes below, what will be the output of obj->print();?
class A {
public:
virtual void print() { std::cout << "A"; }
};
class B : public A {
public:
void print() override { std::cout << "B"; }
};
class C : public B {
public:
void print() override { std::cout << "C"; }
};
int main() {
A* obj = new C();
obj->print();
delete obj;
return 0;
}
hard
A. A
B. B
C. C
D. Compilation error
Solution
Step 1: Understand pointer and object types
Pointer 'obj' is of type A*, but it points to an object of class C.
Step 2: Follow virtual function overriding chain
Since print() is virtual and overridden in B and C, the most derived override (C::print) is called.
Final Answer:
C -> Option C
Quick Check:
Virtual call resolves to most derived override [OK]
Hint: Virtual calls use most derived override at runtime [OK]
Common Mistakes:
Expecting base class output
Confusing pointer type with object type
Ignoring virtual function behavior
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.