Assg2_OOP_solution
Assg2_OOP_solution
malloc() malloc stands for "memory allocation". Allocates requested size of bytes and
returns a pointer first byte of allocated space
Syntax:
ptr = (cast-type*) malloc(byte-size)
Example :
Object Oriented Programming (C++) SY BCA SEM – III
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes
respectively and the pointer points to the address of first byte of memory.
calloc() Allocates space for an array elements, initializes to zero and then returns a
pointer to memory (Prof. Viral S. Patel)
---------------------------------------------------------------------------------------------------------------------------
Ambiguity : some time situation created like default constructor A::A() and the default
argument constructor A::A(int = 0) forms are created ambiguity as compiler cannot decide
whether to call A::A() or A::(int =0) when called with no arguments.
7. List out the operators that cannot be overloaded as member and friend function.
Operators that cannot be overloaded using Operators that cannot be overloaded using
member function friend function
. Membership operator = Assignment operator
.* Pointer-to-member operator () Function call operator
:: Scope resolution operator [] Subscripting operator
?: Conditional operator -> Class member access operator
Long Questions :
1. List memory management operators. Point out reasons why using new is better idea than
using malloc() ? (5) (Prof. Viral S. Patel)
new and delete are memory management operator.
new automatically computes the size of data object. We need not use the operator
sizeof(). In given example pointer ptr have 200 bytes memory to store 100 integer values. In
malloc we have used sizeof to calculate the size of int bytes and in new operator no need of
it.
new automatically returns the current pointer type so there is no need to type cast.
In given example for malloc we have to first casting memory to (int *) then we can give it to
ptr. In new operator no need of type casting.
Like any other operator new and delete can be overloaded. malloc function cannot be
used for other purpose.
2. What do you mean by default argument ? When it is useful ? (5) (Prof. Viral S. Patel)
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 :
Here function calling give answer 20 as pass value 2 in variable i and value of k is default 10.
Rules : Only the trailing arguments can have default values and therefore we must add
defaults from right to left.
Useful when :
1. Some Situations where some arguments always have the same value. For example,
bank interest may remain the same for all customers for a particular period of
deposit.
2. we can use default arguments to add new parameters to the existing functions.
3. Default arguments can be used to combine similar functions into one.
3. What is destructor ? How destructor get called ? Also describe the importance of destructor.
(5) (Prof. Viral S. Patel)
A destructor, as the name implies, is used to destroy the objects that have been created
by a constructor and releases memory space for future use.
Like a constructor, the destructor is a member function whose name is the same as the
class name but is preceded by a tilde.
For example, ~A(){ }
A destructor never takes any argument nor does it return any value.
Object Oriented Programming (C++) SY BCA SEM – III
It will be invoked implicitly by the compiler upon exit from the program or block or
function as scope of the object end. Objects are destroyed in the reverse order of creation.
It clean up storage that is no longer accessible.
Whenever new is used to allocate memory in the constructors, we should use delete to
free that memory. This is required because when the pointers to objects go out of scope, a
destructor is not called implicitly.
Example :
class A A(int a,int b) // parameterized constructor
{ {
int x; x=a; y=b;
int y; }
public : };
A() // default constructor void main()
{ {
x = 10; y=20; A obj1; // called default constructor implicitly
} A obj2(obj1); // called copy constructor
A( A & ob) //copy constructor A obj3 = obj1; // also called copy constructor
{ A obj4(5,6); // called parameterized constructor
obj1 = A(2,3) // explicitly called parameterized constructor
x = ob.x; y = ob.y;
getch();
}
}
Advantages :
No need of calling statement for constructor as it is invoked automatically.
When necessary we can also called it as explicitly.
We cannot refer to their addresses. So it is secured.
We can initialize the object just after it is created.
We can make copy of object by using copy constructor.
Compiler supply default and copy constructor if we have not created.
They make implicit calls to the operators new and delete when memory allocation is
required.
Example:
class A void disp()
{ {
int x; cout<<x<< “ “<<y;
int y; }
public : };
A(){ } void main()
A(int a,int b) // parameterized constructor {
{ A obj1(2,3), obj3;
x=a; y=b; // obj1 call to parameterized constructor
} A obj2(obj1); // call to copy structor
A( A & ob) //parameterize copy constructor A ob3 = A(4,5); // explicitly call
obj1.disp(); // output : 2 3
{
obj2.disp(); // output : 2 3
x = ob.x; y = ob.y; obj3.disp(); // output : 4 5
} getch();
}
We can pass the parameters in two ways : pass by value or pass by reference.
We can call parameterized constructor two ways : explicitly and implicitly.
A obj1(2,3) is implicit call in above program.
A obj3 = A(4,5) is explicit call in above program.
We cannot pass object of same class in parameter. But we can pass reference of it.
A(A) is illegal
A(A&) is valid. It is called copy constructor.
In above example, A obj2(obj1) calling to copy constructor. And obj1 pass as reference.
These constructors can also be inline, same as inline function.
(5&6 : What is constructor ? Explain different types of constructor.(7) ) (Prof. Viral S. Patel)
7. Write a program to create a class scale. Define two variable feet and inches. Define member
function getscale, putscale and sum function to add to measure passed object as argument
to the function sum. (7)
#include<iostream.h>
#include<conio.h>
class scale
{
int feet;
int inches;
public : void getscale()
{
cout<<"Feet :";
cin>>feet;
cout<<"Inches :";
cin>>inches;
}
void putscale()
{
cout<<feet<< " " <<inches;
}
scale sum(scale & o2)
Object Oriented Programming (C++) SY BCA SEM – III
{
scale temp;
int tot_inch = inches + o2.inches;
temp.feet = feet + o2.feet + (tot_inch / 12);
temp.inches = tot_inch % 12;
return temp;
}
};
void main()
{
scale obj1,obj2,obj3;
clrscr();
obj1.getscale();
obj2.getscale();
obj3=obj1.sum(obj2);
obj3.putscale();
getch();
}
(Prof. Viral S. Patel)
Example :
class BILL void main()
{ {
int items; BILL ob(5,20);
int price; int total_price;
public :
BILL(int i, int p) total_price = ob; // call casting operator function
{ //also called as total_price = ob.operator int();
items = i; price = p; cout<<total_price;
}
operator int() // casting operator getch();
{ }
return (items * price); Output : 100
}
};
Object Oriented Programming (C++) SY BCA SEM – III
Unary member Function Calling example : Binary member Function Calling example:
Unary Friend function have only one Binary Friend function have two arguments.
argument.
Unary Friend function example : Binary friend function example :
For overloading the operator a special function is used called operator function.
Syntax :
return type classname :: operator op (arglist)
{
Function body
}
Operator function may be member function or friend function.
Operator function may be for unary or binary operators.
[ * note : Write here answer of question no. 9 for unary and binary operator overloading]
12. How many arguments are required in the definition of an overloaded unary operator ? Why
? (7)
[* note : Read answer of question no. 9 for unary operator ] (Prof. Viral S. Patel)
13. How many arguments are required in the definition of an overloaded binary operator ? Why
? (7)
[* note : Read answer of question no.9 for binary operator ]
14. What is type conversion ? Explain basic to class type with proper example. (7)
When variables and constants of different types are mixed in an expression or different
types of operand are used left and right side of assignment operator then automatic type
conversion occur as per certain rules. But the user defined data types are designed by us to
suit our requirements , the complier does not support automatic type conversions for such
data type. (Prof. Viral S. Patel)
Three types of situations occur for user defined data type conversion.
(1) Conversion from basic type to class type
(2) Conversion from class type to basic type
(3) Conversion from one class type to another class type
Basic to class type conversion : The constructors used for this type conversion take a single
argument whose type is to be converted. Constructor is calling implicitly.
Example :
Class to basic type conversion : The overloaded casting operator could be used to convert a
class type data to basic type.
Example :
one class to another class type conversion : This conversion takes place either by
constructor in destination class or by casting operator in source class.
Example :
Object Oriented Programming (C++) SY BCA SEM – III
....
class Dollar class Rupees We can convert class type Dollar
{ { to type Rupees either by
int d; int r; constructor in destination class
public : public : Rupees or by casting operator in
.... .... source class Dollar.
Dollar(int t) Rupees(Dollar ob)
{ {
d = t; r = ob.d * 65;
} }
.... ....
/* operator Rupees() }
{ void main()
Rupees ob; {
ob.r = d * 65; Dollar D1(3);
return ob; Rupees R1;
} R1 = D1;
*/ ....
} }
16. Explain basic to class and class to basic type conversion. (7)
[ * note : read answer of question 15 ] (Prof. Viral S. Patel)