UNIT -1: Introduction to C++
JUNE-JULY 2009
1a. Differentiate between procedure oriented and object oriented programming.
(06 marks)
Procedure oriented programming divides the code into functions
Data is not secure. It can be manipulated by any procedure.
Compilers also do not prevent unauthorized functions from accessing/
manipulating structure variable.
The code design is centered around procedures.
OOPs ensure security of data.
Interfaces perfectly manipulate the internal parts of the objects, with exclusive
rights.
Functions logically related to data can only access the data, others can be
prevented from accessing the data members of the variables of this structure.
This is achieved with the help of Data encapsulation in classes.
Compile time errors against pieces of code are thrown which access unauthorized
data.
Data can be grouped either as private or public data.
1b. Why should default values be given to function arguments in function
prototyping and not in function definition? Write a program to add three numbers
using function which has one or more default arguments. (09 marks)
C++ allows a function to assign a parameter a default value when no argument
corresponding to that parameter is specified in a call to that function. The default value is
specified in a manner syntactically similar to a variable initialization. For example, this
declares myfunc() as taking one double argument with a default value of 0.0:
void myfunc(double d =
0.0)
{
// ...
}
Now, myfunc() can be called one of two ways, as the following examples
show:
myfunc(198.234); // pass an explicit
value myfunc(); // let function use
default
When you are creating functions that have default arguments, it is important to remember
that the default values must be specified only once, and this must be the first time the
function is declared within the file. If you try to specify new (or even the same) default
values in function definition,
Dept of CSE, SJBIT 2
,Object Oriented Programming in C++ 10CS36
the compiler will display an error and not compile your program. Even though default
arguments for the same function cannot be redefined, you can specify different default
arguments for each version of an overloaded function
#include <iostream>
class example{
int x, y, z;
public:
example(int i=0, int j=0, int k=0) {
x=i;
y=j;
z=k;
}
Example(int i=10,int j=20)
{
y=I;z=j;
}
Example(int i=1)
{
z=I;
}
int add() {
return x+y+z;
}
};
int main()
{
example a(2,3,4), b(2),c(2,3);
cout << a.add() << endl;
cout << b.add();
cout<< c.add();
return 0;
}
1c. What is data abstraction. How it is implemented in C++. Explain with an example
(05 marks)
Data abstraction refers to, providing only essential features by
hiding its background details.
example:
class result
{
int marks;
float percentage;
char name[20]; void
input();
void output();
}
Dept of CSE, SJBIT 3
,Object Oriented Programming in C++ 10CS36
main()
{
bank b1;
b1.input();
b1.output();
}
in the above example, b1 is an object calling input and output member
functions, but that code is invisible to the object b1.
Dept of CSE, SJBIT 4
, Object Oriented Programming in C++ 10CS36
MAY/JUNE 2010
1a Explain the various features of object oriented programming (10 marks)
Essential features of object oriented programming
Encapsulation
Inheritance
Polymorphism
Encapsulation in OOP-terminology means that an object has something like a shell
around it protecting the attributes or status variables from being accessed from outs ide of
the object. The belong to the object and to nobody else. Only the methods of the object
have access to them, can read and modify their values. The methods, providing the
services of the object, are the interface to the outside world.
Inheritance means that a derived class inherits all attributes and methods from its super
class, the class from which it is derived. This feature alleviates programmers from having
to start always from scratch when writing an object-oriented applications. Many times it
will be possible to design and implement a class that can be derived from existing ones in
some sort of class library, which one has either written oneself or that can be obtained
from another source.
Polymorphism in OOP-Terminology means that a certain message may be implemented
by different objects in different ways. A good example is the `display'-message sent to a
number, a string of characters or a list. It is always the message 'display yourself!'. The
object must know itself what has to be done and how, nobody cares as long as the object
appears on the screen.
1b. Discuss function prototyping, with an example. Also write its advantage (05
marks)
In C++ all functions must be declared before they are used. This is normally
accomplished using a function prototype. The use of parameter names is optional.
However, they enable the compiler to identify any type mismatches by name when an
error occurs, so it is a good idea to include them. It produces anThe function prototyping
tells the number of arguments,type of arguments and type of the return type to the
compiler.The function prototyping is a declaration statement in the calling program.
General Format:
returnType methodName(Argument List);
*Where Argument List specify the type and name of the arguments.
Dept of CSE, SJBIT 5