Introduction to C++ Programming: Identifiers, Variables, Data Types, and Operators
Fundamentals of C++: Identifiers and Variable Management
Definition of an Identifier: An identifier is a name assigned to various parts of a program, such as variables, functions, and other user-defined items.
Rules for Constructing Identifiers:
- First Character: Must be an alphabetic character (a-z, A-Z) or an underscore (
_). It cannot begin with a numeric digit. - Subsequent Characters: After the initial character, identifiers may consist of letters, digits (-), or underscores.
- Case Sensitivity: C++ is case-sensitive. For example,
Semandsemare treated as two distinct variables. - Reserved Keywords: Identifiers cannot be any of the C++ reserved keywords (e.g.,
int,float,void). These words have predefined meanings in the language.
- First Character: Must be an alphabetic character (a-z, A-Z) or an underscore (
Identifier Validation Examples:
totalSales: Valid (standard camelCase).total_Sales: Valid (uses underscore).total.Sales: Invalid (contains a period.which is not permitted).4thQtrSales: Invalid (starts with a numeric digit ).totalSale$: Invalid (contains the special character$which is not permitted).
Variables and Memory Allocation
Definition of a Variable: A variable is a named location in the computer's memory designated to hold data.
Key Characteristics:
- It possesses a specific name (identifier).
- It has a specific data type that defines the kind of data it can store.
- Pre-declaration Requirement: In C++, a variable must be declared (defined) before it can be utilized within the program.
Variable Declaration Syntax:
- Format:
data type variable name; - Single declaration example:
int semester; - Multiple declarations on one line:
float radius = 2, pi = 3.142, area;
- Format:
Primary Data Types in C++
- Integer Types (
int/long): Used for storing whole numbers without decimals.- Examples: , , .
- Floating-Point Types (
float/double): Used for storing real numbers or numbers with fractional parts.- Examples: , , .
- Character Type (
char): Used to store individual characters.- Examples in transcript:
char name;,char name[21];(character array for names),char address[30];. - Note: The transcript lists
alias an example for char, thoughchartypically holds a single character unless defined as an array or string.
- Examples in transcript:
- Boolean Type (
bool): Used to store logical values of true or false.- Representation:
trueis often represented as andfalseas . - Examples:
bool allDone = true;,bool finished = false;.
- Representation:
Special Characters and Syntax Symbols
- // (Double Slash): Marks the beginning of a single-line comment.
- # (Pound Sign): Indicates the beginning of a preprocessor directive (e.g.,
#include). - < > (Open/Close Brackets): Used to enclose filenames within a
#includedirective. - ( ) (Open/Close Parentheses): Used during the naming and calling of functions.
- { } (Open/Close Brace): Encloses a group of program statements, such as the body of a function.
- " " (Quotation Marks): Encloses a string of characters (string literals).
- ; (Semicolon): Marks the end of a programming statement.
C++ Input and Output Statements
Output Statement (
cout):- Purpose: Displays text or data on the computer screen.
- Operator: Uses the stream insertion operator
<<. - Syntax:
cout << "Text content"; - Examples:
cout << "UPSI";results in:UPSIcout << "FSKIK" << endl;(Moves the cursor to a new line after printing).
- New Line Manipulators:
endl: Starts a new line of output.\n: Escape sequence for starting a new line. It is placed inside the double quotes (e.g.,cout << "UPSI \n";).
Input Statement (
cin):- Purpose: Reads data entered by the user via the keyboard.
- Operator: Uses the stream extraction operator
>>. - Syntax:
cin >> variableName; - Examples:
cin >> semester;,cin >> cgpa;.
User Interaction (Prompts):
- A prompt is a message that instructs a user on what data to enter.
- Best Practice: Always use a
coutstatement to display a prompt immediately before acinstatement. - Example:
cpp cout << "What is your semester? "; cin >> semester;
Operators in C++
Arithmetic Operators: Used for numeric mathematical calculations.
- Modulus (
%): Computes the remainder resulting from integer division. - Modulus Example:
cout << 13 % 5;will display . - Restriction: The modulus operator requires both operands to be integers. Using a floating-point number (e.g.,
13 % 5.0) will result in a compilation error.
- Modulus (
Assignment Operator (
=):- Purpose: To store a specific value in a variable.
- Example:
int semester = 1;(This initializes the variablesemesterwith the value ).
Program Development and Logic
Simple Program Structure
#include <iostream>
using namespace std;
void main()
{
// statements go here
cout << "Hello there";
}
Application Exercise: Calculating the Area of a Circle
Given Constraints:
radius=pi=- Formula:
Development Steps:
Define the Problem: Read and understand the requirements.
Analysis:
- Input:
radius - Process:
- Output:
area
- Input:
Design (Pseudo-code):
- START
- SET
radius= ,area= ,pi= area=pi*radius*radius- PRINT
area - END
Implementation (Coding): ```cpp // sample C++ program
include
using namespace std;
void main() { float radius = 2, pi = 3.142, area; area = pi * radius * radius; cout << "Area of the circle is: " << area; } ```
Expected Output:
Area of the circle is: 12.568