Oops Unit 4
Oops Unit 4
constants.
functions, variables, and classes that your program will use. Standard
libraries like iostream for input/output are included using #include.
4. Main Function: Every C++ program must have a main function, which is
the entry point for program execution. It has the following format:
5. C++
Assignment 4 (OOP)
int main() {
// Your program's code goes here
return 0; // Indicates successful termination
}
6.
7.
8. Function Definitions: Here you define the functionality of your custom
functions declared earlier. These functions can perform specific tasks and
may take arguments and return values.
9. Code Body: The main function's body contains the actual program
statements that instruct the computer what to do. This might involve
calculations, input/output operations, function calls, and control flow
statements.
example). Namespaces allow you to group these functions under their respective
namespaces, like std::calculateArea and customMath::calculateArea.
To use a function or variable from a namespace, you can either prefix it with the
namespace name (e.g., std::cout) or use a using statement to bring it into the
current scope.
and lowercase), digits, and underscores. However, they cannot start with a
digit.
3. Reserved Keywords: You cannot use reserved keywords (e.g., int, if, for,
variables and functions, where each word (except the first) is capitalized,
6. All Uppercase for Constants: Constants are typically named using all
PI_VALUE, MAX_SIZE.
● Valid Identifiers:
● myVariable
● _value
● sum_of_values
● variable123
● Invalid Identifiers:
● if (reserved keyword)
makes it easier for other developers (and yourself, in the future) to understand
C++ offers various data types for storing different kinds of values.
Here are some common ones:
Select the data type that can efficiently store the range of values your
variable will hold. Consider memory usage and required precision
when making a choice.
Variables:
Example:
C++
content_copy
Constants:
(#define).
Example:
C++
content_copy
Significance:
Assignment 4 (OOP)
Operators in C++:
● Arithmetic Operators: These are used for basic arithmetic operations such
as addition (+), subtraction (-), multiplication (*), division (/), and modulus
(%).
● Relational Operators: These are used for comparisons between two values.
They include less than (<), greater than (>), less than or equal to (<=),
include logical AND (&&), logical OR (||), and logical NOT (!).
include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~),
example, =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=.
Assignment 4 (OOP)
(decrement).
evaluated.
operators with lower precedence. For example, multiplication (*) has higher
same precedence. For example, addition (+) and subtraction (-) have left
1. Static_cast:
double).
2. Dynamic_cast:
be used in a program.
Sure, let's start with discussing the control structures available in C++:
Assignment 4 (OOP)
1. if-else statement: This control structure is used for decision making. It executes a block
#include <iostream>
int main() {
int x = 10;
if (x > 5) {
} else {
return 0;
2. switch-case statement: This control structure is used to execute one block of code from
int main() {
char grade = 'B';
switch(grade) {
case 'A':
cout << "Excellent!" << endl;
Assignment 4 (OOP)
break;
case 'B':
cout << "Good!" << endl;
break;
case 'C':
cout << "Fair!" << endl;
break;
default:
cout << "Invalid grade" << endl;
}
return 0;
3. Loops:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << "Iteration: " << i << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 5) {
cout << "Iteration: " << i << endl;
i++;
}
return 0;
}
do-while loop: It is similar to a while loop, but the block of code is executed at least once before
int main() {
int i = 0;
do {
cout << "Iteration: " << i << endl;
i++;
} while (i < 5);
return 0;
}
arguments and returns their sum. Demonstrate how this function can
#include <iostream>
return a + b;
int main() {
return 0;
In this program, the add function takes two integers a and b as arguments and returns their sum.
The main function calls this add function with two integers num1 and num2, and then prints the
result.
1. Call by Value:
Changes made to the parameter inside the function do not affect the
original argument.
It's suitable when you don't want the function to modify the original data.
It's less efficient for large data types since it involves making a copy.
Example:
#include <iostream>
Assignment 4 (OOP)
void increment(int x) {
x++;
int main() {
int num = 5;
increment(num);
cout << "Original value of num: " << num << endl;
return 0;
Output:
2. Call by Reference:
Assignment 4 (OOP)
Changes made to the parameter inside the function directly affect the
original argument.
It's suitable when you want the function to modify the original data.
It's more efficient for large data types since it avoids making a copy.
Example:
#include <iostream>
x++;
int main() {
int num = 5;
increment(num);
cout << "Updated value of num: " << num << endl;
return 0;
}
Assignment 4 (OOP)
Output:
In the call by value example, the function `increment` receives a copy of the
`num` variable, so changes made inside the function don't affect the
original `num` variable. However, in the call by reference example, the
function `increment` receives the memory address of `num`, so changes
made inside the function directly modify the original `num` variable.
In C++, inline functions are a technique to suggest to the compiler that it should
substitute the function's code directly at each call site. This can potentially
improve performance by eliminating the function call overhead, which involves
pushing arguments onto the stack, jumping to the function code, saving the
return address, and returning from the function.
inserted directly into the calling code, there's no need to push arguments
onto the stack, jump to the function, execute its code, and then return.
2. Code Size: Inline functions can lead to larger executable code size because
the function's code is duplicated at each call site. This can result in
increased binary size, which may or may not be desirable depending on the
situation.
because the compiler needs to process the function's code at each call site
5. Code Duplication: Since the function's code is duplicated at each call site,
inline functions can lead to code duplication. This can be problematic if the
● Increased Code Size: When a function is inlined, the same code gets
duplicated at each call site, potentially bloating the program size. This can
be an issue for large or frequently called functions.
● Compile Time Impact: Inlining can increase compile times because the
compiler needs to analyze the function code at each call site.
● Loss of Optimization Opportunities: The compiler might have
opportunities to optimize the function code in its separate definition, but
inlining bypasses those optimizations.
● Not Guaranteed: As mentioned earlier, the compiler has the final say on
whether to inline a function or not.
In Summary
Macros and inline functions are both mechanisms in C++ that facilitate code
reuse and optimization, but they serve slightly different purposes and have
different characteristics. Let's discuss their differences and when it's appropriate
1. Macros:
● Macros lack type checking and scoping rules because they operate
● Macros do not create function calls; they directly replace the macro
bloat.
● Macros are often used for simple, repetitive tasks or for defining
constants.
2. Inline Functions:
● They are actual functions that the compiler can choose to inline at
the call site, essentially replacing the function call with the function
body.
● Inline functions follow all the usual rules of C++ functions, including
type safety.
● They don't guarantee inlining; the compiler may choose not to inline
● Macros:
● Use macros when you need to manipulate the source code before
● Inline Functions:
Assignment 4 (OOP)
● Use inline functions when you need type safety, scoping, and other
● Use inline functions for small, frequently called functions, where the
● Use inline functions when you want the compiler to have the option
● Use inline functions for non-trivial code that benefits from inlining
and maintainability.
In summary, while macros and inline functions both offer ways to optimize code,
macros are more about textual substitution and are suitable for simple tasks,