cpp
cpp
age;
a) List any four features of OOPs (Object-
Oriented Programming):
#define PI 3.14
cpp
#include <iostream>
class B : public A { };
void display(string s) {
2. Multiple Inheritance – One derived
cout << "String: " << s << endl;
class inherits from more than one
} base class.
}; cpp
CopyEdit
class C : public B { };
b) What is inheritance? Explain types of
4. Hierarchical Inheritance – Multiple
inheritance:
classes inherit from the same base
Inheritance is the mechanism by which one class.
class (derived class) can acquire the
cpp
properties and behaviors (members) of
another class (base class). CopyEdit
class B : public A { };
class C : public A { }; }
int Counter::count = 0;
c) Explain static data members and static
member functions with example:
int main() {
Static data members are class-level
variables shared by all objects of the class. Counter c1, c2, c3;
Static member functions can be called
Counter::showCount(); // Output: Count:
using the class name and can access only
3
static data.
return 0;
Example:
}
cpp
CopyEdit
d) What is friend function? Write
#include <iostream>
characteristics of friend function:
using namespace std;
A friend function is a function that is not a
member of a class but is allowed to access
private and protected members of the class.
class Counter {
Characteristics:
private:
1. Defined outside the class but
static int count;
declared inside with friend keyword.
}; Q5
a) Array of Objects
#include <iostream>
e) Explain use of any four file opening
modes: using namespace std;
CopyEdit public:
cpp
void display() {
CopyEdit
cout << "Name: " << name << ", Age: "
ofstream file("data.txt", ios::out); << age << endl;
CopyEdit
int main() {
ofstream file("data.txt", ios::app);
Student s[2]; // Array of 2 Student objects
public:
}
c) Constructor in Derived Class
Example:
b) Access Specifiers
cpp
Access specifiers control the access levels of
CopyEdit
class members (variables and functions).
There are three main access specifiers: #include <iostream>
public: Accessible from anywhere. using namespace std;
};