Oops
Oops
❖ Tokens-keywords
A token is the smallest meaningful unit in a C++ program. When you write code, the compiler breaks it down into these
small parts (tokens) to understand it.
There are five types of tokens in C++:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuation (Separators)
1. Keywords
2. Identifiers
An identifier in C++ is the name you give to variables, functions, arrays, or any user-defined element in a program. It is
how you refer to those elements in your code.
Rules for Naming Identifiers
1. Must begin with a letter or an underscore (_).
• It cannot start with a digit.
• Example: myVariable, _temp
2. Can contain letters, digits, and underscores.
• No special characters like @, #, !, or spaces are allowed.
• Example: value1, total_sum, max_value
3. Cannot be a keyword.
• You cannot use reserved keywords (like int, if, while) as identifiers.
• Example: int is invalid as an identifier because it’s a keyword.
4. Case-sensitive.
• C++ treats uppercase and lowercase letters as different, so myVariable and MyVariable are two
different identifiers.
5. Should be meaningful (recommended).
• Use descriptive names that make your code easier to understand.
Practical:-
#include <iostream>
int main() {
int age = 25; // age is an identifier
float height = 5.9; // height is an identifier
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
return 0; // return is a keyword, not an identifier
}
3. Literals :-
A literal is a constant value directly written in the code. It represents fixed data that doesn’t change during program
execution.
Types:-
1.Integer Literals
These are whole numbers.
Examples: 10, -25, 0, 1000
2.Floating-Point (Decimal) Literals
These are numbers with decimal points.
Examples: 3.14, -0.001, 2.5e3 (scientific notation for 2500)
3.Character Literals
These are single characters enclosed in single quotes (' ').
Examples: 'a', 'Z', '#'
4.String Literals
These are sequences of characters enclosed in double quotes (" ").
Examples: "Hello", "C++ Programming"
Practical:-
#include <iostream>
int main() {
int age = 30; // Integer literal
float height = 5.9; // Floating-point literal
char initial = 'A'; // Character literal
std::string name = "Alex"; // String literal
bool isStudent = false; // Boolean literal
std::cout << "Name: " << name << ", Age: " << age << std::endl;
std::cout << "Height: " << height << ", Initial: " << initial << std::endl;
std::cout << "Is student: " << isStudent << std::endl;
return 0;
}
4. Operators
Operators are symbols that perform operations on variables and values.
Examples of operators:
❖ constants-integer
In C++, constants are fixed values that do not change during the execution of a program. Integer constants specifically
represent whole numbers without any fractional or decimal part.
Types of Integer Constants in C++
Integer constants represent fixed whole numbers.
• Types of integer constants:
• Decimal (base 10)
• Octal (base 8, prefixed with 0)
• Hexadecimal (base 16, prefixed with 0x or 0X)
• Use the const keyword or #define to create constants that cannot be changed during program execution.
Practical:-
#include <iostream>
#define MAX_SPEED 120 // Defining an integer constant using #define
int main() {
std::cout << "Max speed is: " << MAX_SPEED << std::endl;
return 0;
}
❖ character and string constants
1. Character Constants
A character constant represents a single character enclosed in single quotes ('). It can store any
single character, such as a letter, digit, or symbol.
Example:-
char letter = 'A'; // 'A' is a character constant
char digit = '5'; // '5' is a character constant
char symbol = '#'; // '#' is a character constant
Rules for Character Constants:
1. Must be enclosed in single quotes (' ').
2. Can only contain one character.
3. Can be an alphanumeric character, punctuation, or a special symbol.
4. The size of a character constant is 1 byte (since it stores an ASCII value).
Special Character Constants:
Some characters have special meanings and are represented using escape sequences. These start
with a backslash (\).
Escape Sequence Meaning
\n Newline
2. String Constants
A string constant (also called a string literal) represents a sequence of characters enclosed in
double quotes (" "). Strings in C++ are actually arrays of characters, ending with a special null
character (\0).
Example: Using Character Constants in Arithmetic
#include <iostream>
int main() {
char letter = 'A';
std::cout << "ASCII value of 'A': " << int(letter) << std::endl;
std::cout << "Next letter: " << char(letter + 1) << std::endl;
return 0;
}
Example :Program Using Character and String Constants
#include <iostream>
#include <string> // For using std::string
int main() {
char grade = 'A'; // Character constant
std::string message = "Welcome!"; // String constant
return 0;
}
❖ backslash constants
In C++, backslash constants are used to represent special characters in strings and character constants. These
are called escape sequences, and they allow us to include characters that are difficult or impossible to type
directly (like newlines, tabs, or even a backslash itself).
Escape sequences always begin with a backslash (\), followed by a character or sequence of characters that
defines the special meaning.
int main() {
std::cout << "Hello\nWorld!" << std::endl; // 'World!' will be printed on the next line
std::cout << "Name\tAge\tLocation" << std::endl; //Horizontal tab \tInserts a tab space between
characters.
std::cout << "John\t25\tUSA" << std::endl; ////Horizontal tab \tInserts a tab space between
characters.
std::cout << "It\'s a beautiful day!" << std::endl; //backslash \To print a backslash (\), use two
backslashes (\\).
std::cout << "It\'s a beautiful day!" << std::endl; //Single Quote (\') Used when you need to print
a single quote within a string or character constant.
std::cout << "He said, \"Hello!\"" << std::endl; // Double Quote (\") Used to print double quotes
within a string.
std::cout << str << std::endl; // Only prints "Hello" because '\0' marks the end of the string
std::cout << "Hello\rWorld!" << std::endl; //Carriage Return (\r): "World!" will overwrite
"Hello"
std::cout << "Helloo\b World!" << std::endl; // One 'o' will be removed
return 0;
}
❖ features of C++ and its basic structure
C++ is a powerful, flexible, and high-performance programming language. It is widely used for
system/software development, game development, and applications requiring high performance.
1. Object-Oriented Programming (OOP)
C++ is an object-oriented language, which means it allows the use of objects and classes. Key
OOP features include:
• Encapsulation: Grouping data and functions into classes.
• Inheritance: Creating new classes from existing ones, promoting code reuse.
• Polymorphism: Using the same function or operator for different data types.
• Abstraction: Hiding complex implementation details and exposing only the necessary parts.
2. Rich Library Support
C++ provides a rich standard library, including functions for:
• Input/Output (I/O)
// Function declaration
void greet();
int main() {
// Variable declaration
int number = 5;
// Output statement
std::cout << "The number is: " << number << std::endl;
// Function call
greet();
// Function definition
void greet() {
std::cout << "Hello from the greet function!" << std::endl;
}
Summary of Basic C++ Program Structure
1. Preprocessor Directives: #include for libraries.
2. Main Function: int main() — program entry point.
3. Statements and Expressions: Perform actions in the program.
int main() {
// Declare two variables to store user input
double num1, num2, sum;