Question Paper of Semester no. v CS-16 Object Oriented Systems Dec-1999
l(a) Define the following terms with examples:
(i) Overloading
(ii) Templates
(iii) Multiple inheritance and Multilevel inheritance
(iv) Base and Derh ed classes.
Qnsl(a)
Answer :
(i) Overloading:- The method of implementing polymorphism i.e. different behavior of an object, an operator or a function in different conditions, overloading can be performed over the functions and over the operators.
Image no.1
Example:
# include < iostream .h >
Void main ( )
{
int max (int, int);
float max (float, float);
}
int max (int a; int b)
{
return(a> b? a: b);
}
float max( float a, float b)
{
return / a .b ? a
;
}
}
(ii) Template
Please refer Qus l(a) (iv) june2000
(iii) Multiple inheritances
Ability of a derived class to inherit the characteristic of more than one class as base class is called multiple inheritance.
Image no.2
Multilevel Inheritance means the inheritance chain where a derived class acts as base class for other classes.
Image no.3
(iv) Base class & Derived Class
In inheritance hierarchy the class, which is devoting its characteristics to the lone, or more classes is known as Base Class or parent class. Whereas the classes, which are consuming the characteristics or can extend base class functionalities are known as derived classed.
Image no.4
Ql(b) Define a class for storing, evaluating and printing simple arithmetic expressions consisting of. Integer constants and the operators +, -, * and /. The public interface should look like this:
class expr {
// �
public:
expr(char *);
int eval( );
void print( );};
The string argument for the constructor
expr:: expr() is the expression. The function
expr: : eval() returns the value of the expression, and
expr : : print() prints a representation of the expression on the
cout. A program might look like this:
exprx("123/4+123*4-3″);
cout �"x =” �x eval( ) �"n";
x.print( );
Define class expr twice: once using a linked list of nodes as the representation and once using a character string as the representation.
Answer :
# include < iostream . h >
# include < string .h >
# define max 80 Typedef struct valvenode
{
valvenode * val;
valvenode * prev ,* next:
};
Class list
{
Valve * head * tail;
public: List ( )
head = tail ~ NULL ;
Void append
char* SI
}
int empty ( )
{
return head = = NULL;
}
};
class input
{
char Sl(max+1);
int end;
list valuenode;
void size ( );
public:
Input ( )
{
return end;
}
list * get list ( )
{
return ( * value node);
}
};
class stack
{
list value nodes;
public:
void push (int value);
int pop ( ) ;
};
class evaluate {
stack stl;
public:
inteval(List*ll);
};
value node:: value node (char ? New value node)
{
int I,max,a;
if (! NULL)
{
new value node (i) = ” ;
value = a;
}
else
value = 0;
}
valuenode list: : remove ()
{
int (empty ( ))
{
return NULL;
valuenode * retval = head -� NEXT;
valuenode * next = head -> next;
if (next + NULL)
next -> prev = NULL;
delete head;
head = next;
return. Retval;
}
void list: : append (char * SI)
{
valuenode * ( n = new valuenode );
next -�prev = tail;
next -> next = NULL;
If( tail = Null);
Tail -> next = next;
Tail = next;
If (head = NULL)
{
HEAD = NEXT;
Next �> next = knew valuenode (S1)
}
Void list:: prepaid ( Char * SI)
{
valuenode *next=new valuenode;
Next -> prev = NULL;
Next -> next = head ;
If (head-Null)
Head -� prev = next;
tail =next;
}
int stack :: pop ( )
{
valuenode * t = valuenoderemove
int retval= t-� get value ( );
delete t;
return retval;
}
VOID Stack : : Push (int value)
{
Char str [ max ]
Cont < < Str;
Valve node. Prepaid (str);
Void input: : size ( )
{
Char * head, * tail;
Head = str;
While (I NULL)
{
tail = head;
}
}
int evaluate : : eval (list * 11)
{
valuenode * VN;
int a, b, c;
while (empty ( )
{
VN = list -> remove ( );
If(VN ~* operand( ))
stack push (VN get value ( ))
else
{
b = stack. Push ( );
a = stack. Pop( );
Switch (VN Operator ( ))
{
case * :
C=A*B;
Break;
Case -:
C = A-B;
Break;
Case 7 ‘ ;
C=A/B;
Break;
Case’ + ‘:
C = A + B
Break;
Case’%':
C=A%B;
Break;
Stack. Push ©;
Delete VN !
Return stack pop( );
}
int main ( )
Evaluate ABC;
Input * IP - new input;
While (IP ! = NULL)
{
Cout < < evaluate . eval( Ip. Get Iist());
Cout<<' n ':
IP = new Input;
}
}
Ql © Design a template class for sorting a list of elements.
Answer :
# include < iostream.h >
# define max 5.
Template < class T > class sort
{
private: T arr [ MAX]; Public :
Void Input ( ) ;
Void Sortl ( ) ;
Void output ( );
}
Template < class T > void sort < T > :: input ( )
{
cout < < " Elements to be sort "< < endl;
for ( int i = 0; i< max; i + + )
{
cin�arr[i ];
}
}
Template < class T > void sort < T > :: sortl ( )
{
T temp;
For ( int i = 0 ; i, max; i + +)
{
if(arr[i]>arr[i+l])
{
temp - arr [i];
arr [ i ] - arr [ i + 1 ];
arr[ i + 1 ] = temp;
}
}
}
Template < class T > void sort < T. >: : output( )
{
cout < < " elements are sorted" < < endl;
for( int i = 0; i< max i + +)
{
cout < < arr;
}
}