Bird
Raised Fist0
C++programming~5 mins

OOP principles overview in C++ - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: OOP principles overview
O(n)
Understanding Time Complexity

When we use object-oriented programming (OOP), we organize code into objects and classes. It is important to understand how the time it takes to run our programs grows as we use these OOP ideas.

We want to know how the main actions in OOP affect the program's speed as the program gets bigger.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


class Box {
public:
    int length, width, height;
    Box(int l, int w, int h) : length(l), width(w), height(h) {}
    int volume() {
        return length * width * height;
    }
};

void printVolumes(Box boxes[], int n) {
    for (int i = 0; i < n; i++) {
        std::cout << boxes[i].volume() << std::endl;
    }
}
    

This code defines a Box class with a method to calculate volume. Then it prints the volume of each box in an array.

Identify Repeating Operations
  • Primary operation: Looping through each box and calling the volume method.
  • How many times: The loop runs once for each box, so n times.
How Execution Grows With Input

As the number of boxes increases, the total work grows in a straight line with the number of boxes.

Input Size (n)Approx. Operations
10About 10 volume calculations and prints
100About 100 volume calculations and prints
1000About 1000 volume calculations and prints

Pattern observation: Doubling the number of boxes doubles the work done.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows directly with the number of boxes we process.

Common Mistake

[X] Wrong: "Calling a method inside a loop makes the program slower in a way that changes the overall time complexity."

[OK] Correct: Each method call here does a simple calculation, so it only adds a small fixed amount of work per item. The overall time still grows linearly with the number of items.

Interview Connect

Understanding how loops and method calls affect time helps you explain your code clearly and shows you know how programs scale. This skill is useful when discussing your designs and choices.

Self-Check

"What if the volume method itself contained a loop over some internal data? How would the time complexity change?"

Practice

(1/5)
1. Which of the following is NOT one of the four main principles of Object-Oriented Programming (OOP)?
easy
A. Encapsulation
B. Compilation
C. Polymorphism
D. Inheritance

Solution

  1. Step 1: Recall the four main OOP principles

    The four main principles are encapsulation, inheritance, polymorphism, and abstraction.
  2. Step 2: Identify the option not in the list

    Compilation is a process of converting code, not an OOP principle.
  3. Final Answer:

    Compilation -> Option B
  4. Quick Check:

    OOP principles exclude compilation [OK]
Hint: Remember the four OOP pillars: E, I, P, A [OK]
Common Mistakes:
  • Confusing compilation with abstraction
  • Mixing OOP principles with programming processes
2. Which keyword is used in C++ to create a new class?
easy
A. class
B. struct
C. object
D. define

Solution

  1. Step 1: Identify the C++ keyword for defining a class

    In C++, the keyword to define a class is class.
  2. Step 2: Check other options

    struct defines a structure, object is not a keyword, and define is for macros.
  3. Final Answer:

    class -> Option A
  4. Quick Check:

    Use class to define classes [OK]
Hint: Use 'class' keyword to define classes in C++ [OK]
Common Mistakes:
  • Confusing struct with class
  • Using 'object' as a keyword
  • Mixing macros with class definition
3. What will be the output of the following C++ code?
class Animal {
public:
    void sound() { std::cout << "Animal sound"; }
};

class Dog : public Animal {
public:
    void sound() { std::cout << "Bark"; }
};

int main() {
    Animal* a = new Dog();
    a->sound();
    delete a;
    return 0;
}
medium
A. Runtime error
B. Bark
C. Compilation error
D. Animal sound

Solution

  1. Step 1: Understand method call without virtual keyword

    The sound method is not virtual, so the pointer type Animal* calls Animal's method.
  2. Step 2: Determine output

    Since sound is not virtual, Animal sound is printed.
  3. Final Answer:

    Animal sound -> Option D
  4. Quick Check:

    Non-virtual method calls base class method [OK]
Hint: Without virtual, base class method runs [OK]
Common Mistakes:
  • Assuming polymorphism without virtual
  • Expecting derived class method call
  • Ignoring pointer type effect
4. Identify the error in the following C++ code snippet:
class Vehicle {
private:
    int speed;
public:
    void setSpeed(int s) { speed = s; }
};

int main() {
    Vehicle v;
    v.speed = 100;
    return 0;
}
medium
A. setSpeed method is not defined
B. Missing semicolon after class definition
C. Cannot access private member 'speed' directly
D. Vehicle object not created properly

Solution

  1. Step 1: Check access to private member

    The member speed is private and cannot be accessed directly outside the class.
  2. Step 2: Identify the error in main()

    Trying to assign v.speed = 100; causes an access violation error.
  3. Final Answer:

    Cannot access private member 'speed' directly -> Option C
  4. Quick Check:

    Private members need public methods for access [OK]
Hint: Private members can't be accessed outside class [OK]
Common Mistakes:
  • Trying to access private variables directly
  • Ignoring access specifiers
  • Assuming public by default
5. You want to design a C++ class hierarchy where a base class Shape defines a method area() but each derived class calculates area differently. Which OOP principle does this best demonstrate?
hard
A. Polymorphism
B. Inheritance
C. Encapsulation
D. Abstraction

Solution

  1. Step 1: Understand the scenario of different area calculations

    Each derived class implements area() differently, but shares the same interface.
  2. Step 2: Identify the OOP principle

    This is polymorphism: same method name, different behaviors in subclasses.
  3. Final Answer:

    Polymorphism -> Option A
  4. Quick Check:

    Same method, different behaviors = Polymorphism [OK]
Hint: Different behaviors, same method name = polymorphism [OK]
Common Mistakes:
  • Confusing with inheritance or abstraction
  • Thinking encapsulation means method overriding
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard