Object Oriented Programming Using C++
Q.1) What is inheritance? What are its ad-vantages & disadvantages?
Ans. Inheritance is a fundamental concept in object-oriented programming (OOP) that
enables a class to inherit properties and behaviours from another class, which is referred to as
the parent or base class. The class that inherits from the parent class is called the child or
derived class.
Advantages of Inheritance:
1. Reusability: Inheritance allows classes to reuse code and functionality from a parent
class, reducing the need to rewrite code and increasing efficiency.
2. Polymorphism: Inheritance enables polymorphism, which means that objects of the
derived class can be treated as objects of the base class, allowing for greater flexibility
and ease of use.
3. Modularity: Inheritance facilitates modular design, making it easier to separate
functionality into different classes and keeping code organized and manageable.
Disadvantages of Inheritance:
1. Tight Coupling: Inheritance can lead to tight coupling between classes, making it
harder to modify and maintain code as changes made to the base class may affect the
derived class.
2. Overuse: Overuse of inheritance can result in a complex and convoluted class
hierarchy, which can be difficult to understand and maintain.
3. Inflexibility: Inheritance can be inflexible as it is difficult to change the
implementation of the base class without affecting all of its derived classes.
Q. 2) Give the structure of the C++ program with suitable examples.
Ans. A C++ program typically consists of several components, including preprocessor directives,
global variables, functions, and the main function. Here is a basic structure of a C++ program with
suitable examples:
// Preprocessor directive
#include <iostream>
// Global variable declaration
int myGlobalVar = 42;
// Function declaration
void myFunction();
,// Main function
int main () {
// Local variable declaration and initialization
int myLocalVar = 7;
// Output to console
std::cout << "Hello, World!" << std::endl;
// Call to function
myFunction();
// Output global variable
std::cout << "The value of myGlobalVar is " << myGlobalVar << std::endl;
return 0;
}
// Function definition
void myFunction () {
// Output to console
std::cout << "This is my function!" << std::endl;
}
The #include <iostream> directive allows us to use the standard input/output library in our
program. The myGlobalVar variable is a global variable that can be accessed from anywhere
in the program. The myFunction() function is declared above the main function and defined
below it. The main function is the starting point of the program and is where the program
logic is written. In this example, the main function initializes a local variable, outputs a
message to the console, calls the myFunction() function, outputs the value of the global
variable, and then returns 0 to indicate successful completion of the program.
Overall, this is a very basic example of a C++ program, but it provides a clear structure that
can be built upon with additional functionality and complexity as needed.
Q.3) Define data members, member functions, and private and public members with examples.
Ans. In C++, a class is a user-defined data type that can contain data members and member
functions. These members can be declared as either public or private, which determines their
accessibility and visibility within the class and outside of it. Here are definitions and
examples of data members, member functions, private and public members:
1. Data Members: Data members are variables declared inside a class that store data or
information related to the class. They can be of any data type, including primitive data
types, user-defined data types, and pointers. Data members are usually declared as
private to restrict direct access from outside the class.
Example:
class Rectangle {
private:
int width;
int height;
public:
, // Constructor
Rectangle(int w, int h) {
width = w;
height = h;
}
};
In this example, width and height are private data members of the Rectangle class that
store the dimensions of a rectangle.
2. Member Functions: Member functions are functions defined inside a class that
operate on data members of the class. They can be used to manipulate data members,
perform calculations, and implement the logic of the class. Member functions are
usually declared as public to allow access from outside the class.
Example:
class Rectangle {
private:
int width;
int height;
public:
// Constructor
Rectangle(int w, int h) {
width = w;
height = h;
}
// Member function to calculate the area of the rectangle
int area() {
return width * height;
}
};
In this example, area() is a public member function of the Rectangle class that calculates
the area of a rectangle based on its width and height data members.
3. Private and Public Members: Private and public members are used to control the
accessibility and visibility of data members and member functions within and outside
the class. Private members can only be accessed within the class, while public
members can be accessed from outside the class.
Example:
class Rectangle {
private:
int width;
int height;
public:
// Constructor
Rectangle(int w, int h) {