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

Size: px
Start display at page:

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

Transcription

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

2 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 the program aborts with a memory segmentation fault. Why? 2

3 class X { int* p; int size; public: X() { p = 0; size = 0; X( int* ptr, int sz ) : size( sz ) { p = new int[ size ]; for ( int i=0; i<size; i++ ) p[i] = ptr[i]; ~X() { delete[] p; ; class Y : public X { int n; public: Y() {; Y( int* ptr, int sz, int nn ) : X( ptr, sz ), n( nn ) { Y( const Y& other ) : X( other ), n( other.n ) { Y& operator=( const Y& other ) { if ( this == &other ) return *this; X::operator=( other ); n = other.n; return *this; ; int main() { int data[ 3 ] = {3, 2, 1; Y y1( data, 3, 10 ); Y y2; y2 = y1; cout << "hello" << endl; return 0; //(A) //(B) 3

4 2. In terms of what happens at compile time and what happens at run time, what s the difference between function overloading and function overriding? 4

5 3. What program declaration makes it possible for a function to behave polymorphically? 5

6 4. What is meant by static binding and what is meant by dynamic binding? 6

7 5. Does overload resolution result in static binding or dynamic binding? 7

8 6. What is RTTI and what s it used for? 8

9 7. What is meant by a polymorphic type? 9

10 8. While polymorphic behavior of a function is obviously important, why is the concept of a polymorphic type important also? 10

11 Virtual Destructors in C++ Even when a base class does not directly appropriate system resources, you may still need to declare its destructor virtual if you want polymorphic destruction of derived-class objects. 11

12 Consider the case of a vector of pointers to the base-class type, where some of the pointers are actually pointing to objects of a derived-class type. Let s say that you now set up a loop in which you invoke the delete operator on each of the pointers, with the hope that the destructor invoked for each object would be the one defined specifically for it. In other words, you d want the destructor invocation to behave polymorphically. This will only happen if you declare the destructor to be virtual in the base class. 12

13 A simple demonstration of a virtual destructor in action, consider... #include <iostream> using namespace std; class X { public: virtual ~X(); ; // BASE //(A) X::~X(){ cout << "X s destructor" << endl; //(B) class Y : public X { // DERIVED public: ~Y() { cout << "Y s destructor" << endl; ; class Z : public Y { // DERIVED public: ~Z() { cout << "Z s destructor" << endl; ; int main() { X* p = new Z(); //(C) delete p; //(D) return 0; 13

14 Inmain of the above program, we construct an object of typezand assign it to a base-class pointer of type X* in line (C). When we invoke delete on this pointer in line (D), we get the following output from the program: Z s destructor Y s destructor X s destructor 14

15 What would be the output of the program for the following in main: Y* q = new Z(); delete q; What would be the output of the program if the keyword virtual was dropped in line (A) of the program? 15

16 Constructor Order Dependencies in C++ Order dependency in a derived-class constructor refers to the order in which the base-class sub-objects are constructed inside a derived-class object; the order in which the specified initializations carried for the data members of the derived class; etc. 16

17 class X { public: X() { cout << "X object under construction" << endl; ; class Y { public: Y() { cout << "Y object under construction" << endl; ; class Base { X xobj; // (A) Y yobj; // (B) public: Base() : xobj( X() ), yobj( Y() ) { // (C) ; int main() { Base b; 17

18 The official rules for the order in which the code for a derived-class constructor is executed are: 1. When a derived-class constructor is invoked, first the memory needed for constructing the derived-class object is appropriated. 2. Next, the constructor of the base-class is invoked to construct the baseclass slice of the derived-class object. (If the derived class has multiple bases, the base-class constructor for each base is invoked in the order in which the bases are declared in the header of the derived class, and regardless of the order used by the programmer in his/her coding of the derived-class constructor.) 3. If the derived-class constructor uses the member initialization syntax for the data member of the derived class, invoke the initializers in the order in which the data members are declared in the class definition, and regardless of the order they are shown in the member initialization syntax by the programmer. 4. Execute the code in the body of the derived-class constructor. 18

19 class X { public: virtual void foo(){ cout << "X s foo invoked" << endl; X() { foo(); ; class Y : public X { public: void foo(){ cout << "Y s foo invoked" << endl; Y() { ; int main() { Y yobj; 19

20 Abstract Classes in C++ Shape /\ /\ /\ / \ / \ / \ / \ / \ / \ / \ / \ Circle Rectangle... double area( ); double circumference(); class Shape { public: virtual double area( ); virtual double circumference(); //... ; 20

21 class Shape { public: virtual double area( ) = 0; virtual double circumference() = 0; //... ; Shape* shapes[ 3 ]; shapes[0] = new Circle(... ); shapes[1] = new Rectangle(... ); shapes[2] = new Rectangle(... ); double total_area = 0; for (int i=0; i < 3; i++ ) total_area += shapes[i]->area(); // (A) 21

22 #include <iostream> class Shape { public: virtual double area() = 0; virtual double circumference() = 0; ; class Circle : public Shape { protected: double r; static double PI; public: Circle() { r = 1.0; Circle( double r ) { this->r = r; ; double area() { return PI*r*r; double circumference() { return 2 * PI * r; double getradius() {return r; double Circle::PI = ; class Rectangle : public Shape { double w, h; public: Rectangle() { w=0.0; h = 0.0; Rectangle( double w, double h ) { this->w = w; this->h = h; ; double area() { return w * h; double circumference() { return 2 * (w + h); double getwidth() { return w; double getheight() { return h; 22

23 int main() { Shape* shapes[ 3 ]; shapes[0] = new Circle( 2.0 ); shapes[1] = new Rectangle( 1.0, 3.0 ); shapes[2] = new Rectangle( 4.0, 2.0 ); double total_area = 0; for (int i=0; i < 3; i++ ) total_area += shapes[i]->area(); cout << "Total area = " << total_area << endl; 23

24 A C++ class is abstract if it has at least one virtual function that is pure. As one would expect, it is not possible to make objects of a class that is abstract. Therefore, the sole purpose of an abstract class is to serve as an interface to the derived classes. You could also say that an abstract class serves as an organizational principle in a class hierarchy. A pure virtual function that is not defined in a derived class remains a pure virtual function. So, in such a case, the derived class is also an abstract class. This allows us to build an implementation in stages. 24

25 Shape /\ /\ /\ / \ / \ / \ / \ / \ / \ / \ / \ Polygon... CurvedShape /\ /\ /\ /\ /\ /\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ Square Rectangle... Circle Ellipse... 25

26 #include <iostream> class Shape { public: virtual double area() = 0; virtual double circumference() = 0; ; class Polygon : public Shape { protected: int numvertices; bool starshaped; ; class CurvedShape : public Shape { public: virtual void polygonalapprox() = 0; ; class Circle : public CurvedShape { protected: double r; static double PI; public: Circle() { r = 1.0; Circle( double r ) { this->r = r; double area() { return PI*r*r; double circumference() { return 2 * PI * r; double getradius() {return r; void polygonalapprox() { 26

27 ; cout << "polygonal approximation code goes here" << endl; double Circle::PI = ; class Rectangle : public Polygon { double w, h; public: Rectangle() { w=0.0; h = 0.0; numvertices = 0; starshaped = true; Rectangle( double w, double h ) { this->w = w; this->h = h; numvertices = 4; starshaped = true; double area() { return w * h; double circumference() { return 2 * (w + h); double getwidth() { return w; double getheight() { return h; ; int main() { Shape* shapes[ 3 ]; shapes[0] = new Circle( 2.0 ); shapes[1] = new Rectangle( 1.0, 3.0 ); shapes[2] = new Rectangle( 4.0, 2.0 ); double total_area = 0; for (int i=0; i < 3; i++ ) 27

28 total_area += shapes[i]->area(); cout << "Total area = " << total_area << endl; 28

29 Protected and Private Derived Classes in C++ class Derived_class : public Base_class { //... ; class Derived_class : private Base_class { //... ; results in what s referred to as implementation inheritance. This name reflects the fact this kind of derivation is good primarily for using locally the non-private interface of the base class, but not for making this inherited interface available to other classes, or even to further derived classes. 29

30 A special case of this implementation inheritance is the protected inheritance obtained by a protected derivation, as in class Derived_class : protected Base_class { //... ; Now the non-private interface inherited from the base class is made available for inheritance to only the subclasses of the Derived class. 30

31 When you do not carry out a public derivation, you can no longer assign a Derived* to a Base* without explicit conversion. For example, in our Employee Manager example, if we had derived Manager using the following syntax class Manager : protected Employee { // same as before ; we would no longer be allowed to say Employee* e3 = new Manager( "ms", "importante" ); because a Manager is no longer automatically an Employee. Now if wish to make a single list, in the form of a vector, ofemployee* types, we must first use explicit conversion as in Employee* e3 = (Employee*) new Manager( "ms", "importante" ); 31

32 Employee ^ > ExecutiveRole / Manager ^ Director class ExecutiveRole { public: void sayexecutivehello(){ cout << "Hello from Executive ranks" << endl; ; 32

33 #include <iostream.h> #include <mstring.h> #include <vector.h> class Employee { string firstname, lastname; int age, yearsinservice; //... public: Employee( string fnam, string lnam ) { firstname = fnam; lastname = lnam; virtual void print() const { cout << firstname << " " << lastname << endl; void sayemployeehello() { cout << "hello from Employee class" << endl; ; 33

34 class ExecutiveRole { public: void sayexecutivehello(){ cout << "Hello from Executive ranks" << endl; ; //class Manager : public Employee, private ExecutiveRole { class Manager : public Employee, protected ExecutiveRole { short level; //.. public: Manager( string fnam, string lnam, short lvl ) : Employee( fnam, lnam ), level( lvl ) { cout<< "In Manager constructor: "; sayemployeehello(); sayexecutivehello(); // WILL NOT CO // WORKS FINE void print() const { Employee::print(); cout << "level: " << level << endl; ; 34

35 class Director : public Manager { short grade; //... public: Director( string fnam, string lnam, short lvl, short gd ) : Manager( fnam, lnam, lvl ), grade( gd ) { cout << "In Director constructor: "; sayemployeehello(); sayexecutivehello(); void print() const { Manager::print(); cout << "grade: " << grade << endl << endl; ; int main() { vector<employee*> emplist; Employee* e1 = new Employee( "john", "doe" ); Employee* e2 = (Employee*) new Manager( "jane", "joe", 2 ); Employee* e3 = (Employee*) new Director( "mister", "bigshot", 3, 4 ); emplist.push_back( e1 ); emplist.push_back( e2 ); emplist.push_back( e3 ); vector<employee*>::iterator p = emplist.begin(); while ( p < emplist.end() ) (*p++)->print(); 35

36 // e3->sayhi(); Manager* m = new Manager( "jane", "doe", 2 ); m->sayemployeehello(); Director* d = new Director( "john", "doe", 3, 4 ); d->sayemployeehello(); 36

37 Extending Classes in Java What is accomplished by making a public derivation from a class in C++ is achieved by using the extends clause in Java. 37

38 class Employee { private String firstname, lastname; //... public Employee( String fnam, String lnam ) { firstname = fnam; lastname = lnam; public void print() { System.out.print( firstname + " " + lastname ); class Manager extends Employee { private short level; //... public Manager( String fnam, String lnam, short lvl ) { super( fnam, lnam ); level = lvl; public void print() { super.print(); System.out.println( "level: " + level ); 38

39 Object construction in Java has one feature that is not shared by object construction in C++: If a derived-class constructor in Java does not invoke the base class s constructors (or if the base class does not have a no-arg constructor), the derived-class constructor is allowed to invoke another one of the constructors for the derived class by using the this() construct. 39

40 class Manager extends Employee { private short level; //... public Manager( String fnam, String lnam, short lvl ) { super( fnam, lnam ); level = lvl; public Manager( String fnam, String lnam ) { this( fnam, lnam, 10 ); public void print() { super.print(); System.out.println( "level: " + level ); 40

41 With regard to the base-class functions that are available in a derived class, another very important difference between C++ and Java is as follows: In C++ a derived-class function of a given name hides all base-class functions of the same name, regardless of their signatures; the hidden names can only be accessed through the scope operator in the derived class. That is not the case in Java. 41

42 //NameLookup.java class Base { public void foo() { System.out.println( "Base s foo() invoked" ); public void foo( int i ) { System.out.println( "Base s foo( int ) invoked" ); public void foo( int i, int j ) { System.out.println( "Base s foo( int, int ) invoked" ); class Derived extends Base { public void foo() { System.out.println( "Derived s foo() invoked" ); //(A) //(B) //(C) //(D) public class Test { public static void main( String[] args ) { Derived d = new Derived(); d.foo(); // Derived s foo() invoked //(E) d.foo( 3 ); // Base s foo( int ) invoked //(F) d.foo( 3, 4 ); // Base s foo( int, int ) invoked //(G) 42

43 //NameLookup.cc // WILL NOT COMPILE #include <iostream> using namespace std; class Base { public: void foo() { cout << "Base s foo() invoked" << endl; void foo( int i ) { cout << "Base s foo( int ) invoked" << endl; void foo( int i, int j ) { cout << "Base s foo( int, int ) invoked" << endl; ; class Derived : public Base { public: void foo() { cout << "Derived s foo() invoked" << endl; ; int main() { Derived d; d.foo(); d.foo( 3 ); d.foo( 3, 4 ); //(A) //(B) //(C) //(D) //(E) //(F) //(G) 43

44 Restrictions on Overriding Functions in Java The definition of an overriding method in a derived class must not violate the following restrictions: 1. The return type of an overriding method in a derived class must be the same as the return type of the overridden method in the base class. class X { public float foo( double m ) { return m; class Y extends public X { public double foo( double n ) { return n; // Er 44

45 2. The access restriction for an overriding method can be no tighter than the restriction on the base-class overridden method. So if the access restriction on the base-class method is, say, protected, the overriding method in the derived class can either be protected or public, but not private. 45

46 3. The exception specification for an overriding function in a derived class must be a subset of the exception specification on the overridden baseclass method. class Exception_1 extends Exception { class Exception_2 extends Exception { class X { private int m; public X( int mm ) { m = mm; public void foo() throws Exception_1 { System.out.println( "X s foo invoked" ); throw new Exception_1(); class Y extends X { private int n; public Y( int mm, int nn ) { super( mm ); n = nn; public void foo() throws Exception_2 { // WRON System.out.println( "Y s foo invoked" ); throw new Exception_2(); 46

47 class Exception_1 extends Exception { class Exception_2 extends Exception { class X { private int m; public X( int mm ) { m = mm; public void foo() throws Exception_1 { System.out.println( "X s foo invoked" ); throw new Exception_1(); class Y extends X { private int n; public Y( int mm, int nn ) { super( mm ); n = nn; public void foo() { System.out.println( "Y s foo invoked" ); 47

48 Constructor Order Dependencies in Java 1. Invoke the constructor of the derived class by appropriating the required amount of memory and set all the data members in this memory to their default values (zero for all numeric types, false for boolean, \u0000 for char, and null for object reference). 2. Invoke the base-class constructor. 3. If there is any initialization code attached to any of the data members in the base class, it is executed and the data members initialized. 4. Execute the code in the body of the base class constructor. This base class constructor could be a no-arg constructor. 5. If there is any initialization code attached to any of the data members in the derived class, it is now executed and the data members initialized. 6. Execute the code in the body of the derived class constructor. 48

49 class X { void foo(){ System.out.println( "X s foo invoked" ); public X() { foo(); class Y extends X { void foo(){ System.out.println( "Y s foo invoked" ); public Y() { class Test { public static void main( String[] args ) { Y yobj = new Y(); The output of this program is Y s foo invoked 49

50 Abstract Classes in Java abstract class Shape { abstract public double area( ); abstract public double draw(); //... 50

51 abstract class Shape { abstract protected double area(); abstract protected double circumference(); abstract class Polygon extends Shape { protected int numvertices; protected boolean starshaped; abstract class curvedshape extends Shape { abstract public void polygonalapprox(); class Circle extends curvedshape { protected double r; protected static double PI = ; public Circle() { r = 1.0; public Circle( double r ) { this.r = r; public double area() { return PI*r*r; public double circumference() { return 2 * PI * r; public double getradius() {return r; public void polygonalapprox() { System.out.println("polygonal approximation code goes here"); 51

52 class Rectangle extends Polygon { double w, h; public Rectangle() { w=0.0; h = 0.0; numvertices = 0; starshaped = true; public Rectangle( double w, double h ) { this.w = w; this.h = h; numvertices = 4; starshaped = true; public double area() { return w * h; public double circumference() { return 2 * (w + h); public double getwidth() { return w; public double getheight() { return h; class test { public static void main( String[] args ) { Shape[] shapes = new Shape[ 3 ]; shapes[0] = new Circle( 2.0 ); shapes[1] = new Rectangle( 1.0, 3.0 ); shapes[2] = new Rectangle( 4.0, 2.0 ); double total_area = 0; for (int i=0; i < shapes.length; i++ ) total_area += shapes[i].area(); System.out.println("Total area = " + total_area); 52

53 public abstract class WindowAdapter implements WindowListener public void windowactivated( WindowEvent e ) {; public void windowclosed( WindowEvent e ) {; public void windowclosing( WindowEvent e ) {; public void windowdeactivated( WindowEvent e ) {; public void windowdeiconified( WindowEvent e ) {; public void windowiconified( WindowEvent e ) {; public void windowopened( WindowEvent e ) {; 53

54 With regard to abstract methods, Java has one feature not possessed by C++: In a class hierarchy, any method inherited from any of the superclasses can be declared to be abstract, making that particular class in the hierarchy an abstract class. This can be useful when it is necessary to block the inherited definition of a method to one or more derived classes in a class hierarchy. 54

55 Interfaces in Java interface Drawable { public void setcolor( Color c ); public void setposition( double x, double y ); public void draw( DrawWindow dw ); class DrawableRectangle extends Rectangle implements Drawable { private Color c; private double x, y; public DrawableRectangle( double w, double h ) { super( w, h ); public void setcolor( Color c ) { this.c = c; public void setposition( double x, double y ) { this.x = x; this.y = y; public void draw( DrawWindow dw ) { dw.drawrect( x, y, w, h, c ); 55

56 Shape (abstract) /\ /\ / \ / \ Drawable /\ /\ Rectangle Circle / \ / \ /\ /\ / \ / \ \ \ / \ \ / \ \ / \ \ / \ \/ \ / \ \ / \ DrawableRectangle DrawableCircle

57 Shape[] shapes = new Shape[3]; Drawable[] drawables = new Drawable[3]; DrawableCircle dc = new DrawableCircle( 1.1 ); DrawableSquare ds = new DrawableSquare( 2.5 ); DrawableRectangle dr = new DrawableRectangle( 2.3, 4.5 ); shapes[0] = dc; shapes[1] = ds; shapes[2] = dr; drawables[0] = dc; drawables[1] = ds; drawables[2] = dr; 57

58 double total_area = 0; for (int i = 0; i < shapes.length; i++ ) { total_area += shapes[i].area(); drawables[i].setposition( i*10.0, i*10.0 ); // drawables[i].draw( draw_window ); // 58

59 abstract class Shape { abstract protected double area(); abstract protected double circumference(); class Circle extends Shape { protected double r; protected static double PI = ; public Circle( double r ) { this.r = r; public double area() { return PI*r*r; public double circumference() { return 2 * PI * r; class Rectangle extends Shape { double w, h; public Rectangle( double w, double h ) { this.w = w; this.h = h; public double area() { return w * h; public double circumference() { return 2 * (w + h); interface Drawable { public void setcolor( Color c ); public void setposition( double x, double y ); public void draw( DrawWindow dw ); 59

60 class DrawableRectangle extends Rectangle implements Drawable { private Color c; private double x, y; public DrawableRectangle( double w, double h ) { super( w, h ); // Implementations of the methods inherited from the interface: public void setcolor( Color c ) { this.c = c; public void setposition( double x, double y ) { this.x = x; this.y = y; public void draw( DrawWindow dw ) { dw.drawrect( x, y, w, h, c ); class DrawableCircle extends Circle implements Drawable { private Color c; private double x, y; public DrawableCircle( double rad ) { super( rad ); public void setcolor( Color c ) { this.c = c; public void setposition( double x, double y ) { this.x = x; this.y = y; public void draw( DrawWindow dw ) { dw.drawcircle( x, y, r, c ); class Color { int R, G, B; 60

61 class DrawWindow { public DrawWindow() {; public void drawrect( double x, double y, double width, double height, Colo { System.out.println( "Code for drawing a rect needs to be invoked" ); public void drawcircle( double x, double y, double radius, Color col ) { System.out.println( "Code for drawing a circle needs to be invoked" ); class test { public static void main( String[] args ) { Shape[] shapes = new Shape[3]; Drawable[] drawables = new Drawable[3]; DrawableCircle dc = new DrawableCircle( 1.1 ); DrawableRectangle dr1 = new DrawableRectangle( 2.5, 3.5 ); DrawableRectangle dr2 = new DrawableRectangle( 2.3, 4.5 ); shapes[0] = dc; shapes[1] = dr1; shapes[2] = dr2; drawables[0] = dc; drawables[1] = dr1; drawables[2] = dr2; int total_area = 0; DrawWindow dw = new DrawWindow(); 61

62 for (int i = 0; i < shapes.length; i++ ) { total_area += shapes[i].area(); drawables[i].setposition( i*10.0, i*10.0 ); drawables[i].draw( dw ); System.out.println("Total area = " + total_area); // (A) // (B) 62

63 Code for drawing a circle needs to be invoked Code for drawing a rect needs to be invoked Code for drawing a rect needs to be invoked Total area = 21 63

64 Implementing Multiple Interfaces in Java interface Scalable { public Shape scaletransform(); class DrawableScalableRectangle extends Rectangle implements Drawable, Scalable { // the methods of the Drawable and Scalable interfaces // must be implemented here 64

65 Extending Interfaces in Java interface Drawable { public void setcolor( Color c ); public void setposition( double x, double y ); public void draw( DrawWindow dw ); interface DrawScalable extends Drawable { public void drawscaledshape( int scalefactor, DrawWindow dw ); 65

66 class DrawScalableRectangle extends Rectangle implements DrawScala private Color c; private double x, y; public DrawScalableRectangle( double w, double h ) { super( w, // Implementations of the methods inherited from the interface public void setcolor( Color c ) { this.c = c; public void setposition( double x, double y ) { this.x = x; th public void draw( DrawWindow dw ) { dw.drawrect( x, y, w, h, c public void drawscaledshape( int scalefactor, DrawWindow dw ) { dw.drawscaledrect( x, y, w, h, c, scalefactor ); 66

67 abstract class Shape { abstract protected double area(); abstract protected double circumference(); class Circle extends Shape { protected double r; protected static double PI = ; public Circle( double r ) { this.r = r; public double area() { return PI*r*r; public double circumference() { return 2 * PI * r; class Rectangle extends Shape { double w, h; public Rectangle( double w, double h ) { this.w = w; this.h = h; public double area() { return w * h; public double circumference() { return 2 * (w + h); interface Drawable { public void setcolor( Color c ); public void setposition( double x, double y ); public void draw( DrawWindow dw ); 67

68 interface DrawScalable extends Drawable { public void drawscaledshape( int scalefactor, DrawWindow dw ); class DrawScalableRectangle extends Rectangle implements DrawScalable { private Color c; private double x, y; public DrawScalableRectangle( double w, double h ) { super( w, h ); // Implementations of the methods inherited from the interface: public void setcolor( Color c ) { this.c = c; public void setposition( double x, double y ) { this.x = x; this.y = y; public void draw( DrawWindow dw ) { dw.drawrect( x, y, w, h, c ); public void drawscaledshape( int scalefactor, DrawWindow dw ) { dw.drawscaledrect( x, y, w, h, c, scalefactor ); class DrawScalableCircle extends Circle implements DrawScalable { private Color c; private double x, y; public DrawScalableCircle( double rad ) { super( rad ); public void setcolor( Color c ) { this.c = c; public void setposition( double x, double y ) { this.x = x; this.y = y; public void draw( DrawWindow dw ) { dw.drawcircle( x, y, r, c ); public void drawscaledshape( int scalefactor, DrawWindow dw ) { dw.drawscaledcircle( x, y, r, c, scalefactor ); 68

69 class Color { int R, G, B; class DrawWindow { public DrawWindow() {; public void drawrect( double x, double y, double width, double height, Colo { System.out.println( "Code for drawing a rect needs to be invoked" ); public void drawscaledrect( double x, double y, double width, double height, Color col, int scale ) { System.out.println( "Code for drawing a scaled rect needs to be invoked public void drawcircle( double x, double y, double radius, Color col ) { System.out.println( "Code for drawing a circle needs to be invoked" ); public void drawscaledcircle( double x, double y, double radius, Color col, int scale ) { System.out.println( "Code for drawing a scaled circle needs to be invok 69

70 class test { public static void main( String[] args ) { Shape[] shapes = new Shape[3]; DrawScalable[] drawscalables = new DrawScalable[3]; DrawScalableCircle dc = new DrawScalableCircle( 1.1 ); DrawScalableRectangle dr1 = new DrawScalableRectangle( 2.5, 3.5 ); DrawScalableRectangle dr2 = new DrawScalableRectangle( 2.3, 4.5 ); shapes[0] = dc; shapes[1] = dr1; shapes[2] = dr2; drawscalables[0] = dc; drawscalables[1] = dr1; drawscalables[2] = dr2; int total_area = 0; DrawWindow dw = new DrawWindow(); for (int i = 0; i < shapes.length; i++ ) { total_area += shapes[i].area(); drawscalables[i].setposition( i*10.0, i*10.0 ); // (A) drawscalables[i].drawscaledshape( 2, dw ); // System.out.println("Total area = " + total_area); 70

71 Code for drawing a scaled circle needs to be invoked Code for drawing a scaled rect needs to be invoked Code for drawing a scaled rect needs to be invoked Total area = 21 71

72 Constants in Interfaces class A { public static final double PI = ; class test { //... void foo() { double x = A.PI; //... 72

73 interface A { public static final double PI = ; class test implements A { //... void foo() { double x = PI; //... 73

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

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom 8359 Object-oriented Programming with Java, Part 2 Stephen Pipes IBM Hursley Park Labs, United Kingdom Dallas 2003 Intro to Java recap Classes are like user-defined types Objects are like variables of

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

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

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

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

The Notion of a Class and Some Other Key Ideas (contd.) Questions: The Notion of a Class and Some Other Key Ideas (contd.) Questions: 1 1. WHO IS BIGGER? MR. BIGGER OR MR. BIGGER S LITTLE BABY? Which is bigger? A class or a class s little baby (meaning its subclass)?

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

2. The object-oriented paradigm!

2. The object-oriented paradigm! 2. The object-oriented paradigm! Plan for this section:! n Look at things we have to be able to do with a programming language! n Look at Java and how it is done there" Note: I will make a lot of use of

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

Comments are almost like C++

Comments are almost like C++ UMBC CMSC 331 Java Comments are almost like C++ The javadoc program generates HTML API documentation from the javadoc style comments in your code. /* This kind of comment can span multiple lines */ //

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

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

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

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

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

CH. 2 OBJECT-ORIENTED PROGRAMMING

CH. 2 OBJECT-ORIENTED PROGRAMMING CH. 2 OBJECT-ORIENTED PROGRAMMING ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OBJECT-ORIENTED

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

OBJECT ORİENTATİON ENCAPSULATİON

OBJECT ORİENTATİON ENCAPSULATİON OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual

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

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

Polymorphism CSCI 201 Principles of Software Development

Polymorphism CSCI 201 Principles of Software Development Polymorphism CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D. jeffrey.miller@usc.edu Program Outline USC CSCI 201L Polymorphism Based on the inheritance hierarchy, an object with a compile-time

More information

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance. Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit III Inheritance The mechanism that allows us to extend the definition of a class without making

More information

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism. Outline Inheritance Class Extension Overriding Methods Inheritance and Constructors Polymorphism Abstract Classes Interfaces 1 OOP Principles Encapsulation Methods and data are combined in classes Not

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

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

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland, College Park Object Oriented Programming (OOP) OO Principles Abstraction Encapsulation

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

index.pdf January 21,

index.pdf January 21, index.pdf January 21, 2013 1 ITI 1121. Introduction to Computing II Circle Let s complete the implementation of the class Circle. Marcel Turcotte School of Electrical Engineering and Computer Science Version

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

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance (part II) Polymorphism Version of January 21, 2013 Abstract These lecture notes

More information

OOP: Key Concepts 09/28/2001 1

OOP: Key Concepts 09/28/2001 1 OOP: Key Concepts Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring 09/28/2001 1 Overview of Part

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

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value Paytm Programming Sample paper: 1) A copy constructor is called a. when an object is returned by value b. when an object is passed by value as an argument c. when compiler generates a temporary object

More information

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS 162, Lecture 25: Exam II Review. 30 May 2018 CS 162, Lecture 25: Exam II Review 30 May 2018 True or False Pointers to a base class may be assigned the address of a derived class object. In C++ polymorphism is very difficult to achieve unless you

More information

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Object-Oriented Programming (OOP) Fundamental Principles of OOP Object-Oriented Programming (OOP) 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 1 Object-oriented programming is the successor of procedural programming. The problem with procedural programming is

More information

Get Unique study materials from

Get Unique study materials from Downloaded from www.rejinpaul.com VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : IV Section : EEE - 1 & 2 Subject Code

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

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

2. The object-oriented paradigm

2. The object-oriented paradigm 2. The object-oriented paradigm Plan for this section: Look at things we have to be able to do with a programming language Look at Java and how it is done there Note: I will make a lot of use of the fact

More information

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE PART A UNIT I 1. Differentiate object oriented programming from procedure oriented programming. 2. Define abstraction and encapsulation. 3. Differentiate

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

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

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

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

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

Java. Representing Data. Representing data. Primitive data types

Java. Representing Data. Representing data. Primitive data types Computer Science Representing Data Java 02/23/2010 CPSC 449 161 Unless otherwise noted, all artwork and illustrations by either Rob Kremer or Jörg Denzinger (course instructors) Representing data Manipulating

More information

Lecture 5: Inheritance

Lecture 5: Inheritance McGill University Computer Science Department COMP 322 : Introduction to C++ Winter 2009 Lecture 5: Inheritance Sami Zhioua March 11 th, 2009 1 Inheritance Inheritance is a form of software reusability

More information

Object Oriented Java

Object Oriented Java Object Oriented Java I. Object based programming II. Object oriented programing M. Carmen Fernández Panadero Raquel M. Crespo García Contents Polymorphism Dynamic binding Casting.

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

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

SSE2034: System Software Experiment 3 Spring 2016

SSE2034: System Software Experiment 3 Spring 2016 SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong ( jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Object Initialization class Rectangle { private:

More information

Friend Functions, Inheritance

Friend Functions, Inheritance Friend Functions, Inheritance Friend Function Private data member of a class can not be accessed by an object of another class Similarly protected data member function of a class can not be accessed by

More information

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007 ECE 462 Midterm Exam 1 10:30-11:20AM, September 21, 2007 1 Template Classes and the STL Library 1.1 Container Classes Which statement is correct? Answer: B A. An element can be inserted anywhere in a stack.

More information

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College CISC 3115 TY3 C09a: Inheritance Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/20/2018 CUNY Brooklyn College 1 Outline Inheritance Superclass/supertype, subclass/subtype

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Inheritance (continued) Inheritance

Inheritance (continued) Inheritance Objectives Chapter 11 Inheritance and Polymorphism Learn about inheritance Learn about subclasses and superclasses Explore how to override the methods of a superclass Examine how constructors of superclasses

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

Data Structures and Other Objects Using C++

Data Structures and Other Objects Using C++ Inheritance Chapter 14 discuss Derived classes, Inheritance, and Polymorphism Inheritance Basics Inheritance Details Data Structures and Other Objects Using C++ Polymorphism Virtual Functions Inheritance

More information

Making New instances of Classes

Making New instances of Classes Making New instances of Classes NOTE: revised from previous version of Lecture04 New Operator Classes are user defined datatypes in OOP languages How do we make instances of these new datatypes? Using

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

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

COEN244: Polymorphism

COEN244: Polymorphism 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

More information

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2 Review Fall 2017 CISC2200 Yanjun Li 1 Outline Array Pointer Object-Oriented Programming Fall 2017 CISC2200 Yanjun Li 2 1 Computer Fall 2017 CISC2200 Yanjun Li 3 Array Arrays are data structures containing

More information

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods. Inheritance Inheritance is the act of deriving a new class from an existing one. Inheritance allows us to extend the functionality of the object. The new class automatically contains some or all methods

More information

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism http://en.wikipedia.org/wiki/encapsulation_(object-oriented_programming)

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

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

CS1150 Principles of Computer Science Objects and Classes

CS1150 Principles of Computer Science Objects and Classes CS1150 Principles of Computer Science Objects and Classes Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Object-Oriented Thinking Chapters 1-8

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

Chapter 1: Object-Oriented Programming Using C++

Chapter 1: Object-Oriented Programming Using C++ Chapter 1: Object-Oriented Programming Using C++ Objectives Looking ahead in this chapter, we ll consider: Abstract Data Types Encapsulation Inheritance Pointers Polymorphism Data Structures and Algorithms

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

IUE Faculty of Engineering and Computer Sciences Spring Semester

IUE Faculty of Engineering and Computer Sciences Spring Semester IUE Faculty of Engineering and Computer Sciences 2010-2011 Spring Semester CS116 Introduction to Programming II Midterm Exam II (May 11 th, 2011) This exam document has 5 pages and 4 questions. The exam

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Inheritance and Polymorphism Dr. M. G. Abbas Malik Assistant Professor Faculty of Computing and IT (North Jeddah Branch) King Abdulaziz University, Jeddah, KSA mgmalik@kau.edu.sa www.sanlp.org/malik/cpit305/ap.html

More information

OVERRIDING. 7/11/2015 Budditha Hettige 82

OVERRIDING. 7/11/2015 Budditha Hettige 82 OVERRIDING 7/11/2015 (budditha@yahoo.com) 82 What is Overriding Is a language feature Allows a subclass or child class to provide a specific implementation of a method that is already provided by one of

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 12 continue 12.6 Case Study: Payroll System Using Polymorphism This section reexamines the CommissionEmployee- BasePlusCommissionEmployee hierarchy that we explored throughout

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Inheritance Concept Polygon Rectangle Triangle class Polygon{ private: int numvertices; float *xcoord, *ycoord; void set(float *x, float *y, int nv); class Rectangle{

More information

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien PowerPoint Slides Object-Oriented Design Using JAVA by Dale Skrien Chapter 2 Object-oriented Programming Divides the program into a set of communicating objects Encapsulates in an object all the behavior

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

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

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University) Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

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

OO Design with Multiple Inheritance. Questions:

OO Design with Multiple Inheritance. Questions: OO Design with Multiple Inheritance Questions: 1 1. What is one single issue in C++ that you are allowed to become emotional about? 2 2. State a basic tenet of OO programming? 3 3. If the class Assemble

More information

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1 Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

More information

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name

More information

Chapter 6: Inheritance

Chapter 6: Inheritance Chapter 6: Inheritance EECS 1030 moodle.yorku.ca State of an object final int WIDTH = 3; final int HEIGTH = 4; final int WEIGHT = 80; GoldenRectangle rectangle = new GoldenRectangle(WIDTH, HEIGHT, WEIGHT);

More information

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber Chapter 10 Inheritance and Polymorphism Dr. Hikmat Jaber 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the

More information

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.) Inheritance Inheritance (cont.) Object oriented systems allow new classes to be defined in terms of a previously defined class. All variables and methods of the previously defined class, called superclass,

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

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

Function Overloading

Function Overloading Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures

More information