COEN244: Polymorphism

Size: px
Start display at page:

Download "COEN244: Polymorphism"

Transcription

1 COEN244: Polymorphism Aishy Amer Electrical & Computer Engineering Polymorphism means variable behavior / ability to appear in many forms Outline Casting between objects Using pointers to access objects Static polymorphism: compile-time type and function checking Dynamic polymorphism: run-time type and function checking Virtual functions: can be over-ridden at run time Pure Virtual functions: must be over-ridden Abstract Classes: classes with at least one pure virtual function Virtual destructor c A. Amer Polymorphism 1

2 Casting Casting: conversion from one data type to another type c o n v e r t t o t y p e ( expression ) ; // Example : t = i n t ( x ) ; q=double ( y ) ; c A. Amer Polymorphism 2

3 Casting Casting: conversion from one data type to another type c o n v e r t t o t y p e ( expression ) ; // Example : t = i n t ( x ) ; q=double ( y ) ; Standard numerical conversions are used for integral promotions (e.g., enum to int) integral conversions (e.g., int to unsigned int) floating point conversions (e.g., float to double) floating-integral conversions (e.g., int to float) arithmetic conversions (e.g., converting operands to the type of the widest operand before evaluation) c A. Amer Polymorphism 2

4 Casting Implicit conversions: Some conversions are performed automatically by the compiler without intervention by the programmer Standard C++ conversions and user-defined conversions are performed implicitly by the compiler where needed Examples? Explicit conversions: Conversions which must be explicitly specified by the programmer c A. Amer Polymorphism 3

5 C++ explicit casting operators static cast: convert one type to another type can be used to reverse any of the implicit conversions (e.g., int to float) rely on static (compile-time) type information fast and safe c A. Amer Polymorphism 4

6 C++ explicit casting operators static cast: convert one type to another type can be used to reverse any of the implicit conversions (e.g., int to float) rely on static (compile-time) type information fast and safe dynamic cast: careful, type-sensitive casting at run time for safe navigation of an inheritance hierarchy reinterpret cast: type conversions on un-related types produces something that has the same bit pattern as the original const cast : (UNSAFE!!) cast away the const-ness or volatility of a type c A. Amer Polymorphism 4

7 C++ explicit casting operators All casting operators have the same syntax: static_cast convert_to_type ( expr ) ; / / ==> Convert expr to type convert_to_type static_cast <DerivedClass > ( PointerToBaseObj ) Use the least dangerous (most specific) casting alternative c A. Amer Polymorphism 5

