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

#include <iostream.h> #include <conio.h> void main() { clrscr(); // Statements and variable declarations getch(); }

Example

#include <iostream.h> #include <conio.h> void main() { clrscr(); cout << "Welcome to Turbo C++ Programming!" << endl; getch(); }

#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

cout << "Prompt text: "; cin >> variable_name; cout << "Output: " << variable_name << endl;

Example

#include <iostream.h> #include <conio.h> void main() { int age; clrscr(); cout << "Enter your age: "; cin >> age; cout << "You are " << age << " years old." << endl; getch(); }

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

if (condition) { // statement block } else { // alternate block } switch (choice) { case 1: // code break; default: // fallback }

Example

#include <iostream.h> #include <conio.h> void main() { int num; clrscr(); cout << "Enter a number: "; cin >> num; if (num % 2 == 0) cout << num << " is Even." << endl; else cout << num << " is Odd." << endl; getch(); }

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

for (initialization; condition; increment) { // loop body } while (condition) { // loop body } do { // loop body } while (condition);

Example

#include <iostream.h> #include <conio.h> void main() { int i, n, sum = 0; clrscr(); cout << "Enter N: "; cin >> n; for (i = 1; i <= n; i++) { sum += i; } cout << "Sum of first " << n << " natural numbers = " << sum << endl; getch(); }

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

data_type array_name[size]; // 1D Array data_type matrix_name[rows][columns]; // 2D Array

Example

#include <iostream.h> #include <conio.h> void main() { int a[5], i; clrscr(); cout << "Enter 5 integers: "; for (i = 0; i < 5; i++) { cin >> a[i]; } cout << "Array elements are: "; for (i = 0; i < 5; i++) { cout << a[i] << " "; } cout << endl; getch(); }

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

// Pass-by-Value void func(int x); // Pass-by-Reference void func(int &x); // Pass-by-Pointer void func(int *x);

Example

#include <iostream.h> #include <conio.h> void swapRef(int &a, int &b) { int temp = a; a = b; b = temp; } void main() { int x = 10, y = 20; clrscr(); cout << "Before swap: x=" << x << ", y=" << y << endl; swapRef(x, y); cout << "After swap: x=" << x << ", y=" << y << endl; getch(); }

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

class ClassName { private: // data members public: // member functions void getData(); void showData(); };

Example

#include <iostream.h> #include <conio.h> class Student { private: int roll; char name[30]; public: void getDetails() { cout << "Enter Roll No: "; cin >> roll; cout << "Enter Name: "; cin >> name; } void display() { cout << "Roll: " << roll << " | Name: " << name << endl; } }; void main() { Student s1; clrscr(); s1.getDetails(); cout << "\n--- Student Information ---" << endl; s1.display(); getch(); }

Student class encapsulates roll and name. Member functions getDetails() and display() access private members. main() instantiates s1 object.

← Back to C++