Académique Documents
Professionnel Documents
Culture Documents
Question 1:
Answer: C++ references allow you to create a second name for a variable that you
can use to read or modify the original data stored in that variable.
Int a = 10;
Int &b=a;
Now if we modify the value of a, this modification will be reflected to b and vice-
versa. It allows a programmer to reduce the amount of copying of data.
b) Suppose a programmer does not specify the return type of a function at the time
of declaration and definition of that function. Discuss the results of doing this.
Answer: If a function declaration does not specify a return type, the compiler
assumes an implicit return type of int.
c) What are the differences between Procedures Oriented Programming and Object
Oriented Programming?
1. POP gives more emphasis to functions. OOP gives more emphasis to data.
2. POP divides a large program into smaller parts known as functions. OOP
divides the program into smaller parts known as objects.
3. Data is not secured in case of POP because it openly moves from function to
function. In OOP, data is secured. Not every function in the program can
access the private data.
Answer: if the default arguments are not supplied to the trailing arguments then the
compiler will display an error message; “default value missing…”. The reason for this
is that compiler is not intelligent enough to guess a match for formal arguments and
actual arguments.
Example
e) What is an inline function and how it is different from other normal functions?
Answer: An inline function is a function which is expanded in line. It means the call
statement to the function will be substituted by its corresponding definition. A request
for a function to make it inline can be done by using the keyword ‘inline’. Inline
function is different from other non-inline functions because transfer of control does
not take place from one function to another function if we use inline functions. This
saves a lot of time.
f) How can we define a member function outside of the class? Write the syntax.
// Function Body
Answer: Each operator in C++ has some priority (precedence) associated with it. In
an arithmetic expression, the operator which has higher priority gets executed first. If
the priority of two operators is same then the compiler looks for the associativity. It
may be either form left-to-right or from right-to-left.
Answer: Yes, it is possible to initialize the data members of a class as soon as the
object is created. This is possible with the help of constructors. A constructor is a
member function of a class that gets executed automatically when an object of its
class gets created. Within the body of a constructor we can have the code to initialize
the object data.
Answer: yes, this is absolutely true. Let’s take an example of ‘+’ operator. We use ‘+’
operator to add primitive data types.
Int a=10,b=20,sum;
sum = a + b;
Complex C1(2,4),C2(4,2),C;
C = C1 + C2;
It is obvious from the given example that the syntax is not changed.
j) Is it possible for a non member function to access the private member of a class?
Give your answer with an example.
Answer: yes, it is possible for a non-member function to access the private data
members of a class if the nonmember function is declared as a friend for that class.
Example:
class A
int a;
public:
A(int x){a=x;}
};
// data of class A
int main()
A Obj;
show(Obj);
Part B
Question 2:
• private
• public
• protected
A private member within a class denotes that only members of the same class have
accessibility. The private member is inaccessible from outside the class.
b) A programmer wants to use the data members of a class within its child class,
but not in any other class. Give the programmer a solution for this with a suitable
example.
Class A
Protected:
Int a;
Public:
A(int p=0):x(p){}
};
Class B:public A
Public:
Void show()
Cout<<a;
};
Void main()
B X(5);
X.show();
Question 3:
Answer:
Constructor: A constructor is a special method that is created when the object is created
or defined. This particular method holds the same name as that of the object and it
initializes the instance of the object whenever that object is created. The constructor
also usually holds the initializations of the different declared member variables of its
object. Unlike some of the other methods, the constructor does not return a value, not
even void. It must be defined in the public section of the class.
Answer: A constructor which allocates the memory dynamically at run time is known
as dynamic constructor. In this case a constructor contains new operator within its
body.
Example:
class vector
int *v;
int sz;
public:
vector(int size)
sz = size;
~vector()
delete v;
void read();
void show_sum();
};
void vector::read()
cin>>v[i];
void vector::show_sum()
int sum = 0;
sum += v[i];
void main()
int count;
cin>>count;
vector v1(count);
v1.read();
v1.show_sum();
Question 4:
a) WAP (overload * operator) to multiply the data members of two objects and store
the result in a third object.
Answer:
Class A
Int a;
Public:
A(int p):a(p){}
A operator*(A X)
A Y;
Y.a=a*X.a;
Return Y;
Void main()
A ob1(5),ob2(4),ob;
Ob=ob1*ob2;
b) Is it possible to create a nameless temporary object if the program does not have
any constructor in the class? Give your answer with example.
#include <iostream.h>
class Index
private:
int value;
public:
{}
{}
int getindex()
return value;
Index operator++()
value = value+1;
};
Void main()
Cout<<idx1.getindex();
Cout<<idx2.getindex();
Idx1 = ++idx2;
Idx2++;
Cout<<idx1.getindex();
Cout<<idx2.getindex();
Question 5:
a) What do you understand by the term ‘Automatic Type Conversion’? Explain with
an example.
double d;
long l;
int i;
if (d > i) d = i;
if (i > l) l = i;
if (d == l) d *= 2;
b) How a programmer can convert the basic data type to a class type and vice-
versa? Give your answer with proper syntax of the conversion functions for both
types of conversion.
Constructor(Basic Type)
// object attributes
}
operator BasicType()