Oop QB Ans
Oop QB Ans
ptr=&a;
cout<<*ptr;
cout<<(*ptr)+1;`
int *ptr: Declares a pointer variable ptr that can hold the address of an integer.
cout<<*ptr;: Prints the value at the address stored in ptr, which is the value of a, i.e., 5.
ifstream: Input file stream class used for reading data from a file.
• Example:
ifstream infile("data.txt");
int num;
ofstream: Output file stream class used for writing data to a file.
• Example:
ofstream outfile("output.txt");
• At least one operand of the operator must be a user-defined type (class or struct).
• The overloaded operator must be a non-member function or a member function with at
least one parameter of the class type.
• The return type of the overloaded operator must be defined.
The pointer operator is used to declare a pointer and to dereference it (access the value stored at the
memory address it points to).
Example:
#include<iostream.h>
#include<conio.h>
void main() {
int a = 5;
int *ptr;
ptr = &a;
cout << "Value stored at ptr: " << *ptr << endl;
getch();
The address operator is used to get the memory address of a variable. When you use & before a
variable, it returns the address of that variable.
Example:
#include<iostream.h>
#include<conio.h>
void main() {
int a = 5;
int *ptr;
ptr = &a;
cout << "Address stored in ptr: " << ptr << endl;
getch();
1. `ios::app`
2. `ios:: trunc`
ios::app: Appends to the end of an existing file or creates a new file if it doesn't exist.
‘This’ pointer in C++ is an implicit pointer available in non-static member functions. It points to the
current object of the class. It is used to refer to the object's members, especially when there's
ambiguity between member variables and function parameters.
Example:
#include<iostream.h>
#include<conio.h>
class Box {
int length;
public:
Box(int length) {
this->length = length;
void display() {
};
void main() {
Box box1(10);
box1.display();
getch();
}
8. Differentiate between Static Binding and Dynamic Binding. (any four points)
• Syntax: &variable_name
• Example:
int x = 10;
• Syntax: *pointer_name
• Example:
• Used to access members of a structure or class through a pointer to that structure or class.
• Syntax: pointer_name->member_name
• Example:
struct Point {
int x, y;
};
Point p1 = {10, 20};
• Syntax: pointer_name[index]
• Example:
10. List C++ file stream classes along with their function (any two classes).
A pure virtual function is a virtual function declared with a pure specifier (= 0). It has no
implementation in the base class, forcing derived classes to provide their own implementations.
12. Differentiate between compile time and run time polymorphism. (any four points).
13. Give the syntax for constructor in derived classes.
DerivedClassName::DerivedClassName(parameters) : BaseClassName(parameters)
ios::in:
ios::out:
ios::app:
• Adds new content to the end of an existing file, or creates a new file if it doesn't exist.
ios::trunc:
• Example:
#include <fstream>
int main() {
ifstream infile("input.txt");
string line;
infile.close();
return 0;
• Example:
#include <fstream>
int main() {
ofstream outfile("output.txt");
outfile.close();
return 0;
• Example:
#include <fstream>
int main() {
string line;
file.close();
return 0;
A virtual base class is used to avoid multiple inheritance ambiguity. When a class inherits from two or
more classes that have a common base class, the virtual base class ensures that only one instance of
the common base class is inherited.
Example:
class A {
public:
void funcA()
};
public:
};
class C : virtual public A {
public:
};
public:
void funcD() {
};
3. Write a C++ program to declare a class `college` with name and code. Derive new class as
`student` with members as name. Accept and display details of one student along with college
data.
#include <iostream>
#include <string>
class College {
public:
string name;
int code;
void input() {
}
void display() {
};
public:
string name;
void input() {
void display() {
};
int main() {
Student s;
s.input();
s.display();
return 0;
}
4. Write a C++ program to overload binary operator `+` to concatenate two strings.
#include <iostream>
#include <cstring>
class String {
char str[100];
public:
void getInput() {
String operator+(String s) {
String temp;
strcpy(temp.str, str);
strcat(temp.str, s.str);
return temp;
void display() {
};
int main() {
s1.getInput();
s2.getInput();
s3 = s1 + s2;
s3.display();
return 0;
5. Write a C++ program to overload `add` function to add two integer numbers and two double
numbers.
#include <iostream.h>
#include <conio.h>
return a + b;
return a + b;
void main() {
clrscr();
int x, y;
double p, q;
getch();
}
6. Develop C++ program to open and read content of file also write “object oriented” string in file
and close it.
#include <fstream.h>
#include <conio.h>
void main() {
clrscr();
if (outfile) {
outfile.close();
ifstream infile("example.txt");
char ch;
if (infile) {
while (infile.get(ch)) {
infile.close();
} else {
getch();
}
7. Write a program to overload operator `+` to add two complex numbers.
#include <iostream.h>
#include <conio.h>
class Complex {
public:
Complex() {
real = 0;
imag = 0;
Complex(int r, int i) {
real = r;
imag = i;
Complex temp;
return temp;
void display() {
cout << real << " + " << imag << "i";
};
void main() {
clrscr();
Complex c3 = c1 + c2;
c3.display();
getch();
#include <fstream.h>
#include <conio.h>
void main() {
clrscr();
if (!infile) {
getch();
return;
if (!outfile) {
getch();
return;
}
char ch;
infile.close();
outfile.close();
getch();
9. Write a C++ program to overload `area()` function to calculate area of shapes like triangle,
square, circle.
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main() {
clrscr();
int side;
cout << "Area of triangle: " << area(base, height) << endl;
getch();
10. Write a C++ program to write ‘Welcome to poly’ in a file. Then read the data from file and
display it on screen.
#include <fstream.h>
#include <conio.h>
void main() {
clrscr();
ofstream outfile("message.txt");
if (outfile) {
outfile.close();
} else {
getch();
return;
ifstream infile("message.txt");
char ch;
if (infile) {
while (infile.get(ch)) {
infile.close();
} else {
getch();
11. Identify the type of inheritance and write a C++ program to implement following inheritance:
#include <iostream.h>
#include <conio.h>
class CollegeStudent {
protected:
int student_id;
int college_code;
public:
void setCollegeStudentData() {
void displayCollegeStudentData() {
};
protected:
float percentage;
public:
void setTestScore() {
void displayTestScore() {
cout << "Percentage: " << percentage << "%\n";
};
protected:
char grade;
public:
void setSportsGrade() {
void displaySportsGrade() {
};
public:
void displayResult() {
displayCollegeStudentData();
displayTestScore();
displaySportsGrade();
};
void main() {
clrscr();
Result r;
cout << "Enter details for the student:\n";
r.setCollegeStudentData();
r.setTestScore();
r.setSportsGrade();
r.displayResult();
getch();
12. Define classes to appropriately represent class hierarchy as shown in above figure. Use
member functions for both classes and display Salary for a particular employee.
#include <iostream>
class Employee {
protected:
char name[30];
int emp_id;
public:
void Getdata() {
void Putdata() {
};
public:
void Getdata() {
Employee::Getdata();
void CalculateSalary() {
}
void DisplaySalary() {
Employee::Putdata();
};
int main() {
Salary s;
s.Getdata();
s.CalculateSalary();
s.DisplaySalary();
return 0;