[go: up one dir, main page]

0% found this document useful (0 votes)
7 views15 pages

OOP Imp (1)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Object Oriented Programming

Imp Short

1) What are the four pillars of OOP (Encapsulation, Abstraction, Inheritance,


Polymorphism)

Answer:-

here are the four pillars of Object-Oriented Programming (OOP):

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.

2) What is the difference between an object and a class?

Answer:-

here is the difference between an object and a class in OOP in a point-to-point


format:

Class:

• Blueprint or template for creating objects.


• Defines properties (attributes) and behaviors (methods) that the objects created from it
will have.
• Does not occupy memory until instantiated.

Object:

• Instance of a class.
• Represents a specific entity with actual data and methods.
• Occupies memory when created.

3) What is the benefit of using OOP in C++? (Code reusability, maintainability,


modularity)

Answer:-

here are the benefits of using OOP in C++ in a point-to-point format:

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:-

Polymorphism in Object-Oriented Programming (OOP) refers to the ability of a function,


method, or object to take on multiple forms. This allows a single interface to be used for different data
types, making it possible for functions or methods to operate differently based on the object they are
acting upon.

5) Types of polymorphism.

Answer :-
Types:

• Compile-time Polymorphism (Static):


o Achieved through method overloading (multiple methods with the same name
but different parameters).
o Operator overloading (customizing the behavior of operators for user-defined
types).
• Run-time Polymorphism (Dynamic):
o Achieved through method overriding (subclass provides a specific
implementation of a method already defined in its superclass).
o Use of virtual functions (functions defined in a base class and overridden in
derived classes to achieve dynamic binding).

6) What is Run time polymorphism.

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 :-

Static Polymorphism (Compile-Time Polymorphism):

o Definition: Static polymorphism, also known as compile-time polymorphism, is a type of


polymorphism that is resolved during the compilation of a program, rather than at run-
time. It allows for method and operator overloading based on compile-time type
information.
o Resolution: Determined at compile-time based on method signatures.

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.

9) Default constructor give code example..

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:

• Default Constructor: A constructor with no parameters.


• Automatic Call: Invoked when an object is created without passing any arguments.
10) Copy constructor

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.

11) What is Overloading constructor.

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;

// Constructor with no parameters


MyClass() : a(0), b(0) {}

// Constructor with one parameter


MyClass(int x) : a(x), b(0) {}

// Constructor with two parameters


MyClass(int x, int y) : a(x), b(y) {}
};

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.

12) What are set method write their use.

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:

• Encapsulation: Allows controlled access and modification of private data.


• Validation: Can include logic to validate or modify the data before setting it.
• Maintainability: Provides a single point to modify how data is set, improving code
maintainability.

Code Example……..

class MyClass {
private:
int value;

public:
// Setter method
void setValue(int v) {
if (v >= 0) { // Example validation
value = v;
}
}

// Getter method to access the value


int getValue() const {
return value;
}
};

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:

• Definition: Pre-defined functions provided by the programming language or standard


libraries.
• Example: std::sqrt for calculating square roots in C++.

#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:

• Definition: Functions created by the programmer to perform specific tasks.


• Example: add function to sum two integers.

#include <iostream>

int add(int a, int b) {


return a + b;
}

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

14) What is a class? (Blueprint for creating objects)

Answer:-

A class is a blueprint for creating objects in Object-Oriented Programming (OOP). It


defines a data structure by encapsulating data and methods that operate on the data.

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.

15) What is an object? (An instance of a class)

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:

• Instance of Class: Created based on the blueprint provided by the class.


• Encapsulates Data: Holds its own data and can interact with methods of the class.

16) What are the different access specifiers in C++ (public, private, protected)?

Answer:-

Access Specifiers in C++:

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

class Derived : public Base {


public:
void setValue(int v) {
value = v; // Accessing protected member
}
};

17) What is inheritance.

Answer:-

Inheritance is an Object-Oriented Programming (OOP) concept where a new class (derived


class) is based on an existing class (base class). The derived class inherits attributes and methods
from the base class, allowing for code reuse and the creation of a hierarchical relationship
between classes.

Key Points:

• Base Class: The class being inherited from.


• Derived Class: The class that inherits from the base class and can extend or modify its behavior.
• Code Reuse: Allows derived classes to use, extend, or override base class functionality.
18) Types of inheritance.

Answer:-

Types of Inheritance:

1. Single Inheritance:
o Definition: A derived class inherits from one base class.
o Example:

class Base {};


class Derived : public Base {};

2. Multiple Inheritance:
o Definition: A derived class inherits from multiple base classes.
o Example:

class Base1 {};


class Base2 {};
class Derived : public Base1, public Base2 {};

3. Multilevel Inheritance:
o Definition: A class is derived from another derived class.
o Example:

class Base {};


class Intermediate : public Base {};
class Derived : public Intermediate {};

4. Hierarchical Inheritance:
o Definition: Multiple derived classes inherit from a single base class.
o Example:

class Base {};


class Derived1 : public Base {};
class Derived2 : public Base {};

5. Hybrid Inheritance:
o Definition: A combination of two or more types of inheritance.
o Example:

class Base {};


class Intermediate1 : public Base {};
class Intermediate2 : public Base {};
class Derived : public Intermediate1, public Intermediate2 {};

19) Multiple Inheritance.


Answer:-

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

class Derived : public Base1, public Base2 {


public:
void method3() {}
};

In this example, Derived inherits methods from both Base1 and Base2.

20) Multilevel inheritance

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

class Intermediate : public Base {


public:
void intermediateMethod() {}
};

class Derived : public Intermediate {


public:
void derivedMethod() {}
};

In this example, Derived inherits from Intermediate, which in turn inherits from Base.

21) What do you know about abstraction give example.

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:

class Shape { // Abstract class


public:
virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {


public:
void draw() override {
std::cout << "Drawing Circle\n";
}
};

class Square : public Shape {


public:
void draw() override {
std::cout << "Drawing Square\n";
}
};

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:-

Main Functions of OOP:

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.

23) What is friendfunction give code example

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

// Declare friend function


friend void display(const MyClass &obj);
};
// Definition of friend function
void display(const MyClass &obj) {
std::cout << "Data: " << obj.data << std::endl; // Accesses private
member
}

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.

24) What is virtual class and normal class

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

class Derived1 : virtual public Base {


public:
void show() override { std::cout << "Derived1 class\n"; }
};

class Derived2 : virtual public Base {


public:
void show() override { std::cout << "Derived2 class\n"; }
};

class Final : public Derived1, public Derived2 {


// Only one instance of Base will exist
};

int main() {
Final obj;
obj.show(); // Calls Final's show method
return 0;
}

In this example:

• Base is a normal class.


• Derived1 and Derived2 use virtual inheritance from Base to avoid the diamond problem.

25) Why we need virtual base class

Answer:-

Need for Virtual Base Class:

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

You might also like