Assg1_OOP_solution
Assg1_OOP_solution
Patel
Example :
Here function calling give answer 20 as pass value 2 in variable i and value of k is default 10.
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();
}
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
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;
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.
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
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.
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;
}
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.
(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);
}
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.
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
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.
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
};
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:
#include <iostream>
using namespace std;
class Adder {
public:
// constructor
Adder(int i = 0) {
total = i;
}
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);
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,
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.
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
}
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
}