0% found this document useful (0 votes)
10 views9 pages

Object Oriented Concepts Through Cpp

The document contains a series of questions and answers related to Object-Oriented Programming concepts in C++, including encapsulation, binding, inline functions, streams, friend functions, constructors, inheritance, and file manipulation. It provides definitions, examples, and explanations of various programming constructs and their usage in C++. Additionally, it includes sample C++ programs demonstrating specific functionalities such as file copying, area calculation, and string comparison.

Uploaded by

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

Object Oriented Concepts Through Cpp

The document contains a series of questions and answers related to Object-Oriented Programming concepts in C++, including encapsulation, binding, inline functions, streams, friend functions, constructors, inheritance, and file manipulation. It provides definitions, examples, and explanations of various programming constructs and their usage in C++. Additionally, it includes sample C++ programs demonstrating specific functionalities such as file copying, area calculation, and string comparison.

Uploaded by

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

Q1) Attempt any EIGHT of the following (out of TEN).

[2×8=16]
a) What is Encapsulation?
Encapsulation is a fundamental principle of Object-Oriented Programming (OOP)
that binds data and functions together into a single unit (class) while restricting
direct access to the data from outside the class.
b) Define the following terms:
i) Early Binding: Early binding (also called static binding) occurs at compile-time
when the function to be called is resolved. Example: Function overloading.
ii) Late Binding: Late binding (also called dynamic binding) occurs at runtime
when the function to be called is resolved. Example: Function overriding using
virtual functions.
c) What is an Inline function?
An inline function is a function whose code is substituted at the place where it is
called, reducing function call overhead.
Syntax:
cpp
CopyEdit
inline int add(int a, int b) {
return a + b;
}
d) Explain get() and put() function.
 get(): Used to read a single character from an input stream.
cpp
CopyEdit
char ch;
cin.get(ch);
 put(): Used to write a single character to an output stream.
cpp
CopyEdit
cout.put('A');
e) What is a Stream?
A stream is a sequence of bytes used for input and output operations. Examples:
cin, cout, cerr, clog, ifstream, ofstream.
f) Define Friend function.
A friend function is a function that is not a member of a class but has access to its
private and protected members.
Syntax:
cpp
CopyEdit
class Sample {
int num;
friend void display(Sample);
};
g) Explain the use of new operator, state the syntax.
The new operator dynamically allocates memory in C++.
Syntax:
cpp
CopyEdit
int *ptr = new int; // Allocates memory for an integer
h) State the need of the virtual keyword.
The virtual keyword is used in base class methods to allow function overriding in
derived classes, enabling runtime polymorphism.
i) State user-defined data types in C++.
1. Class
2. Structure
3. Union
4. Enumeration (enum)
j) Explain the use of the Scope Resolution operator (::).
The :: operator is used to access global variables or functions, resolve ambiguity in
multiple inheritance, and define functions outside the class.
Example:
cpp
CopyEdit
class Sample {
public:
void display();
};
void Sample::display() { // Scope resolution
cout << "Hello!";
}

Q2) Attempt any FOUR of the following (out of FIVE). [4×4=16]


a) List different types of constructors. Explain any one with an example.
Types of constructors:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Destructor (though not a constructor, it plays a similar role)
Example of a Copy Constructor:
cpp
CopyEdit
class Sample {
int x;
public:
Sample(int a) { x = a; }
Sample(const Sample &obj) { x = obj.x; }
void display() { cout << "Value: " << x; }
};
b) What is function overloading? Explain with a suitable example.
Function overloading allows multiple functions with the same name but different
parameters.
cpp
CopyEdit
class Sample {
public:
void display(int a) { cout << a; }
void display(double b) { cout << b; }
};
c) Describe different types of inheritance.
1. Single Inheritance: One class derives from another.
2. Multiple Inheritance: A class inherits from more than one base class.
3. Multilevel Inheritance: A derived class is used as a base class for another
class.
4. Hierarchical Inheritance: Multiple classes inherit from a single base class.
5. Hybrid Inheritance: A combination of multiple and multilevel inheritance.
d) Explain virtual base class with a suitable diagram.
A virtual base class prevents duplicate members in multiple inheritance.
cpp
CopyEdit
class A { public: int x; };
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
e) Describe file manipulators with their syntax.
File manipulators modify the format of output streams.
Examples:
 setw(n): Set width
 setprecision(n): Set decimal precision
 setfill(c): Fill with a character
Syntax:
cpp
CopyEdit
cout << setw(10) << setfill('*') << 25;

Q3) Attempt any FOUR of the following (out of FIVE). [4×4=16]


a) C++ program to copy contents of one file to another.
cpp
CopyEdit
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream fin("source.txt");
ofstream fout("destination.txt");
char ch;
while (fin.get(ch)) fout.put(ch);
fin.close();
fout.close();
}
b) C++ program to calculate area and circumference of a circle using inline
function.
cpp
CopyEdit
#include <iostream>
using namespace std;
inline float area(float r) { return 3.14 * r * r; }
inline float circumference(float r) { return 2 * 3.14 * r; }
int main() {
float r = 5;
cout << "Area: " << area(r) << "\nCircumference: " << circumference(r);
}
c) Class with inheritance (Vehicle example).
cpp
CopyEdit
class Vehicle {
public:
void display() { cout << "This is a vehicle\n"; }
};
class TwoWheeler : public Vehicle {};
class ThreeWheeler : public Vehicle {};
class FourWheeler : public Vehicle {};
d) Program using setfill() and setiosflags().
cpp
CopyEdit
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setiosflags(ios::left) << setw(10) << setfill('*') << "Hello";
}
e) Program to compare two strings using overloaded == operator.
cpp
CopyEdit
#include <iostream>
#include <string.h>
using namespace std;
class String {
char str[20];
public:
void get() { cin >> str; }
bool operator==(String s) { return strcmp(str, s.str) == 0; }
};
int main() {
String s1, s2;
s1.get();
s2.get();
cout << (s1 == s2 ? "Equal" : "Not Equal");
}

Q4) Attempt any FOUR of the following (out of FIVE). [4×4=16]


a) Trace the output of the given program:
cpp
CopyEdit
#include <iostream.h>
int i, j;
class sample {
public:
sample(int a = 0, int b = 0) {
i = a;
j = b;
show();
}
void show() { cout << j << " "; }
};
void main() {
sample (5, 10);
int &x = i;
int &y = j;
i++;
cout << x-- << " " << ++y;
}
Output Explanation:
 sample(5,10) initializes i=5, j=10, then show() prints 10.
 i++ makes i = 6, x-- prints 6 and decrements x to 5.
 ++y increments j to 11.
Final Output:
CopyEdit
10 6 11

You might also like