OOP Imp (1)
OOP Imp (1)
OOP Imp (1)
Imp Short
Answer:-
1. Encapsulation:
o Bundling data and methods that operate on the data within a single unit (class).
o Restricts direct access to some of an object's components, which can protect the
integrity of the data.
2. Abstraction:
o Hides complex implementation details and shows only the essential features of an
object.
o Simplifies the interaction with the object by providing a clear interface.
3. Inheritance:
o Allows a class (child class) to inherit properties and methods from another class
(parent class).
o Promotes code reuse and establishes a hierarchical relationship between classes.
4. Polymorphism:
o Enables objects to be treated as instances of their parent class rather than their
actual class.
o Allows methods to be used interchangeably, depending on the object type,
facilitating method overriding and overloading.
Answer:-
Class:
Object:
• Instance of a class.
• Represents a specific entity with actual data and methods.
• Occupies memory when created.
Answer:-
Code Reusability:
• Inheritance allows new classes to reuse and extend the properties and methods of existing
classes.
• Polymorphism enables methods to be used interchangeably, allowing for flexible and reusable
code structures.
Maintainability:
• Encapsulation ensures that internal object details are hidden, making it easier to update and
maintain code without affecting other parts of the program.
• Abstraction simplifies complex systems by exposing only necessary components, making the
codebase easier to understand and manage.
Modularity:
• Classes and objects allow code to be organized into distinct modules or components, each
responsible for specific functionality.
• Promotes separation of concerns, where each module can be developed, tested, and debugged
independently.
4) What is polymorphism.
Answer:-
5) Types of polymorphism.
Answer :-
Types:
Answer :-
Run-time polymorphism, also known as dynamic polymorphism, is a feature of Object-
Oriented Programming (OOP) that allows objects to be treated as instances of their base class while
invoking methods that are specific to the derived class. This type of polymorphism is resolved during the
program's execution rather than at compile-time.
7) Static polymorphism.
Answer :-
8) Dynamic polymorphism.
Answer:-
Dynamic Polymorphism:
1. Definition:
o Ability to call methods of a derived class through a base class pointer or reference, with
method resolution occurring at run-time.
2. Key Concepts:
o Method Overriding:
▪ Subclass provides a specific implementation of a method that is already defined
in its base class.
o Virtual Functions:
▪ Methods in the base class declared with the virtual keyword, allowing them
to be overridden in derived classes.
Answer:-
A default constructor is a constructor that can be called with no arguments. It either has no parameters
or all its parameters have default values. Example………..
class Example {
public:
// Default constructor
Example() {
// Initialization code (optional)
}
void show() {
std::cout << "Default constructor called\n";
}
};
int main() {
Example obj; // Calls the default constructor
obj.show();
return 0;
}
Key Points:
Answer:-
A copy constructor is a special constructor in a class that initializes a new object as a copy of an
existing object. Example………
class MyClass {
public:
int data;
// Default constructor
MyClass(int d) : data(d) {}
// Copy constructor
MyClass(const MyClass &obj) : data(obj.data) {
std::cout << "Copy constructor called\n";
}
};
int main() {
MyClass obj1(10); // Calls default constructor
MyClass obj2 = obj1; // Calls copy constructor
return 0;
}
In this example, MyClass has a copy constructor that initializes obj2 with the values from obj1.
Answer:-
Overloading constructors refers to having multiple constructors in a class with the same
name but different parameter lists (different types, numbers, or both). Example….
class MyClass {
public:
int a, b;
int main() {
MyClass obj1; // Calls default constructor
MyClass obj2(10); // Calls constructor with one parameter
MyClass obj3(10, 20); // Calls constructor with two parameters
return 0;
}
In this example, MyClass has three overloaded constructors, each initializing the object
differently based on the parameters provided.
Answer:-
Set methods (or setter methods) are functions in a class that are used to set or update the
values of private member variables.
Use:
Code Example……..
class MyClass {
private:
int value;
public:
// Setter method
void setValue(int v) {
if (v >= 0) { // Example validation
value = v;
}
}
int main() {
MyClass obj;
obj.setValue(10); // Calls setter method
std::cout << obj.getValue() << std::endl; // Outputs: 10
return 0;
}
In this example, the setValue method allows setting the value with validation, while the
getValue method retrieves it.
13) What are built in functions and user defined functions, give code example.
Answer:-
Built-in Functions:
#include <iostream>
#include <cmath>
int main() {
double number = 16.0;
double result = std::sqrt(number);
std::cout << "Square root of " << number << " is " << result
<< std::endl;
return 0;
}
User-Defined Functions:
#include <iostream>
int main() {
int x = 5, y = 10;
int sum = add(x, y);
std::cout << "Sum of " << x << " and " << y << " is " << sum
<< std::endl;
return 0;
}
Answer:-
Key Points:
• Encapsulates Data: Defines attributes (data members) and behaviors (methods) for objects.
• Creates Objects: Provides a template for instantiating objects with specific attributes and
methods.
Answer:-
An object is an instance of a class. It represents a specific realization of the class with its
own unique data and can use the methods defined in the class.
Key Points:
16) What are the different access specifiers in C++ (public, private, protected)?
Answer:-
1. Public:
o Definition: Members are accessible from outside the class.
o Usage: Allows unrestricted access to class members.
o Example:
class MyClass {
public:
int value; // Public member
};
2. Private:
o Definition: Members are accessible only within the class.
o Usage: Encapsulates data and restricts access from outside.
o Example:
class MyClass {
private:
int value; // Private member
public:
void setValue(int v) {
value = v;
}
};
3. Protected:
o Definition: Members are accessible within the class and by derived classes.
o Usage: Allows controlled access in inheritance.
o Example:
class Base {
protected:
int value; // Protected member
};
Answer:-
Key Points:
Answer:-
Types of Inheritance:
1. Single Inheritance:
o Definition: A derived class inherits from one base class.
o Example:
2. Multiple Inheritance:
o Definition: A derived class inherits from multiple base classes.
o Example:
3. Multilevel Inheritance:
o Definition: A class is derived from another derived class.
o Example:
4. Hierarchical Inheritance:
o Definition: Multiple derived classes inherit from a single base class.
o Example:
5. Hybrid Inheritance:
o Definition: A combination of two or more types of inheritance.
o Example:
Multiple Inheritance:
• Definition: A derived class inherits from more than one base class.
• Usage: Allows a derived class to combine features from multiple base classes.
• Example:
class Base1 {
public:
void method1() {}
};
class Base2 {
public:
void method2() {}
};
In this example, Derived inherits methods from both Base1 and Base2.
Answer:-
Multilevel Inheritance:
• Definition: A class is derived from another derived class, creating a chain of inheritance.
• Usage: Allows a derived class to inherit from a class that is itself derived from another class.
• Example:
class Base {
public:
void baseMethod() {}
};
In this example, Derived inherits from Intermediate, which in turn inherits from Base.
Answer:-
Abstraction:
• Definition: Abstraction is the concept of hiding complex implementation details and showing
only essential features of an object.
• Usage: Simplifies interactions with objects by providing a clear and simplified interface.
• Example:
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Square();
shape1->draw(); // Outputs: Drawing Circle
shape2->draw(); // Outputs: Drawing Square
delete shape1;
delete shape2;
return 0;
}
In this example, Shape provides an abstract interface with a pure virtual function draw(), while
Circle and Square provide specific implementations of draw().
22) What is main functions of OOP
Answer:-
1. Encapsulation:
o Definition: Bundles data and methods that operate on the data into a single unit (class).
o Purpose: Protects data and hides internal implementation details.
2. Abstraction:
o Definition: Hides complex implementation details and exposes only essential features.
o Purpose: Simplifies interaction with objects and reduces complexity.
3. Inheritance:
o Definition: Enables a class (derived class) to inherit attributes and methods from
another class (base class).
o Purpose: Promotes code reuse and establishes hierarchical relationships.
4. Polymorphism:
o Definition: Allows objects to be treated as instances of their parent class and enables
method overriding.
o Purpose: Provides flexibility and the ability to use a unified interface for different
underlying implementations.
Answer:-
Friend Function:
• Definition: A friend function is a function that is not a member of a class but has access to its
private and protected members.
• Purpose: Allows external functions to access private data of a class.
Code Example:
#include <iostream>
class MyClass {
private:
int data;
public:
MyClass(int d) : data(d) {}
int main() {
MyClass obj(10);
display(obj); // Calls friend function
return 0;
}
In this example, display is a friend function of MyClass and can access its private member
data.
Answer:-
Virtual Class:
• Definition: A virtual class (often referred to as a "virtual base class") is a base class that is
specified as virtual in multiple inheritance scenarios to ensure only one instance of the class
is inherited by derived classes.
• Purpose: Prevents the "diamond problem" in multiple inheritance by ensuring that a class is not
instantiated more than once.
Normal Class:
• Definition: A standard class with no special inheritance rules. It can be used as a base class in
inheritance but does not involve virtual inheritance.
• Purpose: Provides a basic structure for creating objects and defining methods and attributes.
Code Example:
#include <iostream>
class Base {
public:
virtual void show() { std::cout << "Base class\n"; }
};
int main() {
Final obj;
obj.show(); // Calls Final's show method
return 0;
}
In this example:
Answer:-
• Avoid Diamond Problem: Ensures only one instance of a base class is inherited in multiple
inheritance scenarios.
• Prevent Ambiguity: Eliminates ambiguities caused by multiple copies of a base class when a
derived class inherits from multiple paths.
Example Scenario:
Without virtual base class, a class inheriting from multiple classes that share a common base
class might end up with multiple instances of that base class, leading to ambiguous references
and increased memory usage. Virtual base classes solve this by ensuring a single shared instance.
-------------- O -------------