0% found this document useful (0 votes)
9 views

Assg1_OOP_solution

Uploaded by

Maniya Daxit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assg1_OOP_solution

Uploaded by

Maniya Daxit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented Programming (C++) Viral S.

Patel

Objective Questions (2 marks)

1. Explain default arguments with example.


C++ allows us to call a function without specifying all its arguments. In such cases, the
function assigns a default value to the parameter which does not have a matching argument
in the function call.

Example :

int mul(int i, int k = 10) //function defination


{
int val;
val = i * k;
return val;
}

value = mul(2); // function calling

Here function calling give answer 20 as pass value 2 in variable i and value of k is default 10.

2. What do you mean by array of objects ? How are they created ?


We can create array of variables that are of the type class. Such variables are called arrays
of objects. Following example show how they can create and can use.

Example :
class A
{
int x;
public : void input()
{
cin>>x;
}
};

void main()
{
A obj[3]; // create array of object which have 3 elements(objects) obj[0],obj[1], obj[2]
for(i=0;i<3;i++)
{
obj[i].input(); // use array of object to call function for every objects
}
getch();
}

3. What are the characteristic of static data member ? (Viral S. Patel)


It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetime is the entire program.

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

4. Which methods cannot call through an object ?


Friend functions cannot call through an object. Because it is not a member of class.

5. What is function overloading ?


Overloading refers to the use of the same thing for different purposes. This means that
we can use the same function name to create functions that perform a variety of different
tasks. This is also known as function polymorphism in OOP.

Example:
int add (int a, int b);
int add (int a, int b, int c); // change no of parameters
double add (double x, double y); // change data types

double add (int p, double q);


double add (double p, int q) // change sequence

6. When to make function as inline ? (Viral S. Patel)


Every time a function is called, it takes a lot of extra time in executing a series of
instructions for tasks such as jumping to the function, saving registers, pushing arguments
into the stack and returning to the calling function. When a function is small, a substantial
percentage of execution time may be spent in such overheads. So in c++ inline function is
used. An inline function is a function that is expanded in line when it is invoked(called). It
means, The complier replaces the function call with the corresponding function code (
something similar to macros expansion). It gives speed benefits as a program run faster.

7. Define static data member and function.


Static data member : It is a data member of class which is initialized to zero when first
object of class is created. Only one copy of that member is created for the entire class. It is
shared by all the objects of that class. It is not part of an object.

Static member function : It is a member function of class which can have access to only
other static members (functions or variables). Static member function can be called using
the class name as class-name :: function-name;

8. How do structures in C and C++ differ ? (Viral S. Patel)


The structures in C can have only variables as members. They do not permit data hiding.
Structure members can be directly accessed by the structure variables by any function
anywhere in their scope.
Example :
struct stud
{
int rno;
}s1;

scanf(͞%d͟,&s1.rno);

In C++ a structure can have both variables and functions as members. This structure is
known as class. It can also declare some of its members as ͚private͛ so that they cannot be
accessed directly by external functions.

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

Example :
class stud
{
int rno;
public : void input() { cin>>rno;}
};

stud s1;
s1.input(); // private data can access using public function
cout<<s1.rno; // This statement give error as cannot access private data directly

9. Explain main() of c++.


Every C++ program must have a main(). Execution begins at main(). main() returns an
integer type value to the operating system. Default return type of main() or any functions in
C++ is int. Therefore, every main() in c++ should end with a return (0) statement; otherwise a
warning or an error might occur.

main() //give warning as without type and return value.


{
.....
}

10. List characteristics of OOP. (Viral S. Patel)


Emphasis in on data rather than procedure
Programs are divided into what are known as objects
Functions that operate on the data of an object are tied together in the data structure.
Data is hidden and cannot be accessed by external functions.
Objects may communicate with each other through functions.
New data and functions can be easily added whenever necessary.
Follows bottom-up approach in program design.
Abstraction, encapsulation, Inheritance, Polymorphism, Dynamic binding, message
Passing all these properties show real characteristics of OOP.

11. What is inline function ? Why it is used ?


An inline function is a function that is expanded in line when it is invoked(called). It
means, The complier replaces the function call with the corresponding function code (
something similar to macros expansion).
Every time a function is called, it takes a lot of extra time in executing a series of
instructions for tasks such as jumping to the function, saving registers, pushing arguments
into the stack and returning to the calling function. When a function is small, a substantial
percentage of execution time may be spent in such overheads. One solution is to use macro
definitions, but drawback is that error checking does not occur during compilation. So in c++
inline function is used, which can show error during compilation if error occurs and gives
speed benefits as a program run faster.

12. Define class and object. (Viral S. Patel)


