C++

C++ extends C with object-oriented features, stronger type checks, and libraries for data structures, algorithms, and more. It is used in software ranging from operating systems to games. Programs are saved with a .cpp extension and compiled with compilers like g++. Clear structure in C++ means organizing code into functions and classes with readable naming. This chapter introduces basics such as output with cout, variables, conditions, loops, functions, and ends with a subtraction calculator as the final example.
Hello world
The first program includes iostream for input and output and uses cout.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
Variables and types
C++ supports types such as int, double, char, and string. Use descriptive names to improve clarity.
int age = 15;
double weight = 52.3;
char grade = 'B';
string name = "Alice";
Conditions
C++ conditions use if, else, and switch. Braces { } group instructions clearly.
int score = 85;
if (score >= 50) {
cout << "Pass" << endl;
} else {
cout << "Fail" << endl;
}
Loops
Loops in C++ repeat tasks: for for counters, while for conditions. Keep logic short for clarity.
for (int i = 1; i <= 3; i++) {
cout << "Count " << i << endl;
}
Functions
Functions help organize reusable logic. A return type must always be declared in C++.
int multiply(int x, int y) {
return x * y;
}
Final example — calculator for subtraction
The following C++ program reads two numbers, subtracts them, and prints the difference. Save as main.cpp and compile with g++ main.cpp -o main.
main.cpp
#include <iostream>
using namespace std;
int main() {
double minuend, subtrahend, difference;
cout << "Enter minuend: ";
cin >> minuend;
cout << "Enter subtrahend: ";
cin >> subtrahend;
difference = minuend - subtrahend;
cout << "Difference: " << difference << endl;
return 0;
}
MCQs
1. Which header file is required for input and output in C++?
(a) stdio.h
(b) iostream
(c) conio.h
(d) string.h
► (b) iostream
2. What is the correct file extension for a C++ source file?
(a) .c
(b) .cpp
(c) .ccs
(d) .cp
► (b) .cpp
3. Which operator is used with cout to send data to the output stream?
(a) >>
(b) <<
(c) :
(d) :=
► (b) <<
4. Which C++ statement declares a variable that stores a decimal number?
(a) int x;
(b) float x;
(c) double x;
(d) Both (b) and (c)
► (d) Both (b) and (c)
5. Which statement is true about C++ strings?
(a) They are arrays of characters ending with '\0'
(b) They can be used with the string class in <string>
(c) Both (a) and (b) are correct
(d) None of the above
► (c) Both (a) and (b) are correct
6. What will the condition (7 < 3) evaluate to in C++?
(a) true
(b) false
(c) 1
(d) 0
► (d) 0
7. Which loop in C++ is guaranteed to execute at least once?
(a) for
(b) while
(c) do...while
(d) foreach
► (c) do...while
8. Which operator is used for equality comparison in C++?
(a) =
(b) ==
(c) !=
(d) :=
► (b) ==
9. Which is a valid C++ function definition?
(a) int add(x, y) { return x + y }
(b) int add(int x, int y) { return x + y; }
(c) function add(int x, int y) return x + y;
(d) def add(x, y): return x + y
► (b) int add(int x, int y) { return x + y; }
10. Which statement about C++ arrays is correct?
(a) Array indexes start at 1
(b) Array indexes start at 0
(c) The size of the array can grow automatically
(d) Arrays can only store characters
► (b) Array indexes start at 0
11. What is the correct syntax for including a standard header in C++?
(a) #include <filename>
(b) include "filename"
(c) import filename
(d) #header filename
► (a) #include <filename>
12. Which keyword allows using names from the std namespace directly?
(a) using namespace std;
(b) include std;
(c) import std;
(d) namespace std();
► (a) using namespace std;
13. Which operator is used to access members of a class or struct through an object?
(a) .
(b) ->
(c) ::
(d) *
► (a) .
14. Which operator is used to access members through a pointer to an object?
(a) .
(b) ->
(c) *
(d) ::
► (b) ->
15. Which keyword stops a loop and transfers control after it?
(a) stop
(b) break
(c) exit
(d) continue
► (b) break
16. Which C++ operator creates a new object dynamically?
(a) malloc
(b) allocate
(c) new
(d) create
► (c) new
17. Which keyword is used to define a class in C++?
(a) object
(b) class
(c) defclass
(d) structclass
► (b) class
18. In the subtraction calculator, which variables store the inputs?
(a) x and y
(b) first and second
(c) minuend and subtrahend
(d) num1 and num2
► (c) minuend and subtrahend