Object Oriented Concepts Through Cpp
Object Oriented Concepts Through Cpp
[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!";
}