0% found this document useful (0 votes)
12 views3 pages

C++ Control Flow and Functions Guide

The document outlines control flow in C++, detailing three main types: conditional/selection statements, iteration/loop statements, and jump statements. It also explains functions, including their parts, specifications, and the concept of overloading, as well as constructors and destructors in object-oriented programming. Additionally, it covers principles of OOP such as abstraction, encapsulation, and inheritance, along with design patterns.

Uploaded by

yefyegolme
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)
12 views3 pages

C++ Control Flow and Functions Guide

The document outlines control flow in C++, detailing three main types: conditional/selection statements, iteration/loop statements, and jump statements. It also explains functions, including their parts, specifications, and the concept of overloading, as well as constructors and destructors in object-oriented programming. Additionally, it covers principles of OOP such as abstraction, encapsulation, and inheritance, along with design patterns.

Uploaded by

yefyegolme
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

Data Structures & Algorithms goto – jumps to a labeled statement in

the same function.


Control Flow
return – ends function execution and
Control flow in C++ determines the
optionally returns a value.
order in which statements and
instructions are executed in a program Chapter 2.2 (Functions,
Constructors, Destructors)
Three main types of control flow in
C++ Function

 Conditional/Selection Statements  A function is a chunk of code that


 Iteration/Loop Statements performs a specific task.
 Jump Statements  Functions that return no value are
sometimes called procedures.
Conditional/Selection Statements
Parts of a Function
 These execute specific blocks of
code based on whether a given a. Return Type – specifies type of value
condition is true or false. returned (int, float, char, void).

if statement b. Function Name – descriptive


identifier.
 Executes a block of code if the
given condition is true. c. Argument List – placeholders for
values passed to function.
if-else statement
d. Function Body – block of C++
 Executes one block if the condition
statements inside { }.
is true, otherwise executes the else
block. Function Specification in C++

if-else if ladder Declaration (Prototype): tells compiler


about name, return type, parameters;
 Checks multiple conditions one
ends with ;.
after another. The first true
condition’s block is executed. Definition: actual implementation of
function.
switch statement
Call: where function is invoked.
 Executes different blocks of code
based on the value of an Constant Reference
expression. A simpler alternative to
 A reference (&) that is read-only
multiple if-else-if conditions.
because of const.
Iteration/Loop Statements  Used for efficiency (avoids copying
large objects).
 Allow repeated execution of a block
 Provides safety (prevents
of code until a condition is met.
modification of original data).
for loop – repeats a block for a specific
Syntax:
number of iterations.
 Void function(const Type
while loop – repeats as long as the
&parameter);
condition is true (entry-controlled).
Array Arguments
do-while loop – executes at least once
before checking the condition (exit-  Passing an array means passing
controlled). the address of the first element,
not the whole array.
Jump Statements
Overloading
Used to alter the flow of a program.
Function Overloading → same function
break – terminates the loop entirely.
name, different parameter list.
continue – skips the current iteration
Operator Overloading → redefine
and moves to the next.
operators (+, -, ==, []) for user-defined
types.
Robustness → handles unexpected
inputs gracefully.

Adaptability → evolves with changing


conditions.
Inline Functions
Portability → runs on different
 Compiler replaces function call with platforms.
actual code.
Reusability → code reused in future
 Efficient for small, frequent
applications.
functions.
 Not recommended for large ---
functions (causes code bloat).
Principles of OOP
Constructors
1. Abstraction – hide details, show only
 Special member function, auto- essentials.
executes when object is created.
2. Encapsulation – wrap data &
Purpose: initialize object data members. methods into a single unit (class).

Characteristics: Protects data (private), allows controlled


access (getters/setters).
Same name as class
3. Modularity – divide program into
No return type
independent modules.
Can be overloaded
Design Patterns
Runs once when object is created
 General reusable template for
Types: solving common problems.

1. Default Constructor (no arguments) Elements: Name, Context, Template,


2. Parameterized Constructor (with Result.
arguments)
Inheritance
3. Copy Constructor (uses another
object to initialize)  Mechanism where a derived class
acquires properties & behavior of a
Destructors
base class.
 Special member function, auto-
Types:
executes when object is destroyed.
 Single: one base → one derived.
Purpose: free resources (memory, files,
 Multiple: one derived → multiple
connections).
bases.
Characteristics:  Multilevel: derived from another
derived.
Same name as class, preceded by ~
 Hierarchical: multiple derived
No arguments, no return type classes share one base.
Only one destructor per class Hybrid: combination of types.
Called automatically at end of scope Modes:
Order:  Public inheritance: base public →
derived public; base protected →
 Constructors run in defined order.
derived protected.
Destructors run in reverse order (LIFO).  Protected inheritance: base
public/protected → derived
Chapter 2.3 (OOP Principles &
protected.
Inheritance)
 Private inheritance: base
Goals of OOP public/protected → derived private.
Correctness → right output for all valid
inputs.

You might also like