Complete the code to declare a class Dog that inherits from Animal.
class Dog : public [1] { };
Inheritance means a class (Dog) gets properties and behaviors from another class (Animal). So Dog inherits from Animal.
Complete the code to call the base class constructor from the derived class Car.
class Vehicle { public: Vehicle(int wheels) {} }; class Car : public Vehicle { public: Car() : [1](4) {} };
The derived class constructor calls the base class constructor by using the base class name (Vehicle) followed by arguments.
Fix the error in the code to correctly override the makeSound method in the derived class Cat.
class Animal { public: virtual void makeSound() { std::cout << "Some sound" << std::endl; } }; class Cat : public Animal { public: [1] void makeSound() override { std::cout << "Meow" << std::endl; } };
To override a virtual function, the derived class method should also be declared virtual (optional but good practice) and match the signature. The keyword 'virtual' before the method helps clarity.
Fill both blanks to create a derived class Bird that inherits publicly from Animal and overrides the fly method.
class Animal { public: virtual void fly() { std::cout << "Cannot fly" << std::endl; } }; class Bird : [1] [2] { public: void fly() override { std::cout << "Flying high" << std::endl; } };
The class Bird inherits publicly from Animal, so use 'public Animal'. This allows Bird to override the fly method.
Fill all three blanks to create a dictionary-like map of animal names to their sound lengths, filtering only those with sound length greater than 3.
std::map<std::string, int> soundLengths = {
{"dog", 3},
{"elephant", 8},
{"cat", 3},
{"bird", 4}
};
std::map<std::string, int> filtered;
for (const auto& [1] : soundLengths) {
if ([2].second [3] 3) {
filtered.insert([2]);
}
}We use a loop variable 'entry' to iterate over the map. We check if entry.second (the sound length) is greater than 3, then insert it into filtered.