0% found this document useful (0 votes)
3 views14 pages

Comprog2 Notes

The document provides a comprehensive guide to getting started with C++, covering installation of IDEs, basic programming concepts, data types, operators, variable scope, and decision-making structures. It outlines the history of C++, its popularity, and advantages, along with practical examples of code structures and syntax. Additionally, it includes information on debugging common errors and resources for further learning.

Uploaded by

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

Comprog2 Notes

The document provides a comprehensive guide to getting started with C++, covering installation of IDEs, basic programming concepts, data types, operators, variable scope, and decision-making structures. It outlines the history of C++, its popularity, and advantages, along with practical examples of code structures and syntax. Additionally, it includes information on debugging common errors and resources for further learning.

Uploaded by

JM Hiponia Jaem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WEEK 1 1.

Install IDE: Download and install your


chosen IDE.
What is C++?
2. Create a New Project: Set up a new C++
C++ is a general-purpose programming language
project within the IDE.
that evolved from C. It adds object-oriented features
to C, making it suitable for complex software 3. Configure Compiler: Adjust compiler
development while maintaining the efficiency of settings if necessary.
low-level memory manipulation.
4. Familiarize with IDE: Learn the layout of
the IDE, including the code editor, project
explorer, and output console.
History of C++
Basic Structure of a C++ Program:
 1979: Bjarne Stroustrup begins work on "C
with Classes" at Bell Labs.  #include <iostream>: This line includes
the iostream library, which provides
 1985: The first official version of C++, "C+
functions for input and output operations.
+ 1.0," is released.
 int main(): This declares the main function,
 1998: The first standardized version of C++
the entry point of every C++ program.
(C++98) is published.
 std::cout << "Hello, World!" <<
 2003-2023: Subsequent standards (C++03,
std::endl;: std::cout is used to output text to
C++11, C++14, C++17, C++20, C++23)
the console. std::endl inserts a newline
introduce new features and improvements.
character, moving the cursor to the next line.
 return 0;: This line returns a value of 0,
Why Use C++? indicating that the program executed
successfully.
 Popularity: C++ is one of the world's most
popular programming languages, used in
operating systems, graphical user interfaces,
Compiling and Running the Program:
and embedded systems.
1. Save: Save the code with a .cpp extension
 Object-Oriented: C++'s object-oriented
(e.g., hello.cpp).
nature provides a structured approach to
programming, allowing for code reusability 2. Compile: Use the IDE's built-in compiler to
and reduced development costs. compile the code.
 Portability: C++ applications can be easily 3. Run: Execute the generated executable file.
adapted to different platforms.
 Efficiency: C++ provides low-level memory
Debugging Common Errors
control, making it ideal for performance-
critical applications.  Syntax Errors: Misspelled keywords,
missing semicolons, etc.
 Community Support: C++ has a vast and
active community, providing ample  Linker Errors: Missing or incorrect library
resources and support for learning and links.
problem-solving.
 Runtime Errors: Errors that occur during
program execution.
Getting Started with C++
Common IDEs for C++: Exploring the IDE
 Visual Studio: A comprehensive and  Code Editor: Where you write and edit
feature-rich IDE with support for multiple your C++ code.
languages.
 Project Explorer: Manages the files and
 Code::Blocks: A lightweight, open-source directories within your project.
IDE specifically designed for C++.
 Output Console: Displays program output
 Eclipse: A versatile, open-source IDE with and error messages.
C++ support through plugins.
Steps to Configure:
 Debugger: Helps you step through your o Enclosed in single quotes: 'k'.
code and inspect variables to identify and fix
o Example: char test = 'k';
errors.
3. Floating-point (float)
o Represents numbers with decimal
Additional Resources:
points.
 C++ Tutorials: Explore comprehensive
o Size: 4 bytes.
tutorials and resources online to learn C++
in detail. o Example: float area = 75.01;
 C++ Documentation: Refer to the official 4. Double Floating-point (double)
C++ documentation for language
specifications and standard library o Represents numbers with decimal
references. points with higher precision
than float.
 Online Communities: Engage with the C++
community on forums and websites to ask o Size: 8 bytes.
questions, share knowledge, and get support. o Example: double volume =
WEEK 2 135.64534;

C++ Data Types


