Oops Question Bank
Oops Question Bank
Oops Question Bank
2 MARKS QUESTION
1) What is OOPS?
OOPS is abbreviated as Object Oriented Programming system in which programs are
considered as a collection of objects. Each object is nothing but an instance of a class.
2) What is OOPS?
C++ is an object-oriented programming language developed by Bjarne Stroustrup.
C++ is a cross-platform language that can be used to create high- performance applications.
C++ is a versatile language for handling very large programs.
4) What is a class?
A class is simply a representation of a type of object. It is the blueprint/plan/template that
describes the details of an object.
5) What is an Object?
An object is an instance of a class. It has its own state, behavior, and identity.
6) What is Encapsulation?
Encapsulation is an attribute of an object, and it contains all data which is hidden. That hidden
data can be restricted to the members of that class.
Levels are Public, Protected, Private, Internal, and Protected Internal.
7) What is Polymorphism?
Polymorphism is nothing but assigning behavior or value in a subclass to something that was
already declared in the main class. Simply, polymorphism takes more than one form.
8) What is Inheritance?
Inheritance is a concept where one class shares the structure and behavior defined in another
class. If Inheritance applied to one class is called Single Inheritance, and if it depends on
multiple classes, then it is called multiple Inheritance.
9) What are manipulators?
Manipulators are the functions which can be used in conjunction with the insertion (<<) and
extraction (>>) operators on an object. Examples are endl and setw.
4. Adding new data and function is not Adding new data and function is easy.
easy.
5. Procedural programming does not have Object-oriented programming provides data
any proper way for hiding data so it is hiding so it is more secure.
less secure.
6. In procedural programming, Overloading is possible in object-oriented
overloading is not possible. programming.
7. In procedural programming, function is In object-oriented programming, data is
more important than data. more important than function.
The basic idea behind object oriented programming is to combine into a single unit both the
data and the functions that operate on the data.
This unit is called as object. The functions in the object are called member functions and the
data within it is called instance variables
The data can’t be accessed directly, it can be accessed using function. Hence accidental use
of data can be avoided in object oriented programming.
This property is known as data hiding. The data and related functions are enclosed within an
object. This is known as data encapsulation.
1. Using inheritance the redundant code can be eliminated and the existing classes can be used.
2. The standard working modules can be created using object oriented programming. These
modules can then communicate to each other to accomplish certain task.
3. Due to data hiding property, important data can be kept away from unauthorized access.
5. For upgrading the system from small scale to large scale is possible due to object oriented
feature.
6. Due to data centered nature of object oriented programming most of the details of the
application model can be captured.
7. Message passing technique in object oriented programming allows the objects to communicate
to the external systems.
8. Partitioning the code for simplicity, understanding and debugging is possible due to object
oriented and modular approach
2) Explain the various features of OOPs
Class:
A class in C++ is a user-defined type declared with class keyword.
A class has data and functions as its members.
Its data & function is accessed by the three access specifiers private, protected or public.
By default access to members of a C++ class is private.
A C++ class is like a blueprint for an object.
Declaration of Class
A class is defined in C++ using keyword class followed by the name of class.
The body of class is defined inside the curly brackets and terminated by a semicolon at
the end.
Syntax:
Class class_Name
{
private/public/protected : Data_type variable ;
…………………….. ; Return_type fun(arg.);
…………………….. ;
};
Object
An Object is an instance of a Class.
When a class is defined, no memory is allocated but when an object is created memory
is allocated.
Declaring Objects:
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:
Class_Name Object_Name ;
Class Structure
Class is generally used in large programs. Structure are used in small programs.
The data member of a class can be protected. The data member of Structure can’t be
protected.
Function member of the class can be virtual or Function member of the Structure cannot be
abstract. virtual or abstract.
Abstraction:
Data abstraction means representing only essential features by hiding all the implementation details.
In C++, class is an entity used for data abstraction purpose.
Example
class Student {
int roll;
char name [10];
public;
void input ( );
void display ( );
}:
In main function we can access the functionalities using object. For instance
Student obj;
obj.input ( );
obj.display ( );
Information Hiding:
The data member of member function of a class can be declared as public or private. If particular data
attribute is declared as public then it is accessible to any other class.
But if the data member is declared as private then only the member function of that class can access
the data values. Another class cannot access these data values.
This property is called data hiding.
Advantages of Abstraction
Encapsulation means enclosing the data/variables and the methods for manipulating the data
into a single entity called a class.
It helps to hide the internal implementation of the functions and state of the variables,
promoting abstraction.
Polymorphism
The word polymorphism means to have many forms. So, by using polymorphism, you can add
different meanings to a single component.
Run-time polymorphism
Compile-time polymorphism
Inheritance is one of the most important features of object oriented programming. It allows a class to
inherit the properties and methods of another class called the parent class, the base class, or the
super-class.
It helps to avoid duplication of codes by allowing code reuse as you need not define the same
methods and properties present in a super-class in the sub-classes again. The sub-class can simply
inherit them.
Types of inheritance
Single Inheritance.
Multiple Inheritance.
Multilevel Inheritance.
Hierarchical Inheritance.
Hybrid Inheritance
The language C++ (pronounced as see plus plus) was developed by Bjarne Strousstrup in 1979 at
Bell Labs.
Class Structure
C makes use of top down approach of problem C++ makes use of bottom up approach of
solving. problem solving.
The input and output is done using scanf and The input and output is done using cin and cout
printf statements. statements.
C does not support inheritance, polymorphism, C++ supports inheritance, polymorphism, class
class and object concepts. and object concepts.
The data type specifier or format specifier (%d, The format specifier is not required in cin and
%f, %c) is required in printf and scanf functions. cout functions.
Structure of C++
A. Namespace :
Namespace defines a scope for the identifiers that are used in a program.
For using the identifiers defined in the namespace scope we must include the using directive :
using namespace std;
Here, std is the namespace where standard class libraries are defined.
This directive will bring all the identifiers defined in std to the current global scope.
B. Identifiers :
Identifiers refer to the names of variables, functions, arrays, classes, etc., which are created by the
programmer.
They are the fundamental requirement of any language.
Each language has its own rules for naming these identifiers. Following are the rules for C++ :
Only alphabetic characters, digits and underscores are permitted.
The name cannot start with a digit.
Uppercase and lowercase letters are distinct.
A declared keyword cannot be used as a variable name.
C. Constants :
Constants refer to fixed values that do not change during the execution of a program.
They include integers, characters, floating point numbers and strings.
Literal constant do not have memory locations.
Examples :
123 // decimal integer
12.34 // floating point integer
037 // octal integer
0X2 // hexadecimal integer
“C++” // string constant
‘A’ // character constant
L ‘ab’ // wide-character constant
D. Enumerated :
An enum specifier defines the set of all names that will be permissible values of the type. These
permissible values are called members.
The enum type days_of_week has seven members : Sun, Mon, Tue, and so on, up to Sat.
Enumerated means that all the values are listed.
This is unlike the specification of an int, for example, which is given in terms of a range of possible
values.
In an enum you must give a specific name to every possible value.
E. Variables :
A variable is a symbolic name that can be given a variety of values.
Variables are stored in particular places in the computer’s memory.
When a variable is given a value, that value is actually placed in the memory space occupied by
the variable.
Most popular languages use the same general variable types, such as integers, floating-point
numbers, and characters.
C++ defines two unary operators new and delete that perform the task of allocating and
freeing the memory in a easier way.
Since these operators manipulate memory on the free store, they are also known as free
store operators.
The new operator can be used to create objects of any type. It takes the following general
form :
When a data object is no longer needed, it is destroyed using the delete operator.
Manipulators :
Manipulators are operators that are used to format the data display.
The most commonly used manipulators are endl and setw.
The endl manipulator, when used in an output statement, causes a linefeed to be inserted.
It has the same effect as using the newline character “\n”.
C++ permits explicit type conversion of variables or expressions using the type cast
operator.
A private member cannot be accessed from outside the class. Only the class
member functions can access private members. By default all the members of a
class would be private.
The Scope Resolution Operator (::) can be used to uncover the hidden variable and function.
9) Define the Static Data member and Static Function member .
It is a variable which is declared with static keyword. when a data member is defined as static,
only one variable is created in the memory even if there are many object of that class.
A state data item is useful when all object of the same class mush shore a common item of
information.
All static data is initialized to zero when the first object is created, if no initialization present there.
Any change in the static data member through one member function will reflect in all member
function and state member function.
Declaration :
Static data_type member_name;
Example
#include<iostream>
using namespace std;
class ABC{
static int x; // Declaration of static data member
public:
static void fun() // Declaration of static member function
{
Cout<<”value of x : “<<x;
}
};
int main()
{
ABC obj; // // Access without static member function
x.fun();
cout<<ABC::x; // Access without static member function
return 0;
}
STATIC FUNCTION MEMBER
A static member function is a special member function which is used to acess only static data
member we can use a static member function to determine. where object of the class have been
created or not.
Example
#Include<lostream>
Class item{
Static string name;
Static Int a:
public:
int main(){
item obj;
Obj.item_detaila();
Cout<<item:: item-details();
Return 0;
}
FRIEND FUNCTION
when a function is declared as a friend then it can access the private and protected data of the
class.
Friend function is not a member of class
Syntax:
friend return type name of function (argument);
A friend function declared inside the class using friend keyword. But definition is also outside the
class . it defined as a nor mal function.
Ex:
Void printdata(test &p){
Cout<<”a=”<<p.a<<endl;
Cout<<”b=’<<p.b<<endl;
}
It cann’t be called using the object because it is not part of member function .
We cann’t access directly data member . we need to use object name and membership operator.
Ex: