0% found this document useful (0 votes)
14 views50 pages

C++ Programming Basics and Concepts

C++ was developed by Bjarne Stroustrup in 1979 as an extension of the C language to support object-oriented programming, and it was released commercially in 1985. The document covers various aspects of C++, including its program structure, algorithms, pseudocode, functions, and data types, emphasizing the importance of planning and modularity in programming. It also discusses operators, input/output handling, and the use of loops and arrays in C++.

Uploaded by

abidmobile7433
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views50 pages

C++ Programming Basics and Concepts

C++ was developed by Bjarne Stroustrup in 1979 as an extension of the C language to support object-oriented programming, and it was released commercially in 1985. The document covers various aspects of C++, including its program structure, algorithms, pseudocode, functions, and data types, emphasizing the importance of planning and modularity in programming. It also discusses operators, input/output handling, and the use of loops and arrays in C++.

Uploaded by

abidmobile7433
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HISTORY OF C++ LANGUAGE

C++ was developed by Bjarne Stroustrup in 1979 at Bell Labs.

Designed as an extension of the C language to support object-

oriented programming (OOP).

Released in 1985 as a commercial product.

C++ is widely used for system software, game development, and

real-time simulations.
TRANSLATORS OVERVIEW
Translators convert high-level code into machine code.
Types of Translators:
Compiler: Translates entire code into executable machine code (e.g., C++ uses a compiler).
Interpreter: Translates and executes code line by line.
Assembler: Converts assembly language into machine code.
C++ uses compilers like GCC, Clang, or MSVC.
Visual: Flowchart showing the process of compilation and interpretation.
C++ PROGRAM STRUCTURE
Include Directives: #include<iostream> - Includes standard input/output library.
main() Function: The starting point of a C++ program.

#include <iostream>
using namespace std;
int main ( ) {
cout << "Hello, World!";
return 0;
}

The return 0; indicates the successful completion of the program.


Visual: Code snippet with basic program structure.
ALGORITHM, PSEUDOCODE, AND FLOW CHART

Outline the importance of planning in programming.


Introduce each term briefly.
Algorithm: Step-by-step instructions to solve a problem.
Pseudocode: A way to write down an algorithm in plain language.
Flow Chart: A diagrammatic representation of an algorithm.
WHAT IS AN ALGORITHM?
Definition: An algorithm is a finite sequence of well-defined instructions to
solve a problem or perform a task.
Characteristics of a Good Algorithm:
Finiteness: Should terminate after a finite number of steps.
Definiteness: Clear instructions in each step.
Input and Output: Takes inputs and produces outputs.
Effectiveness: Each step must be doable.
Example: A simple algorithm to make tea.
EXAMPLE ALGORITHM - SUM OF TWO NUMBERS
Step-by-step example:
1. Start
2. Take input values for two numbers, a and b.
3. Add a and b; store the result in sum.
4. Display sum.
5. End
Discuss how each step follows the characteristics of a good algorithm.
WHAT IS PSEUDOCODE?
Definition: Pseudocode is a way to write algorithms using plain language mixed with
programming constructs.
Benefits:
Easier for both programmers and non-programmers to understand.
Provides a high-level blueprint for actual code.
Helps visualize logic without worrying about syntax.
Common constructs in Pseudocode:
Input/Output Statements
Conditional Statements (IF, ELSE)
Loops (FOR, WHILE)
EXAMPLE PSEUDOCODE:

BEGIN
PRINT "Enter first number:"
READ a
PRINT "Enter second number:"
READ b
sum = a + b
PRINT "The sum is", sum
END

Discuss each line to show how it represents each algorithmic step in plain language.
Emphasize using capitalized keywords like BEGIN, END, READ, PRINT for clarity.
Introduction
C++ provides simple and effective ways to handle input and output through the
cin and cout objects, which are part of the iostream library. These objects help in
interacting with the user by taking input and displaying output on the console.
cin: Used for taking input from the user.
cout: Used for displaying output to the user.
cin (character input) is an input stream object used to read data from standard input, typically the keyboard.

cin >> variable;

>> is the extraction operator that takes input and assigns it to the specified variable.
cin (character input) is an input stream object used to read data from standard input, typically the keyboard.

#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age; // Takes input from the user and stores it in the 'age' variablereturn
0; }

