Unit 1 OOP
Unit 1 OOP
Unit 1 OOP
3. Write a class ''Student'' with attributes like name, roll number & mark. Include member
functions to set & display these attributes? [SPPU Oct 2023, Marks:6]
Ans :
// C++ program to create student class, read and print student's details
#include <iostream>
using namespace std;
class student {
private:
char name[30];
int rollNo;
float perc;
public:
//member function to get student's details
void setDetails();
//member function to print student's details
void putDetails();
};
//member function definition, outside of the class
void student::setDetails()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter Percentage marks : ";
cin >> perc;
}
cout << "\n Name:" << name << "\n Roll Number:" << rollNo << "\n Percentage:" <<
perc;
}
int main()
{
student std; // objects creation
cout << "Enter details of student \n";
std.setDetails();
cout << endl;
cout << "Details of student \n";
std.putDetails();
return 0;
}
Output of above program will be as follows:
Output :
Enter details of student
Enter name: Mike
Enter roll number: 101
Enter Percentage marks : 9.1
Details of student
Student details:
Name:Mike
Roll Number:101
Percentage:9.1
4. State differences between abstraction and encapsulation. [SPPU Oct 2023, Marks:4]
Ans :
Abstraction is defined as a process of hiding the implementation details of a system from
the user. Thus, by using abstraction, we provided only the functionality of the system to the user.
Consequently, the user will have information on what the system does, but not on how the system
does it.
Encapsulation is one of the fundamental OOP concepts. Encapsulation is defined as a method
by which data wrapping is done into a single unit. It is used in wrapping up the data and the code
acting on the data together as a single unit.
In encapsulation, the variables of a class are hidden from other classes, and can be accessed
only by methods of the current class. Therefore, encapsulation is also called data hiding.
Encapsulation is implemented using access modifiers like public, private and protected.
Difference between Abstraction and Encapsulation
The following table highlights all the important differences between abstraction and
encapsulation –
It is the process of gaining information. It is a method that helps wrap up data into a single
1.
module.
2. The problems in this technique are solved Problems in encapsulation are solved at the
at the interface level. implementation level.
It helps hide the unwanted It helps hide data using a single entity, or using a
3. details/information. unit with the help of method that helps protect the
information.
It can be implemented using abstract It can be implemented using access modifiers like
4.
classes and interfaces. public, private and protected.
The complexities of the implementation are The data is hidden using methods such as getters
5.
hidden using interface and abstract class. and setters.
5. What are C++ access specifiers? Write down their significance. [SPPU Oct 2023, Marks:5]
Ans :
Access modifiers are used to implement an important aspect of Object-Oriented
Programming known as Data Hiding. Access Modifiers or Access Specifiers in a class are used
to assign the accessibility to the class members, i.e., they set some restrictions on the class
members so that they can’t be directly accessed by the outside functions.
There are 3 types of access modifiers available in C++:
Public
Private
Protected
If we do not specify any access modifiers for the members inside the class, then by default
the access modifier for the members will be Private.
Public: All the class members declared under the public specifier will be available to
everyone. The data members and member functions declared as public can be accessed by
other classes and functions too. The public members of a class can be accessed from anywhere
in the program using the direct member access operator (.) with the object of that class.
Private: The class members declared as private can be accessed only by the member
functions inside the class. They are not allowed to be accessed directly by any object or
function outside the class. Only the member functions or the friend functions are allowed to
access the private data members of the class.
Protected: The protected access modifier is similar to the private access modifier in the sense
that it can’t be accessed outside of its class unless with the help of a friend class. The
difference is that the class members declared as Protected can be accessed by any subclass
(derived class) of that class as well.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to
the class members, i.e., they set some restrictions on the class members so that they can't be
directly accessed by the outside functions.
6. Write a class ''Calculator'' with methods for addition, subtraction, multiplication and
division functions. Create a object to perform arithmetic operation.
[SPPU Oct 2023, Marks:5]
Ans :
// C++ program to implement the class ''Calculator'' with methods for addition, subtraction,
// multiplication and division functions.
#include <iostream>
#include <math.h>
using namespace std;
// Class calculator
class Calculator
{
float a, b;
public:
// Function to take input from user
void result()
{
cout << "Enter First Number: ";
cin >> a;
cout << "Enter Second Number: ";
cin >> b;
}
// Function to add two numbers
float add()
{
return a + b;
}
// Function to subtract two numbers
float sub()
{
return a - b;
}
// Function to multiply two numbers
float mul()
{
return a * b;
}
// Function to divide two numbers
float div()
{
if (b == 0)
{
cout << "Division By Zero" << endl;
return INFINITY;
}
else
{
return a / b;
}
}
};
// Driver code
int main()
{
int ch;
Calculator c;
cout << "Enter 1 to Add 2 Numbers" <<
"\nEnter 2 to Subtract 2 Numbers" <<
"\nEnter 3 to Multiply 2 Numbers" <<
"\nEnter 4 to Divide 2 Numbers" <<
"\nEnter 0 To Exit";
do
{
cout << "\nEnter Choice: ";
cin >> ch;
switch (ch)
{
case 1:
7. What is the use of ‘this’ pointer. Explain with Example. [SPPU Oct 2022, Marks:4]
Ans :
When a member function is called, it is automatically passed an implicit argument that is
a pointer to the invoking object (i.e. the object on which the function is invoked). This pointer
is known as 'this' pointer. It is internally created at the time of function call.
In C++ programming, this is a keyword that refers to the current instance of the class. There can
be 3 main usage of this keyword in C++.
o It can be used to pass current object as a parameter to another method.
o It can be used to refer current class instance variable.
o It can be used to declare indexers.
Output:
101 Sonoo 890000
102 Nakul 59000
8. What are the different ways to define member functions of a class? Give Examples of Each.
[SPPU Oct 2022, Marks:4]
Class Member Functions:
A class member function is a function that, like any other variable, is defined or prototyped
within the class declaration. It has access to all the members of the class and can operate on any
object of that class.
Let us use a member function to access the members of a previously created class instead of
directly accessing them.
class Dice {
public:
double L; // a dice's length
double B; // a dice's breadth
double H; // a dice's height
double getVolume(void); // Returns dice volume
};
Member functions can be defined either within the class definition or separately with the scope
resolution operator, :: Even if the inline specifier is not used, specifying a member function
within the class declaration declares the function inline. So, you may either define the Volume()
function as shown below.
class Dice {
public:
SE AI & DS 2024 SRCOE Prof. Ashvini Swami 8
Object Oriented Programming Unit 1 – Fundamentals of OOP
Ans :
The main use of the inline function in C++ is to save memory space. Whenever the function
is called, then it takes a lot of time to execute the tasks, such as moving to the calling function.
If the length of the function is small, then the substantial amount of execution time is spent in
such overheads, and sometimes time taken required for moving to the calling function will be
greater than the time taken required to execute that function.
The solution to this problem is to use macro definitions known as macros. The preprocessor
macros are widely used in C, but the major drawback with the macros is that these are not normal
functions which means the error checking process will not be done during the compilation.
C++ has provided one solution to this problem. In the case of function calling, the time for
calling such small functions is huge, so to overcome such a problem, a new concept was
introduced known as an inline function. When the function is encountered inside the main ()
method, it is expanded with its definition thus saving time.
// calculate an area of triangle using the inline function
#include <iostream>
using namespace std;
// inline function, no need prototype
inline float triangle_area (float base, float height)
{
float area;
area = (0.5 * base * height);
return area;
}
int main(void)
{
float b, h, a;
b = 4;
h = 6;
// compiler will substitute the inline function code here.
a = triangle_area (b, h);
cout<<"Area = (0.5*base*height)"<<endl;
cout<<"where, base = 4, height = 6"<<endl;
cout<<"\nArea = "<<a<<endl;
return 0;
}
Output example:
Area = (0.5*base*height)
where, base = 4, height = 6
Area = 12
an article or entity that exists in the real world. The meaning of oriented is interested in a
particular kind of thing or entity. In layman's terms, it is a programming pattern that rounds
around an object or entity.
6. Access There are no access modifiers in The access modifiers in OOP are
modifiers procedural programming. named as private, public, and
protected.
10. Importance It gives importance to functions over It gives importance to data over
data. functions.
11. Virtual class In procedural programming, there are In OOP, there is an appearance of
no virtual classes. virtual classes in inheritance.
13. Data hiding There is not any proper way for data There is a possibility of data
hiding. hiding.
11. What is difference between pointer and references? [SPPU Oct 2022, Marks:4]
Ans :
Reference :
A reference is a variable that is referred to as another name for an already existing
variable. The reference of a variable is created by storing the address of another variable.
A reference variable can be considered as a constant pointer with automatic indirection.
Here, automatic indirection means that the compiler automatically applies the indirection
operator (*).
Example of reference:
int &a = i;
In the above declaration, 'a' is an alias name for 'i' variable. We can also refer to the 'i' variable
through 'a' variable also.
Pointer :
A pointer is a variable that contains the address of another variable. It can be
dereferenced with the help of (*) operator to access the memory location to which the pointer
points.
Difference between References and Pointers in C++
References Pointers
References Pointers
12. Write C++ code that defines a class and declares and array of objects to that class
[SPPU Oct 2022, Marks:7]
Ans :
When a class is defined, only the specification for the object is defined; no memory or
storage is allocated. To use the data and access functions defined in the class, you need to
create objects.
Syntax:
ClassName ObjectName[number of objects];
The Array of Objects stores objects. An array of a class type is also known as an array of
objects.
// C++ program to implement the array of objects
#include<iostream>
using namespace std;
class Employee
{
int id;
char name[30];
public:
// Declaration of function
void getdata();
// Declaration of function
void putdata();
};
// Driver code
int main()
{
// This is an array of objects having
// maximum limit of 30 Employees
Employee emp[30];
int n, i;
cout << "Enter Number of Employees - ";
cin >> n;
Output :