LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 15 of 27

Abstraction

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Abstraction · Study material

One queue abstraction, two implementations

A linked-list queue and a fixed-size ring buffer offer exactly the same push/pop/empty interface, so the same template function drives both and prints the same result.

#include <array>
#include <iostream>
#include <list>

// The same queue abstraction, two implementations: callers cannot tell them apart
template <typename T>
class ListQueue {
    std::list<T> d_;
public:
    void push(const T& x) { d_.push_back(x); }
    T pop() { T x = d_.front(); d_.pop_front(); return x; }
    bool empty() const { return d_.empty(); }
};

template <typename T, std::size_t N>
class RingQueue {
    std::array<T, N> d_{};
    std::size_t head_ = 0, count_ = 0;
public:
    void push(const T& x) { d_[(head_ + count_++) % N] = x; }
    T pop() { T x = d_[head_]; head_ = (head_ + 1) % N; --count_; return x; }
    bool empty() const { return count_ == 0; }
};

template <typename Q>
void demo(Q q) {
    for (int i = 1; i <= 3; ++i) q.push(i * 10);
    std::cout << q.pop() << ' ';
    q.push(40);
    while (!q.empty()) std::cout << q.pop() << ' ';
    std::cout << '\n';
}

int main() {
    demo(ListQueue<int>{});
    demo(RingQueue<int, 3>{});
    return 0;
}

Output

10 20 30 40
10 20 30 40

A stack ADT that protects its own rules

The first example in the introduction shows an abstract data type: users see stack operations only, and the class refuses illegal operations.

Layered functions that read like the requirement

The report example in the introduction builds a readable high-level function from small, single-purpose functions over standard algorithms.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links