L25 - polymorphism

L25 - polymorphism - Remember - Base Classes, and Derived...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Remember -- Base Classes, and Derived Classes Even though a derived class object (like a SuperPlus account, for example) is-a base class object (i.e. a Checking account), the derived class type and the base class type are different types •W i t h public inheritance, derived class objects can be treated as base class objects – the derived class has the full set of base class data members, after all It is not true, though, that base class objects can be treated as derived class objects – the non-corresponding derived class data members would be undefined (unless care were taken to handle the situation) – Look at the structure of Checking type objects! This means that assigning a derived class object to an
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pointers, Base Classes, and Derived Classes •W i t h public inheritance, a pointer (or tracking handle) to a derived class object can be implicitly converted to a pointer (or tracking handle) to a base-class object – why? because a derived class object is a base class object, too There are four ways of mixing and matching base class pointers (tracking handles) and derived class pointers (tracking handles) with base class objects and derived class objects: – base-class object base class pointer – derived-class object derived class pointer – derived-class object
Background image of page 2
Pointers, Base Classes, and Derived Classes (cont.) Referring to a base class object with a base class pointer or tracking handle is fine Referring to a derived class object with a derived class pointer or tracking handle is fine Referring to a derived class object with a base class pointer or tracking handle is safe – the derived class object is-a base class thing, too – if you try to refer to derived-class-only members through the base class pointer, a syntax error occurs Referring to a base class object with a derived class pointer or tracking handle is an error – the derived class pointer has to be cast to a base class pointer or tracking handle first
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Some Examples (class definitions) class point { public: point(); point (double x, double y); point (point &p); point operator = (point p); void setPoint(double, double); double get_x(); double get_y(); protected: double x, y; }; class circle : public point { public: circle (double r = 0.0, int x = 0, int y = 0); void setRadius(double); double getRadius(); double area(); protected: double radius; };
Background image of page 4
Examples (point class implementation) #include "stdafx.h" #include "PointCircle.h" point::point(){ x = -1.0; y = -1.0;} point::point (double xval, double yval) // overloaded constructor { setPoint (xval, yval);} void point::setPoint(double xval, double yval){ x = xval; y = yval; } double point::get_x(){return x;} double point::get_y(){ return y;} // copy constructor double xval = p.get_x();
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course EE 361 taught by Professor Conry during the Spring '08 term at Clarkson University .

Page1 / 31

L25 - polymorphism - Remember - Base Classes, and Derived...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online