0% found this document useful (0 votes)
33 views23 pages

Oop QB Ans

Uploaded by

pritish8754
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views23 pages

Oop QB Ans

Uploaded by

pritish8754
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

2 Marks Questions

1.Give the meaning of following statement

`int *ptr, a=5;

ptr=&a;

cout<<*ptr;

cout<<(*ptr)+1;`

int *ptr: Declares a pointer variable ptr that can hold the address of an integer.

a=5: Declares an integer variable a and initializes it with the value 5.

ptr=&a: Assigns the address of variable a to the pointer ptr.

cout<<*ptr;: Prints the value at the address stored in ptr, which is the value of a, i.e., 5.

cout<<(*ptr)+1;: Prints the value of a plus 1, which is 6.

2. Explain any 2 file stream classes.

ifstream: Input file stream class used for reading data from a file.

• Example:

ifstream infile("data.txt");

int num;

infile >> num;

ofstream: Output file stream class used for writing data to a file.

• Example:

ofstream outfile("output.txt");

outfile << "Hello, World!";

3. State the rule for operator overloading.

• 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.

4. Explain pointer operator and address operator with example.

1. Pointer Operator (*):

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 of a: " << a << endl;

cout << "Address of a: " << &a << endl;

cout << "Value stored at ptr: " << *ptr << endl;

getch();

2. Address Operator (&):

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 of a: " << &a << endl;

cout << "Address stored in ptr: " << ptr << endl;

getch();

5. Describe the meaning of the following:

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.

ios::trunc: Truncates the file to zero length before writing.

6. Differentiate between constructor and destructor. (any four points)


7. Describe ‘this’ pointer with example.

‘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() {

cout << "Length: " << this->length << endl;

};

void main() {

Box box1(10);

box1.display();

getch();

}
8. Differentiate between Static Binding and Dynamic Binding. (any four points)

9. List pointer operators with syntax and example.

Address-of Operator (&):

• Used to get the memory address of a variable.

• Syntax: &variable_name

• Example:

int x = 10;

int *ptr = &x; // ptr now holds the address of x

Dereference Operator (*):

• Used to access the value stored at a memory address pointed to by a pointer.

• Syntax: *pointer_name

• Example:

int y = *ptr; // y now holds the value of x (which is 10)

Member Access Operator (->):

• 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};

Point *ptr = &p1;

cout << ptr->x << " " << ptr->y; // Output: 10 20

Array Subscript Operator ([ ]):

• Used to access elements of an array through a pointer.

• Syntax: pointer_name[index]

• Example:

int arr[] = {1, 2, 3, 4, 5};

int *ptr = arr;

cout << ptr[2]; // Output: 3

10. List C++ file stream classes along with their function (any two classes).

ifstream: Input file stream class for reading from files.

ofstream: Output file stream class for writing to files.

11. Define pure virtual function.

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)

// Initialization list for base class members

// Initialization list for derived class members

14. Give any four rules of operator overloading.

• Only existing operators can be overloaded.


• Overloaded operators cannot have new precedence or associativity.
• Overloaded operators cannot change the number of operands.
• Overloaded operators cannot change the basic meaning of the operator.

15. List and describe any four file opening modes.

ios::in:

• Opens a file for reading.

• Used with ifstream objects.

ios::out:

• Opens a file for writing.

• Creates a new file if it doesn't exist, or overwrites an existing file.

• Used with ofstream objects.

ios::app:

• Opens a file for appending.

• Adds new content to the end of an existing file, or creates a new file if it doesn't exist.

• Used with ofstream objects.

ios::trunc:

• Opens a file for writing, truncating it to zero length.

• Any existing content in the file is erased.

• Used with ofstream objects.


4 Marks Questions

1.Explain different ways to open a file with syntax and example.

Using ifstream for Input:

• Syntax: ifstream infile("filename.txt");

• Example:

#include <fstream>

