LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 1 of 27

Basic Syntax and Data Types

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

Coding Techniques

Basic Syntax and Data Types · Study material

Brace initialization catches narrowing

Copy initialization silently truncates; braces turn the same mistake into a compile error. Digit separators (C++14) make long literals readable.

#include <iostream>

int main() {
    double price = 99.75;
    int a = price;                        // silently drops .75 (compilers only warn)
    int b = static_cast<int>(price);      // same result, but clearly intended
    // int c{price};                      // error: narrowing conversion – caught at compile time
    long long big{3'000'000'000};         // digit separators make large literals readable
    std::cout << a << ' ' << b << ' ' << big << '\n';
    return 0;
}

Output

99 99 3000000000

Prefer constexpr for true constants

constexpr int max_students = 60;          // known at compile time: usable as an array size
constexpr double pi = 3.141592653589793;
int seats[max_students]{};                // OK

const int today = read_day();             // const: fixed after initialization, value known at run time
// int list[today];                       // error: not a constant expression

Use constexpr whenever the value is known while compiling; use const for values computed at run time that must not change. Avoid #define for constants – macros have no type and no scope.

Use auto where the type is obvious or long

auto count = 0;                                  // int
auto ratio = 0.75;                               // double
auto it = scores.begin();                        // std::map<std::string, int>::iterator – no need to spell it
auto name = std::string{"Asha"};                 // std::string, not const char*

auto total = 0;                                  // careful: int, even if you meant double
for (double s : prices) total += s;              // fractions are lost

Avoid integer overflow with wider types

int seconds_per_year = 365 * 24 * 60 * 60;            // 31,536,000 – fits in int
int ms_per_year = 365 * 24 * 60 * 60 * 1000;          // overflow! undefined behaviour

long long ms_ok = 365LL * 24 * 60 * 60 * 1000;        // LL makes the whole product long long
auto bytes = std::int64_t{4} * 1024 * 1024 * 1024;    // fixed width from <cstdint>

The type of an expression is decided by its operands, not by the variable it is stored in. Make the first operand wide enough and the rest follow.

Compare floating-point values with a tolerance

#include <cmath>

bool nearly_equal(double a, double b, double eps = 1e-9) {
    return std::fabs(a - b) <= eps * std::fmax(1.0, std::fmax(std::fabs(a), std::fabs(b)));
}

nearly_equal(0.1 + 0.2, 0.3);   // true, although 0.1 + 0.2 == 0.3 is false

Good C++ basics habits

  • Initialize every variable where you declare it.
  • Write std:: explicitly; never put using namespace std; in a header.
  • Use '\n' instead of std::endl unless you need a flush.
  • Use static_cast, never C-style casts.
  • Prefer std::int64_t or long long for values that can exceed 2 billion.
  • Compile with warnings on: -Wall -Wextra.

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