Class is a user define data type. It is a way to bind data and its associated functions
together. It allows the data to be hidden if necessary, from external use. Object is a variable
of class. Object is a run-time entity. Class provides only template and object create memory
space to store value of data members of class.

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

13. Explain limitation of inline functions.


There are some of the situations where inline expansion may not work are :
For functions if a loop, a switch or a goto exists.
For functions not returning values, if a return statement exists.
If functions contain static variables.
If inline functions are recursive.

14. Difference between class and structure.


The structures in C can have only variables as members. They do not permit data hiding.
Structure members can be directly accessed by the structure variables by any function
anywhere in their scope.
In C++ a class can have both variables and functions as members. It can also declare some
of its members as ͚private͛ so that they cannot be accessed directly by external functions. So
class permit data hiding.

15. What are the advantages of cin and cout over scanf and printf respectively ?
cin and cout requires to define exactly what type of input is expected (%d, %lf, %c, etc)
means must have to use type specifier. If this doesn't match the type of the given variable
then it will give error. On the other hand, cin and cout use the c++ iostream which allows it
to read the given values and then automatically match to the requested variable type. There
is no need to specify type. (Viral S. Patel)

Long Questions :

1. What is OOP ? How it is differ from POP ? List applications of OOP. (5)
OOP is object oriented programming as an approach that provides a way of modularizing
programs by creating partitioned memory area for both data and functions that can be
used as templates for creating copies of such modules on demand. Thus, an object is
considered to be a partitioned area of computer memory that stores data and set of
operations that can access that data.

Procedure Oriented Programming Object Oriented Programming


Divided Into In POP, program is divided into In OOP, program is divided into
small parts called functions. parts called objects.
Importance In POP, Importance is not given In OOP, Importance is given to the
to data but to functions as well data rather than procedures or
as sequence of actions to be done. functions because it works as a real
world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach.
Access POP does not have any access OOP has access specifiers named
Specifiers specifier. Public, Private, Protected, etc.
Data In POP, Data can move freely from In OOP, objects can move and
Moving function to function in the system. communicate with each other
through member functions.
Expansion To add new data and function in OOP provides an easy way to add
POP is not so easy. new data and function.
Data Access In POP, Most function uses Global In OOP, data cannot move easily
data for sharing that can be from function to function, it can be
accessed freely from function to kept public or private so we can
function in the system. control the access of data.

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

Data Hiding POP does not have any proper OOP provides Data Hiding so
way for hiding data so it is less provides more security.
secure.
Overloading In POP, Overloading is not In OOP, overloading is possible in
possible. the form of Function Overloading
and Operator Overloading.
Examples Example of POP are : C, VB, Example of OOP are : C++, JAVA,
FORTRAN, Pascal. VB.NET, C#.NET.

Applications of OOP:
Real-time systems
Simulation and modeling
Object-oriented databases
Hypertext, hypermedia and expertext
AI and expert systems
Neural networks and parallel programming
Decision support and office automation systems
CIM/CAM/CAD systems

2. What do you mean by objects as function arguments ? Explain pass-by-value and pass-by-
reference with example. (5) (Viral S. Patel)
Like any other data type, an object may be used as a function argument. This can be in
two ways:
A copy of the entire object is passed to the function.
Only the address of the object is transferred to the function.

The first method is called pass-by-value. Since a copy of the object is passed to the function,
any changes made to the object inside the function do not affect the object used to call the
function.

The second method is called pass-by-reference. When an address of the object is passed,
the called function works directly on the actual object used in the call. This means that any
change made to the object inside the function will reflect in the actual object.

Example :
class A
{
int x;
public : void input(int i)
{
x = i;
}
void disp()
{
cout<<x;
}
void addObjects(A ob) // pass by value
{
int res = x + ob.x;
cout<<res;
}

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

void exchObjects(A & ob) // pass by reference


{
int temp;
temp = x;
x = ob.x;
ob.x = temp;
}
};
void main()
{
A obj1,obj2;
clrscr();
obj1.input(2);
obj2.input(3);
obj1.addObjects(obj2);
obj1.exchObjects(obj2);
obj1.disp(); //display 3
obj2.disp(); //display 2
getch();
}
Here ͚addObjects͛ function copy ͚obj2͛ object in ͚ob͛ object and then perform addition
between ͚obj1͛ and ͚ob͛ (copy of ͚obj2͛) objects.
͚exchObjects͛ function pass address of ͚obj2͛ and create just create alias name ͚ob͛ , so
exchange occur between ͚obj1͛ and ͚obj2͛ objects.

3. Explain inline function giving suitable example. (3) (Viral S. Patel)


(Problem)Every time a function is called, it takes a lot of extra time in executing a series of
instructions for tasks such as jumping to the function, saving registers, pushing arguments
into the stack and returning to the calling function. When a function is small, execution time
may be spent in such overheads.

