OOPSTHEORY

advertisement
OOPS THEORY
OF C++
AND REVIEW QUESTIONS
Polymorphism
is the phenomenon where different
objects produces different set of actions for the same
message.
Types of polymorphism are function overloading ,constructor overloading,
For example:
Function overloading
void add (int x ,int y){}
int add(int x,int y,intz
{
}
float add( flaot x,float y);
Abstraction
refers to the act of representing the essential features of an object without including the nonessential details.
Encapsulation or information hiding is the process of hiding non-essential details of an object
and showing only the essential details . Encapsulation hides the complexity of an object.
Disadvantages of OOPS

OOP is a high level concepts so takes more time to execute as many routines run
behind at the time of execution.

Offers less number of functions as compared to low level programming which
interacts directly with hardware.

Increased burden on part of OOP developers classes tend to be overly
generalized .

The relation between the classes is artificial at times.
What is the essence of Object Oriented programming >
Ans
The essence of object-oriented programming is to treat data and the
procedures that act upon the data as a single "object"--a self-contained entity
with an identity and certain characteristics of its own
Q2
Explain the concept of encapsulation ?
Ans
The property of being a self-contained unit is called encapsulation. With
encapsulation, we can implement data hiding. Data hiding is the highly
valued characteristic that an object can be used without the user knowing or
caring how it works internally. Just as you can use a refrigerator without
knowing how the compressor works, you can use a well-designed object
without knowing about its internal data members.
Q3
What is the advantage of inheritance ?
Ans
C++ supports the idea of reuse through inheritance. A new type, which is an
extension of an existing type, can be declared. This new subclass is said to
derive from the existing type and is sometimes called a derived type..
Q4
What is Polymorphism ?
Ans
C++ supports the idea that different objects do different things for the same
message .Poly means many, and morph means form. Polymorphism refers to
the same name taking many forms. Polymorphism plays an important role in
allowing objects having different internal structures to share the same external
interface.
Q5
Define encapsulation?
Ans
The wrapping up of data and functions into a single unit (called class) is
known as encapsulation. Data encapsulation is most remarkable feature of a
class. The data is not accessible to the outside world, and only those functions
which are wrapped in the class can access it.
Q6
Define Abstraction ?
Ans
Abstraction refers to the act of representing essential features without
including the background details or explanations.
Q7
What is inheritance ?
Ans
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.
Q8
What is reusability of code ?
Ans
The concept of Inheritance provides the idea of reusability. This means that we
can add additional features to an existing class without modifying it. This is
possible by deriving a new class form the existing one. The new class will have
the combined features of both the classes.
Q9
Write one difference between procedural ,event driven programming and
object oriented programming
Ans
Old-fashioned programs forced the user to proceed step-by-step through a
series of screens. Modern event-driven programs present all the choices at
once and respond to the user's actions.
Object-oriented programming attempts to respond to these needs, providing
techniques for managing enormous complexity, achieving reuse of software
components, and coupling data with the tasks that manipulate that data.
The essence of object-oriented programming is to treat data and the
procedures that act upon the data as a single "object"--a self-contained entity
with an identity and certain characteristics of its own.
Q10
What is instantiation process in C++?
Ans
It is the process of creating an object from the class
Q11
What is the difference between the base class and the sub class ?
Ans
A base class is one ,from which another , more specialized classes are
derived.
A derived class is one that inherits properties from another class.
Q12
Give advantages of Object oriented approach .
Ans
1.
2.
Realistic Modeling : Object Oriented approach is more closer to real life
It makes such a complex entity software easier to deal with by breaking
3.
the code into smaller manageable independent components .
Reusability code :Code once written ,can be reused again using
4.
inheritance ,hence it saves time and cost.
Flexibility to change , hence systems are easier to maintain .
Q13
Give disadvantages of traditional programming methods
Ans


Data and operations are separated
No data abstraction or info hiding


Not responsive to changes in problem space
Inadequate for concurrent problems
Q14 What is the difference between data hiding and data encapsulation
Ans: Encapsulation is wrapping of data into single unit (eg. class)
Abstraction is hiding unessential parts and showing only essential data.
(eg. student class- name, age are essential parts while height, weight are not essential, so hiding
information of height and weight)
Q15 What is the difference between data encapsulation and data abstraction?
Data Encapsulation : It is a process of bundling the data, and the functions that use them, as a
single unit( atleast in C++) . In C++ the data type "class" is used to achieve this.
Data Abstraction : It is a process of exposing only the interfaces and hiding the implementation
details from the user. The interface exposed will remain the same: even if the internal
implementation changes. This helps the user to use the new functionality without rewriting the
client that was designed for previous implementation
Review of C++
1. What is the difference between Logical Error and syntax error ?:
Error occurred due to incorrect logic applied by the programmer.
Syntax Error:
Error occurred due to not following the proper grammar/syntax of the language
OR
Error occurred due to violating rules of the programming language
Example:
//Program to find area and perimeter of rectangle
void main()
{
int A,B,AR,P;
A=10;
B=20;
AR=2*(A*B); //Logical Error – Wrong Formula
P=2*(A+B);
cout<<A<<P>>endl; //Syntax Error – Use of >> with cout
}
2. Differentiate between a Run Time Error and Syntax Error. Also give suitable
examples of each in C++.
1. Ans: Syntax error
It is a type of error that occurs when rules of C++ language are violated in the program. It
occurs due to grammatical mistake .
For example in the following code :
#include<iostream.h>
void main()
{
cout <<”hello”
}
; statement missing is a syntax error.
The above code will result in Declaration syntax error due to missing ; (terminator) at the end
of the statement cout<< “hello”;
Run time error:
2.
It occurs due to utilizing unavailable memory at the time of execution of the program For
instance if you have written program to divide a number by zero is run time rror.
int a;
a = 5;
cout << a / 0;
Run time error
3. What is constant?
Ans: A constant is a value of any type that has the same value and can never change.
The simplest use is to declare a named constant. is adding ‘const’ before variable name
.One has to initialize it immediately in the constructor because, of course, one cannot set
the value later as that would be altering it. For example,
const
int Constant1 = 30;
keyword to declare constant
const will create an integer constant, unimaginatively called ‘Constant1’, with the value
30
4. What are literals in C++.How many types of literals you know in C++.
A:
Literals (also known as constants) are expressions with a fixed value. These are the
data items whose value never change during execution of the program. It is often
referred as literals.
Five types of literals are there in C++
Integer literals , Floating point literals, character literal, string literals, Boolean values .
5. Name the different types of error messages displayed in C++.
A:
Syntax error
Semantic error
Type error
Logical error
: when typographic rules of programming are violated
: when program statements are not meaningful
:when datatype is mismatch.
: when data fed is logically incorrect.
Run time error : when data reaches out of memory at the time of execution.
6. find the output of the following code:
void main()
{
clrscr();
int x = 2,y = 3;
cout<< x++ << ++ x << -- x << x <<endl;
// evaluation is from right to left and print
//
from left to right in both the expressions.
cout<< y++ << --y << y ;
getch();
}
Ans: 2212
223
7.
What is actual parameter and formal parameter .
Ans: Formal parameters/parameters :
The parameters specified in the function declarator are known as formal parameters.
These are the names used inside a function to refer to its arguments.
For example in the above program :
float area( int rad)
Formal parameter
where rad is telling the compiler to accept an integer value at the time of function
calling.
Actual parameters /arguments :
The parameters in the body of the function call are called actual arguments and are
also known as arguments.
They may be expressed as constants, single variables or more complex expressions.
like
Actual parameter passed as variable
:area ( r ) ;
Actual parameter passed as constant
: area( 4) ;
Actual parameter passed as an expression : area( 4*r) ;
Actual parameters
These formal arguments are called actual parameters when they are used in function
reference. These are the values used as arguments when the function is actually called.
In other words, the values that the formal parameters will have on entry to the function.
8. what is the difference between call by value and call by reference?
What are the two ways of calling a function?
A: Call by Value: In this method, the value of each of the actual arguments in the
calling function is copied into corresponding formal arguments of the called
function. In pass by value, the changes made to formal arguments in the called
function have no effect on the values of actual arguments in the calling function.
For example : int add (int x,int y) ;
Call by Reference: In this method, the addresses of actual arguments in the calling
function are copied into formal arguments of the called function. This means that
using these addresses, we would have an access to the actual arguments and hence
we would be able to manipulate them.
For example : int add(int & x,int y);
9. What is macro. give an example?
Ans:
A macro is an operation defined in a # define preprocessor directive. It acts like
a template for performing operation in program .It doesnot require data type checking.
A macro can have one or more arguments or no arguments , and a macro with no
argument is processed like a symbolic constant. A macro simply behaves for text
substitution.
Consider the following example
#define getmax(a,b) a>b?a:b
This would replace any occurrence of getmax followed by two arguments by the
replacement expression, but also replacing each argument by its identifier, exactly as
you would expect if it was a function:
Random numbers :
Header File : #include<stdlib.h>
Method
rand()
Prototype
int rand(void) ;
srand()
void srand( unsigned seed)
Description
Generates random number .
Ex: int x= rand();
Cout<< x; // will generate random no from 0 to 32767
Initializes random number generator
Ex: srand(time(0));
int
random
(int
n);
random()
generates a random number in the range of 0 to n-1
example: random(10) will generate numbers from 0
to 9
randomize()
void random( void)
Macro that initializes random number generator .
randomize(); // will initialize random ()
Predict the maximum possible value in the following code.
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
void main( )
{
randomize();
int Score[] = {25,20,34,56, 72, 63}, Myscore;
Myscore = Score[2+random(4)];
cout<<Myscore<<endl;
getch();
}
Solution : Output: 72 is the maximum possible value.
Explanation:
score[] is an array where indexing starts from 0 and each index refers to the element provided in
brackets like this
score[0] = 25
score[1] = 20
score[2] = 34
score[3] = 56
score[4] =72
and score[5] =63
Now when random function is used in the program then
Myscore = score[2 +random(4) ]
random(4) means it will generate random numbers from 0 to 4-1 i.e 0 to 3
Step1.
Myscore = score [2 + 0 ]  score[2] = 34
Step2 .
Myscore = score [2 + 1 ]  score[3] = 56
Step3 .
Myscore = score [2 + 2 ]  score[4] = 72
Step4 .
Myscore = score [2 + 3 ]  score[5] = 63
Thus 72 is the maximum possible value .
1. In the following program, if the value of N given by the user is 15, what maximum
and minimum values the program could possibly display? 2
#include <iostream.h>
#include <stdlib.h>
void main()
{
int N,Guessme;
randomize();
cin>>N;
Guessme=random(N)+10;
cout<<Guessme<<endl;
}
A:
Maximum Value: 24 Minimum Value: 10
2. In the following program, if the value of N given by the user is 20, what
maximum
and minimum values the program could possibly display? 2
#include <iostream.h>
#include <stdlib.h>
void main()
{
int N,Guessnum;
randomize();
cin>>N;
Guessnum=random(N-10)+10;
cout<<Guessnum<<endl;
}
A:
Maximum Value: 19 Minimum Value: 10
3. In the following C++ program what is the expected value of Myscore from
Options (i) to (iv) given below. Justify your answer. 2
#include<stdlib.h>
#include<iostream.h>
void main( )
{
randomize();
int Score[] = {25,20,34,56, 72, 63}, Myscore;
Myscore = Score[2 + random(2)];
cout<<Myscore<<endl; }
(i) 25
(ii) 34
(iii) 20
(iv) None of the above
A:
(iv) None of the above because it will generate 56 because
score[2+ random(2) ] where random (2) will generate random numbers from 0to1
therefore maximum value of myscore=Score[3] i.e 56
(remember : indexing of array is from zero)
Arrays:
a. sum of diagonals of matrix
//to print the sum of right and left diagonals
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[3][3],lsum,rsum,i,j;
cout<<"enter the elements"<<endl;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
for (i=0;i<3;i++)
{ cout<<"\n";
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
}
for(i=0;i<j;i++)
{lsum=0;
if(i==j);
lsum=lsum+a[i][i];}
for(i=3;i>0;i--)
{rsum=0;
if(i==j);
rsum=rsum+a[i][i];}
cout<<"\nleft diagonal sum"<<lsum;
cout<<"\n";
cout<<"\nright diagonal sum"<<rsum;
getch();
}
Output:
enter the elements
1
3
5
7
9
11
13
15
17
1
3
5
7
9
11
13
15
17
left diagonal sum17
right diagonal sum9
b. Write a user-defined function named Lower_half() which takes 2D array A, with size N
rows and N columns as argument and prints the lower half of the array.
Eg;
1
2
3
output
5
6
7
5
6
9
1
2
9
1
Ans:
void Lower_half( int A[ ][ ],int N)
{
int i,j;
for(i=0;i<N;i++)
1
2
for(j=0;j<N;j++)
{
if(i<j)
cout<<A[i][j]<<’\t’;
cout<<endl;
}
}
All the best ………………………………..
Miss Sofia Goel
sofia_goel@rediffmail.com
Download