C++ Programming Study Notes
C++ Output
C++ Output (Print Text):
The
coutobject is used along with the<<operator to output values and print text.Syntax:
Surround the text to be printed with double quotes (" ").
Example:
cout << "Hello, World!";Multiple
coutobjects:You can concatenate multiple
coutcalls, but it does not automatically insert a new line at the end.
C++ Print Numbers
Printing Numbers:
You can use
coutto print numerical values.Important Note:
Unlike text, numbers should not be enclosed in double quotes.
Example:
cout << 10;Mathematical Calculations:
You can perform calculations within the
coutstatement as well.
New Lines
Inserting New Lines:
Use the newline escape character
\nto insert a new line in output.Syntax Example:
cout << "Hello \nWorld!";Alternatively, use
endlmanipulator for a new line:Example:
cpp cout << "Hello World!" << endl; cout << "I am learning C++";Behavior of
\n:The newline character (
\n) forces the cursor to move to the beginning of the next line on the screen, resulting in a new line.
Escape Sequences
Common Escape Sequences:
Escape Sequence
Description
\tCreates a horizontal tab
\\Inserts a backslash character ()
\"Inserts a double quote character
Comments
Purpose of Comments:
Comments serve to explain and clarify C++ code, enhancing readability.
Used for testing by preventing execution of certain code sections.
Types of Comments:
Comments can be either single-lined or multi-lined.
Single-line Comments
Syntax:
Start with two forward slashes (//). Everything following will be ignored by the compiler.
Example:
cpp // This is a single line comment cout << "Hello!";
Multi-line Comments
Syntax:
Start with
/*and end with*/. Everything between these markers will be ignored.Example:
cpp /* This is a multi-line comment cout << "Hello!"; */
C++ User Input
Input Using
cin:cinis a predefined object used to read data from the keyboard, employing the extraction operator (>>).Example:
cpp int x; cin >> x; cout << "You entered: " << x;
Pronunciation:
coutis pronounced "see-out" and is used for output (using<<).cinis pronounced "see-in" and is used for input (using>>).
C++ iostream Objects
Overview:
The
<iostream>library provides objects that can read user input and produce output data either to the console or to files.
C++ Data Types
Declaring Variables:
A variable in C++ must have a specified data type.
Examples of Data Type Declarations:
int myNum = 5;float myFloatNum = 5.99;double myDoubleNum = 9.98;char myLetter = 'D';bool myBoolean = true;string myText = "Hello";
Types of Data:
Integer: Whole numbers (e.g., 5)
Float: Floating point numbers (e.g., 5.99)
Double: More precision than float (e.g., 9.98)
Character: Single characters (e.g., 'A')
Boolean: True or false values
String: Sequence of characters (e.g., "Hello")
Basic Data Types
Description of Basic Data Types:
Data Type
Size
Description
boolean
1 byte
Stores true or false values
char
1 byte
Stores a single character/letter/number
int
2 or 4 bytes
Stores whole numbers, without decimals
float
4 bytes
Stores fractional numbers, sufficient for 6-7 decimal digits
double
8 bytes
Stores fractional numbers, sufficient for 15 decimal digits
C++ Numeric Data Types
Usage Guidelines:
Use
intfor whole numbers (e.g., 35, 1000).Use
floatordoublefor floating-point numbers (e.g., 9.99, 3.14515).
Float vs. Double
Precision Differences:
floatprecision: 6-7 decimal digits.doubleprecision: about 15 decimal digits.It is generally safer to use
doublefor most calculations due to its greater precision.
Scientific Numbers
Using Scientific Notation in Floating Points:
A floating-point number can be written in scientific notation using an "e" to denote the power of 10.
Example:
cpp float scientificNum = 1.23e10; // equivalent to 1.23 * 10^10
Boolean Types
Definition:
A boolean data type is declared using the
boolkeyword.It can take only two values:
trueorfalse.When returned,
truecorresponds to 1 andfalseto 0.
Application:
Boolean values are predominantly used for conditional testing.
Character Types
Character Storage in C++:
The
chardata type stores a single character.Characters must be surrounded by single quotes (e.g., 'A', 'c').
Alternatively, ASCII values can represent characters.
String Types
Storing Text:
The
stringtype is used for sequences of characters (text).Strings must be surrounded by double quotes (e.g., "Hello").
To use string types, include the
<string>library in your source code:cpp #include <string>
The
autoKeywordFunctionality:
The
autokeyword automatically identifies a variable's type based on the assigned value.It enables cleaner code and reduces redundancy in long or complex types.
Example:
cpp auto x = 5; // 'x' is automatically inferred as int
Important Notes on
autoRequirements:
automust be assigned a value at the time of its declaration.Example:
cpp auto x; // This is incorrect, as 'x' must be initialized
Stability of Type:
Once the type is determined, it remains constant:
cpp auto x = 5; // 'x' is now int x = 10; // OK x = 9.99; // Error, cannot assign double to int