Object Oriented Programming
Unit I
1. Define Object Oriented Programming and explain features of object-
oriented programming.
Ans :
OOP is a programming paradigm that revolves around the concept of
object which contains properties and methods.
It combines the group of related attributes and behavior within a single
unit name class that enhances program design structure and code
readability.
Features of OOP are as follows :
1. Class :
A class is a user defined data type and the main block of object oriented
programming. It is a user defined data type which holds data members
and member functions in a single unit. Without object class does not
exists.
2. Object :
An object is an instance of a class. Without object no memory is allocated
to class’s data member and member functions. Object represents a real-
world entity having set of attributes and behavior.
3. Inheritance :
The ability to inherit the properties of one class to another or inherit
properties from base class to an inherited class is known as inheritance.
4. Polymorphism :
Poly means many and morphs means forms in Greek. In OOP
polymorphism means one name with many forms. Using polymorphism,
we can use same function name again and again with different signatures.
5. Data abstraction :
Data abstraction means hiding the background details and provide only
essential details of an object.
6. Data encapsulation :
Data encapsulation means binding data and code into a single unit. With
the help of access modifiers such as public, private, protected, we can
control the access of data members and member functions.
7. Message passing :
Objects communicate with each other by sending and receiving
information through message passing to each other.
2. Differentiate between PPL and OOP.
Procedural Oriented Programming Object Oriented Programming
Program is divided into small parts Program is divided into small parts
called functions. called objects.
It follows top down approach. It follows bottom up approach.
There is no access specifier in OOP have access specifiers like
PPL. public, private and protected.
Overloading is not possible. Overloading is possible.
In PPL function is more important In OOP data is more important
than data. than function.
It is less secure. It provides security of data.
Example : BASIC, C etc. C++, Java, Python
3. Explain the concept of ‘this’ pointer with example.
Ans :
Every object in C++ has access to its own address through an important
pointer called this pointer. The ‘this’ pointer is an implicit parameter to
all member functions. Therefore, inside a member function, this may be
used to refer to the invoking object.
Friend functions do not have ‘this’ pointer, because friends are not
members of a class. Only member functions have ‘this’ pointer.
To understand ‘this’ pointer, it is important to know how objects look at
functions and data members of a class.
Each object gets its own copy of the data member.
All-access the same function definition as present in the code segment.
For example :
#include<iostream>
using namespace std;
/* local variable is same as a member's name */
class Test {
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print() {
cout << "x = " << x << endl;
}
};
int main(){
Test obj;
int x = 20;
[Link](x);
[Link]();
return 0;
}
Output:
x = 20
4. What is memory management operator? Explain new and delete.
Ans :
To avoid the wastage of memory, you can dynamically allocate memory
required during runtime using new and delete operators in C++.
In C++, when you want dynamic memory, you can use operator new. It
returns a pointer to the beginning of the new block of memory allocated.
It returns a pointer to the beginning of the new block of memory
allocated.
When memory allocated by new operator is no longer required, it is freed
using operator delete.
Dynamic memory allocation can make memory management more
efficient.
Especially for arrays, where a lot of the times we don't know the size of
the array until the run time.
For example :
// C++ Program to store GPA of n number of students and display it
// where n is the number of students entered by the user
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter total number of students: ";
cin >> num;
float* ptr;
// memory allocation of num number of floats
ptr = new float[num];
cout << "Enter GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
}
// ptr memory is released
delete[] ptr;
return 0;
}
5. With the help of example explain Function Overloading in C++.
Ans :
Function Overloading :
In function overloading we can have same name of the function but it
should have different no. of parameters or different datatype of
parameters.
For example :
#include<iostream>
using namespace std;
void display(int );
void display(float );
void display(int , float);
int main() {
int a = 5;
float b = 5.5;
display(a);
display(b);
display(a,b);
return 0;
}
void display(int x) {
cout<< “ Integer number : ” <<x<< endl;
}
void display(float y ) {
cout<< “ Float number : ” <<y<< endl;
}
void display(int m, float n) {
cout<< “ Integer and float numbers are : ” <<m<<n endl;
}
6. What are the different benefits of OOP? Enlist applications of OOP.
Ans :
Benefits of OOP are as follows :
1. Re-usability
It means reusing some facilities rather than building them again and
again. This is done with the use of a class. We can use it ‘n’ number of
times as per our need.
2. Data Redundancy
This is a condition created at the place of data storage (you can say
Databases)where the same piece of data is held in two separate places. So
the data redundancy is one of the greatest advantages of OOP. If a user
wants a similar functionality in multiple classes, he/she can go ahead by
writing common class definitions for similar functionalities and inherit
them.
3. Code Maintenance
This feature is more of a necessity for any programming languages; it
helps users from doing re-work in many ways. It is always easy and time-
saving to maintain and modify the existing codes by incorporating new
changes into them.
4. Security
With the use of data hiding and abstraction mechanism, we are filtering
out limited data to exposure, which means we are maintaining security
and providing necessary data to view.
Applications :
User interface design such as windows, menu.
Real Time Systems
Simulation and Modeling
Object oriented databases
AI and Expert System
Neural Networks and parallel programming
Decision support and office automation systems etc.
7. Write a C++ program for addition and subtraction of two numbers. (use
different functions).
Ans :
#include<iostream>
using namespace std;
void add (int a, int b) {
int c = a + b;
cout<<"Additon : "<<c<<endl;
}
void sub (int a, int b) {
int c = b - a;
cout<<"Subtraction : "<<c<<endl;
}
int main() {
int a = 20, b = 10;
add(a,b);
sub(b,a);
return 0;
}
8. What is the use inline function?
Ans :
C++ provides an inline function to reduce the function call overhead.
Inline function is a function that is expanded in line when it is called.
When the inline function is called whole code of the inline function gets
inserted or substituted at the point of inline function call. This
substitution is performed by the C++ compiler at compile time. Inline
function may increase efficiency if it is small.
For example:
#include<iostream>
using namespace std;
class Number {
public :
//functions are defined in public to make them inline
float Square(float a) {
return a * a;
}
float Cube(float n) {
return n*n*n;
}
};
int main() {
float x , y , z;
Number m;
cout<<"Enter two numbers : "<<endl;
cin>>x;
z = [Link](x);
cout<<"Square of given number is : "<<z<<endl;
cout<<"Enter number : "<<endl;
cin>>x;
y = [Link](x);
cout<<"Cube of given number is : "<<y<<endl;
return 0;
}
9. Describe about Encapsulation, Inheritance and Polymorphism.
Ans :
Inheritance :
The ability to inherit the properties of one class to another or inherit
properties from base class to an inherited class is known as inheritance.
Polymorphism :
Poly means many and morphs means forms in Greek. In OOP
polymorphism means one name with many forms. Using polymorphism,
we can use same function name again and again with different signatures.
Data encapsulation :
Data encapsulation means binding data and code into a single unit. With
the help of access modifiers such as public, private, protected, we can
control the access of data members and member functions.
10. Define
1. Dynamic Binding
2. Message Passing
Ans :
Message passing :
Objects communicate with each other by sending and receiving
information through message passing to each other.
Dynamic Binding :
Dynamic binding refers to linking a procedure call to code that will
execute only once. The code associated with the procedure is not known
until the program is executed, which is also known as late binding.
Unit II
1. Write a short note on:
1. Class
2. Object
Ans :
1. Class :
A class is a user defined data type and the main block of object oriented
programming. It is a user defined data type which holds data members
and member functions in a single unit. Without object class does not
exists.
2. Object :
An object is an instance of a class. Without object no memory is allocated
to class’s data member and member functions. Object represents a real-
world entity having set of attributes and behavior.
2. What is the use constructor? Explain the concept of default and copy
constructor with suitable example.
Ans :
A constructor is a special type of member function of a class which
initializes objects of a class. In C++, Constructor is automatically called
when object (instance of class) is created. It is special member function of
the class because it does not have any return type. It will have exact same
name as class.
Default Constructors:
Default constructor is the constructor which doesn’t take any argument. It
has no parameters.
Copy Constructor:
A copy constructor is a member function which initializes an object using
another object of the same class.
For example :
3. Write a C++ program to illustrate the concept of parameterized
constructor.
Ans :
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << [Link]() << ", p1.y = " << [Link]();
return 0;
}
4. Explain the terms :
a. Reading console input
b. Writing console output
Ans :
Reading console input :