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

Static data members hold global data that is common to all objects of the class. Examples of such global data are

i. count of objects currently present,

ii. common data accessed by all objects, etc.

Let us consider class Account. We want all objects of this class to calculate interest at the rate of say 4.5%. Therefore, this data should be globally available to all objects of this class.

This data cannot and should not be a member of the objects themselves. Otherwise, multiple copies of this data will be embedded within the objects taking up unnecessary space. Same value would have to be maintained for this data in all objects. This is very difficult. Thus, this data cannot be stored in a member variable of class Account.

At the same time, this data should not be stored in a global variable. Then the data is liable to be changed by even non-member functions. It will also potentially lead to name conflicts. However, this means that it should be stored in a member variable of class Account!

How can this conflict be resolved? Storing the data in a static variable of the class resolves this conflict. Static data members are members of the class and not of any object of the class, that is, they are not contained inside any object.

Declaring a static data member

Program 6.1 Declaring a static data member

6.2 Program to count Number of Objects.

Program to count Number of Objects



Advertisement

Structure is a programming construct in C that allows us to put together variables that should be together.

A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.

7.1 How to create a structure?

The 'struct' keyword is used to create a structure. The general syntax to create a structure is as shown below:

structure

Structures in C++ can contain two types of members:

  1. Data Member

    : These members are normal C++ variables. We can create a structure with variables of different data types in C++.
  2. Member Functions

    : These members are normal C++ functions. Along with variables, we can also include functions inside a structure declaration.

7.2 Program:

Program


BASIS FOR COMPARISON

Procedure oriented programming(POP)

Object-oriented programming(OOP)

Basic

Procedure/Structure oriented .

Object oriented.

Approach

Top-down.

Bottom-up.

Basis

Main focus is on "how to get the task done" i.e. on the procedure or structure of a program .

Main focus is on 'data security'. Hence, only objects are permitted to access the entities of a class.

Division

Large program is divided into units called functions.

Entire program is divided into objects.

Entity accessing mode

No access specifier observed.

Access specifier are "public", "private", "protected".

Overloading or Polymorphism

Neither it overload functions nor operators.

It overloads functions, constructors, and operators.

Inheritance

Their is no provision of inheritance.

Inheritance achieved in three modes public private and protected.

Data hiding & security

There is no proper way of hiding the data, so data is insecure

Data is hidden in three modes public, private, and protected. hence data security increases.

Data sharing

Global data is shared among the functions in the program.

Data is shared among the objects through the member functions.

Friend functions or friend classes

No concept of friend function.

Classes or function can become a friend of another class with the keyword "friend".
Note: "friend" keyword is used only in c++

Virtual classes or virtual function

No concept of virtual classes .

Concept of virtual function appear during inheritance.

Example

C, VB, FORTRAN, Pascal

C++, JAVA, VB.NET, C#.NET.



Advertisement

Inline functions

are used to increase the speed of execution of the executable files. C++ inserts calls to the normal functions and the inline functions in different ways in an executable

The executable program that is created after compiling the various source codes and linking them consists of a set of machine language instructions. When a program is started, the operating system loads these instructions into the computer's memory. Thus, each instruction has a particular memory address.

Obviously, an overhead is involved in

i. making the control jump back and forth and

ii. storing the address of the instruction to which the control should jump after the function terminates

The C++ inline function provides a solution to this problem. An inline function is a function whose compiled code is 'in line' with the rest of the program. That is, the compiler replaces the function call with the corresponding function code.

With inline code, the program does not have to jump to another location to execute the code and then jump back. Inline functions, thus, run a little faster than regular functions.

However, there is a trade-off between memory and speed. If an inline function is called repeatedly, then multiple copies of the function definition appear in the code. Thus, the executable program itself becomes so large that it occupies a lot of space in the computer's memory during run time. Consequently, the program runs slow instead of running fast. Thus, inline functions must be chosen with care.

For specifying an inline function, you must:

i. prefix the definition of the function with the inline keyword and

ii. define the function before all functions that call it, that is, define it in the header file itself.

The following listing illustrates the inline technique with the inline cube( ) function that cubes its argument. Note that the entire definition is in one line. That is not a necessary condition. But if the definition of a function does not fit in one line, the function is probably a poor candidate for an inline function!

Transfer of control in a non-inline function

Fig 9.1: Transfer of control in a non-inline function

Control does not get transferred in an inline function

Fig 9.2 Control does not get transferred in an inline function

Illustrates Inline functions

Program 9.3: Illustrates Inline functions

However, under some circumstances, the compiler, despite your indications, may not expand the function inline. Instead, it will issue a warning that the function could not be expanded inline and then compile all calls to such functions in the ordinary fashion. Those conditions are:

i. The function is recursive.

ii. There are looping constructs in the function.

iii. There are static variables in the function.


BASIS FOR COMPARISON

STRUCTURE

CLASS

Basic

If access specifier is not declared, by default all member are 'public'.

If access specifier is not declared, by default all members are 'private'.

Declaration

struct structure_name{
type struct_element 1;
type struct_element 2;
type struct_element 3;
.
};

class class_name{
data member;
member function;
};

Instance

Instance of 'structure' is called 'structure variable'.

Instance of a 'class' is called 'object'.

Polymorphism and inheritance

Not supported

Supports polymorphism and a class can also be inherited.

Nature

Value type

Reference type

Memory is allocated on

Stack

Heap

Null values

Not possible

Can have null values

Requires constructor and destructor

No

Yes


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