Unit 1 QB
Unit 1 QB
Procedural Programming:
Data Encapsulation is the bundling of data (variables) and methods (functions) that operate on
the data into a single unit or class. It restricts direct access to some of an object's components,
which can prevent accidental data modification and promotes modularity and maintainability.
Importance:
Typecasting allows conversion between different data types. In C++, typecasting can be done
using explicit or implicit operators.
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
double x = 5.75;
int y;
return 0;
}
● Global Variable: Declared outside of any function, accessible by any function in the
program.
● Local Variable: Declared within a function, accessible only within that function.
The Scope Resolution Operator (::) can be used to distinguish global variables from local
variables.
cpp
Copy code
#include <iostream>
using namespace std;
void func() {
int x = 5; // Local variable
cout << "Local x: " << x << endl;
cout << "Global x: " << ::x << endl; // Accessing global variable
}
int main() {
func();
return 0;
}
5. Analyze the challenges associated with code reuse in procedural
programming compared to Object-Oriented Programming (OOP) that uses
inheritance and polymorphism. (4 M)
● Procedural Programming:
○ Functions may need to be repeated in different parts of the program.
○ Reuse is limited to function calls and requires rewriting or copying code.
○ Maintenance becomes difficult as the program grows.
● Object-Oriented Programming (OOP):
○ Inheritance allows code reuse through class hierarchies.
○ Polymorphism allows for flexible method overrides and function calls without
changing the underlying code.
○ Easier to maintain and extend due to encapsulation and abstraction.
Dynamic initialization happens when the value of a variable is determined at runtime, typically
using user input or a runtime calculation.
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n; // Dynamic initialization based on user input
class MyClass {
public:
int var = 10; // Member variable of MyClass
void show() {
cout << "Member var: " << var << endl; // Accessing member
variable
cout << "Global var: " << ::var << endl; // Accessing global
variable using scope resolution
}
};
int main() {
MyClass obj;
obj.show();
return 0;
}
An inline function is defined using the inline keyword. It tells the compiler to insert the
function's code directly at the point of the function call, potentially reducing overhead from
function calls.
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}
Procedural Programming:
Object-Oriented Programming:
● Focuses on objects (instances of classes), which encapsulate both data and functions.
● Uses concepts like inheritance, polymorphism, and encapsulation.
Applications of OOP:
class MyClass {
public:
int value;
MyClass(int val) : value(val) {}
};
int main() {
MyClass* obj = new MyClass(10);
cout << "Value: " << obj->value << endl; // Dereferencing member
delete obj;
return 0;
}
Operator precedence determines the order in which operators are evaluated in an expression.
Operators with higher precedence are evaluated first. For example:
Data Abstraction:
● Simplifies Complexity: Hides the implementation details and exposes only essential
features.
● Improves Maintainability: Modifications to internal details don't affect the external
interface.
● Increases Extensibility: New features can be added without disrupting existing
functionality.
class Account {
private:
double balance; // Encapsulated data
public:
void deposit(double amount) {
balance += amount;
}
double getBalance() {
return balance;
}
};
int main() {
Account acc;
acc.deposit(1000);
cout << "Balance: " << acc.getBalance() << endl;
return 0;
}
16. Design and implement a C++ program that demonstrates the use of
global and local variables. (5 D)
cpp
Copy code
#include <iostream>
using namespace std;
void demo() {
int localVar = 20; // Local variable
cout << "Local variable: " << localVar << endl;
cout << "Global variable: " << globalVar << endl;
}
int main() {
demo();
return 0;
}
Data Abstraction helps in isolating the complexity of a system by allowing users to interact with
objects without understanding their internal workings. This leads to:
18. Demonstrate the role and functionality of the scope resolution operator
in programming. (3 E)
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int var = 10;
cout << "Local var: " << var << endl;
cout << "Global var: " << ::var << endl; // Scope resolution to
access global variable
return 0;
}
int main() {
display(10); // Uses default argument
display(10, 20); // No default argument
return 0;
}
20. Analyze the concepts of data encapsulation and data abstraction in
object-oriented programming. (5 D)
Data Encapsulation:
Data Abstraction: