C++ Long Answer Solutions - 4 Marks
1. Virtual Base Class
Theory: In C++, a virtual base class is used to solve the diamond problem in multiple inheritance. When a
class is derived from two classes which are themselves derived from a common base class, there can be
ambiguity because the derived class inherits two copies of the base class. Virtual inheritance ensures only
one copy exists.
Example:
#include <iostream>
using namespace std;
class A {
public:
int x;
};
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C {
public:
void show() {
x = 10;
cout << "Value of x: " << x << endl;
}
};
int main() {
D obj;
obj.show();
return 0;
}
2. 'this' Pointer
Theory: - Every object has an implicit pointer called 'this' pointing to itself. - Useful for resolving naming
conflicts, returning current object, and method chaining.
Example:
1
#include <iostream>
using namespace std;
class Sample {
int x;
public:
void setValue(int x) {
this->x = x;
}
Sample* getObject() {
return this;
}
void show() {
cout << "Value: " << x << endl;
}
};
int main() {
Sample obj;
obj.setValue(5);
obj.show();
Sample* ptr = obj.getObject();
ptr->show();
return 0;
}
3. Write into a File Using File Operations
Theory: - ofstream is used to write data to files. - Open file, write, close file.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("example.txt");
if (file.is_open()) {
file << "Hello, this is C++ file writing example.\n";
file << "We can write multiple lines into the file.\n";
file.close();
cout << "Data written successfully." << endl;
} else {
2
cout << "Unable to open file." << endl;
}
return 0;
}
4. Compile-time vs Run-time Polymorphism
Feature Compile-Time Polymorphism Run-Time Polymorphism
Definition Resolved at compile time Resolved at runtime
Achieved by Function/Operator Overloading Virtual functions
Performance Faster Slightly slower
Flexibility Less flexible More flexible
Example of Compile-time:
class Example {
public:
void show(int x) { cout << "Int: " << x << endl; }
void show(double y) { cout << "Double: " << y << endl; }
};
Example of Run-time:
class Base {
public:
virtual void show() { cout << "Base class" << endl; }
};
class Derived : public Base {
public:
void show() override { cout << "Derived class" << endl; }
};
5. Class Student and Pointer Access
#include <iostream>
using namespace std;
3
class Student {
public:
int rollno;
string name;
int age;
void display() {
cout << "Roll No: " << rollno << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Student s;
Student* ptr = &s;
ptr->rollno = 101;
ptr->name = "Aryan";
ptr->age = 20;
ptr->display();
return 0;
}
6. Single Inheritance
#include <iostream>
using namespace std;
class Employee {
public:
int emp_id;
string name;
};
class Emp_info : public Employee {
public:
float basic_salary;
void display() {
cout << "ID: " << emp_id << endl;
cout << "Name: " << name << endl;
cout << "Salary: " << basic_salary << endl;
}
};
4
int main() {
Emp_info e;
e.emp_id = 101;
e.name = "Aryan";
e.basic_salary = 50000;
e.display();
return 0;
}
7. Detection of End-of-File and File Modes
• Use eof() to detect end of file.
• File Modes: ios::in , ios::out , ios::app , ios::ate , ios::binary .
Example:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("example.txt");
string line;
while (!file.eof()) {
getline(file, line);
cout << line << endl;
}
file.close();
return 0;
}
8. Multilevel Inheritance
#include <iostream>
using namespace std;
class A { public: int a; };
class B : public A { public: int b; };
class C : public B {
public:
int c;
void display() { cout << "A: " << a << ", B: " << b << ", C: " << c <<
5
endl; }
};
int main() {
C obj;
obj.a = 10;
obj.b = 20;
obj.c = 30;
obj.display();
return 0;
}
9. Copy Contents of One File into Another
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream source("source.txt");
ofstream dest("dest.txt");
if (source.is_open() && dest.is_open()) {
string line;
while (getline(source, line)) {
dest << line << endl;
}
source.close();
dest.close();
cout << "File copied successfully." << endl;
} else {
cout << "Error opening files." << endl;
}
return 0;
}