Future Vision BIE Future Vision BIE


ONE STOP FOR ALL STUDY MATERIALS & LAB PROGRAMS


E MENU Whatsapp Share Join Telegram, to get Instant Updates
×

NOTE!

Click on MENU to Browse between Subjects...

Advertisement

18CS45 - OBJECT ORIENTED CONCEPTS

Answer Script for Module 1

Solved Previous Year Question Paper

CBCS SCHEME


OBJECT ORIENTED CONCEPTS

(Effective from the academic year 2018 -2019)

SEMESTER - IV

Course Code 18CS45 CIE Marks 40

Number of Contact Hours/Week 3:2:0

SEE Marks 60

Total Number of Contact Hours 50

Exam Hours 03


Advertisement

These Questions are being framed for helping the students in the "FINAL Exams" Only (Remember for Internals the Question Paper is set by your respective teachers). Questions may be repeated, just to show students how VTU can frame Questions.

- ADMIN

18CS45 - OBJECT ORIENTED CONCEPTS

Answer Script for Module 1

Solved Previous Year Question Paper

CBCS SCHEME




× CLICK ON THE QUESTIONS TO VIEW ANSWER

The C++ allows member functions in Structure. The Structures (the

struct

keyword) have been redefined to allow member functions also. Illustrates Below

Loading Image

Program 1.1:

Illustration of C++ allows member functions in structures

First, we must notice that functions have also been defined within

the scope of the structure definition.

This means that not only the member data of the structure can be accessed through the variables of the structures but also the member functions can be invoked.

The

struct

keyword has actually been redefined in C++. This latter point is illustrated by the

main( ) function

in Program 1.1 above. We must make careful note of the way variables of the structure have been declared and how the member functions have been invoked.

Member functions

are invoked in much the same way as

member data

are accessed, that is, by using the

variable-to-member

access

operator

.

In a

member function

, one can refer directly to members of the object for which the member function is invoked. For example, as a result of the second line of the

main( ) function

in Program 1.1, it is

d1.iFeet

that gets the value of

2

.

On the other hand, it is

d2.iFeet

that gets the value of

3

when the fourth line is invoked. This is explained in the section on '

this' pointer

that follows shortly.

Each structure variable contains a separate copy of the member data within itself.

However, only one copy of the member function exists. Again, the section on the '

this'

pointer explains this. However, in the above example, note that the member data of structure variables can still be accessed directly.

The following line of code illustrates

this

.

d1.iFeet=2; //legal



Advertisement

Data abstraction

is a

virtue

by which

an object hides its

internal operations

from the

rest of the program

. It makes it

unnecessary

for the

client programs to know how the data is internally arranged

in the

object

. Thus, it obviates the need for the client programs to write precautionary code upon creating and while using objects.

Now, in order to understand this concept, let us take an example in C++. The library programmer, who has designed the

Distance class

, wants to ensure that the

fInches portion

of an object of the class should never exceed

12.

If a value

larger than 12

is specified by an application programmer while calling the

Distance::setInches( ) function

, the logic incorporated within the definition of the function should automatically increment the value of

iFeet

and decrement the value of

fInches

by suitable amounts. A

modified

definition of the

Distance::setInches( )

function is as follows.

Loading Image

Here, we notice that an application programmer need

notsend values less than 12

while

calling

the

Distance::setInches( ) function

. The

default logic

within the

Distance::setInches( )

function does the necessary adjustments. This is an

example of data abstraction

.

The above

restriction may not appear mandatory

. However, very soon we will create classes where similar restrictions will be absolutely necessary (and also complicated).

Similarly, the definition of the

Distance::add( )

function should also be modified as follows by the library programmer. Here, it can be assumed that the value of

fInches portion

of neither the invoking object nor the object appearing as

formal argument ('dd') can be greater than 12

.

Enforcing restrictions on the data members of a class

Now, if we write the statements shown in 2.2, (Enforcing restrictions on the data members of a class) then the value of

d3.fInches

will become

3

(not 15) and the value of

d3.iFeet

will become

4

(not 3).

It has already been mentioned that real-world objects never attain an invalid state. They also do not start in an invalid state.

Let us continue with our earlier example-the

Distance class

. Recollect that it is the library programmer's intention to ensure that the value of

fInches portion of none

of the objects of the

class Distance should exceed 12

. Now, let us consider Program 2.3.

Object gets created with improper values

Program 2.3:

Object gets created with improper values

As you can see, the value of

fInches

of '

d1

' is larger than

12

! This happened because the value of both

iFeet

and

fInches

automatically got set to junk values when '

d1

' was allocated memory and the junk value is

larger than 12

for

d1.fInches

. Thus, the objective of the library programmer to keep the value of

fInches less than 12

has

not yet been achieved

.

Distance d1;

d1.setFeet(0); //initialization

d1.setInches(0.0); //initialization

Obviously, the library programmer would like to add a function to the

Distance class

that gets called

automatically

whenever an object is created and sets the values of the data members of the object properly. Such a function is the

constructor.

Data abstraction

is

effective

due to

data hiding only.


C++ allows

two or more functions

to have the

same name

. For this, however, they

must have different signatures

.

