LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 15 of 27

Abstraction

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

Coding Techniques

Abstraction · Study material

Strong types make interfaces hard to misuse

Instead of passing raw doubles that could mean anything, wrap each meaning in its own small type. The abstraction costs nothing at run time, and swapped arguments become compile errors.

#include <iostream>

// Hide "a double that means metres" behind a type: mixing units becomes a compile error
class Metres {
    double v_;
public:
    explicit constexpr Metres(double v) : v_(v) {}
    constexpr double value() const { return v_; }
    constexpr Metres operator+(Metres o) const { return Metres(v_ + o.v_); }
};
class Seconds {
    double v_;
public:
    explicit constexpr Seconds(double v) : v_(v) {}
    constexpr double value() const { return v_; }
};

constexpr double speed(Metres d, Seconds t) { return d.value() / t.value(); }

int main() {
    Metres run = Metres(400) + Metres(100);
    std::cout << speed(run, Seconds(125)) << " m/s\n";
    // speed(Seconds(125), run);   error: arguments in the wrong order no longer compile
    return 0;
}

Output

4 m/s

Expose operations, not data

// ✘ Leaky: users must know the balance is a double and maintain the rules themselves
class Account {
public:
    double balance;            // anyone can set it to -5000
};

// ✔ Abstract: users ask for operations; the class keeps its own rules
class Account {
public:
    void deposit(long paise);
    bool withdraw(long paise);  // refuses if it would go below zero
    long balance() const;       // read-only view
private:
    long paise_ = 0;            // representation can change (paise, decimal type …)
};

Split a class into header and source files

// temperature.h – the interface: included by users
#pragma once
class Temperature {
public:
    static Temperature celsius(double c);
    double fahrenheit() const;
private:
    explicit Temperature(double k) : kelvin_(k) {}
    double kelvin_;
};

// temperature.cpp – the implementation: compiled once, can change freely
#include "temperature.h"
Temperature Temperature::celsius(double c) { return Temperature(c + 273.15); }
double Temperature::fahrenheit() const { return (kelvin_ - 273.15) * 9 / 5 + 32; }

Signs of a good abstraction

  • Its name says what it is; its functions say what they do.
  • It has a small public interface with clear contracts.
  • Users never need to know the data structure inside.
  • Invalid states cannot be created through the interface.
  • The implementation can be replaced without touching user code.
  • It does one thing (single responsibility).

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