One solution is to use macro definitions, but drawback is that error checking does not occur
during compilation. So in c++ inline function is used.

(Solution) An inline function is a function that is expanded in line when it is invoked


(called). It means, The complier replaces the function call with the corresponding function
code ( something similar to macros expansion).

(Advantage and limitation) Inline function can show error during compilation if error occurs
and gives speed benefits as a program run faster. But size of program grows up.

Example :
inline double cure(double a)
{
return (a * a * a);
}

c = cube(3.0); // calling statement

Some or the situations where inline expansion may not work are :
1. For functions which have a loop or a switch case or a goto statement.

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

2. For functions not returning values, if a return statement exits.


3. If functions contain static variable.
4. If inline function are recursive.

4. What is friend function ? Explain with example. (7) (Viral S. Patel)


The function which is not member of class cannot access private data of class. For the case
where two class want to share a particular function. In this circumstances, the function to be
made friendly with both the classes, thereby C++ allowing the friend function to access the
private data of these classes. Such function need not be a member of any of these classes.

Characteristic of friend function :


Friend function is not in the scope of the class to which it has been declared as friend.
Since it is not in the scope of the class, it cannot be called using the object of that class.
It can be invoked like a normal function without the help of any object.
Unlike member functions, it cannot access the member names directly and has to use an
object name and dot membership operator with each member name (e.g. A.x).
It can be declared either in the public or private part of a class without affecting its
meaning.
Usually, it has the objects as arguments.

Example :
class A
{
int a;
int b;
public :
void setValue()
{
a=25;
b=40
}
friend void add(A ob) // friend function
{
int res;
res = ob.a + ob.b; // members must have to use object dot (ob.a and ob.b)
cout<<res;
}
}

void main()
{
A obj;
obj.setValue();
add(obj); // calling friend function without object dot
}
Output : 65

Member function can be friend function of another class.


class X
{
......

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

int fun1(); // member function of X


.......
};
class Y
{
........
friend int X :: fun1(); // fun1() of X is friend of Y
........
};

We can also declare all the member function of one class as the friend function of another
class.
class Z
{
.....
friend class X; // all the member functions of X are friends to Z
.....
};

5. In which circumstances functions can be made a friend ? Write the advantages of friend
function. Demonstrate example of friend function. (7) (Viral S. Patel)
The function which is not member of class cannot access private data of class. For the
case where two class want to share a particular function. In this circumstances, the function
to be made friendly with both the classes, thereby C++ allowing the friend function to access
the private data of these classes. Such function need not be a member of any of these
classes.

Advantages of friend function :


It can be invoked like a normal function without the help of any object.
It can be declared either public or private part of class without affecting its meaning.
It can directly access private data of class where it is declared as friend.
Easy to do process/communication between two class͛s objects.

Example :
#include<iostream.h>
#include<conio.h>
class B;

class A
{
int x;
public : void input(){ x=5; }
friend void add(A oA,B oB); //declared as friend of A
};

class B
{
int x;
public : void input(){ x=2; }
friend void add(A oA,B oB); // declared as friend of B
};

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

void add(A oA,B oB) // friend function of both class A & B


{
int res;
res = oA.x + oB.x;
cout<<res;
}

void main()
{
A objA;
B objB;
clrscr();
objA.input();
objB.input();
add(objA,objB); // calling of friend function without object
getch();
}
Output : 7

6. List out features of object oriented programming. Explain any two of them. (7)
 Encapsulation
 Data hiding and access mechanisms
 Abstraction
 Polymorphism - Overloading
 Inheritance
 Dynamic binding

Encapsulation : The wrapping up of data and functions into a single unit (called class) is
known as encapsulation. In data encapsulation, data is not accessible to the outside world,
and only those functions which are wrapped in the class can access it. These functions
provide the interface between the object͛s data and the program.

Data hiding and access mechanisms : The data is hidden inside the class by declaring it as
private inside the class. When data or functions are defined as private it can be accessed
only by the class in which it is defined. This concept is called data hiding. When data or
functions are defined as public then it can be accessed anywhere outside the class. When
data or functions are defined as protected it can access from the same class and its
subclasses, but not objects of a different class. (Viral S. Patel)

Abstraction : Data abstraction refers to, providing only essential information to the outside
world and hiding their background details, i.e., to represent the needed information in
program without presenting the details. Classes use the concept of abstraction and are
defined as a list of abstract attributes such as size, weight and cost and functions to operate
on these attributes. They encapsulate all the essential properties of the objects that are to
be created. Since the classes use the concept of data abstraction, they are known as
Abstract Data Types (ADT).
---------------------------------------Only for knowledge---------------------------------------------------------
Data Abstraction Example:
Any C++ program where you implement a class with public and private members is an
example of data abstraction. Consider the following example:

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

