abstractCopy

abstractCopy - Abstract Classes Wednesday, September 28,...

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

View Full Document Right Arrow Icon
Abstract Classes Wednesday, September 28, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Abstract classes Abstract classes are classes for which objects cannot be constructed They can be derived from What are they good for? Three things 1. Can lend organization to a class hierarchy, Can provide a common base class Wednesday, September 28, 2011
Background image of page 2
Abstract classes 2. Can represent a specialized behavior that when mixed with other classes gives a desired behavior 3. Can help build up an implementation Let’s look at a concrete example to make these concepts clearer. In particular, let’s look at a shape class such as might be used in a drawing program Wednesday, September 28, 2011
Background image of page 3

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

View Full DocumentRight Arrow Icon
A shape class It makes sense to construct a Circle , a Rectangle , etc., but not necessarily a shape It is useful to be able to refer to all shapes with a common class Circle Rectangle Shape . . . Wednesday, September 28, 2011
Background image of page 4
The Shape abstract class It also makes sense for Shape to insists on certain things being computed for any shape class Shape { public: virtual double area( ) = 0; virtual double circumference() = 0; // . . . }; A C++ abstract class, which can also contain functions that contain code and variables Wednesday, September 28, 2011
Background image of page 5

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

View Full DocumentRight Arrow Icon
Can now write code like Shape* shapes[3]; shapes[0] = new Circle( . .. ); shapes[1] = new Rectangle( . shapes[2] = new Rectangle( . double totArea = 0; for (int i=0; i < 3; i++) { totArea += shapes[i] = shapes[i]->area( ); } polymorphism Wednesday, September 28, 2011
Background image of page 6
#include <iostream> using namespace std; class Shape { public: virtual double double( ) = 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=3.14159265358979323846; Wednesday, September 28, 2011
Background image of page 7

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

View Full DocumentRight Arrow Icon
#include <iostream> using namespace std; class Shape { public: virtual double double( ) = 0; virtual double circumference( ) = 0; }; class Rectangle : public Shape { double w, h; public: Rectangle( ) {w=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;} } Wednesday, September 28, 2011
Background image of page 8
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 totArea = 0; for (int i=0; i < 3; i++) totArea += shapes[i]->area( ); cout << "Total area = " << totArea << end; return 0; } Wednesday, September 28, 2011
Background image of page 9

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

View Full DocumentRight Arrow Icon
A Shape abstract class in Java abstract class Shape { public: abstract public double area( ); abstract public double circumference; // . . . }; A Java abstract class -- functions that will not be deFned (i.e. contain code) in this class must also use the abstract keyword Wednesday, September 28, 2011
Background image of page 10
A Shape interface in Java interface Shape { public: abstract public double area( ); abstract public double circumference; // . . . }; A Java interface - interfaces can only contain abstract classes and (non-abstract) constants.
Background image of page 11

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

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

Page1 / 75

abstractCopy - Abstract Classes Wednesday, September 28,...

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

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