LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 17 of 27

Operator Overloading

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

Programs & Examples

Operator Overloading · Study material

Fibonacci numbers with an overloaded matrix multiplication

Once a 2×2 matrix type has operator*, fast exponentiation works on it like on numbers, giving F(n) in O(log n) steps.

#include <array>
#include <iostream>

struct Mat2 {
    std::array<long, 4> a;                    // [0 1; 2 3]
    Mat2 operator*(const Mat2& o) const {
        return {{a[0] * o.a[0] + a[1] * o.a[2], a[0] * o.a[1] + a[1] * o.a[3],
                 a[2] * o.a[0] + a[3] * o.a[2], a[2] * o.a[1] + a[3] * o.a[3]}};
    }
    bool operator==(const Mat2&) const = default;
};

Mat2 power(Mat2 m, int n) {                   // fast exponentiation, using only operator*
    Mat2 r{{1, 0, 0, 1}};
    while (n) {
        if (n & 1) r = r * m;
        m = m * m;
        n >>= 1;
    }
    return r;
}

int main() {
    Mat2 fib{{1, 1, 1, 0}};
    for (int n : {10, 30, 50}) std::cout << "F(" << n << ") = " << power(fib, n).a[1] << '\n';
    std::cout << (power(fib, 2) == fib * fib) << '\n';
    return 0;
}

Output

F(10) = 55
F(30) = 832040
F(50) = 12586269025
1

A 2D vector type with arithmetic and printing

The first example in the introduction shows the core arithmetic operators, unary minus, equality and stream output for a small value type.

Sorting with the spaceship operator

The comparison example shows a defaulted <=> for version numbers and a custom one that sorts students by marks, then name.

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