Syntax Recap — C++
7 topic(s) — definitions, syntax, and examples.
Definition
A Turbo C++ program is a collection of functions and preprocessor directives. Execution begins at void main() or int main(). Header files like <iostream.h> and <conio.h> supply built-in declarations.
Purpose
Provides the foundational skeleton required by the Turbo C++ compiler for console screen clearing, input/output streaming, and pausing output.
General Syntax
Example
#include <iostream.h> includes stream I/O functions. #include <conio.h> includes console functions. clrscr() clears the screen, cout prints text, and getch() freezes the console screen until a key is pressed.
Definition
cout is the standard output stream object used with the insertion operator (<<). cin is the standard input stream object used with the extraction operator (>>).
Purpose
Enables interactive data reading from the keyboard and formatted displaying of variables and text to the console screen.
General Syntax
Example
Declares integer age, clears console with clrscr(), reads input via cin >> age, and prints result via cout << age. getch() holds the screen.
Definition
Conditional statements allow branching based on boolean evaluation. If-Else tests conditions, and Switch-Case selects one branch among multiple constant integer/character values.
Purpose
Controls the execution flow based on logical criteria and menu-driven program choices.
General Syntax
Example
Calculates remainder num % 2. If 0, prints Even; otherwise prints Odd. getch() keeps the output displayed.
Definition
Loops repeatedly execute a block of code while a specified condition evaluates to true. For loops are counter-controlled, while loops are entry-controlled, and do-while loops are exit-controlled.
Purpose
Eliminates code redundancy for repetitive calculations, series generation, and array traversals.
General Syntax
Example
For loop runs from i=1 up to n, accumulating total sum. clrscr() clears screen, getch() halts console for review.
Definition
An array is a contiguous collection of elements of the same data type indexed from 0 to N-1. 2D arrays represent rows and columns (matrices).
Purpose
Stores and manipulates tabular data, lists of numbers, strings, and mathematical matrices.
General Syntax
Example
Declares array a of 5 elements, reads elements sequentially using for loop, prints them with spaces, and pauses screen with getch().
Definition
A function is a reusable subprogram. C++ supports Pass-by-Value (copy), Pass-by-Reference (alias &), and Pass-by-Pointer/Address (*).
Purpose
Promotes modularity, code reuse, and enables modification of caller variables through references and pointers.
General Syntax
Example
swapRef accepts integer references int &a, swapping actual arguments x and y without creating pointer syntax.
Definition
A class is a user-defined blueprint bundling data members (variables) and member functions (methods) with public, private, or protected access specifiers.
Purpose
Implements object-oriented programming principles like encapsulation, data hiding, and abstraction.
General Syntax
Example
Student class encapsulates roll and name. Member functions getDetails() and display() access private members. main() instantiates s1 object.