Signature of a function

means the

number, type, and sequence of formal arguments

of the function.

In order to

distinguish

amongst the

functions

with the

same name,

the

compiler expects

their

signatures to be different

. Depending upon the

type of parameters

that are

passed

to the f

unction call

, the

compiler decides

which of the

available definitions

will be

invoked

.

For this,

function prototypes

should be

provided

to the

compiler

for matching the

function calls

. Accordingly,

the linker

, during

link time

,

links

the

function call

with the

correct function definition

. Program 3.1 clarifies this.

Loading Image

Program 3.1 Function overloading

Just like ordinary functions, the definitions of overloaded functions are also put in libraries. Moreover, the function prototypes are placed in header files.

The two function prototypes at the beginning of the program tell the compiler the two different ways in which the

add( )

function can be

called

. When the compiler encounters the

two distinct calls to the add( ) function

, it already has the

prototypes to satisfy them both

. Thus, the compilation phase is

completed successfully

. During linking, the linker finds the two necessary definitions of the

add( ) function

and, hence, links successfully to create the executable file.

The

compiler decides

which function is to be called based upon the number, type, and sequence of parameters that are passed to the function call. When the compiler encounters the first function call,

x=add(10,20);

It decides that the function that takes two integers as formal arguments is to be executed. Accordingly, the linker then searches for the definition of the add() function where there are two integers as formal arguments.

Similarly, the second call to the add() function y=add(30,40,50); is also handled by the compiler and the linker.

Note the importance of

function prototyping

. Since function prototyping is

mandatory in C++,

it is possible for the compiler to support function overloading properly. The compiler is able to not only restrict the number of ways in which a function can be called but also support more than one way in which a function can be called

Function overloading

is possible because of the necessity to prototype functions.

Function overloading

is also known as

function polymorphism

because, just like

polymorphism in the real world where an entity exists in more than one form

, the same function name carries different meanings.

Function polymorphism

is static in nature because the function definition to be

executed is selected

by the compiler during compile time itself. Thus, an

overloaded function is said to exhibit static polymorphism

.



Advertisement

A class can be a

friend of another class

.

Member functions

of a

friend class

can access

private data members

of objects of the class of which it is a

friend

. If class B is to be made a friend of class A, then the statement

friend class B;

should be written within the definition of class A. Program 4.1 illustrates this.

Declaring friend classes

Program 4.1 Declaring friend classes

It does not matter whether the statement declaring class B as a friend is mentioned within the private or the public section of class A. Now, member functions of class B can access the private data members of objects of class A. Program 4.2 exemplifies this

Friend member functions:

Program 4.2 Effect of declaring a friend class

4.2

Friend member functions:

How can we make some specific member functions of one class friendly to another class? For making only B::test_friend() function a friend of class A, replace the line

friend class B;

in the declaration of the class A with the line

friend void B::test_friend();

The modified definition of the class A is

Loading Image

However, in order to compile this code successfully, the compiler should first see the definition of the class B. Otherwise, it does not know that test_friend( ) is a member function of the class B. This means that we should put the definition of class B before the definition of class A.

However, a pointer of type A * is a private data member of class B. So, the compiler should also know that there is a class A before it compiles the definition of class B. This problem of circular dependence is solved by forward declaration.

class A; //Declaration only! Not definition!!

before the definition of class B. Now, the declarations and definitions of the two classes appear as shown in Program 4.3.

Forward declaring a class that requires a friend

Program 4.3 Forward declaring a class that requires a friend


A constructor in C++ is a

special method

that is automatically called when an object of a class is created.

To create a constructor, use the same name as the class, followed by parentheses ():

Example Program:

Types of Constructors

  1. Constructor has same name as the class itself
  2. Constructors don't have return type
  3. A constructor is automatically called when an object is created.
  4. If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).

5.2

Types of Constructors:

Default Constructors

5.2.1 Default Constructors:

Default constructor is the constructor which doesn't take any argument. It has no parameters.

Program Illustrating Default Constructor

Program 5.3 Program Illustrating Default Constructor

5.2.2

Parameterized Constructors:

It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor's body, use the parameters to initialize the object.

Program Illustrating Parameterized Constructors

Program 5.4 Program Illustrating Parameterized Constructors

Uses of Parameterized constructor:

I. It is used to initialize the various data elements of different objects with different values when they are created.

II. It is used to overload constructors.

5.2.3

Copy Constructor

: A copy constructor is a member function which initializes an object using another object of the same class.

Whenever we define one or more non-default constructors (with parameters) for a class, a default constructor (without parameters) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is not necessary but it's considered to be the best practice to always define a default constructor.

Program Illustrating Copy Constructor

Program 5.5: Program Illustrating Copy Constructor


lIKE THE CONTENT? FOLLOW US ON INSTAGRAM: @futurevisionbie
Get Notified on Instagram Also

For immediate Notification Join the Telegram Channel



× NOTE: Each Page Provides only 5 Questions & Answer
Below Page NAVIGATION Links are Provided...
All the Questions on Question Bank Is SOLVED

Advertisement



× SUGGESTION: SHARE WITH ALL THE STUDENTS AND FRIENDS -ADMIN