Assignment Oops
Assignment Oops
of
Object oriented programming with C++
Submitted by:
DEEPIKA
05504092018
MCA 1st yr.
public:
derived()
{ cout<<"Constructing derived \n"; }
~derived()
{ cout<<"Destructing derived \n"; }
};
int main(void)
{
derived *d = new derived();
base *b = d;
delete b;
getchar()void
return 0;
return 0;
}
Output:
x = 107
y=a
z = 108
Usage
To use a generic class, put the type in the square brackets
in place of A.
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
println(stack.pop) // prints 2
println(stack.pop) // prints 1
The instance stack can only take Ints. However, if the type
argument had subtypes, those could be passed in:
class Fruit
class Apple extends Fruit
class Banana extends Fruit
val stack = new Stack[Fruit]
val apple = new Apple
val banana = new Banana
stack.push(apple)
stack.push(banana)
Class Apple and Banana both extend Fruit so we can push
instances apple and banana onto the stack of Fruit.
Note: subtyping of generic types is invariant. This means
that if we have a stack of characters of
type Stack[Char] then it cannot be used as an integer stack
of type Stack[Int]. This would be unsound because it
would enable us to enter true integers into the character
stack. To conclude, Stack[A] is only a subtype
of Stack[B] if and only if B = A. Since this can be quite
restrictive, type parameter annotation mechanism to
control the subtyping behavior of generic types.