Tuesday, 16 December 2025

Writing a Program in C++

 
Declaration of Variables in C++
A variable in C++ is a named memory location used to store data. Before using a variable, it must be declared.

1. Local Variable
Declared inside a function or block.
void func() {
    int x = 5;  // local variable
}

2. Global Variable
Declared outside all functions.
int count;
int main() {
    count = 10;
}

3. Static Variable
Retains its value between function calls.
static int num = 0;

4. Constant Variable
Value cannot be changed.
const int MAX = 100;

Example Program
#include <iostream>
using namespace std;
int main() {
    int age = 20;
    float salary = 25000.50;
    char grade = 'A';
    cout << age << endl;
    cout << salary << endl;
    cout << grade << endl;
    return 0;
}

Types of Statements in C++

1. Declaration Statements
Used to declare variables.
int a;
float marks;

2. Assignment Statements
Used to assign values to variables.
a = 10;
marks = 85.5;

3. Input / Output Statements
Used to take input and display output.
cin >> a;
cout << a;

4. Control Statements
Control the flow of program execution.

Features of iostream.h in C++
iostream.h is a header file used for input and output operations in C++.
(It is mainly used in old/early C++ compilers; modern C++ uses <iostream>.)

Main Features of iostream.h

1. Supports Input and Output Operations
It provides standard input and output streams.
cin → Standard input (keyboard)
cout → Standard output (screen)
cerr → Error output
clog → Log output
Example:
cout << "Hello";
cin >> x;

2. Stream-Based I/O
Uses streams instead of formatted I/O like printf() and scanf().
Data flows as a stream of bytes
Type-safe input/output

3. Provides Insertion and Extraction Operators
<< → Insertion operator (output)
>> → Extraction operator (input)
Example:
cout << "Value = " << x;
cin >> x;

4. Easy to Use
Simplifies input/output without format specifiers.
No need to specify data type formats like %d, %f.

5. Object-Oriented
Input/output is handled using objects.
cin and cout are objects
Makes I/O more flexible and powerful

6. Faster Development
Reduces coding effort and improves readability.
cout << a << b << c;

7. Automatic Type Conversion
Automatically handles different data types.
int a;
float b;
cin >> a >> b;

Important Note (Exam Point)
1. iostream.h is non-standard and outdated
2. Modern C++ uses:
#include <iostream>
using namespace std;

Keyboard and Screen I/O in C++
In C++, keyboard input and screen output are performed using the iostream library.
Input and Output Operators
Operator Name         Use
>>         Extraction operator Input
<<         Insertion operator Output

Using endl
endl moves the cursor to a new line.
cout << "Hello" << endl;

Example Program
#include <iostream>
using namespace std;
int main() {
    int a, b, sum;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    sum = a + b;
    cout << "Sum = " << sum << endl;
    return 0;
}

Manipulator Functions in C++
Manipulator functions are used with input/output streams to format data while displaying or reading it. They are mainly used with cout and cin.

Complete Example Program
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    float pi = 3.14159;
    cout << fixed << setprecision(2);
    cout << "Pi = " << pi << endl;
    cout << setw(10) << setfill('*') << 25 << endl;
    return 0;
}

Predefined Manipulators in C++
Predefined manipulators are built-in formatting tools used with cin and cout to control input and output appearance. They are provided by the iostream library.

Example Program
#include <iostream>
using namespace std;
int main() {
    int a = 15;
    float b = 12.5;
    cout << showpos << a << endl;
    cout << fixed << showpoint << b << endl;
    cout << hex << uppercase << a << endl;
    return 0;
}

Input and Output (I/O) Stream Flags in C++
I/O stream flags are used to control the formatting and behavior of input and output streams (cin, cout). They are defined in the ios / ios_base class.

1. Number System Flags
Flag Description
ios::dec Decimal (base 10)
ios::oct Octal (base 8)
ios::hex Hexadecimal (base 16)
Example:
cout.setf(ios::hex);
cout << 25;   // Output: 19

2. Floating-Point Format Flags
Flag             Description
ios::fixed     Fixed notation
ios::scientific     Scientific notation
Example:
cout.setf(ios::fixed);
cout << 3.14159;

3. Sign Display Flags
Flag                 Description
ios::showpos         Show + sign
ios::noshowpos Hide + sign

4. Decimal Point Flags
Flag         Description
ios::showpoint Always show decimal
ios::noshowpoint         Hide trailing zeros

5. Field Alignment Flags
Flag Description
ios::left         Left alignment
ios::right Right alignment
ios::internal Sign before padding
Example:
cout.setf(ios::left);
cout << setw(10) << 25;

6. Uppercase Flags
Flag         Description
ios::uppercase Uppercase letters
ios::nouppercase         Lowercase letters

Using setf() with Mask
Some flags belong to the same group and need a mask.
cout.setf(ios::hex, ios::basefield);
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::right, ios::adjustfield);

Example Program
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    cout.setf(ios::showpos);
    cout.setf(ios::hex, ios::basefield);
    cout << 100 << endl;
    cout.unsetf(ios::showpos);
    cout.setf(ios::dec);
    cout << 100 << endl;
    return 0;
}

0 comments

Post a Comment