8 C++ casting: examples const i n t x =5; const i n t px ; / / changeable p o i n t e r to constant i n t px = 3; / / i l l e g a l can t use px to modify an i n t px = &someotherintvar ; / / l e g a l px can p o i n t somewhere else i n t const py ; / / constant p o i n t e r to changeable i n t py = 4; / / l e g a l can use py to modify an i n t py = &someotherintvar ; / / i l l e g a l can t make py p o i n t anywhere else const i n t const pz ; / / const p o i n t e r to const i n t pz = 5; / / i l l e g a l can t use pz to modify an i n t pz = &someotherintvar ; / / i l l e g a l can t make pz p o i n t anywhere else class Person { public : / / r e t u r n a const char const ==> can t change the p o i n t e r to p o i n t / / somewhere else, and you can t modify what the p o i n t e r p o i n t s to const char const GetName ( ) { return m_szname ; } ;... c A. Amer Polymorphism 6

9 C++ casting: examples i n t x = 37; i n t y = 8; double q = x / y ; / / c l a s s i c mistake, r e s u l t i s rounded to an i n t cout << q ; / / p r i n t s " " double q = ( double ) x / y ; / / cast r e s u l t as double so i t s not rounded cout << q ; / / p r i n t s " " / / Bas c a s t i n g : f o r c e the compiler to put the address of / / a const i n t v a r i a b l e i n t o a normal i n t const i n t x = 4; / / x i s const, i t can t be modified const i n t px = &x ; / / you can t modify x through the px p o i n t e r cout << x << endl ; / / p r i n t s " 4 " i n t px2 = ( i n t )px ; / / e x p l i c i t l y cast px as an i n t px2 = 3; / / r e s u l t i s undefined cout << x << endl ; / / who knows what i t p r i n t s? / / The code compiles and runs w i t h o u t crashing, but who knows what i s x c A. Amer Polymorphism 7

10 C++ casting: examples / / The Const_cast Operator const i n t x = 4; / / x i s const, i t can t be modified const i n t px = &x ; / / you can t modify x through the px p o i n t e r cout << x << endl ; / / p r i n t s " 4 " i n t px2 = const_cast < i n t > ( px ) ; / / e x p l i c i t l y cast px as non const px2 = 3; / / r e s u l t i s undefined cout << x << endl ; / / who knows what i t p r i n t s? / / With const_cast : change the const ness of a v a r i a b l e, and never i t s type c A. Amer Polymorphism 8

11 Object casting Object casting: conversion of the type of an object to that of another type s t a t i c c a s t <DerivedClass > ( PointerToBaseObj ) / / Example c i r c l e P t r = s t a t i c c a s t < C i r c l e >( p o i n t P t r ) ; An object of a publicly derived class can also be treated as an object of its base class c A. Amer Polymorphism 9

12 Object casting Object casting: conversion of the type of an object to that of another type s t a t i c c a s t <DerivedClass > ( PointerToBaseObj ) / / Example c i r c l e P t r = s t a t i c c a s t < C i r c l e >( p o i n t P t r ) ; An object of a publicly derived class can also be treated as an object of its base class A base class object cannot automatically be treated as a derived class object! We can use an explicit cast to convert a base-class pointer to a derived-class pointer Results of such conversion cannot be guaranteed in general c A. Amer Polymorphism 9

13 Object casting Object casting is used for pointer conversion (e.g., derived class pointer to base class pointer) reference conversion (e.g., derived class reference to base class reference) pointer-to-member conversion (e.g., from pointer to member of a base class to pointer to member of a derived class) A conversion from type SS to TT can ONLY be done if the conversion from type TT to SS is an implicit conversion c A. Amer Polymorphism 10

14 Object pointer casting: static_cast static_cast: no run-time type checking Down-casting: convert a base class pointer to a derived class pointer only if the conversion is unambiguous (& base class is non-polymorphic) c A. Amer Polymorphism 11

15 Object pointer casting: static_cast static_cast: no run-time type checking Down-casting: convert a base class pointer to a derived class pointer only if the conversion is unambiguous (& base class is non-polymorphic) Example: c l a s s BankAcct {... } ; c l a s s SavingsAcct : public BankAcct {... } ; / / Given a b a s e c l a s s p o i n t e r, / / we can c a s t i t t o a d e r i v e d c l a s s p o i n t e r : void foo ( BankAcct a c c t ) { SavingsAcct d1 = s t a t i c c a s t <SavingsAcct > ( a c c t ) ; } c A. Amer Polymorphism 11

16 Object pointer casting: static_cast static cast : no run-time type checking if acct does not refer to an actual SavingsAcct then the result of the cast is undefined c A. Amer Polymorphism 12

17 Object pointer casting: static_cast static cast : no run-time type checking if acct does not refer to an actual SavingsAcct then the result of the cast is undefined Applied to pointers to objects: Cast a pointer of a derived class to its base class Cast a pointer of a base class to its derived class The base class that is being casted is not checked to determine whether this is a complete class of the destination type or not: c l a s s Base {... } ; c l a s s Derived : public Base {... } ; Base a = new Base ; Derived b = s t a t i c c a s t <Derived > ( a ) ; c A. Amer Polymorphism 12

18 static_cast examples Circle is derived from Point: class Point { friend ostream& operator <<( ostream &, Point & ) ; public : Point ( i n t = 0, i n t = 0 ) ; void s e t P o i n t ( int, i n t ) ; / / access only base p a r t s i n t getx ( ) const { return x } ; protected : i n t x ; i n t y ; } ; / / Constructor Point : : Point ( i n t a, i n t b ) { s e t P o i n t ( a, b ) ; } c A. Amer Polymorphism 13

19 static_cast examples const double PI = ; class C i r c l e : public Point { friend ostream& operator <<( ostream &, C i r c l e & ) ; public : C i r c l e ( f l o a t = 0.0, i n t = 0, i n t = 0 ) ; void setradius ( f l o a t ) ; / / access d e r i v e p a r t s only i n t getradius ( ) const { return r ; } f l o a t area ( ) const { return PI r r ; } protected : f l o a t r ; } ; C i r c l e : : C i r c l e ( f l o a t rad, i n t a, i n t b ) : Point ( a, b ) { setradius ( rad ) ; } c A. Amer Polymorphism 14

20 static_cast examples / / Normal Use Point p ( 4, 5 ), p o i n t P t r ; C i r c l e c (0.707, 1, 2 ), c i r c l e P t r ; / / Assign p o i n t e r s normally p o i n t P t r = &p ; c i r c l e P t r = &c ; cout << p o i n t P t r ; / / C a l l operator << f o r Point / / Output : ( x, y )= 4 5 cout << c i r c l e P t r ; / / C a l l operator << f o r C i r c l e / / Output : ( x, y, r )= c A. Amer Polymorphism 15

21 static_cast examples / / Up c a s t i n g : using C i r c l e o b j e c t as a Point o b j e c t Point p ( 4, 5 ), p o i n t P t r ; C i r c l e c (0.707, 1, 2 ), c i r c l e P t r ; / / Assign C i r c l e o b j e c t to Point o b j e c t p o i n t e r / / Can only access the Point i n f o! p o i n t P t r = &c ; cout << p o i n t P t r ; / / C a l l operator << f o r Point / / Output : ( x, y )= 1 2 c A. Amer Polymorphism 16

22 static_cast examples / / Down c a s t i n g : Point to C i r c l e Point p ( 4, 5 ), p o i n t P t r ; C i r c l e c (0.707, 1, 2 ), c i r c l e P t r ; / / Assign C i r c l e o b j e c t to Point o b j e c t p o i n t e r / / Can only access the Point i n f o! p o i n t P t r = &c ; / / Since p o i n t P t r r e a l l y p o i n t s to a C i r c l e, we can / / cast i t back to a C i r c l e c i r c l e P t r = static_cast < C i r c l e >( p o i n t P t r ) ; cout << p o i n t P t r ; / / C a l l operator << f o r Point / / Output : ( x, y )= 1 2 cout << c i r c l e P t r ; / / C a l l operator << f o r C i r c l e / / Output : ( x, y, r )= c A. Amer Polymorphism 17

23 static_cast examples / / Wrong / Unsafe use Point p ( 4, 5 ), p o i n t P t r ; C i r c l e c (0.707, 1, 2 ), c i r c l e P t r ; / / Assign C i r c l e o b j e c t to Point o b j e c t p o i n t e r / / Can only access the Point i n f o! p o i n t P t r = &p ; / /!!!!!! Can I do t h i s? c i r c l e P t r = static_cast < C i r c l e >( p o i n t P t r ) ; cout << p o i n t P t r ; / / C a l l operator << f o r Point / / Output : ( x, y )= 4 5 cout << c i r c l e P t r ; / / C a l l operator << f o r C i r c l e / / Output : ( x, y, r )= 4 5 garbage c A. Amer Polymorphism 18

24 static_cast summary Address 0x1000 0x1001 0x1002 Memory map. 4 x 5 x p r. 0x30A8 0x30A9 0x30AA x x r c Point p(4, 5), *pointptr; Circle c(0.707, 1, 2), *circleptr; 0x1000 0x30A8. pointptr circleptr p o i n t P t r = &p ; p o i n t P t r = &c ; c i r c l e P t r = s t a t i c c a s t <C i r c l e > ( p o i n t P t r ) ; c A. Amer Polymorphism 19

25 static_cast summary Address 0x1000 0x1001 0x1002 Memory map. 4 x 5 x p r. 0x30A8 0x30A9 0x30AA x x r c Point p(4, 5), *pointptr; Circle c(0.707, 1, 2), *circleptr; 0x1000 0x30A8. pointptr circleptr p o i n t P t r = &p ; p o i n t P t r = &c ; c i r c l e P t r = s t a t i c c a s t <C i r c l e > ( p o i n t P t r ) ; Safe casting: from a derived object to a base object Be careful when casting from a base class to a derived class c A. Amer Polymorphism 19

26 Object pointer casting: Example / / L i s t i n g 15.1 Using p o i n t e r s to access o b j e c t s of base and derived classes class Base { / / base class protected : i n t x ; public : Base ( i n t a ) { x = a ; } / / to be used by Derived void set ( i n t a ) { x = a ; } / / to be i n h e r i t e d i n t show ( ) const { return x ; } / / to be i n h e r i t e d } ; class Derived : public Base { / / derived class private : i n t y ; public : Derived ( i n t a, i n t b ) : Base ( a ), y ( b ) { } / / empty c o n s t r u c t o r body void access ( i n t &a, i n t &b ) const / / added i n derived class { a = Base : : x ; b = y ; } } ; c A. Amer Polymorphism 20

27 Object pointer casting: Example i n t x, y ; Derived pd = new Derived ( 5 0, 8 0 ) ; / / unnamed derived o b j e c t cout << " 1. Derived p o i n t e r, object, and derived method \ n " ; pd >access ( x, y ) ; / / no problem : type match cout << " x = " <<x << " y = " <<y <<endl <<endl ; / / x=50 y=80 cout << " 2. Derived p o i n t e r, derived object, base method \ n " ; cout << " x = " << pd >show ( ) << endl << endl ; / / x = 50 Base pb = pd ; / / p o i n t e r to same o b j e c t cout << " 3. Base p o i n t e r, derived object, base method \ n " ; cout << " x = " << pb >show ( ) << endl << endl ; / / x = 50 / / pb >access ( x, y ) ; / / e r r o r : no access to derived method c A. Amer Polymorphism 21

28 Object pointer casting: Example cout << " 4. Converted p o i n t e r, derived o b j e c t and method \ n " ; ( ( Derived ) pb) >access ( x, y ) ; / / we know i t i s there cout << " x = " <<x << " y = " <<y <<endl <<endl ; / / x=50 y=80 pb = new Base ( 6 0 ) ; / / unnamed base o b j e c t cout << " 5. Base p o i n t e r, base object, base method \ n " ; cout << " x = " << pb >show ( ) << endl <<endl ; / / x = 60 cout << " 6. Converted p o i n t e r, base object, derived method \ n " ; ( ( Derived ) pb) >access ( x, y ) ; / / pass on your own r i s k cout << " x = " <<x << " y = " <<y <<endl <<endl ; / / junk!! delete pd ; delete pb ; / / necessary t i d i n e s s c A. Amer Polymorphism 22

29 Compile time versus Run time Static type checking / function checking Also called compile-time checking Examples?: d.show(); checked at compile time Dynamic type checking / function checking Also called run-time checking, i.e., at execution time Examples?: BasePtr->show(); c A. Amer Polymorphism 23

30 Polymorphism An object is denoted by its name (identifier) and the type associated with this identifier c A. Amer Polymorphism 24

31 Polymorphism An object is denoted by its name (identifier) and the type associated with this identifier Polymorphism (dynamic binding, virtual function): The principle that behavior of a function can vary depending on the actual type of an object With polymorphism: Objects of different classes related by inheritance may respond differently to the same member function call This allows a programmer to manipulate an object without knowing what kind of object it is c A. Amer Polymorphism 24

32 Types of polymorphism Static polymorphism (static binding or early binding): At compile time (at the earliest possible time): the compiler selects a method from several candidates Selection is based on the type of the pointer to the object c A. Amer Polymorphism 25

33 Types of polymorphism Static polymorphism (static binding or early binding): At compile time (at the earliest possible time): the compiler selects a method from several candidates Selection is based on the type of the pointer to the object Dynamic polymorphism (dynamic binding or late binding): At run time (at the latest possible time): the compiler selects a method from several candidates Based on the type of the object This allows a programmer to manipulate an object without knowing what kind of object it is c A. Amer Polymorphism 25

34 Static polymorphism Recall: In a derived class, you can redefine (this is?) functions of the base class compile-time solution c A. Amer Polymorphism 26

35 Static polymorphism Recall: In a derived class, you can redefine (this is?) functions of the base class compile-time solution Static polymorphism (early binding): When the compiler selects a method from several possible candidates at compile time The legality of the implementation (e.g., a member function invocation) is checked at compile time Example: if Vehicle has a certain member function, Car also has that member function c A. Amer Polymorphism 26

36 Dynamic polymorphism Dynamic polymorphism (late binding): When the compiler selects a method from several possible candidates at run time The legality of some types and functions is determined based on the dynamic type of the object at run time Dynamic polymorphism is implemented through virtual functions c A. Amer Polymorphism 27

37 Dynamic polymorphism Dynamic polymorphism (late binding): When the compiler selects a method from several possible candidates at run time The legality of some types and functions is determined based on the dynamic type of the object at run time Also called "dynamic binding": the binding to the code that gets called is accomplished dynamically (at run time) Dynamic polymorphism is implemented through virtual functions c A. Amer Polymorphism 27

38 Dynamic polymorphism: When? Suppose: We have a base class "Shape" with various derived classes "Triangle", "Rectangle", etc. Each derived class with its own print() member function Collect objects of these derived classes into an array of pointers to these objects CLASS arr[10] c A. Amer Polymorphism 28

39 Dynamic polymorphism: When? Suppose: We have a base class "Shape" with various derived classes "Triangle", "Rectangle", etc. Each derived class with its own print() member function Collect objects of these derived classes into an array of pointers to these objects CLASS arr[10] But what sort of pointers could we use? Recall: A derived pointer can be assigned a base pointer Shape arr[10]: an array of pointers to the base class Rectangle rec; arr[5] = &rec;: rec is the actual object; arr[5] is a pointer of type Shape c A. Amer Polymorphism 28

40 Dynamic polymorphism: When? Assume we want to print the content of each object in the array arr[i]->print() Since the pointer points to a Shape object, the print() function of the Shape (base) class is called NOT the appropriate derived class function!! Solution? Dynamic binding (using virtual functions) c A. Amer Polymorphism 29

41 Dynamic polymorphism: When? So: when you have a pointer to an object, this actual object may be of a derived class A pointer to an object of type Vehicle* maybe actually be pointing to a Car object A pointer to an object of type Shape* maybe actually be pointing to a Rectangle object c A. Amer Polymorphism 30

42 Dynamic polymorphism: When? So: when you have a pointer to an object, this actual object may be of a derived class A pointer to an object of type Vehicle* maybe actually be pointing to a Car object A pointer to an object of type Shape* maybe actually be pointing to a Rectangle object Dynamic binding implies that a base class pointer figures out which derived class it really points to at run-time c A. Amer Polymorphism 30

43 Dynamic binding Create a group of related derived classes under a common public base class Equip each derived class with a function that performs processing specific to this derived class Make sure that each function has the same name and signature (interface) Call this function through a base class pointer c A. Amer Polymorphism 31

44 Dynamic binding Create a group of related derived classes under a common public base class Equip each derived class with a function that performs processing specific to this derived class Make sure that each function has the same name and signature (interface) Call this function through a base class pointer The called function does not depend on the type of the pointer that points to the object It depends on the type of the object pointed to by the pointer c A. Amer Polymorphism 31

45 Dynamic binding: Useful? Before OOP: software reuse when new code call old code Dynamic binding can improve reuse by letting old code call new code c A. Amer Polymorphism 32

46 Dynamic binding: Useful? Before OOP: software reuse when new code call old code Dynamic binding can improve reuse by letting old code call new code Dynamic binding permits software extensibility: With OOP: a new code may be created, compiled, and tested Later, this code can be called by a framework that was written long time ago There is no need to change the old code: it does not even need to be recompiled c A. Amer Polymorphism 32

47 Dynamic binding: Useful? Dynamic binding does not make static binding irrelevant It introduces many additional dimension of complexity c A. Amer Polymorphism 33

48 Dynamic binding: Useful? Dynamic binding does not make static binding irrelevant It introduces many additional dimension of complexity poly.method(...); OR poly->method(...); Check if the target (here poly) is an object or a pointer If it is an object: Only static binding possible Check the function signature and Verify if the function call is correct If the target is a pointer: consider dynamic binding In most cases: the method being called depends on the type of the pointer not on the type of the object c A. Amer Polymorphism 33

49 Dynamic binding: a scenario If the target is a pointer: consider dynamic binding Define at which place in the inheritance hierarchy the pointer belongs If the pointer is of the derived type: only static binding is possible If the pointer is of the base type: dynamic binding is possible c A. Amer Polymorphism 34

50 Dynamic binding: a scenario If the pointer is of the base type: dynamic binding is possible if the object the pointer points to is of the base type: no dynamic binding if the object is of a derived type: dynamic binding is possible If the function called is redefined in the appropriate derived class and has the same name and signature as the (virtual) function in the base class, Apply dynamic binding implemented through virtual functions c A. Amer Polymorphism 35

51 Types of member functions Defined in the base class and inherited in a derived class without redefinition Defined in a derived class without a counterpart in the base class c A. Amer Polymorphism 36

52 Types of member functions Defined in the base class and inherited in a derived class without redefinition Defined in a derived class without a counterpart in the base class Defined in the base class and redefined in a derived class with the same name and same signature Defined in the base class and redefined in a derived class with the same name and different signature Defined in the base class and redefined in a derived class with the same name and same signature as virtual In which case you can apply dynamic binding? c A. Amer Polymorphism 36

53 Virtual functions A virtual function is a member function of the base class that can be redefined at run-time Even if the object is accessed by a base pointer virtual functions allow derived classes to replace the implementation of a function provided by the base class at run time c A. Amer Polymorphism 37

54 Virtual functions A virtual function is a member function of the base class that can be redefined at run-time Even if the object is accessed by a base pointer virtual functions allow derived classes to replace the implementation of a function provided by the base class at run time The replacement is always called whenever the object in question is of the derived class (even if the object is accessed by a base pointer) This allows algorithms in the base class to be replaced in the derived class, even if users don t know about the derived class c A. Amer Polymorphism 37

55 Example: without virtual functions class Shape { protected : i n t width, h e i g h t ; public : void set ( i n t a, i n t b ) { width=a ; h e i g h t =b ; } / / access base p a r t only } ; class Rectangle : public Shape { public : i n t area ( void ) { return ( width h e i g h t ) ; } / / s p e c i f i c to Rect. } ; class T r i a n g l e : public Shape { public : i n t area ( void ) { return ( width h e i g h t / 2 ) ; } / / s p e c i f i c to T r g l. } ; c A. Amer Polymorphism 38

56 Example: without virtual functions Rectangle r e c t ; T r i a n g l e t r g l ; Shape ppoly1 = &r e c t ; / / two p o i n t e r s to o b j e c t s of base class Shape Shape ppoly2 = & t r g l ; / / They are assigned the addresses of / / derived classes r e c t and t r g l ( v a l i d?) / / ==> we can only r e f e r the members t h a t / / Rectangle and T r i a n g l e i n h e r i t from Shape ppoly1 >set ( 4, 5 ) ; / / can use ppoly1 ; ppoly2 >set ( 4, 5 ) ; cout << r e c t. area ( ) << endl ; / / cannot use ppoly1 ; Output 20 cout << t r g l. area ( ) << endl ; / / cannot use ppoly2 ; Output 10 c A. Amer Polymorphism 39

57 Example: without virtual functions To make it possible for the pointers to the base class to access a member, we have to declared it in the base class But what is the area of an undefined shape? c A. Amer Polymorphism 40

58 Example: without virtual functions To make it possible for the pointers to the base class to access a member, we have to declared it in the base class But what is the area of an undefined shape? To access a (virtual) member of a base class we must declare it as virtual so that the use of pointers to base objects can be fully applied to derived objects c A. Amer Polymorphism 40

59 Example: with virtual functions class Shape { protected : i n t width, h e i g h t ; public : void set ( i n t a, i n t b ) { width=a ; h e i g h t =b ; } / / set ( ) not v i r t u a l : i t does the same f o r a l l classes v i r t u a l i n t area ( void ) / / area ( ) as v i r t u a l f u n c t i o n / / Does not make much sense here, but we need i t l a t e r { return ( 0 ) ; } / / v i r t u a l : works s p e c i f i c i n derived c l s. } ; class Rectangle : public Shape { public : i n t area ( void ) { return ( width h e i g h t ) ; } } ; c A. Amer Polymorphism 41

60 Example: with virtual functions class T r i a n g l e : public Shape { public : i n t area ( void ) { return ( width h e i g h t / 2 ) ; } } ; / / Testing Rectangle r e c t ; T r i a n g l e t r g l ; Shape poly ; Shape ppoly1 = &r e c t ; Shape ppoly2 = & t r g l ; Shape ppoly3 = &poly ; c A. Amer Polymorphism 42

61 Example: with virtual functions ppoly1 >set ( 4, 5 ) ; ppoly2 >set ( 4, 5 ) ; ppoly3 >set ( 4, 5 ) ; / / dynamic binding : / / At run time the implementation of the v i r t u a l f u n c t i o n / / i s replaced by t h a t of the derived class cout << ppoly1 >area ( ) << endl ; / / output : 20 cout << ppoly2 >area ( ) << endl ; / / output : 10 cout << ppoly3 >area ( ) << endl ; / / output : 0 / / despite v i r t u a l i t y we can declare an o b j e c t of type Shape / / and to c a l l i t s area ( ) f u n c t i o n c A. Amer Polymorphism 43

62 Virtual & non-virtual functions Non-virtual member functions are resolved statically it is selected at compile-time based on the type of the pointer to the object c A. Amer Polymorphism 44

63 Virtual & non-virtual functions Non-virtual member functions are resolved statically it is selected at compile-time based on the type of the pointer to the object Virtual member functions are resolved dynamically it is selected at run-time based on the type of the object (not the type of the pointer to that object) Dynamic binding creates extra space and time overhead but... c A. Amer Polymorphism 44

64 Example without virtual functions class Student { friend ostream& operator <<( ostream &, Student & ) ; public : Student ( char pf, char pl ) ; ~Student ( ) ; protected : char f i r s t ; char l a s t ; } ; class COEN244 : public Student { public : COEN244( char pf, char pl ) ; ~COEN244 ( ) { count ; } void setgrade ( f l o a t, f l o a t, f l o a t, f l o a t ) ; f l o a t grade ( ) ; / / <=== s t a t i c i n t getcount ( ) { return count ; } private : f l o a t assignment ; f l o a t quiz ; f l o a t midterm ; f l o a t f i n a l ; s t a t i c i n t count ; } ; c A. Amer Polymorphism 45

65 Example without virtual functions class ELEC311 : public Student { public : ELEC311( char pf, char pl ) ; ~ELEC311 ( ) { count ; } void setgrade ( f l o a t, f l o a t, f l o a t, f l o a t ) ; f l o a t grade ( ) ; / / <=== s t a t i c i n t getcount ( ) { return count ; } private : f l o a t assignment ; f l o a t l a b o r a t o r y ; f l o a t midterm ; f l o a t f i n a l ; s t a t i c i n t count ; } ; c A. Amer Polymorphism 46

66 Example without virtual functions class ELEC490 : public Student { public : ELEC490( char pf, char pl ) ; ~ELEC490 ( ) { count ; } void setgrade ( f l o a t ) ; f l o a t grade ( ) ; / / <=== s t a t i c i n t getcount ( ) { return count ; } private : f l o a t p r o j e c t ; s t a t i c i n t count ; } ; c A. Amer Polymorphism 47

67 Example without virtual functions void p r i n t I n f o ( const Student ) ; void main { COEN244 c1 ( " Jane ", "Doe" ) ; c1. setgrade (50, 55, 25, 9 5 ) ; p r i n t I n f o (&c1 ) ; } void p r i n t I n f o ( const Student pstud ) { / / Output name v i a stream i n s e r t i o n overload cout << pstud << endl ; / / QUESTION??? How do I know what kind / / of Student pstud i s? cout << pstud >grade ( ) ; / / We cannot do t h i s!!! } c A. Amer Polymorphism 48

68 Example with virtual functions class Student { friend ostream& operator <<( ostream &, Student & ) ; public : Student ( char pf, char pl ) ; ~Student ( ) ; v i r t u a l f l o a t grade ( ) ; / / <=== protected : char f i r s t ; char l a s t ; } ; class COEN244 : public Student { public : COEN244( char pf, char pl ) ; ~COEN244 ( ) { count ; } void setgrade ( f l o a t, f l o a t, f l o a t, f l o a t ) ; v i r t u a l f l o a t grade ( ) ; / / <=== s t a t i c i n t getcount ( ) { return count ; } private : f l o a t assignment ; f l o a t quiz ; f l o a t midterm ; f l o a t f i n a l ; s t a t i c i n t count ; } ; c A. Amer Polymorphism 49

69 Example with virtual functions void void main { COEN244 c1 ( " Jane ", "Doe" ) ; c1. setgrade (50, 55, 25, 9 5 ) ; p r i n t I n f o (&c1 ) ; } void p r i n t I n f o ( const Student pstud ) { / / Output name v i a stream i n s e r t i o n overload cout << pstud << endl ; cout << pstud >grade ( ) ; / / grade ( ) of COEN244 class w i l l be c a l l e d } c A. Amer Polymorphism 50

70 Virtual functions central to OO Virtual functions make OO software extensible and adaptable Because old code (in the base class) calls new code (in the derived class) Programming with classes but without dynamic binding is called "object based," but not "object oriented" c A. Amer Polymorphism 51

71 Virtual functions: summary When a base class member function is declared virtual, it permits run-time (dynamic) selection of the equivalent member function of the appropriate derived class c A. Amer Polymorphism 52

72 Virtual functions: summary When a base class member function is declared virtual, it permits run-time (dynamic) selection of the equivalent member function of the appropriate derived class Base class virtual functions may have their own definition It will be called if the referenced object is a base class object or if the referenced derived class did not define an equivalent member function Derived classes defining a virtual function need not be explicitly declared virtual Polymorphic behavior works with pointers & references of base classes c A. Amer Polymorphism 52

73 Virtual functions: Advice If using public inheritance, many base class member functions should be declared virtual to ensure that the derived class customizations will override the base class behavior even in a context where a base class object is expected c A. Amer Polymorphism 53

74 Abstract Classes Some classes represent abstract concepts for which objects cannot exist (There is no reason to create such objects) Person 2DShape Base Faculty Student Admine Triangle Rectangle Circle Derived Derived/Base Derived Derived Derived Derived... Base c A. Amer Polymorphism 54

75 Abstract Classes In an inheritance hierarchy with an abstract base class The functions have to be defined in the derived classes The equivalent functions in the base classes are called pure virtual functions virtual void print() =0; An object of an abstract base class cannot be instantiated c A. Amer Polymorphism 55

76 Abstract Classes In an inheritance hierarchy with an abstract base class The functions have to be defined in the derived classes The equivalent functions in the base classes are called pure virtual functions virtual void print() =0; An object of an abstract base class cannot be instantiated Abstract Class: a class with at least one pure virtual function Abstract Data Type (ADT): An abstract class c A. Amer Polymorphism 55

77 Virtual destructor You cannot have virtual constructor, but you can have virtual destructor A virtual destructor is a virtual function that is needed if we will delete (using the delete operator) a derived-class object via a base-class pointer c A. Amer Polymorphism 56

78 Virtual destructor You cannot have virtual constructor, but you can have virtual destructor A virtual destructor is a virtual function that is needed if we will delete (using the delete operator) a derived-class object via a base-class pointer void main ( ) { / / pstud p o i n t s t o a Student o b j e c t Student pstud = new COEN244( John, Smith ) ;... delete pstud ; / / w i l l c a l l t h e Student o b j e c t s d e s t r u c t o r } The COEN244 object destructor will not be called Not all memory is deallocated!!! c A. Amer Polymorphism 56

79 Virtual destructor c l a s s Student { friend ostream& operator <<(ostream&, Student &); public : Student ( char pf, char pl ) ; v i r t u a l Student ( ) ; v i r t u a l f l o a t grade ( ) ; protected : char f i r s t ; char l a s t ; } ; c A. Amer Polymorphism 57

80 Virtual destructor c l a s s Student { friend ostream& operator <<(ostream&, Student &); public : Student ( char pf, char pl ) ; v i r t u a l Student ( ) ; v i r t u a l f l o a t grade ( ) ; protected : char f i r s t ; char l a s t ; } ; When a Student pointer, pointing to a COEN244 object, is deleted, the COEN244 destructor is called which in turn will call the Student destructor Virtual destructor violate the rule of virtual functions? (name?) but memory leaks are very dangerous c A. Amer Polymorphism 57

81 Virtual destructor A class should have a virtual destructor unless that class has NO virtual functions If we have any virtual functions, then we will probably going to process derived objects via a base pointer Such processing may include invoking a destructor (normally done implicitly via delete) c A. Amer Polymorphism 58

82 Virtual destructor A class should have a virtual destructor unless that class has NO virtual functions If we have any virtual functions, then we will probably going to process derived objects via a base pointer Such processing may include invoking a destructor (normally done implicitly via delete) Recall virtual functions bind to the code associated with the class of the object, rather than with the class of the pointer/reference When you say delete baseptr, and the base class has a virtual destructor, the destructor that gets invoked is the one associated with the type of the object *baseptr, rather than the one associated with the type of the pointer c A. Amer Polymorphism 58

83 P: a summary@ Classes: provides data abstraction and enca a means to hide implementations but docum Inheritance: promotes software reuse Polymorphism: offers software extensibility c A. Amer Polymorphism 59

84 Polymorphism: Employee example class Employee { public : Employee ( const char, const char ) ; ~Employee ( ) ; / / d e s t r u c t o r reclaims memory const char getfirstname ( ) const ; const char getlastname ( ) const / / Pure v i r t u a l f u n c t i o n makes Employee a b s t r a c t base class v i r t u a l double earnings ( ) const = 0; / / pure v i r t u a l v i r t u a l void p r i n t ( ) const ; / / v i r t u a l } ; private : char firstname ; char lastname ; c A. Amer Polymorphism 60

85 Polymorphism: Employee example class Boss : public Employee { public : Boss ( const char, const char, double = 0. 0 ) ; void setweeklysalary ( double ) ; v i r t u a l double earnings ( ) const ; v i r t u a l void p r i n t ( ) const ; private : double weeklysalary ; } ; c A. Amer Polymorphism 61

86 Polymorphism: Employee example class SalaryWorker : public Employee { public : SalaryWorker ( const char, const char, double = 0. 0 ) ; void setsalary ( double ) ; v i r t u a l double earnings ( ) const ; v i r t u a l void p r i n t ( ) const ; protected : double s a l a r y ; / / weekly s a l a r y } ; c A. Amer Polymorphism 62

87 Polymorphism: Employee example class HourlyWorker : public Employee { public : HourlyWorker ( const char, const char, double = 0. 0, double = 0. 0 ) ; void setwage ( double ) ; void sethours ( double ) ; v i r t u a l double earnings ( ) const ; v i r t u a l void p r i n t ( ) const ; private : double wage ; / / wage per hour double hours ; / / hours worked f o r week } ; c A. Amer Polymorphism 63

88 Polymorphism: Employee example class CommissionWorker : public Employee { public : CommissionWorker ( const char, const char, double = 0. 0, i n t = 0 ) ; void setcommission ( double ) ; void s e t Q u a n t i t y ( i n t ) ; v i r t u a l double earnings ( ) const ; v i r t u a l void p r i n t ( ) const ; protected : double commission ; / / amount per item sold i n t q u a n t i t y ; / / t o t a l items sold f o r week } ; c A. Amer Polymorphism 64

89 Polymorphism: Employee example / / Testing Employee l i s t [ 10 ] ; Boss b ( "CCC", "TTTTT", ) ; CommissionWorker c1 ( "SSSS", "QQQQ", 3.0, 150); CommissionWorker c2 ( " JJJJ ", "DDDD", 4.0, 6 0 ) ; SalaryWorker s1 ( "BBBB", "VVVVV", 500); SalaryWorker s2 ( "RRRR", "Red", 500); HourlyWorker h1 ( "GGGG", "PPPP", 17.00, ) ; HourlyWorker h2 ( "KKKK", " I I I I ", 13.75, 4 0 ) ; HourlyWorker h3 ( "MMM", "TT", 10.15, 1 8 ) ; c A. Amer Polymorphism 65

90 Polymorphism: Employee example l i s t [ 0 ] = &b ; l i s t [ 1 ] = &c1 ; l i s t [ 2 ] = &c2 ; l i s t [ 3 ] = &s1 ; l i s t [ 4 ] = &s2 ; l i s t [ 5 ] = &h1 ; l i s t [ 6 ] = &h2 ; l i s t [ 7 ] = &h3 ; / / Dynamic Binding cout << "DYNAMIC BINDING" << endl ; for ( i n t i = 0; i < 8; i ++) { l i s t [ i ] > p r i n t ( ) ; / / using p o i n t e r s cout << " earned $ " << ( l i s t [ i ] ). earnings ( ) ; / / using o b j e c t s c A. Amer Polymorphism 66

91 Polymorphism: Employee example / / S t a t i c Binding cout << " STATIC BINDING" << endl ; / / access through o b j e c t s : no polymorphism / v i r t u a l i t y b. p r i n t ( ) ; cout << " earned $ " << b. earnings ( ) ; c1. p r i n t ( ) ; cout << " earned $ " << c1. earnings ( ) ; c2. p r i n t ( ) ; cout << " earned $ " << c2. earnings ( ) ; s1. p r i n t ( ) ; cout << " earned $ " << s1. earnings ( ) ; s2. p r i n t ( ) ; cout << " earned $ " << s2. earnings ( ) ; h1. p r i n t ( ) ; cout << " earned $ " << h1. earnings ( ) ; h2. p r i n t ( ) ; cout << " earned $ " << h2. earnings ( ) ; h3. p r i n t ( ) ; cout << " earned $ " << h3. earnings ( ) ; c A. Amer Polymorphism 67

92 Polymorphism: Person example / / L i s t i n g 15.5 Heterogeneous l i s t processing using v i r t u a l f u n c t i o n s class Person { protected : char i d [ 1 0 ] ; / / data common to both types char name ; / / v a r i a b l e l e n g t h public : Person ( const char i d [ ], const char nm [ ] ) / / Kind type { s t r c p y ( Person : : id, i d ) ; / / copy i d name = new char [ s t r l e n (nm) + 1 ] ; / / get space f o r name i f (name == 0) { cout << " Out of memory \ n " ; e x i t ( 0 ) ; } s t r c p y (name,nm ) ; / / copy name } v i r t u a l void w r i t e ( ) const / / to d i s p l a y data { } / / not much to do at the moment ~Person ( ) / / r e t u r n heap memory { delete [ ] name ; } / / f o r Person o b j e c t only } ; c A. Amer Polymorphism 68

93 Polymorphism: Person example class Faculty : public Person { private : char rank ; / / f o r f a c u l t y only public : Faculty ( const char i d [ ], const char nm [ ], const char r [ ] ) : Person ( id,nm) / / i n i t i a l i z a t i o n l i s t { rank = new char [ s t r l e n ( r ) + 1 ] ; i f ( rank == 0) { cout << " Out of memory \ n " ; e x i t ( 0 ) ; } s t r c p y ( rank, r ) ; } void w r i t e ( ) const / / d i s p l a y record { cout << " i d : " << i d << endl ; / / p r i n t id, name cout << " name : " << name << endl ; cout << " rank : " << rank <<endl <<endl ; } / / f a c u l t y only } ; ~ Faculty ( ) { delete [ ] rank ; } / / r e t u r n heap memory c A. Amer Polymorphism 69

94 Polymorphism: Person example class Student : public Person { private : char major ; / / f o r student only public : Student ( const char i d [ ], const char nm [ ], const char m[ ] ) : Person ( id,nm) / / i n i t i a l i z a t i o n l i s t { major = new char [ s t r l e n (m) + 1 ] ; i f ( major == 0) { cout << " Out of memory \ n " ; e x i t ( 0 ) ; } s t r c p y ( major,m) ; } void w r i t e ( ) const / / d i s p l a y record { cout << " i d : " << i d << endl ; / / p r i n t id, name cout << " name : " << name << endl ; cout << " major : " << major <<endl <<endl ; / / student only } ~Student ( ) { delete [ ] major ; } / / r e t u r n heap memory } ; c A. Amer Polymorphism 70

95 Polymorphism: Person example void read ( i f s t r e a m& f, Person & person ) { / / read one record char kind [ 8 ], i d [ 1 0 ], name [ 8 0 ], buf [ 8 0 ] ; f. g e t l i n e ( kind, 8 0 ) ; / / recognize the incoming type f. g e t l i n e ( id, 1 0 ) ; / / read i d f. g e t l i n e (name, 8 0 ) ; / / read name f. g e t l i n e ( buf, 8 0 ) ; / / rank or major? i f ( strcmp ( kind, "FACULTY" ) == 0) { person = new Faculty ( id, name, buf ) ; } / / o b j e c t i s Faculty else i f ( strcmp ( kind, "STUDENT" ) == 0) { person = new Student ( id, name, buf ) ; } / / o b j e c t i s Student else { cout << " Corrupted data : unknown type \ n " ; e x i t ( 0 ) ; } } void w r i t e ( const Person p ) / / d i s p l a y record { p >w r i t e ( ) ; } / / Faculty or Student? c A. Amer Polymorphism 71

96 Polymorphism: Person example / / Testing cout << endl << endl ; Person data [ 2 0 ] ; i n t cnt = 0; / / array of p o i n t e r s i f s t r e a m from ( " univ. dat " ) ; / / i n p u t data f i l e i f (! from ) { cout << " Cannot open f i l e \ n " ; return 0; } while (! from. eof ( ) ) { read ( from, data [ cnt ] ) ; / / read u n t i l eof cnt ++; } cout << " T o t a l records read : " << cnt << endl << endl ; for ( i n t i =0; i < cnt ; i ++) { w r i t e ( data [ i ] ) ; } / / d i s p l a y data for ( i n t j =0; j < cnt ; j ++) { delete data [ j ] ; } / / d e l e t e the record c A. Amer Polymorphism 72

97 A sample exam s problem Define a XXXX class that inherits from XXXX class... Declare base pointers each pointing to a derived object Pay attention to the type of the pointer... To be continued in class. c A. Amer Polymorphism 73

Chapter 19 - C++ Inheritance

Chapter 19 - C++ Inheritance Chapter 19 - C++ Inheritance 19.1 Introduction 19.2 Inheritance: Base Classes and Derived Classes 19.3 Protected Members 19.4 Casting Base-Class Pointers to Derived-Class Pointers 19.5 Using Member Functions

More information

Where do we stand on inheritance?

Where do we stand on inheritance? In C++: Where do we stand on inheritance? Classes can be derived from other classes Basic Info about inheritance: To declare a derived class: class :public

More information

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1 Polymorphism Part 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid adult

More information

Polymorphism Part 1 1

Polymorphism Part 1 1 Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid

More information

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University Polymorphism Miri Ben-Nissan (Kopel) 1 Shape Triangle Rectangle Circle int main( ) Shape* p = GetShape( ); p->draw( ); Shape* GetShape( ) choose randomly which shape to send back For example: Shape* p

More information

CS105 C++ Lecture 7. More on Classes, Inheritance

CS105 C++ Lecture 7. More on Classes, Inheritance CS105 C++ Lecture 7 More on Classes, Inheritance " Operator Overloading Global vs Member Functions Difference: member functions already have this as an argument implicitly, global has to take another parameter.

More information

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts.

These new operators are intended to remove some of the holes in the C type system introduced by the old C-style casts. asting in C++: Bringing Safety and Smartness to Your Programs of 10 10/5/2009 1:20 PM By G. Bowden Wise The new C++ standard is full of powerful additions to the language: templates, run-time type identification

More information

Chapter 19 C++ Inheritance

Chapter 19 C++ Inheritance Chapter 19 C++ Inheritance Angela Chih-Wei i Tang Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 19.11 Introduction ti 19.2 Inheritance: Base Classes

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck C++ Crash Kurs Polymorphism Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck http://www.itm.uni-luebeck.de/people/pfisterer C++ Polymorphism Major abstractions of C++ Data abstraction

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

Fig Fig Fig. 10.3

Fig Fig Fig. 10.3 CHAPTER 10 VIRTUAL FUNCTIONS AND POLYMORPHISM 1 Illustrations List (Main Page) Fig. 10.2 Fig. 10.3. Definition of abstract base class Shape. Flow of control of a virtual function call. CHAPTER 10 VIRTUAL

More information

C++ Programming: Polymorphism

C++ Programming: Polymorphism C++ Programming: Polymorphism 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Run-time binding in C++ Abstract base classes Run-time type identification 2 Function

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded

More information

Object Oriented Programming in C++ Basics of OOP

Object Oriented Programming in C++ Basics of OOP Object Oriented Programming in C++ Basics of OOP In this section we describe the three most important areas in object oriented programming: encapsulation, inheritance and polymorphism. 1. INTRODUCTION

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

VALLIAMMAI ENGINEERING COLLEGE

VALLIAMMAI ENGINEERING COLLEGE VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur 603 203 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK B.E. - Electrical and Electronics Engineering IV SEMESTER CS6456 - OBJECT ORIENTED

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

What are the characteristics of Object Oriented programming language?

What are the characteristics of Object Oriented programming language? What are the various elements of OOP? Following are the various elements of OOP:- Class:- A class is a collection of data and the various operations that can be performed on that data. Object- This is

More information

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am CMSC 202 Section 010x Spring 2007 Computer Science II Final Exam Name: Username: Score Max Section: (check one) 0101 - Justin Martineau, Tuesday 11:30am 0102 - Sandeep Balijepalli, Thursday 11:30am 0103

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Motivating

More information

Object-oriented Programming. Object-oriented Programming

Object-oriented Programming. Object-oriented Programming 2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language

More information

Object Oriented Software Design II

Object Oriented Software Design II Object Oriented Software Design II Inheritance Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) C++ Intro February

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 1. The following C++ program compiles without any problems. When run, it even prints out the hello called for in line (B) of main. But subsequently

More information

Midterm Exam 5 April 20, 2015

Midterm Exam 5 April 20, 2015 Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d.

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone else has built and debugged. In composition

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object Polymorphism C++ Object Oriented Programming Pei-yih Ting NTOU CS 26-1 Contents Assignment to base / derived types of objects Assignment to base / derived types of pointers Heterogeneous container and

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Unit 3. C++ Language II. Takayuki Dan Kimura. Unbounded Queue. Extension of Queue Data Type. Static Members. Base and Derived Classes (Inheritance)

Unit 3. C++ Language II. Takayuki Dan Kimura. Unbounded Queue. Extension of Queue Data Type. Static Members. Base and Derived Classes (Inheritance) Unit 3 C++ Language II Object Oriented Programming in C++ Takayuki Dan Kimura Unbounded Queue Extension of Queue Data Type Static Members Base and Derived Classes (Inheritance) Virtual Functions Polymorphism

More information

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory. Pointer C++ Memory Map Once a program is compiled, C++ creates four logically distinct regions of memory: Code Area : Area to hold the compiled program code Data Area : Area to hold global variables Stack

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

Programming in C# Inheritance and Polymorphism

Programming in C# Inheritance and Polymorphism Programming in C# Inheritance and Polymorphism C# Classes Classes are used to accomplish: Modularity: Scope for global (static) methods Blueprints for generating objects or instances: Per instance data

More information

Object Oriented Paradigm

Object Oriented Paradigm Object Oriented Paradigm History Simula 67 A Simulation Language 1967 (Algol 60 based) Smalltalk OO Language 1972 (1 st version) 1980 (standard) Background Ideas Record + code OBJECT (attributes + methods)

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1 CS250 Intro to CS II Spring 2017 CS250 - Intro to CS II 1 Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Increases Program Structure which results in greater reliability. Polymorphism

Increases Program Structure which results in greater reliability. Polymorphism UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the

More information

Polymorphism. Zimmer CSCI 330

Polymorphism. Zimmer CSCI 330 Polymorphism Polymorphism - is the property of OOP that allows the run-time binding of a function's name to the code that implements the function. (Run-time binding to the starting address of the code.)

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED - Polymorphism - Virtual Functions - Abstract Classes - Virtual

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure that you review all previous exams and make sure you fully understand

More information

Computer Programming Inheritance 10 th Lecture

Computer Programming Inheritance 10 th Lecture Computer Programming Inheritance 10 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved 순서 Inheritance

More information

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization

Inheritance: Develop solutions by abstracting real-world object and their interaction into code to develop software solutions. Layering: Organization Final Exam Overview: Monday, 3pm-6pm, in WLH 2005 First pages: Quiz question - No quiz of week 2 - No bit manipulation (shifting and masking) - Quizzes: week 4, 6, 8, 10 One page on C++ language features

More information

Extending Classes (contd.) (Chapter 15) Questions:

Extending Classes (contd.) (Chapter 15) Questions: Extending Classes (contd.) (Chapter 15) Questions: 1 Virtual Functions in C++ Employee /\ / \ ---- Manager 2 Case 1: class Employee { string firstname, lastname; //... Employee( string fnam, string lnam

More information

Inheritance and aggregation

Inheritance and aggregation Advanced Object Oriented Programming Inheritance and aggregation Seokhee Jeon Department of Computer Engineering Kyung Hee University jeon@khu.ac.kr 1 1 Inheritance? Extend a class to create a new class

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued COMP322 - Introduction to C++ Lecture 09 - Inheritance continued Dan Pomerantz School of Computer Science 11 March 2012 Recall from last time Inheritance describes the creation of derived classes from

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

Inheritance. Chapter 15 & additional topics

Inheritance. Chapter 15 & additional topics Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Inheritance

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

Object Oriented Programming: Inheritance Polymorphism

Object Oriented Programming: Inheritance Polymorphism Object Oriented Programming: Inheritance Polymorphism Shahram Rahatlou Computing Methods in Physics http://www.roma1.infn.it/people/rahatlou/cmp/ Anno Accademico 2018/19 Today s Lecture Introduction to

More information

CSC1322 Object-Oriented Programming Concepts

CSC1322 Object-Oriented Programming Concepts CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++.

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship Object classification is typically hierarchical.

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Division of Mathematics and Computer Science Maryville College Outline Inheritance 1 Inheritance 2 3 Outline Inheritance 1 Inheritance 2 3 The "is-a" Relationship The "is-a" Relationship Object classification

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam ( hnam@hanyang.ac.kr ) Lecture 1 1 Data Structures Data? Songs in a smartphone Photos in a camera Files

More information

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4 CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();

More information

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017 OOP components For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Data Abstraction Information Hiding, ADTs Encapsulation Type Extensibility Operator Overloading

More information

PROGRAMMING IN C++ COURSE CONTENT

PROGRAMMING IN C++ COURSE CONTENT PROGRAMMING IN C++ 1 COURSE CONTENT UNIT I PRINCIPLES OF OBJECT ORIENTED PROGRAMMING 2 1.1 Procedure oriented Programming 1.2 Object oriented programming paradigm 1.3 Basic concepts of Object Oriented

More information

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming. Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 It is possible to declare as a friend A member function A global function A class All of the above What

More information

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed CREATED BY: Muhammad Bilal Arslan Ahmad Shaad JAVA Chapter No 5 Instructor: Muhammad Naveed Muhammad Bilal Arslan Ahmad Shaad Chapter No 5 Object Oriented Programming Q: Explain subclass and inheritance?

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; } Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3;

More information

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a. Intro to OOP - Object and class - The sequence to define and use a class in a program - How/when to use scope resolution operator - How/when to the dot operator - Should be able to write the prototype

More information

Inheritance, Polymorphism and the Object Memory Model

Inheritance, Polymorphism and the Object Memory Model Inheritance, Polymorphism and the Object Memory Model 1 how objects are stored in memory at runtime? compiler - operations such as access to a member of an object are compiled runtime - implementation

More information

Object Oriented Programming CS250

Object Oriented Programming CS250 Object Oriented Programming CS250 Abas Computer Science Dept, Faculty of Computers & Informatics, Zagazig University arabas@zu.edu.eg http://www.arsaliem.faculty.zu.edu.eg Polymorphism Chapter 8 8.1 Static

More information

Object Oriented Programming with c++ Question Bank

Object Oriented Programming with c++ Question Bank Object Oriented Programming with c++ Question Bank UNIT-1: Introduction to C++ 1. Describe the following characteristics of OOP. i Encapsulation ii Polymorphism, iii Inheritance 2. Discuss function prototyping,

More information

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Programming C++ Lecture 3 Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Inheritance S Software reuse inherit a class s data and behaviors and enhance with new capabilities.

More information

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018) Lesson Plan Name of the Faculty Discipline Semester :Mrs. Reena Rani : Computer Engineering : IV Subject: OBJECT ORIENTED PROGRAMMING USING C++ Lesson Plan Duration :15 weeks (From January, 2018 to April,2018)