#include <iostream>
using namespace std;

class Adder {
public:
// constructor
Adder(int i = 0) {
total = i;
}

// interface to outside world


void addNum(int number) {
total += number;
}

// interface to outside world

int getTotal() {
return total;
};

private:
// hidden data from outside world
int total;
};

int main( ) {
Adder a;

a.addNum(10);
a.addNum(20);
a.addNum(30);

cout << "Total " << a.getTotal() <<endl;


return 0;
}

When the above code is compiled and executed, it produces the following result:
Total 60

Above class adds numbers together, and returns the sum. The public
members addNum and getTotal are the interfaces to the outside world and a user needs to
know them to use the class. The private member total is something that the user doesn't
need to know about, but is needed for the class to operate properly. (Viral S. Patel)

-----------------------------------------------------------------------------------------------------------------------

Polymorphism : It is a Greek term, means that ability to take more than one form. An
operation may show different behaviours in different instances. Using a single function
name to perform different types of tasks by handle different number and different types of
arguments is known as function overloading.
For example,

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

Consider the operation of addition. For two numbers, the operation will generate a sum. If
the operands are strings, then the operation would produce a third string by concatenation.
Some time process of making operator used to different behaviours in different instances
is known as operator overloading.
Polymorphism is extensively used in implementing inheritance.

Inheritance : It is the process by which objects of one class acquire the properties of objects
of another class. It supports the concept of hierarchical classification.
 It provides the idea of reusability. This means that we can add additional features to an
existing class without modifying it. This is possible by deriving a new class from the existing
one.
If any update/change will be done in base class it automatically affect/update all its
derived class.

Dynamic Binding : Dynamic binding also called dynamic dispatch is the process of linking
procedure call to a specific sequence of code (method) at run-time. It means that the code
to be executed for a specific procedure call is not known until run-time. Dynamic binding is
also known as late binding or run-time binding. it is related with polymorphism and
inheritance. A function call associated with a polymorphic reference depends on the
dynamic type of that reference. (Viral S. Patel)

7. Explain static data and static member function with proper example. (7)
A data member of a class can be qualified as static. A static member variable has certain
special characteristic. These are :
It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetime is the entire program.

A member function can also be declared as static. Static function has following properties :
A static function can have access to only other static members (functions or variables)
declared in the same class.
A static member function can be called using the class name as follows :
class name :: function-name;

Example:

class ITEM
{
int code;
int price;
static int tot_price; // static variable tot_price to calculate total price of all input items.

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

public : void setPrice(int c,int p)


{
code = c;
Price = p;
tot_price = tot_price + price; // use static variable to add new item price
}
static void disp_tot_price() //static function can use only static member
{
cout<<tot_price;
}
}
int ITEM::tot_price;
void main()
{
ITEM i1,i2,i3;
i1.setPrice(101,2000);
i2.setPrice(102,3000);
ITEM :: disp_tot_price(); // calling static function display value of tot_price is 5000
i3.setPrice(103,1000);
ITEM :: disp_tot_price(); //calling static function display value of tot_price is 6000
}

8. Explain object as function argument and returning object. (7) (Viral S. Patel)
Like any other data type, an object may be used as a function argument. This can be in two
ways:
A copy of the entire object is passed to the function.
Only the address of the object is transferred to the function.

The first method is called pass-by-value. Since a copy of the object is passed to the function,
any changes made to the object inside the function do not affect the object used to call the
function. (Viral S. Patel)

The second method is called pass-by-reference. When an address of the object is passed,
the called function works directly on the actual object used in the call. This means that any
change made to the object inside the function will reflect in the actual object.

Example :
class A
{
int a;
public :
void setValue(int t)
{
a=t;
}
A add(A o2) // ͚o2͛ is copy of object ͚obj2͛  object pass-by-value
{
A temp;
temp.a = a + o2.b; // ͚a͛ give value of obj1 as function is called by ͚obj1͛
return temp; // returning object
}

S. V. Patel College of CS and BM, Surat


Object Oriented Programming (C++) Viral S. Patel

void exch(A & o2) // ͚o2͛ is alias name of object ͚obj2͛  object pass-by-reference
{
int tmp;
tmp = a;
a = o2.a;
o2.a = tmp;
}
void disp()
{
cout<<a;
}

void main()
{
A obj1,obj2,obj3;
obj1.setValue(2);
obj2.setValue(3);
obj3 = obj1.add(obj2); // function return object and store in object
obj1.exch(obj2);
obj1.disp(); // display 3
obj2.disp(); // display 2
}

S. V. Patel College of CS and BM, Surat

You might also like