Cpp qb u 1 and 2
C ++
1. Define Object Oriented Programming (OOP).
Object Oriented Programming is an approach that provides a way of modularizing
programs by creating partitioned memory area for both data and functions that can be used
as templates for creating copies of such modules on demand.
2. List out the basic concepts of Object Oriented Programming.
• Objects
• Classes
• Data Abstraction and Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
3. Define Objects.
Objects are the basic run time entities in an object oriented system. They are instance of a
class. They may represent a person, a place etc that a program has to handle. They may
also represent user-defined data. They contain both data and code.
4. Define Class.
Class is a collection of objects of similar data types. Class is a user-defined data type. The
entire set of data and code of an object can be made a user defined type through a class.
5. Define Encapsulation and Data Hiding.
The wrapping up of data and functions into a single unit is known as data encapsulation.
Here the data is not accessible to the outside world. The insulation of data from direct
access by the program is called data hiding or information hiding.
6. Define Data Abstraction.
Abstraction refers to the act of representing the essential features without including the
background details or explanations.
7. Define data members and member functions.
The attributes in the objects are known as data members because they hold the information.
The functions that operate on these data are known as methods or member functions.
8. State Inheritance.
Inheritance is the process by which objects of one class acquire the properties of objects of
another class. It supports the concept of hierarchical classification and provides the idea of
reusability. The class which is inherited is known as the base or super class and class which
is newly derived is known as the derived or sub class.
9. State Polymorphism.
Polymorphism is an important concept of OOPs. Polymorphism means one name, multiple
forms. It is the ability of a function or operator to take more than one form at different
instances.
10. List and define the two types of Polymorphism.
• Operator Overloading – The process of making an operator to exhibit different
behaviors at different instances.
• Function Overloading – Using a single function name to perform different types of
tasks. The same function name can be used to handle different number and different types
of arguments.
11. State Dynamic Binding.
Binding refers to the linking of procedure call to the code to be executed in response to
the call. Dynamic Binding or Late Binding means that the code associated with a given
procedure call is known only at the run-time.
12. Define Message Passing.
Objects communicate between each other by sending and receiving information known as
messages. A message to an object is a request for execution of a procedure. Message
passing involves specifying the name of the object, the name of the function and the
information to be sent.
13. List out some of the benefits of OOP.(any 2 which u remember)
• Eliminate redundant code
• Saves development time and leads to higher productivity
• Helps to build secure programs
• Easy to partition work
• Small programs can be easily upgraded to large programs
• Software complexity can easily be managed
14. Define C++.
C++ is an object oriented programming language developed by Bjarne Stroustrup. It is a
super set of C. Initially it was known as “C with Classes”. It is a versatile language for
handling large programs.
15. What are the input and output operators used in C++?
The identifier cin is used for input operation. The input operator used is >>, which is known
as the extraction or get from operator. The syntax is, cin >> n1; The identifier cout is used
for output operation. The input operator used is
// main() is where program execution begins.
int main() {
cout is needed.
● The line using namespace std; tells the compiler to use the std namespace. Namespaces are a
relatively recent addition to C++.
● The next line ‘// main() is where program execution begins.’ is a single-line comment available
in C++. Single-line comments begin with // and stop at the end of the line.
● The line int main() is the main function where program execution begins.
● The next line cout ,>=,!=
Logical Operators
&&, ||,!
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. &, |, and ^
Assignment Operators
=,+=,*=..
There are following assignment operators supported by C++ language −
Misc Operators
Scope resolution
sizeof, ? :,
::
member access operator
.
→, .*pointer operator
24. Explain control structures in c++?
1. loops : for, while, do while
2. conditional / decision making statements : if, if else, nested if, switch
3. Branching statements : break, goto, continue
C++ programming language provides the following type of loops to handle
looping requirements.
Sr.No
Loop Type & Description
1
while loop
Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the
loop body.
2
for loop
Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
3
do…while loop
Like a ‘while’ statement, except that it tests the condition at
the end of the loop body.
4
nested loops
You can use one or more loop inside any another ‘while’,
‘for’ or ‘do..while’ loop.
Loop Control Statements
Loop control statements change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that
scope are destroyed.
C++ supports the following control statements.
Sr.No
1
Control Statement & Description
break statement
Terminates the loop or switch statement and transfers
execution to the statement immediately following the loop
or switch.
2
continue statement
Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
3
goto statement
Transfers control to the labeled statement. Though it is not
advised to use goto statement in your program.
The Infinite Loop
A loop becomes infinite loop if a condition never becomes false. The for loop
is traditionally used for this purpose. Since none of the three expressions that
form the ‘for’ loop are required, you can make an endless loop by leaving the
conditional expression empty.
#include
using namespace std;
int main () {
for( ; ; ) {
printf(“This loop will run forever.\n”);
}
return 0;
}
When the conditional expression is absent, it is assumed to be true. You may
have an initialization and increment expression, but C++ programmers more
commonly use the ‘for (;;)’ construct to signify an infinite loop.
C++ programming language provides following types of decision making
statements.
Sr.No
1
Statement & Description
if statement
An ‘if’ statement consists of a boolean expression followed
by one or more statements.
2
if…else statement
An ‘if’ statement can be followed by an optional ‘else’
statement, which executes when the boolean expression is
false.
3
switch statement
A ‘switch’ statement allows a variable to be tested for
equality against a list of values.
4
nested if statements
You can use one ‘if’ or ‘else if’ statement inside another ‘if’
or ‘else if’ statement(s).
5
nested switch statements
You can use one ‘switch’ statement inside another ‘switch’
statement(s).
25. Explain Operator overloading in c++?
Operator Overloading in C++
You can redefine or overload most of the built-in operators available in C++.
Thus, a programmer can use operators with user-defined types as well.
Overloaded operators are functions with special names: the keyword
“operator” followed by the symbol for the operator being defined. Like any
other function, an overloaded operator has a return type and a parameter
list.
Box operator+(const Box&);
declares the addition operator that can be used to add two Box objects and
returns final Box object. Most overloaded operators may be defined as ordinary nonmember functions or as class member functions. In case we define above function as
non-member function of a class then we would have to pass two arguments for each
operand as follows −
Box operator+(const Box&, const Box&);
Following is the example to show the concept of operator over loading using
a member function. Here an object is passed as an argument whose
properties will be accessed using this object, the object which will call this
operator can be accessed using this operator as explained below −
#include
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length;
// Length of a box
double breadth;
// Breadth of a box
double height;
// Height of a box
};
// Main function for the program
int main() {
Box Box1;
// Declare Box1 of type Box
Box Box2;
// Declare Box2 of type Box
Box Box3;
// Declare Box3 of type Box
double volume = 0.0;
// Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout
=
++
—
>
==
!=
&&
||
+=
-=
/=
%=
^=
&=
|=
*=
>=
[]
()
->
->*
new
new []
delete
delete []
Following is the list of operators, which can not be overloaded −
::
.*
.
?:
Eg.String1=”wel”,
String2=”come”
String1 + string2=”welcome” which is not a built in but overloaded operation.
…