BCP 3rd Chapter
BCP 3rd Chapter
1. Friend functions are non-member functions that have access to the private and protected
members of a class.
2. They can be useful when you want to allow external functions or classes to access private data
without making them a member of the class.
3. Friend functions promote encapsulation by providing controlled access to class internals.
4. They can simplify code by allowing external functions to directly work with class objects.
5. Friend functions can enhance the flexibility and extensibility of a class by enabling external
functions to perform specialized operations on class objects.
1. Member functions are part of the class and have direct access to all members, including private
and protected ones. Friend functions, on the other hand, are not part of the class and need to
be explicitly declared as friends to gain access to the class's private and protected members.
2. Member functions are invoked using the object of the class, whereas friend functions are
invoked like regular functions.
3. Friend functions can be defined inside or outside the class, while member functions are typically
defined within the class definition.
4. Friend functions can be standalone functions or even members of other classes, whereas
member functions are specific to the class they are defined in.
5. Friend functions can access multiple classes' private and protected members, whereas member
functions can only access the members of their own class.
Q 2 - What are a constructor and destructor? How do we invoke a constructor function? Explain
by one program?
class MyClass {
public:
// Constructor
MyClass() {
// Destructor
~MyClass() {
};
int main() {
// When the object goes out of scope (end of the main function),
return 0;
}
Key points in easy language:
A constructor is a special function that initializes the data members of an object when it is
created.
A destructor is a special function that cleans up resources or performs necessary tasks before
an object is destroyed.
Constructors have the same name as the class and are called automatically when an object is
created.
Destructors also have the same name as the class but are preceded by a tilde (~) character
and are called automatically when an object is destroyed.
Constructors and destructors are useful for proper initialization and cleanup of objects and
can be used to manage resources effectively.
Q3 - What is polymorphism? Explain the difference between function overriding and function
overloading?
Here are some key points about polymorphism, function overriding, and function overloading:
Polymorphism:
Polymorphism means “many forms” and refers to the ability of an object to take on multiple forms.
It allows objects of different classes that inherit from a common superclass to be treated as objects of
that superclass.
Polymorphism enables the use of a common interface to work with different types of objects.
Function Overriding:
Function overriding occurs when a subclass provides its own implementation of a method that is already
defined in its superclass.
The overridden method in the subclass should have the same name, return type, and parameters as the
method in the superclass.
The decision of which method to execute is based on the actual type of the object at runtime (dynamic
binding).
Function Overloading:
Function overloading occurs when multiple functions with the same name exist in a class, but they differ
in the number, type, or order of their parameters.
Overloaded functions must have different parameter lists, but they can have the same or different return
types.
The compiler determines which overloaded function to invoke based on the number and types of
arguments provided during the function call (static binding).
Q 4 – What is inheritance? Explain the different types of inheritance? Explain what is base class and
derived class?
Here are some key points about inheritance and its types, along with the concepts of
base class and derived class:
Inheritance:
Types of Inheritance:
Single Inheritance: In single inheritance, a derived class inherits from a single base class. It forms a
simple parent-child relationship between classes.
Multiple Inheritance: Multiple inheritance allows a derived class to inherit from multiple base classes.
The derived class incorporates features from all the base classes.
Multilevel Inheritance: Multilevel inheritance involves a chain of inheritance, where a derived class
inherits from a base class, and another derived class inherits from the first derived class, forming a
hierarchy.
Hierarchical Inheritance: Hierarchical inheritance occurs when multiple derived classes inherit from a
single base class. It represents a one-to-many relationship.
Base Class: A base class is an existing class from which other classes are derived. It is also known as a
parent class or superclass. It defines the common attributes and behaviors that can be inherited by
derived classes.
Derived Class: A derived class is a new class that is created from an existing base class. It inherits the
properties and behaviors of the base class. It is also known as a child class or subclass. The derived class
can add its own additional features or override the inherited ones.
Q 5 - Explain following:
Virtual function.
Virtual Function:
A virtual function is a member function declared in a base class that can be overridden in its derived
classes.
It allows dynamic binding or late binding, meaning the actual function called is determined based on the
type of the object at runtime.
Virtual functions enable polymorphism, allowing different objects to be treated as objects of a common
base class type.
To declare a function as virtual, the base class should use the virtual keyword, and the derived class
should use the override keyword when overriding the function.
It encapsulates data members (attributes) and member functions (methods) that operate on the data.
A class serves as a blueprint for creating objects, representing real-world entities or concepts.
Objects have their own set of data members and can invoke member functions defined in the class.
Multiple objects can be created from a single class, each with its own unique set of data.
1. Organization of Data: Data structures are a way to organize and store data in a
computer's memory or storage. They provide a systematic way to manage and
manipulate data efficiently.
2. Different Types: There are various types of data structures, each with its own way
of organizing data. Some common examples include arrays, linked lists, stacks,
queues, trees, and graphs.
3. Storage and Retrieval: Data structures determine how data is stored and how it
can be accessed or retrieved. They define the rules and operations for adding,
removing, or modifying data elements.
4. Efficiency: Data structures are designed to optimize the performance of
operations performed on data, such as searching, sorting, inserting, or deleting
elements. Different data structures excel in different operations.
5. Abstraction: Data structures provide an abstraction layer that allows
programmers to work with data at a higher level. They hide the low-level
implementation details and provide convenient interfaces for data manipulation.
Derived class is a class that is derived from another class (base class or parent class) through inheritance.
Derived classes establish an “is-a” relationship with their base class, inheriting attributes and methods.
Derived classes have access to the public and protected members of the base class.
Derived classes can extend the functionality of the base class by adding new attributes and methods.
Derived classes can override the implementation of methods inherited from the base class.
Some programming languages support multiple inheritance, where a derived class can inherit from
multiple base classes.
Derived classes contribute to polymorphism, allowing objects of derived classes to be used
interchangeably with objects of the base class.
Derived classes can serve as base classes for further derived classes, creating a hierarchical structure.