PAPER 1st
QUESTION:1
Explain Features of OOP's
There are a few principle concepts and features that form the foundation of object-oriented programming −
Object
This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.
Class
When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
Abstraction
Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.
For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data.
Encapsulation
Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.
Inheritance
One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.
This is a very important concept of object-oriented programming since this feature helps to reduce the code size.
Polymorphism
The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.
Overloading
The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type, it is said to be overloaded.
[SUBSCRIBE]
QUESTION:2
What is Class? Write a Program to demonstrate the use of a class
A class in C++ is a user defined data type or data Structures declared with keywords class
that has data and functions (also called members variables and member functions) as its members whose access is governed by the three access specifier private, protected or public. By default access to members of a C++ class is private.
C++ Program to Demonstrate the use of a class
// Program to illustrate the working of
// objects and class in C++ Programming
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
// create object of Room class
Room room1;
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
// calculate and display the area and volume of the room
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output
Area of Room = 1309
Volume of Room = 25132.8
QUESTION:3
What is Friend functions? Explain with the Help of an Example
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.
Example:-
// C++ program to demonstrate the working of friend function
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
// friend function definition
int addFive(Distance d) {
//accessing private members from the friend function
d.meter += 5;
return d.meter;
}
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
Distance: 5
QUESTION:4
What is Operator Overloading? Explain with an Example?
Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.
Example:-
// Overload ++ when used as prefix
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output
Count: 6
QUESTION:5
Discuss Various File Modes
Type of Modes are:-
ios::in
This ios prefix is used to open a file to read input from the user.
ios::out
This ios prefix is used to open a file to write the output from the user.
ios::ate
This ios prefix is used to open a file without truncating and allows data to be written anywhere in the mentioned file.
ios::trunc
This ios prefix is used to truncate the existing file.
ios::app
This ios prefix is used to open a file and append it to the end.
ios::binary
This ios prefix is used to treat the given file as a binary format.
Comments
Post a Comment
Please Subscribe and Comments my blog site.