Model Test Paper 4
Model Test Paper 4
A destructor in C++ is a special member function that is invoked automatically when an object goes
out of scope or is explicitly deleted. It is used to perform cleanup operations such as releasing
resources or closing files. Here's an example:
```cpp
#include <iostream>
class MyClass {
public:
// Constructor
MyClass() {
// Destructor
~MyClass() {
};
int main() {
}
```
The 'Protected' access specifier in C++ allows the members of a class to be accessible within the class
itself and its derived classes. It ensures encapsulation while allowing derived classes to access the
base class's members. This is significant for implementing inheritance and building hierarchies of
classes where certain members should be accessible to derived classes.
Ambiguity in multiple inheritance arises when a class inherits from more than one class, and there is
a conflict in member names or functions. It can be resolved using scope resolution operator (::) to
specify from which base class the member should be accessed. Additionally, virtual inheritance can
be used to ensure that only one instance of a common base class is present in the object hierarchy.
### 4. What do you mean by Exception Handling? Write a program to show how it is achieved in
C++.
```cpp
#include <iostream>
int main() {
try {
int result = 10 / 0;
} catch (std::exception& e) {
return 0;
}
```
Polymorphism in C++ allows objects of different types to be treated as objects of a common base
type. It includes both compile-time (function overloading) and runtime (function overriding)
polymorphism. Here's an example demonstrating runtime polymorphism using virtual functions:
```cpp
#include <iostream>
class Shape {
public:
};
public:
};
int main() {
delete shapePtr;
return 0;
```
### Section-C
#### 10. Write a program in C++ to compute the average marks of 50 students in the class. Take
necessary assumptions.
```cpp
#include <iostream>
int main() {
int marks[50];
int sum = 0;
sum += marks[i];
return 0;
}
```
#### 11. What are constructors? Write sample code to show the working of constructors with
inheritance.
A constructor is a special member function that gets called when an object is created. It is used to
initialize the object. Here's an example demonstrating constructors with inheritance:
```cpp
#include <iostream>
class Base {
public:
Base() {
};
public:
Derived() {
};
int main() {
return 0;
```
#### 12. Write a program to overload unary + and unary - operators.
```cpp
#include <iostream>
class Number {
private:
int value;
public:
Number operator+() {
Number operator-() {
void display() {
};
int main() {
Number num(5);
return 0;
```
### Section-B
In call by value, the actual value of the argument is passed to the function, and any changes made to
the parameter inside the function do not affect the original value. In call by reference, the memory
address (reference) of the argument is passed, allowing changes made to the parameter inside the
function to affect the original value.
### Section-B
#### 6. Write a sample code to show the difference between C and C++.
```c
// C code
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
```
```cpp
// C++ code
#include <iostream>
int main() {
return 0;
```
#### 7. Write a code to compute the factorial of a number; use of constructors shall be done.
```cpp
#include <iostream>
class FactorialCalculator {
private:
int number;
public:
result *= i;
void displayResult() {
std::cout << "Factorial of " << number << " is: " << result << std::endl;
};
int main