Vous êtes sur la page 1sur 12

E

CSE 202 : Object Oriented Programming


Solution
Part A

Question 1:

a) What is the significance of a reference variable? Explain with an example


program.

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.

We can create a reference variable like;

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?

Answer: Differences between POP and OOP:

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.

4. POP is a bottom-up approach and OOP is a top-down approach of program


development.

d) “Default arguments must be supplied to the trailing arguments of a function.”


What will be the results if a programmer doesn’t obey this rule? Give example.

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

int function(int a = 10, int b, int c) // Wrong

int function(int a = 10, int b, int c =20) // Wrong

int function(int a, int b=10, int c=20) // Right

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.

Answer: Syntax for the function definition outside of class is as follows:


<return_type> <class_name>: :<function_name>(<argument_list>)

// Function Body

g) What do you understand by precedence and associativity of an operator? Explain


with a suitable example.

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.

h) Is it possible to initialize the data member of an object as soon as it is created?


How? Give an example.

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.

i) Operator overloading provides an operator some additional responsibilities


without changing its syntax. Explain this fact with an example.

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;

when we overload ‘+’ operator, we can add two objects;

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;}

friend void show(A); // show() is a friend of class A

};

void show(A Obj)

cout<<Obj.a; // show() is a non member function of class A but accessing private

// data of class A

int main()

A Obj;

show(Obj);

Part B

Question 2:

a) What do you understand by access specifiers? Explain each of them.


Answer: Access specifiers are used to identify access rights for the data and member
functions of the class. There are three main types of access specifiers in C++
programming language:

• 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.

Public members are accessible from outside the class.

A protected access specifier is a stage between private and public access. If


member functions defined in a class are protected, they cannot be accessed from
outside the class but can be accessed from the derived 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.

Answer: private members of a class cannot be inherited. Public members of a class


can be inherited but it can be accessed from anywhere in the program. If a
programmer wants to use the data members of the class as said in the question, he
must use a third access specifier ‘protected’. A protected data cannot be accessed
from outside the class but can be accessed from the derived class. Following is an
example of protected data member.

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:

a) What are the characteristics of a constructor and a destructor of a class?

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.

Destructor: A destructor is a member function of class, the name of which is same as


class name. It must be preceded by a tilde character ‘~’. It does not have any argument.
It will be called as soon as an object of its class loses its scope.
b) In which situation a constructor is known as a dynamic constructor? Give a
suitable example for this.

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;

v = new int [size];

~vector()

delete v;

void read();

void show_sum();

};

void vector::read()

for(int i =0; i < size; i++)


{

cout<<“Enter Vector [ ”<<i<<“ ];

cin>>v[i];

void vector::show_sum()

int sum = 0;

for(int i =0; i < size; i++)

sum += v[i];

cout<<“Vector sum = “<<sum;

void main()

int count;

cout<<“How many vectors are in the vector: ”;

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.

Answer: No it is not possible. If we want to create a nameless temporary object then


the class definition must have a parameterized constructor. Following is an
example.

#include <iostream.h>
class Index

private:

int value;

public:

Index() : value(0) //Constructor

{}

Index(int c) : value(c) // Parametrized Constructor

{}

int getindex()

return value;

Index operator++()

value = value+1;

return Index(value); // nameless temporary object

};

Void main()

Index idx1, idx2;

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.

Answer: type conversion or typecasting refers to changing an entity of one datatype


into another. There are two types of conversion: implicit and explicit. Implicit type
conversion, also known as automatic type conversion by the compiler. Following is
an example of automatic type conversion.

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.

Answer: There can be two types of conversion:

1. Basic type to user defined conversion: this type of conversion is


performed with the help of constructor function. We pass the basic
data type as an argument and within the constructor body we have
required statements for conversion. Following is the syntax

Constructor(Basic Type)

// steps for converting basic type to

// object attributes
}

2. User defined type to Basic type conversion: achieved through


operator function, which must be defined as overloaded basic data
type with no argument. Following is the syntax

operator BasicType()

// steps for converting object attributes

// to basic data types

Vous aimerez peut-être aussi