Assignment C++
Assignment C++
Archita , 1311
Q1Difference Between Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP)
Focuses on procedures (functions) that operates Focuses on objects that combine data and methods
Definition
data. (functions).
Approach Follows a top-down approach, starting with functionns Follows a bottom-up approach, starting with objects.
Data Data is treated as a global entity and shared across Data is encapsulated within objects, ensuring data
Handling functions. security.
Modularity Programs are divided into functions. Programs are divided into classes and objects.
Code Limited reuse of code as functions are High code reusability using inheritance and
Reusability independent. polymorphism.
Security Less secure, as data is exposed globally. More secure due to data hiding through encapsulation.
Complexity Management Suitable for simple programs. Suitable for complex and large-scale programs.
1. Software Development
2. Web Development
o Backend systems using frameworks like Django (Python) and Spring (Java).
3. Game Development
o Used in creating 2D/3D games with engines like Unity and Unreal Engine.
8. Enterprise Applications
9. Network Applications
1. Class:
A class in C++ is a user-defined data type that acts as a blueprint for creating objects. It defines the properties (data members) and
behaviors (member functions) of objects.
A class allows data and functions to be bundled together, promoting encapsulation and data hiding.
Syntax of a Class:
class ClassName
{ public:
Member functions
void display() {
};
. Object:
An object is an instance of a class. It represents a real-world entity and uses the blueprint provided by the class.
Objects are used to access the data members and member functions defined in the class. Each object has its own copy of data members,
ensuring that changes in one object do not affect others.
Example:
Student s1;
1. Data Abstraction:
Data abstraction is the process of hiding the internal implementation details of an object and showing only the essential features to the
outside world. It focuses on what an object does rather than how it does it.
• Abstraction allows users to interact with objects without knowing their internal complexities.
• It is implemented in C++ using classes and access specifiers such as public, private, and protected.
• Purpose: To simplify code and improve maintainability by separating the interface from the implementation.
2. Encapsulation:
Encapsulation is the process of binding data and methods that operate on the data into a single unit, called a class. It protects the data
from unauthorized access by restricting it through access modifiers (private, public, protected).
• It ensures data security and allows controlled access to the data through getter and setter methods.
• Encapsulation helps achieve data hiding, preventing direct access to internal variables.
• Purpose: To make code more secure, organized, and easier to debug and maintain.
Polymorphism in C++ means "many forms" and allows a single function, operator, or object to behave in multiple ways. It enables
programmers to perform a single action in different forms, enhancing flexibility and reusability in code.
Types of Polymorphism:
Examples:
o Function Overloading: Multiple functions with the same name but different parameter types.
o The function to be executed is determined at runtime using pointers or base class references.
Example:
o Function Overriding: A derived class provides a specific implementation of a function that is already defined in the
base class
• Example:
• Example:
• Input Cascading:
nt a, b; cin >> a >> b; // Takes input for both 'a' and 'b' in one statement
Output Cascading:
cout << "A: " << a << ", B: " << b; // Displays both values in one statement
1. Identifiers in C++:
• An identifier is a name used to identify variables, functions, arrays, classes, etc. in C++.
o The maximum length of an identifier can vary based on the compiler, but it is usually not a concern with modern
compilers.
. Constants in C++:
• Constants can be of various types, such as integer constants, floating-point constants, and character constants.
• In C++, we define constants using the const keyword or #define preprocessor directive.
o const keyword:
Used to declare a constant whose value cannot be modified after initialization.
define directive:
Used to define a constant before the program is compiled. It’s a preprocessor directive, so it doesn't have a data type.
Types of Constants:
• Character Constants: Single characters enclosed in single quotes like 'A', '7'.
In C++, a string is a sequence of characters used to store and manipulate text. C++ provides two primary ways to handle strings:
• They are simply arrays of char where each element holds a character.
• C-style strings require manual handling of memory and length, and functions like strlen() from the <cstring> library are used to
determine the string's length.
• C++ provides the string class in the <string> header that makes string manipulation easier and more flexible.
• A string object in C++ can store any number of characters, and it automatically manages memory for you.
A reference variable in C++ is an alias for another variable. It allows you to access the original variable by a different name. A reference
variable is essentially a second name for an existing variable and must be initialized when declared.
2. No New Memory Allocation: Reference variables do not occupy separate memory. They directly refer to the memory location
of the variable they are linked to.
3. Cannot Be Null: A reference variable must always refer to a valid object or variable. It cannot be assigned to NULL or left
uninitialized after declaration.
4. Once Bound, Cannot Be Changed: After a reference is initialized to a variable, it cannot be reassigned to refer to another
variable.
1. Function Arguments: Reference variables are often used to pass function arguments by reference, allowing the function to
modify the original data without making a copy.
2. Returning Multiple Values: They can be used to return multiple values from a function.
3. Operator Overloading: Reference variables are commonly used in operator overloading to return modified objects.
Advantages:
• Efficiency: Reference variables avoid the overhead of copying large data structures (e.g., arrays, objects).
• Modifying Original Data: They allow easy modification of the original data without needing pointers or return values.
(D)Expressions in C++
An expression in C++ is a combination of variables, constants, operators, and function calls that are evaluated to produce a value. In
simpler terms, an expression represents a computation that results in a value.
C++ supports various types of expressions based on the kind of operation they perform. The primary types of expressions are:
1. Arithmetic Expressions
• These expressions perform arithmetic operations like addition, subtraction, multiplication, division, and modulus.
2. Relational Expressions
• These expressions compare two values and return a Boolean result (true or false).
• Relational operators include ==, !=, >, <, >=, and <=.
3. Logical Expressions
• Logical expressions use logical operators to combine multiple relational expressions and return a Boolean result.
• Logical operators include && (logical AND), || (logical OR), and ! (logical NOT).
4. Assignment Expressions
5. Increment/Decrement Expressions
• Increment operator: ++
• Decrement operator: --
6. Conditional (Ternary) Expression
7. Typecast Expressions
• Typecasting can be implicit (automatic) or explicit (using casting operators like static_cast, dynamic_cast, etc.).
The while loop in C++ is a control flow statement that allows code to be executed repeatedly based on a given condition. The
while loop continues to execute as long as the condition evaluates to true. Once the condition becomes false, the loop
terminates, and the program proceeds to the next statement after the loop .
Example:-
#include <iostream>
int main() {
int i = 1; // Initialization
i++; // Increment
return 0;
Output:-
12345
The do-while loop in C++ is a post-test loop, which means the condition is evaluated after the loop body has been executed. This ensures
that the loop body is executed at least once, even if the condition is false initially.
In this example, the program asks the user for input repeatedly until they enter a negative number:
#include <iostream>
int main() {
int number;
do {
cout << "Enter a number (negative number to stop): ";
return 0;
The switch statement in C++ is a control statement that allows you to execute one of many possible blocks of code based on the value of
an expression. It is an alternative to using multiple if-else statements when you need to check a variable against several possible values.
In C++, if you omit the break statement, the program will continue executing the next case block, even if the value of the expression
doesn't match. This behavior is called fall-through.
#include <iostream>
int main() {
int num = 2;
switch (num) {
case 1:
case 2:
case 3:
default:
return 0;
Function Overloading in C++ is a feature that allows you to define multiple functions with the same name but with different parameter
types, different number of parameters, or both. The compiler differentiates between these functions based on the type and number of
arguments passed when calling the function.
Function overloading helps in increasing code readability and reducing redundancy since multiple functions performing similar tasks can
share the same name.
2. Different types of parameters: Functions can be overloaded if the types of parameters are different (e.g., one takes int and
another takes double).
3. Different order of parameters: You can overload functions by changing the order of parameters of different types.
4. Return type alone cannot be used for overloading: Functions with the same name and different return types are not
considered overloaded if their parameters are the same.
return_type function_name(parameter_list);
An inline function in C++ is a function whose definition is inserted directly into the code at the point where it is called, instead of
performing a regular function call. The main goal of inline functions is to improve the performance of the program by avoiding the
overhead of function calls, particularly for small, frequently called functions.
• Performance Improvement: The main advantage of using inline functions is that they can improve performance by
eliminating the overhead of a function call, such as pushing arguments to the stack and performing a jump to the function
code.
• Efficiency in Small Functions: Inline functions are especially useful for small functions that are called frequently, like getters
and setters, where the function execution time might be significant relative to the overhead.
1. When an inline function is called, the compiler replaces the function call with the actual code of the function.
2. The function code is inserted directly at the point of the call, which eliminates the need for jumping to a separate memory
location.
3. This process is done by the compiler, and the decision to inline a function is made at compile-time.
To declare a function as inline, the inline keyword is placed before the return type of the function.
// Function body
In C++, arrays and pointers are closely related. An array can be treated as a pointer to its first element, and pointers can be used to
access elements of an array. This relationship allows for more flexible and efficient manipulation of arrays, especially when passing
them to functions or working with dynamic memory.
1. Array Name as a Pointer: The name of an array in C++ represents the address of the first element of the array. This means
that an array is essentially a pointer to its first element.
2. Accessing Array Elements Using Pointers: You can use pointers to access the elements of an array. Using pointer arithmetic,
you can move through the array and access each element.
3. Pointer Arithmetic: In C++, pointer arithmetic allows you to navigate through an array by incrementing or decrementing the
pointer. Each increment or decrement moves the pointer by the size of the data type the pointer is pointing to.
In C++, pointers and strings are two important concepts that are often used together, especially when working with dynamic memory
allocation or when passing strings to functions. Understanding how pointers and strings are related is essential for effective C++
programming.
There are two primary ways to represent strings in C++:
Here, we will focus more on C-style strings and their relationship with pointers, as they are closely related to pointers in C++.
A C-style string is an array of characters terminated by a null character ('\0'). This null character is used to mark the end of the string.
C++ program to read the values of a, b, and c, and then calculate the value of x where x = a / b - c:
#include <iostream>
int main() {
float a, b, c, x;
cin >> a;
cin >> b;
cin >> c;
x = a / b - c;
cout << "The value of x = " << a << " / " << b << " - " << c << " is: " << x << endl;
return 0;