Explanation: The user is prompted to enter their age, which is read by cin and stored in the variable age
cout (character output) is an output stream object used to display data to the standard output device (the console).

#include <iostream>
using namespace std;
int main() {
int number = 42; cout << "The number is: " << number << endl; // Displays "The
number is: 42"return 0;
}

Explanation: The cout object outputs the string "The number is: " followed by the value of number.
What is a Variable?
A variable is a named location in memory used to store data that can be modified
during program execution.
It acts as a container to hold values of a specific type.
Syntax of Variable Declaration

datatype variable_name;
datatype: The type of data the variable can store (e.g., int, float, char).
variable_name: A user-defined name for the variable.

int Num1; // Declares an integer variable named 'Num1'


float salary; // Declares a floating-point variable named 'salary'
How Variables Work in Memory?
Each variable is allocated a specific amount of memory based on its data type.
The address of the variable determines its location in memory.
Size in Memory (Common Data Types):
int: Typically 4 bytes
int num = 25;
float: Typically 4 bytes
double: Typically 8 bytes
char: 1 byte

Memory Diagram:
num is the variable name.
Its value (25) is stored in an allocated memory address.
Definition:
An integer variable is used to store whole numbers, both positive and negative.
Declaration and Initialization:

int number; // Declaration


number = 10; // Initialization
int age = 25; // Declaration and Initialization

Examples of Integer Operations:

int a = 10, b = 20;


int sum = a + b; // sum = 30
Definition:
An integer variable is used to store whole numbers, both positive and negative.
Declaration and Initialization:

int number; // Declaration


number = 10; // Initialization
int age = 25; // Declaration and Initialization

Examples of Integer Operations:

int a = 10, b = 20;


int sum = a + b; // sum = 30
Definition:
An integer variable is used to store whole numbers, both positive and negative.
Examples of Integer Operations:

int a = 10, b = 20;


int sum = a + b;
Definition:
A floating-point variable is used to store numbers with decimal points.
Examples of Floating-Point Operations:

float x = 5.5, y = 2.2;


float result = x * y;
Definition:
Variables can be initialized at the time of declaration or after declaration.
Syntax:

datatype variable_name = value;

Examples:

int age = 18;


price;
price = 99.99;
Using Cin for Input
The Cin object is used to get input from the user.

cin >> variable;


Operators are special symbols or keywords in C++ that perform specific
operations on operands (variables or values). They are the building blocks of
expressions and play a critical role in programming.

C++ provides several types of operators. In this lecture, we will cover:


Arithmetic Operators
Logical Operators
Relational Operators
Assignment Operators
Compound Assignment Operators
Increment and Decrement Operators
Definition:
Used for mathematical calculations.

Operator Description Example

+ Addition a+b

- Subtraction a-b

* Multiplication a*b

/ Division a/b

% Modulus (Remainder) a%b


Definition:
Used to combine or negate conditions. Returns true or false.

Operator Description Example

&& Logical AND (a > b && b > c)

` `

! Logical NOT !(a > b)


Definition:
Used to compare two values. The result is true or false.

Operator Description Example

== Equal to a == b

!= Not equal to a != b

> Greater than a>b


Definition:
Used to compare two values. The result is true or false.

Operator Description Example

== Equal to a == b

!= Not equal to a != b

> Greater than a>b

< Less than a<b

>= Greater than or equal to a >= b

<= Less than or equal to a <= b


Definition:
Used to assign values to variables.

Operator Description Example

= Assign value a = 10
Definition:
Used to increase or decrease the value of a variable by 1.

Operator Description Example Effect

++ Increment by 1 ++a Pre-increment

++ Increment by 1 a++ Post-increment

-- Decrement by 1 --a Pre-decrement

-- Decrement by 1 a-- Post-decrement


A character variable is used to store a single character, such as letters (A, B, C),
digits ('1', '2'), or symbols ('$', '@'). In C++, a character variable is declared
using the char keyword.

char variableName;
Initializing a character variable means assigning a value to it at the time of
declaration. Character literals are enclosed in single quotes

char variableName = 'character';

Note:
The value of a character variable must be a single character.
The characters are stored in memory as ASCII values (e.g., 'A' is stored as 65).
In C++, you can take input for character variables using the cin object.

cin >> variableName;