C++ data types define the kind of data a variable C++ Type Modifiers
can store. Each data type has a specific size and Type modifiers can further modify the fundamental
range of values it can hold. data types, affecting their size and range.

Fundamental Data Types


 signed: Indicates a data type can hold both
Data Size (in positive and negative values.
Meaning
Type Bytes)
 unsigned: Indicates a data type can hold only
non-negative values.
int Integer 2 or 4
 short: Reduces the size of the data type
(typically for integers).
float Floating-point 4
 long: Increases the size of the data type
(typically for integers).
Double
double 8
Floating-point
C++ Operators

char Character 1 Operators are symbols that perform specific actions


or operations on data.

Detailed Explanation 1. Arithmetic Operators


1. Integer (int)
Symbo Operator
Represents whole numbers (positive, Syntax
o l Name
negative, or zero).
o Range: -2147483648 to 2147483647 result =
(for 4-byte integers). + Addition operand1 +
o Example: int age = 21; operand2;

2. Character (char)
result =
o Represents single characters. - Subtraction operand1 -
o Size: 1 byte. operand2;
4. Assignment Operators
Symbo Operator
Syntax
l Name
Symbo Operator
Syntax
l Name
result =
* Multiplication operand1 *
Simple variable =
operand2; =
assignment expression

result =
Addition variable +=
/ Division operand1 / +=
assignment expression
operand2;

Subtraction variable -=
result = -=
Modulus assignment expression
% operand1 %
(Remainder)
operand2;
Multiplication variable *=
*=
assignment expression
2. Relational Operators
Division variable /=
Symbo Operator /=
Syntax assignment expression
l Name

variable
operand1 == Modulo
== Equal to %= %=
operand2 assignment
expression

operand1 !=
!= Not equal to
operand2 5. Increment and Decrement Operators
 ++ (Increment): Increases the value of the
operand1 > operand by 1.
> Greater than
operand2
 -- (Decrement): Decreases the value of the
operand by 1.
operand1 <
< Less than
operand2
6. Bitwise Operators

Greater than operand1 >= Bitwise operators manipulate data at the bit level.
>=
or equal to operand2 Symbol Operator Syntax
name
Bitwise AND result =
& operand1 &
operand2
Less than or operand1 <=
<= ‘ Bitwise OR `result = operand2`
equal to operand2 ‘ operand1

Bitwise XOR result =


^ operand1 ^
operand2

Bitwise Not result =


3. Logical Operators ~ ~operand
Bitwise Left result =
Symbol OPERATOR Syntax >> Shift operand <<
n
NAME
Bitwise result =
&& LOGICAL
AND
condition1
&& << Right Shift operand >>
n
condition2
‘ ‘ Logical `condition1 condition2`
OR
! LOGICAL !condition
NOT Important Notes:
 The size of data types can vary depending
on the compiler and system architecture.
 Understanding data types is crucial for
writing efficient and accurate C++ code.
 Operators are essential for performing Multiple Variables
calculations, comparisons, and other
C++ allows you to work with multiple variables in
operations on data.
various ways:
WEEK 3
1. Declaring Multiple Variables in a Single
C++ Variable Scope and Multiple Variables Statement
Variable scope determines where a variable can be o You can declare multiple variables of
accessed and used within a program. Understanding the same type in a single statement,
scope is crucial for managing data and preventing separated by commas.
errors.
Example:
int a, b, c; // Declares three integer variables a, b,
Types of Variable Scope and c
1. Local Scope float x = 1.5, y = 2.5, z = 3.5; // Declares and
initializes three float variables
o Definition: Variables declared inside
a function or a block (within curly Multiple Variables with Different Scopes
braces {}) have local scope.
o You can declare variables with the same
o Visibility: They are accessible only name in different scopes (global, local,
within that specific block or
function.
o Lifetime: They exist only during the
execution of the block or function.
Once the block or function ends, the
variable is destroyed.

block), but they will refer to different


entities.
Using Multiple Variables in Expressions

Global Scope
o Definition: Variables declared outside all
functions and blocks have global scope.
o Visibility: They are accessible from
anywhere in the program after their
declaration.
o Lifetime: They exist throughout the entire
execution of the program and are destroyed o You can perform operations on multiple
when the program terminates. variables within expressions. C++ handles
the operations according to the types and
precedence rules.