More information

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Quiz Start Time: 09:34 PM Time Left 82 sec(s) Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy

Object Oriented Programming. CISC181 Introduction to Computer Science. Dr. McCoy. Lecture 27 December 8, What is a class? Extending a Hierarchy CISC181 Introduction to Computer Science Dr. McCoy Lecture 27 December 8, 2009 Object Oriented Programming Classes categorize entities that occur in applications. Class teacher captures commonalities of

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples: Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before

More information

C++ Polymorphism. Systems Programming

C++ Polymorphism. Systems Programming C++ Polymorphism Systems Programming C++ Polymorphism Polymorphism Examples Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class Objects Aiming Derived-Class

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-4: Object-oriented programming Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428

More information

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers. cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... today: language basics: identifiers, data types, operators, type conversions, branching and looping, program structure

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 10: I n h e r i t a n c e Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

Chapter 5. Object- Oriented Programming Part I

Chapter 5. Object- Oriented Programming Part I Chapter 5 Object- Oriented Programming Part I 5: Preview basic terminology comparison of the Java and C++ approaches to polymorphic programming techniques introduced before in the context of inheritance:

More information

Inheritance. Chapter 15 & additional topics

Inheritance. Chapter 15 & additional topics Inheritance Chapter 15 & additional topics Overview Inheritance Introduction Three different kinds of inheritance Changing an inherited member function More Inheritance Details Polymorphism Inheritance

More information

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions

More information

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can

More information