Key Points:
cin reads a single character input.
To read more than one character or a string, other methods like [Link]() or getline() are used.
The switch statement allows you to execute different blocks of code based on the
value of a character variable. It's an alternative to multiple if-else statements.

Input: The user enters a character grade (e.g., 'A', 'B').


switch: Evaluates the grade and matches it to the appropriate case.
break: Exits the switch block after executing a case.
default: Handles any input that doesn’t match the cases.
Missing break Statements:
If you forget to use break, the program will execute all subsequent cases
until it finds a break.
Using Double Quotes Instead of Single Quotes:
Character literals must be enclosed in single quotes ' ' (e.g., 'A'), not double
quotes (e.g., "A").
Case Sensitivity:
The switch statement is case-sensitive, so 'A' and 'a' are treated as different
values.
Introduction to Functions
Functions in C++ are reusable blocks of code designed to perform a specific task. They make
programs modular, efficient, and easier to debug.
Key Concepts to Teach:
Definition: A function is a named block of code that performs a specific task when called.
Why Functions?
Reduce redundancy (code reusability)
Improve readability
Simplify debugging and maintenance
Types of Functions:
Built-in Functions: Predefined in the C++ Standard Library (e.g., sqrt(), pow()).
User-defined Functions: Functions created by the programmer for specific tasks.
#include <iostream>
using namespace std;
void greet() {
cout << "Hello, welcome to Programming Fundamentals!" << endl;
}
int main() {
greet(); // Calling the function
return 0;
}
Function Declaration: Declares the function to the compiler
(optional for simple programs).
Function Definition: Specifies the function's logic.
Function Call: Executes the function.
Return Statement: (Optional) Returns a result to the caller.
Built-in functions are pre-written functions provided by the
C++ Standard Library to perform common tasks.
Examples of Built-in Functions in C++
Mathematical Functions (from <cmath>):
sqrt(x): Returns the square root of x.
pow(x, y): Returns xyx^yxy.
String Functions (from <string>):
length(): Returns the length of a string.
substr(start, length): Extracts a substring.
Input/Output Functions (from <iostream>):
cin: For input.
cout: For output.
Type Conversion Functions:
Code Reusability: Write once, use multiple times.
Modularity: Break code into smaller, manageable parts.
Readability: Functions make code clearer and easier to
understand.
Maintainability: Easier to debug and update.
In call by value, a copy of the actual parameter is passed to the
function. Changes made to the parameter inside the function do
not affect the original variable.
Key Characteristics:
Only a copy of the variable is passed.
Original variable remains unchanged.
Used for cases where data integrity is important.
In call by reference, the address of the actual parameter is
passed to the function. Changes made to the parameter inside
the function affect the original variable.
Key Characteristics:
Passes the reference (memory address) of the variable.
Original variable is modified.
Used when the function needs to modify the caller's data.
Recursion is a programming technique where a function calls itself to
solve smaller instances of a problem.
Useful for problems that can be broken down into smaller, similar
sub-problems.
A recursive function must have a base case to stop the recursion.
Key Components of Recursion:
1. Base Case: A condition to terminate the recursion.
2. Recursive Case: The function calls itself with a smaller input.
1. A recursive function is one that calls itself within its own definition.

Advantages of Recursion
Simplifies code for problems like factorial, Fibonacci, and tree traversal.
Reduces the need for explicit loops.

Disadvantages of Recursion
Higher memory usage due to function call stack.
Slower execution for problems with deep recursion.
An array is a collection of elements of the same data type stored in
contiguous memory locations. Arrays are used to store multiple values
in a single variable.

Key Characteristics:
Fixed size (declared at the time of creation).
Elements are accessed using indices (starting from 0).
Can store elements of any data type (e.g., int, float, char).
One-Dimensional Array
A linear collection of elements stored in a single row.

Two-Dimensional Array
Used to store data in rows and columns (like a matrix).

Multi-Dimensional Arrays
Arrays with more than two dimensions. Commonly used for
advanced mathematical and scientific computations.
Loops are used to execute a block of code repeatedly.
Types of Loops
1. For Loop
2. While Loop
3. Do-While Loop
For Loop
Executes a block of code for a fixed number of iterations.

While Loop
Executes a block of code as long as the condition is true.

Do-While Loop
Executes a block of code at least once and then repeats as long as the
condition is true.

You might also like