LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 9 of 27

Classes and Objects

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

Programs & Examples

Classes and Objects · Study material

A Time class that always stays valid

Storing a single number of seconds and normalising it in one private helper means hours, minutes and seconds can never become invalid – adding or subtracting simply wraps around midnight.

#include <format>
#include <iostream>
#include <string>

class Time {
    int total_;                                    // seconds since midnight, always 0..86399
    static int wrap(long long s) { return static_cast<int>(((s % 86400) + 86400) % 86400); }
public:
    Time(int h = 0, int m = 0, int s = 0) : total_(wrap(h * 3600LL + m * 60 + s)) {}
    Time add(int seconds) const { Time t; t.total_ = wrap(total_ + static_cast<long long>(seconds)); return t; }
    std::string str() const { return std::format("{:02}:{:02}:{:02}", total_ / 3600, total_ / 60 % 60, total_ % 60); }
};

int main() {
    Time start(23, 45, 30);
    std::cout << start.str() << " + 20 min = " << start.add(20 * 60).str() << '\n';
    std::cout << Time(0, 0, 5).str() << " - 10 s = " << Time(0, 0, 5).add(-10).str() << '\n';
    return 0;
}

Output

23:45:30 + 20 min = 00:05:30
00:00:05 - 10 s = 23:59:55

A Product class with members defined outside

The Product example in the introduction shows the inside/outside split, a validating setter and default member initializers working together.

Sorting objects with defaulted comparisons

The Version struct above becomes sortable with one line, auto operator<=>(const Version&) const = default;, because std::sort uses operator<.

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