Multiple Variables in Loops


o You can declare and use multiple variables
within loops. It's important to manage the
scope of loop variables to avoid interference
with other parts of the program.
Passing Multiple Variables to Functions
o You can pass multiple variables as 3. Code Block: The set of statements to
arguments to a function. C++ allows you to execute if the condition is true, enclosed in
work with them inside the function. curly braces {}.
How It Works:
1. The program evaluates the condition in the
parentheses ().
2. If the condition is true, the code inside the
curly braces {} is executed.
3. If false, the code block is skipped.
Example:
Using Multiple Variables with Arrays
cpp
o Arrays allow you to manage multiple
variables of the same type under a single CopyEdit
name. #include <iostream>
using namespace std;

int main() {
int number = 10;
if (number > 0) {
cout << "The number is positive." << endl;

WEEK 4 Decision/Condition Statement of C++ }

Decision Making Structures in Programming return 0;

Decision-making structures require programmers to }


specify one or more conditions to be evaluated or Output: "The number is positive."
tested, and based on the outcome (true or false), the
program executes specific statements. These
structures allow the program to make decisions 2. if...else Statement
based on conditions and control its flow.
The if...else statement extends the if statement by
providing an alternative block of code when the
1. if Statement condition is false.

The if statement allows the execution of a block of Syntax:


code only if a specific condition is true. cpp
Syntax: CopyEdit
cpp if (condition) {
CopyEdit // Code to execute if the condition is true
if (condition) { } else {
// Code to execute if the condition is true // Code to execute if the condition is false
} }
Components: Components:
1. if keyword: Starts the conditional statement. 1. if keyword: Starts the conditional check.
2. Condition: An expression that evaluates to 2. Condition: An expression that evaluates to
either true or false. true or false.
3. Code Block 1 (if): Runs if the condition is
true.
4. else keyword: The alternative path if the cpp
condition is false.
CopyEdit
5. Code Block 2 (else): Runs if the condition is
#include <iostream>
false.
using namespace std;
Example:
cpp
int main() {
CopyEdit
int score;
#include <iostream>
cout << "Enter your score: ";
using namespace std;
cin >> score;

int main() {
if (score >= 90) {
int number;
cout << "You got an A!" << endl;
cout << "Enter a number: ";
} else if (score >= 80) {
cin >> number;
cout << "You got a B." << endl;
} else if (score >= 70) {
if (number >= 0) {
cout << "You got a C." << endl;
cout << "The number is non-negative." <<
endl; } else if (score >= 60) {
} else { cout << "You got a D." << endl;
cout << "The number is negative." << endl; } else {
} cout << "You got an F." << endl;
return 0; }
} return 0;
Output: "The number is non-negative." or "The }
number is negative."
Output: Based on the input score, the program will
print the corresponding grade.
3. else if Ladder
The else if ladder is used for checking multiple 4. Nested if...else Statements
conditions sequentially. It tests additional conditions
Nested if...else statements are used when one
if the previous ones were false.
decision leads to another, placing an if...else
Syntax: statement inside another.
cpp Syntax:
CopyEdit cpp
if (condition1) { CopyEdit
// Code to execute if condition1 is true if (condition1) {
} else if (condition2) { // Code for condition1
// Code to execute if condition2 is true if (condition2) {
} else { // Code for condition2
// Code to execute if no conditions are true } else {
} // Code if condition2 is false
Example: }
} else { case value1:
// Code if condition1 is false // Code if expression == value1
} break;
Example: case value2:
cpp // Code if expression == value2
CopyEdit break;
#include <iostream> default:
using namespace std; // Code if no case matches
}
int main() { Example:
int number; cpp
cout << "Enter a number: "; CopyEdit
cin >> number; #include <iostream>
using namespace std;
if (number >= 0) {
cout << "The number is non-negative." << int main() {
endl;
int day;
cout << "Enter a number (1-7) for the day of the
if (number == 0) { week: ";
cout << "The number is zero." << endl; cin >> day;
} else {
cout << "The number is positive." << endl; switch (day) {
} case 1: cout << "Sunday" << endl; break;
} else { case 2: cout << "Monday" << endl; break;
cout << "The number is negative." << endl; case 3: cout << "Tuesday" << endl; break;
} case 4: cout << "Wednesday" << endl; break;
return 0; case 5: cout << "Thursday" << endl; break;
} case 6: cout << "Friday" << endl; break;
Output: Depending on the input, the program will case 7: cout << "Saturday" << endl; break;
print whether the number is positive, zero, or
default: cout << "Invalid input!" << endl;
negative.
}

5. switch Statement
return 0;
The switch statement is used to select one of many
possible blocks of code to execute based on the }
value of a single expression. It is an alternative to
Output: Prints the corresponding day based on the
multiple if...else conditions.
input number.
Syntax:
cpp
Key Points:
CopyEdit
 if, if...else, else if, and switch statements
