Object oriented programming with C++ 10CS36

advertisement
QUESTION PAPER SOLUTION
UNIT -1:
Introduction to C++
1. Describe the following characteristics of OOP. [DEC/JAN 2013, DEC//JAN 2014,
DEC/JAN 2015.]
i Encapsulation
Encapsulation is the word which came from the word "CAPSULE" which to put something
in a kind of shell. In OOP we are capsuling our code not in a shell but in "Objects" &
"Classes". So it means to hide the information into objects and classes is called
Encapsulation.
class cat
{
public void eat()
{
console.writeline("cat eats mouse");
}
};
Here we have defined a class and in 'class cat'" public void eat() " and " public void walk() "
are methods. Now we make an object in main class and call these methods...
static void main()
{
cat mycat = new cat();
mycat.eat();
mycat.walk();
console.readline();
}
ii Polymorphism
Polymorphism is the ability to use an operator or method in different ways. Polymorphism
gives different meanings or functions to the operators or methods. Poly, referring to many,
signifies the many uses of these operators and methods. A single method usage or an operator
functioning in many ways can be called polymorphism. Polymorphism refers to codes,
operations or objects that behave differently in different contexts.
iii Inheritance
The Inheritance is one of the main concepts of oop.Inheritance is just to adobe the functionality
(methods & properties) of one class to another. This simple term is known as Inheritance.
Lets have a look of code public
class cat
{
public void eat()
{
console.writeline("cat can eat");
}
public void walk()
{
console.writeline("cat can walk");
}
};
now we make another class of another cat and we inherit the methods of the first class to the
new class. Do remember in programming we inherit one class to another by coding " : " colon
between the parent and child class.
public class EgyptianCat : cat
{
public void jump()
{
console.writeline("Egyptian cat can jump");
}
public void bite()
{
console.writeline("Egyptian cat can not bite");
}
};
static void main()
{
cat mycat=new cat();
mycat.eat();
mycat.walk();
EgyptianCat yourcat=new EgyptianCat();
yourcat.eat();
yourcat.walk();
yourcat.jump();
yourcat.bite(); console.readline();
}
this is the code in main class in which we are inheriting the class Egyptian Cat to the
class cat, so that the new class becomes the child class of the first class which is now
called the parent class.
2. Discuss function prototyping, with an example. Also write its advantage.[ DEC 12/ JAN
13]
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 a 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.
/* This program uses a function prototype to enforce strong type checking. */ void
sqr_it(int *i); /* prototype */
int main(void)
{
int x;
x = 10;
sqr_it(x);
/* type mismatch */
return 0;
}
void sqr_it(int *i)
{
*i = *i * *i;
}
It produces an error message because it contains an attempt to call sqr_it() with an integer
argument instead of the integer pointer required. (It is illegal to convert an integer into a
pointer.)Function prototypes help you trap bugs before they occur. In addition, they help verify
that your program is working correctly by not allowing functions to be called with mismatched
arguments. If the function is defined before the function call, then it acts as function prototype.
3. Write the general form of function. Explain the different types of argument passing
techniques with example. [DEC/ Jan 13, DEC/JAN 15.]
A function is a group of statements that is executed when it is called from some point of the
program. The following is its format:
type name ( parameter1, parameter2, ...) { statements }
where:
•
type is the data type specifier of the data returned by the function.
•
name is the identifier by which it will be possible to call the function.
•
Parameters (as many as needed): Each parameter consists of a data type specifier followed
by an identifier, like any regular variable declaration (for example: int x) and which acts
within the function as a regular local variable. They allow to pass arguments to the function
when it is called. The different parameters are separated by commas.
•
statements is the function's body. It is a block of statements surrounded by braces { }.
Here you have the first function example:
1 // function example
2 #include <iostream>
3 using namespace std;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
The result is 8
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
4. Discuss the issues of procedure oriented systems with respect to object oriented systems?[
JUNE/JULY 13,JUN/JULY 15]
Procedure Oriented Programming
1. Prime focus is on functions and procedures that operate on data
2. Large programs are divided into smaller program units called
functions
3. Data and the functions that act upon it are treated as separate entities.
4. Data move freely around the systems from one function to another.
5. Program design follows “Top Down Approach”.
--------------------------------------…
Object Oriented Programming
1. Here more emphasis is laid on the data that is being operated and not the functions or procedures
2 .Programs are divided into what are called objects.
3. Both data and functions are treated together as an integral entity.
4. Data is hidden and cannot be accessed by external functions.
5. Program design follows “Bottom UP Approach
5. Why C++ introduced reference variable?[ DEC/JAN 14. ]
Declaring a variable as a reference rather than a normal variable simply entails appending
an ampersand to the type name, such as this "reference to an int" int& foo = ....;
When a reference is created, you must tell it which variable it will become an alias for. After
you create the reference, whenever you use the variable, you can just treat it as though it
were a regular integer variable. But when you create it, you must initialize it with another
variable, whose address it will keep around behind the scenes to allow you to use it to modify
that variable.
In a way, this is similar to having a pointer that always points to the same thing. One key
difference is that references do not require dereferencing in the same way that pointers do;
you just treat them as normal variables. A second difference is that when you create a
reference to a variable, you need not do anything special to get the memory address. The
compiler figures this out for you:
Here's a simple example of setting up a function to take an argument "by reference",
implementing the swap function:
void swap (int& first, int& second)
{
int temp = first;
first = second;
second = temp;
}
references should always be valid because you must always initialize a reference. This
means that barring some bizarre circumstances (see below), you can be certain that using a
reference is just like using a plain old non-reference variable. You don't need to check to
make sure that a reference isn't pointing to NULL, and you won't get bitten by an
uninitialized reference that you forgot to allocate memory for.
6. Give the comparison of C and C++ with examples. [DEC/JAN 14]
1. C follows the procedural programming paradigm while C++ is a multi-paradigm
language(procedural as well as object oriented)
In case of C, importance is given to the steps or procedure of the program while C++ focuses on
the data rather than the process.
Also, it is easier to implement/edit the code in case of C++ for the same reason.
2. In case of C, the data is not secured while the data is secured(hidden) in C++
This difference is due to specific OOP features like Data Hiding which are not present in C.
3. C is a low-level language while C++ is a middle-level language (Relatively, Please see the
discussion at the end of the post)
C is regarded as a low-level language (difficult interpretation & less user friendly) while C++
has features of both low-level (concentration on what’s going on in the machine hardware) &
high-level languages (concentration on the program itself) & hence is regarded as a middlelevel language.
4. C uses the top-down approach while C++ uses the bottom-up approach
In case of C, the program is formulated step by step, each step is processed into detail while
in C++, the base elements are first formulated which then are linked together to give rise to
larger systems.
5. C is function-driven while C++ is object-driven
Functions are the building blocks of a C program while objects are building blocks of a C++
program.
6. C++ supports function overloading while C does not
Overloading means two functions having the same name in the same program. This can be done
only in C++ with the help of Polymorphism (an OOP feature)
7. We can use functions inside structures in C++ but not in C.
In case of C++, functions can be used inside a structure while structures cannot contain functions
in C.
7. What are pointers explain with an example. .[ DEC/JAN 14. ]
A pointer is a variable which stores the address of another variable. There are two important
operators when working with pointers in C++: the address of (&) operator and the value of
(*) operator. They have been overloaded in C++ so they may have different uses in different
contexts.
How much storage space does a pointer consume? Use sizeof(ptr) without the '*' operator to
determine the memory utilised on your system. Irrespective of datatype or whether the pointer
points to a single variable or array, as a general rule, pointers must use the same amount of
memory space.
8. What is function overloading give example? [JAN 13/JAN 14/JAN 15]
It is a classification of static polymorphism in which a function call is resolved using the 'best
match technique ', i.e., the function is resolved depending upon the argument list. Method
overloading is usually associated with statically-typed programming languages which enforce
type checking in function calls. When overloading a method, you are really just making a
number of different methods that happen to have the same name. It is resolved at compile time
which of these methods are used.
Method overloading should not be confused with forms of polymorphism where the correct method
is chosen at runtime, e.g. through virtual functions, instead of statically.
Example: function overloading in C++
main()
{ cout<<volume(10);
cout<<volume(2.5,8);
cout<<volume(100,75,15);
}
// volume of a cube int
volume(int s)
{
return(s*s*s);
}
// volume of a cylinder double
volume(double r,int h)
{
return(3.14*r*r*h);
}
// volume of a cuboid long
volume(long l,int b,int h)
{
return(l*b*h);
}
9. Differentiate between procedure oriented and object oriented programming.[
JUNE/JULY 14.
]
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.
10. Explain inline functions? [DEC/JAN 13, DEC/JAN 14, JUNE/JULY 14]
The inline directive can be included before a function declaration to specify that the
function must be compiled as code at the same point where it is called. This is equivalent
to declaring a macro. Its advantage is only appreciated in very short functions, in which
the resulting code from compiling the program may be faster if the overhead of calling a
function (stacking of arguments) is avoided.
The format for its declaration is:
inline type name ( arguments ... ) { instructions ... } and the call is just like the call to any
other function. It is not necessary to include the inline keyword before each call, only in
the declaration
.
Download