๐ Scenario: Imagine you are creating a simple program to manage different types of vehicles. Each vehicle can start and stop, but the way they start might be different. We will use abstract classes to define a general vehicle and then create specific types of vehicles.
๐ฏ Goal: Build a C++ program that uses an abstract class called Vehicle with a pure virtual function start(). Then create two classes, Car and Bike, that inherit from Vehicle and implement the start() method. Finally, create objects of Car and Bike and call their start() methods.
๐ What You'll Learn
Create an abstract class Vehicle with a pure virtual function start()
Create a class Car that inherits from Vehicle and implements start()
Create a class Bike that inherits from Vehicle and implements start()
Create objects of Car and Bike
Call the start() method on both objects and print the output
๐ก Why This Matters
๐ Real World
Abstract classes are used in software design to define common interfaces for different objects, like vehicles, shapes, or devices, ensuring they share certain behaviors.
๐ผ Career
Understanding abstract classes is important for designing flexible and maintainable code in many programming jobs, especially in object-oriented programming.
Progress0 / 4 steps
1
Create the abstract class Vehicle
Write an abstract class called Vehicle with a pure virtual function start() that returns void.
C++
Hint
Use virtual void start() = 0; inside the class to make it abstract.
2
Create the Car class inheriting Vehicle
Create a class called Car that inherits from Vehicle. Implement the start() method to print "Car started".
C++
Hint
Use class Car : public Vehicle and implement start() with std::cout.
3
Create the Bike class inheriting Vehicle
Create a class called Bike that inherits from Vehicle. Implement the start() method to print "Bike started".
C++
Hint
Similar to Car, create Bike class and implement start().
4
Create objects and call start()
In the main() function, create an object myCar of type Car and an object myBike of type Bike. Call start() on both objects to print their start messages.
C++
Hint
Create objects myCar and myBike and call start() on each.
Practice
(1/5)
1. What is true about an abstract class in C++?
easy
A. It can be instantiated like any other class.
B. It cannot be instantiated directly.
C. It must have no member functions.
D. It cannot have any data members.
Solution
Step 1: Understand abstract class instantiation rules
Abstract classes have at least one pure virtual function, so they cannot create objects directly.
Step 2: Check other options
The other options are incorrect because abstract classes can have member functions and data members.
Final Answer:
It cannot be instantiated directly. -> Option B
Quick Check:
Abstract class = no direct objects [OK]
Hint: Abstract classes always have pure virtual functions [OK]
Common Mistakes:
Thinking abstract classes can create objects
Believing abstract classes cannot have data members
Confusing abstract classes with interfaces
2. Which of the following is the correct way to declare a pure virtual function in a C++ abstract class?
easy
A. virtual void func() = 0;
B. void func() = 0;
C. virtual void func();
D. void func() {};
Solution
Step 1: Recall pure virtual function syntax
Pure virtual functions are declared with 'virtual' keyword and '= 0' at the end.
Step 2: Analyze options
virtual void func() = 0; matches the correct syntax. void func() = 0; misses 'virtual'. virtual void func(); is a normal virtual function. void func() {}; is a normal function with a body.
Final Answer:
virtual void func() = 0; -> Option A
Quick Check:
Pure virtual = virtual + '= 0' [OK]
Hint: Pure virtual always ends with '= 0;' [OK]
Common Mistakes:
Omitting 'virtual' keyword
Adding function body to pure virtual
Confusing normal virtual with pure virtual
3. What will be the output of the following C++ code?
#include <iostream>
class Base {
public:
virtual void show() = 0;
};
class Derived : public Base {
public:
void show() { std::cout << "Hello"; }
};
int main() {
Derived d;
d.show();
return 0;
}
medium
A. No output
B. Compilation error
C. Hello
D. Runtime error
Solution
Step 1: Check abstract class and derived implementation
Base has a pure virtual function 'show'. Derived overrides it with a concrete implementation.
Step 2: Analyze main function output
Derived object 'd' calls 'show()', which prints "Hello".
Final Answer:
Hello -> Option C
Quick Check:
Derived implements pure virtual, so prints output [OK]
Hint: Derived must implement pure virtual to avoid errors [OK]
Common Mistakes:
Expecting compilation error without implementation
Thinking pure virtual functions run by themselves
Confusing output with no output
4. Identify the error in this code snippet:
#include <iostream>
class Abstract {
public:
virtual void display() = 0;
};
class Concrete : public Abstract {
public:
void display() { std::cout << "Hi"; }
};
int main() {
Abstract a;
Concrete c;
c.display();
return 0;
}
medium
A. Cannot instantiate abstract class 'Abstract' directly.
B. Missing implementation of display() in Concrete.
C. Syntax error in class declaration.
D. No error, code runs fine.
Solution
Step 1: Check instantiation of abstract class
Abstract class 'Abstract' has a pure virtual function, so cannot create object 'a'.
Step 2: Verify other parts
Concrete implements display(), so no error there. Syntax is correct.
Final Answer:
Cannot instantiate abstract class 'Abstract' directly. -> Option A
Quick Check:
Abstract objects not allowed [OK]
Hint: Never create objects of abstract classes [OK]
Common Mistakes:
Trying to instantiate abstract class
Ignoring pure virtual function rules
Assuming implementation missing in derived
5. You want to design a shape system where every shape must provide its area calculation. Which is the best way to enforce this using abstract classes in C++?
hard
A. Create a class Shape with no functions and add area() only in derived classes.
B. Create a class Shape with a normal function double area() that returns 0, and override it in derived classes.
C. Create a class Shape with a virtual function double area() with empty body.
D. Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area().
Solution
Step 1: Understand requirement for enforcing area() implementation
Using a pure virtual function in an abstract class forces all derived classes to implement area().
Step 2: Evaluate options
Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area(). uses pure virtual function, which is the best way. Create a class Shape with a normal function double area() that returns 0, and override it in derived classes. returns 0 by default, which may hide missing implementations. Create a class Shape with no functions and add area() only in derived classes. does not enforce area() in base. Create a class Shape with a virtual function double area() with empty body. has virtual but not pure, so no enforcement.
Final Answer:
Create an abstract class Shape with a pure virtual function double area() = 0; and derive specific shapes implementing area(). -> Option D
Quick Check:
Pure virtual enforces implementation [OK]
Hint: Use pure virtual to force derived class methods [OK]
Common Mistakes:
Using normal virtual instead of pure virtual
Not enforcing method in base class
Returning default values hiding missing overrides
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.