switch (expression) { allow decision-making based on conditions.
 if and else statements are used for binary using namespace std;
decision-making.
 The else if ladder is useful for testing
int main() {
multiple conditions.
for (int i = 1; i <= 5; i++) {
 Nested if...else statements allow complex
decision-making processes. cout << "Value of i: " << i << endl;
 The switch statement is more efficient when }
comparing a variable to many values.
return 0;
WEEK 5 C++ Loops
}
For Loop in C++
This will print the values of i from 1 to 5.
The for loop is a control flow statement used to
repeat a block of code a specific number of times,
useful when the number of iterations is known While Loop in C++
beforehand.
A while loop repeatedly executes a block of code as
Syntax: long as a specified condition remains true. It's
typically used when the number of iterations is not
cpp
known in advance.
CopyEdit
Syntax:
for (initialization; condition; update) {
cpp
// Statements to be executed
CopyEdit
}
while (condition) {
Components:
// Statements to be executed
1. Initialization: Sets the starting value of the
}
loop control variable. Executed once.
Components:
o Example: int i = 0;
1. Condition: A boolean expression that is
2. Condition: A boolean expression checked
checked before each iteration.
before each iteration. The loop runs if it's
true. o Example: i <= 10;
o Example: i < 10; 2. Loop Body: Executes as long as the
condition is true.
3. Update: Modifies the loop control variable
after each iteration. How it Works:
o Example: i++; 1. Condition is evaluated before each iteration.
If true, the loop body executes.
How it Works:
2. The loop continues until the condition
1. Initialization runs once at the start.
becomes false.
2. The condition is checked before each
Example:
iteration. If true, the loop body executes; if
false, the loop terminates. cpp

3. Update is executed after each iteration to CopyEdit


modify the loop control variable.
#include <iostream>
4. The loop repeats until the condition
using namespace std;
evaluates to false.
Example:
int main() {
cpp
int i = 1;
CopyEdit
while (i <= 5) {
#include <iostream>
cout << "Value of i: " << i << endl; A nested loop is a loop inside another loop. The
inner loop runs completely each time the outer loop
i++; // Incrementing i
iterates once.
}
Syntax:
return 0;
cpp
}
CopyEdit
for (initialization; condition; update) {
Do-While Loop in C++
// Outer loop statements
A do-while loop ensures that the loop body is
for (initialization; condition; update) {
executed at least once before the condition is
checked. // Inner loop statements

Syntax: }

cpp }

CopyEdit How it Works:

do { 1. The outer loop iterates, executing the inner


loop each time.
// Statements to be executed
2. The inner loop runs from start to finish
} while (condition);
before control returns to the outer loop.
Components:
Example:
1. Loop Body: Executes at least once and then
cpp
repeatedly if the condition remains true.
CopyEdit
2. Condition: Checked after each iteration.
#include <iostream>
How it Works:
using namespace std;
1. The loop body executes first.
2. After the body, the condition is evaluated. If
true, the loop repeats. int main() {

Example: for (int i = 1; i <= 3; i++) { // Outer loop

cpp for (int j = 1; j <= 3; j++) { // Inner loop

CopyEdit cout << "* ";

#include <iostream> }

using namespace std; cout << endl; // Move to the next line after
inner loop completes
}
int main() {
return 0;
int i = 1;
}
do {
This prints a 3x3 grid of stars.
cout << "Value of i: " << i << endl;
i++; // Incrementing i
Special Value Type Loops in C++
} while (i <= 5);
A special value type loop terminates when a
return 0;
specific "special" or "sentinel" value is encountered.
}
Common Use Case: These loops are used when the
number of iterations is unpredictable, such as
processing user input or data from external sources.
Nested Loop in C++
Example:
cpp
CopyEdit
#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter numbers (enter -1 to stop): ";
while (true) {
cin >> num;
if (num == -1) {
break; // Exit the loop
}
cout << "You entered: " << num << endl;
}
cout << "Loop terminated as -1 was entered." <<
endl;
return 0;
}
The loop stops when the user enters -1.
Key Points:
 Sentinel Value: A predefined value (e.g., -1)
that signals the loop to stop.
 These loops are often used in situations
where the input is dynamic and
unpredictable.
MIDTERM CopyEdit
WEEK 7 #include <iostream>
Arrays in C++ using namespace std;
📌 Definition

 Array: A collection of elements of the same int main() {


data type stored in contiguous memory
int numbers[5] = {10, 20, 30, 40, 50};
locations.
 Allows you to store and manage multiple
values under a single variable name. for (int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " <<
numbers[i] << endl;
📌 Key Features
}
✅ Same Data Type: All elements must have the
same type (e.g., all int, all float).
✅ Fixed Size: Size is set when declared and cannot
numbers[2] = 100; // Change 3rd element
change.
✅ Indexing: Accessed using zero-based indexing
(first element at index 0).
cout << "Modified array: ";
for (int i = 0; i < 5; i++) {
📌 Syntax
cout << numbers[i] << " ";
Declaration
}
cpp
CopyEdit
return 0;
data_type array_name[size];
}
Example:
Output:
cpp
yaml
CopyEdit
CopyEdit
int scores[10]; // Array of 10 integers
Element at index 0: 10
float temperatures[5]; // Array of 5 floats
Element at index 1: 20
Initialization
Element at index 2: 30
cpp
Element at index 3: 40
CopyEdit
Element at index 4: 50
int numbers[5] = {1, 2, 3}; // First 3 values set, rest
Modified array: 10 20 100 40 50
=0

📌 Size Calculation
📌 Accessing Elements
You can calculate array size using sizeof:
cpp
cpp
CopyEdit
CopyEdit
cout << numbers[2]; // Access 3rd element
int numbers[5];
numbers[0] = 100; // Modify 1st element
int size = sizeof(numbers) / sizeof(numbers[0]); //
size = 5
📌 Example Code

cpp 📌 Advantages
✅ Efficient storage and access
✅ Simple and easy to use
Initialization of Multidimensional Arrays
✅ Good for managing lists of data (grades, sales,
temperatures) You can initialize multidimensional arrays at the
time of declaration, either by listing out all the
WEEK 8
elements or specifying the elements for each sub-
What is a Multidimensional Array? array (row).

A multidimensional array is an array of arrays. It


allows you to store data in a tabular or matrix-like
Example of Initializing a 2D Array:
structure. The most common type of
multidimensional array is the 2-dimensional (2D)
array, which can be visualized as a table with rows
int matrix[2][3] = {
and columns.
{1, 2, 3},
{4, 5, 6}
Example of a 2D Array:
};
A 2D array with 3 rows and 3 columns:
This creates a 2x3 matrix:

int matrix[3][3] = {
1 2 3
{1, 2, 3},
4 5 6
{4, 5, 6},
You can also initialize a 2D array without
{7, 8, 9}
specifying all the elements:
};
In this case, the 2D array matrix can be visualized
int matrix[2][3] = {1, 2, 3}; // First row: {1, 2, 3},
as:
Second row: {0, 0, 0}
1 2 3
4 5 6
Accessing Elements in a Multidimensional Array
7 8 9
Each element in a multidimensional array is
Each element is accessed using two indices: one for accessed by specifying the indices for each
the row and one for the column. dimension. For a 2D array, the first index specifies
the row, and the second index specifies the column.

Declaration of Multidimensional Arrays


Syntax:
The syntax for declaring a multidimensional array is
similar to that of a 1D array, except that you specify array_name[row_index][column_index];
multiple sets of square brackets for the dimensions.
For example, to access the element in the 2nd row
and 3rd column of the following array:

Syntax:
int matrix[3][3] = {

data_type array_name[size1][size2]...[sizeN]; {1, 2, 3},

For a 2D array (matrix) with 3 rows and 4 columns: {4, 5, 6},


{7, 8, 9}

int matrix[3][4]; // 3 rows, 4 columns };

For a 3D array with dimensions 3x3x3: You would use: f

int cube[3][3][3]; int value = matrix[1][2]; // Accessing the value in


the 2nd row and 3rd column (value = 6)
 Matrices in mathematics (e.g., matrix
addition and multiplication).
Multidimensional Arrays and Loops
 Game development, where a 2D grid may
Multidimensional arrays are often used with nested
represent a game board.
loops to traverse or manipulate elements. The outer
loop controls the row index, and the inner loop  Image processing, where a 2D array stores
controls the column index. pixel values of an image.
 Scientific computing, where 3D arrays may
be used for simulations and models.
Example of Traversing a 2D Array:
WEEK 9
Functions in C++
for (int i = 0; i < 3; i++) { // Loop through rows
Definition of a Function in C++:
for (int j = 0; j < 3; j++) { // Loop through
columns A function in C++ is a block of code designed to
perform a specific task. Functions enhance
cout << matrix[i][j] << " ";
modularity, improve code readability, and promote
} code reuse. They help in breaking complex
programs into smaller, manageable sections.
cout << endl;
Key Features of C++ Functions:
}
 Modularity: Divides code into modules for
This would output:
better organization.
 Code Reusability: Allows a function to be
123 used multiple times.

456  Readability: Simplifies complex code by


breaking it into smaller, logical functions.
789
Structure of a Function in C++:
A function in C++ follows this general structure:
Memory Layout of Multidimensional Arrays
return_type function_name(parameter_list) {
In C++, arrays are stored in row-major order. This
means that the elements of each row are stored // Function body (code that performs the task)
consecutively in memory.
return value; // (optional, depends on return_type)
}
For example, consider the following 2D array:
Components:
 return_type: Specifies the type of value
int matrix[2][3] = { returned (e.g., int, void, double). If no value
is returned, void is used.
{1, 2, 3},
 function_name: The identifier used to call
{4, 5, 6}
the function.
};
 parameter_list: A list of inputs (arguments)
In memory, the elements would be laid out as: the function takes. It can be empty if no
inputs are required.
 function body: The block of code defining
1 2 3 4 5 6
the function's operations.
 return statement: Used to return a value
Applications of Multidimensional Arrays (not needed for void functions).

Multidimensional arrays are widely used in Types of Functions in C++:


situations where data needs to be stored in a grid-
1. Void Functions (Do not return any value):
like structure, such as:
void displayMessage() {
cout << "Hello, World!"; Calling display(30); outputs 30 20 because only the
first argument is provided.
}
Function Overloading:
2. Value-Returning Functions (Return a
specific value): Function overloading allows multiple functions with
the same name but different parameter lists (number
int add(int a, int b) {
or types of parameters).
return a + b;
int multiply(int a, int b) {
}
return a * b;
Function Declaration and Definition:
}
 Function Declaration (Prototype): Informs
the compiler about the function's return type,
name, and parameters before its definition. double multiply(double a, double b) {
int add(int a, int b); // Function declaration return a * b;
 Function Definition: The actual }
implementation of the function.
The appropriate function is called based on the
int add(int a, int b) { // Function definition argument types.
return a + b; Summary:
} Functions in C++ enhance modularity, readability,
and code reusability. They can accept input
Calling a Function:
(arguments), perform specific tasks, and return
To execute a function, it must be called from main() output. Understanding function declaration,
or another function: definition, and usage is essential for efficient
programming in C++.
int result = add(5, 3); // Calling the add function
cout << result; // Output: 8
WEEK 10
Function Arguments:
1. Call by Value: A copy of the argument is
passed; changes inside the function do not
affect the original value.
2. Call by Reference: The memory address of
the argument is passed, allowing
modifications inside the function to affect
the original value.
Example:
void swap(int &x, int &y) { // Call by reference
int temp = x;
x = y;
y = temp;
}

Default Arguments:
C++ allows default values for function parameters.
void display(int x = 10, int y = 20) {
cout << x << " " << y;
}

You might also like