int main() {

ifstream infile("input.txt");

string line;

while (getline(infile, line)) {

cout << line << endl;

infile.close();

return 0;

Using ofstream for Output:

• Syntax: ofstream outfile("filename.txt");

• Example:

#include <fstream>

int main() {

ofstream outfile("output.txt");

outfile << "Hello, world!" << endl;

outfile.close();

return 0;

sing fstream for Both Input and Output:

• Syntax: fstream file("filename.txt", ios::in | ios::out);

• Example:
#include <fstream>

int main() {

fstream file("data.txt", ios::in | ios::out);

string line;

// Read existing content

while (getline(file, line)) {

cout << line << endl;

// Write new content

file << "\nNew content added.";

file.close();

return 0;

2. Describe the concept of virtual base class with suitable example.

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()

{ cout << "A" << endl; }

};

class B : virtual public A {

public:

void funcB() { cout << "B" << endl; }

};
class C : virtual public A {

public:

void funcC() { cout << "C" << endl; }

};

class D : public B, public C {

public:

void funcD() {

funcA(); // Calls A::funcA() unambiguously

};

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>

using namespace std;

class College {

public:

string name;

int code;

void input() {

cout << "Enter college name: ";

cin >> name;

cout << "Enter college code: ";

cin >> code;

}
void display() {

cout << "College Name: " << name << endl;

cout << "College Code: " << code << endl;

};

class Student : public College {

public:

string name;

void input() {

College::input(); // Inherit input from College

cout << "Enter student name: ";

cin >> name;

void display() {

College::display(); // Inherit display from College

cout << "Student Name: " << name << endl;

};

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>

using namespace std;

class String {

char str[100];

public:

void getInput() {

cout << "Enter a string: ";

cin >> str;

String operator+(String s) {

String temp;

strcpy(temp.str, str);

strcat(temp.str, s.str);

return temp;

void display() {

cout << str;

};

int main() {

String s1, s2, s3;

s1.getInput();

s2.getInput();
s3 = s1 + s2;

cout << "Concatenated string: ";

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>

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

void main() {

clrscr();

int x, y;

double p, q;

cout << "Enter two integers: ";

cin >> x >> y;

cout << "Enter two doubles: ";

cin >> p >> q;

cout << "Integer addition: " << add(x, y) << endl;

cout << "Double addition: " << add(p, q) << endl;

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();

ofstream outfile("example.txt", ios::app);

if (outfile) {

outfile << "object oriented\n";

outfile.close();

ifstream infile("example.txt");

char ch;

if (infile) {

cout << "File content:\n";

while (infile.get(ch)) {

cout << ch;

infile.close();

} else {

cout << "Error opening file.";

getch();

}
7. Write a program to overload operator `+` to add two complex numbers.

#include <iostream.h>

#include <conio.h>

class Complex {

int real, imag;

public:

Complex() {

real = 0;

imag = 0;

Complex(int r, int i) {

real = r;

imag = i;

Complex operator+(Complex &c) {

Complex temp;

temp.real = real + c.real;

temp.imag = imag + c.imag;

return temp;

void display() {

cout << real << " + " << imag << "i";

};

void main() {
clrscr();

Complex c1(3, 4), c2(5, 6);

Complex c3 = c1 + c2;

cout << "Sum of complex numbers: ";

c3.display();

getch();

8. Write a C++ program to append data from `abc.txt` to `xyz.txt` file.

#include <fstream.h>

#include <conio.h>

void main() {

clrscr();

ifstream infile("abc.txt"); // Open abc.txt for reading

ofstream outfile("xyz.txt", ios::app); // Open xyz.txt for appending

if (!infile) {

cout << "Error opening abc.txt";

getch();

return;

if (!outfile) {

cout << "Error opening xyz.txt";

getch();

return;

}
char ch;

while (infile.get(ch)) { // Read character by character from abc.txt

outfile.put(ch); // Append each character to xyz.txt

infile.close();

outfile.close();

cout << "Data appended from abc.txt to xyz.txt successfully.";

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>

int area(int side) {

return side * side;

double area(double radius) {

return 3.14159 * radius * radius;

double area(double base, double height) {

return 0.5 * base * height;

void main() {
clrscr();

int side;

double radius, base, height;

cout << "Enter side of square: ";

cin >> side;

cout << "Area of square: " << area(side) << endl;

cout << "Enter radius of circle: ";

cin >> radius;

cout << "Area of circle: " << area(radius) << endl;

cout << "Enter base and height of triangle: ";

cin >> base >> height;

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();

// Open file for writing and write the message

ofstream outfile("message.txt");

if (outfile) {

outfile << "Welcome to poly";

outfile.close();
} else {

cout << "Error creating file.";

getch();

return;

// Open file for reading and display the content

ifstream infile("message.txt");

char ch;

if (infile) {

cout << "File content: ";

while (infile.get(ch)) {

cout << ch;

infile.close();

} else {

cout << "Error opening file.";

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() {

cout << "Enter Student ID: ";

cin >> student_id;

cout << "Enter College Code: ";

cin >> college_code;

void displayCollegeStudentData() {

cout << "Student ID: " << student_id << "\n";

cout << "College Code: " << college_code << "\n";

};

class Test : virtual public CollegeStudent {

protected:

float percentage;

public:

void setTestScore() {

cout << "Enter Percentage: ";

cin >> percentage;

void displayTestScore() {
cout << "Percentage: " << percentage << "%\n";

};

class Sports : virtual public CollegeStudent {

protected:

char grade;

public:

void setSportsGrade() {

cout << "Enter Sports Grade: ";

cin >> grade;

void displaySportsGrade() {

cout << "Sports Grade: " << grade << "\n";

};

class Result : public Test, public Sports {

public:

void displayResult() {

displayCollegeStudentData();

displayTestScore();

displaySportsGrade();

};

void main() {

clrscr();

Result r;
cout << "Enter details for the student:\n";

r.setCollegeStudentData();

r.setTestScore();

r.setSportsGrade();

cout << "\nStudent Result:\n";

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>

using namespace std;

class Employee {

protected:

char name[30];

int emp_id;

public:

void Getdata() {

cout << "Enter Employee Name: ";


cin >> name;

cout << "Enter Employee ID: ";

cin >> emp_id;

void Putdata() {

cout << "Employee Name: " << name << "\n";

cout << "Employee ID: " << emp_id << "\n";

};

class Salary : private Employee {

float basic_pay, hra, da, cla, total_salary;

public:

void Getdata() {

Employee::Getdata();

cout << "Enter Basic Pay: ";

cin >> basic_pay;

cout << "Enter HRA: ";

cin >> hra;

cout << "Enter DA: ";

cin >> da;

cout << "Enter CLA: ";

cin >> cla;

void CalculateSalary() {

total_salary = basic_pay + hra + da + cla;

}
void DisplaySalary() {

Employee::Putdata();

cout << "Basic Pay: " << basic_pay << "\n";

cout << "HRA: " << hra << "\n";

cout << "DA: " << da << "\n";

cout << "CLA: " << cla << "\n";

cout << "Total Salary: " << total_salary << "\n";

};

int main() {

Salary s;

cout << "Enter details for the employee:\n";

s.Getdata();

s.CalculateSalary();

cout << "\nEmployee Salary Details:\n";

s.DisplaySalary();

return 0;

You might also like