Shape.h - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Shape.h

Description:

Shape.h // SHAPE.H // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape {public: virtual float area() const { return 0.0; } – PowerPoint PPT presentation

Number of Views:8
Avg rating:3.0/5.0
Slides: 10
Provided by: jmv74
Category:
Tags: area | shape | volume

less

Transcript and Presenter's Notes

Title: Shape.h


1
Shape.h
  • // SHAPE.H
  • // Definition of abstract base class Shape
  • ifndef SHAPE_H
  • define SHAPE_H
  • class Shape
  • public
  • virtual float area() const return 0.0
  • virtual float volume() const return 0.0
  • virtual void printShapeName() const 0 //
    pure virtual
  • virtual void print() const 0 //
    pure virtual
  • endif

Shape
Point
Circle
Cylinder
2
Topics
  • Inheritance
  • Syntax.
  • What does it mean to inherit.
  • Types (public, private, protected).
  • Overriding member functions (virtual keyword).
  • Operator overloading.
  • Friend functions.
  • Constructors can call parent constructors.
  • Destructors are called in order of child to
    parent.

3
Point1.h
  • // POINT1.H
  • // Definition of class Point
  • ifndef POINT1_H
  • define POINT1_H
  • include ltiostream.hgt
  • include "shape.h"
  • class Point public Shape
  • friend ostream operatorltlt(ostream , const
    Point )
  • public
  • Point(float 0, float 0) // default
    constructor
  • void setPoint(float, float)
  • float getX() const return x
  • float getY() const return y
  • virtual void printShapeName() const cout ltlt
    "Point "
  • virtual void print() const
  • private
  • float x, y // x and y coordinates of Point

4
Point1.cpp
  • // POINT1.CPP
  • // Member function definitions for class Point
  • include ltiostream.hgt
  • include "point1.h"
  • PointPoint(float a, float b)
  • setPoint(a, b)
  • void PointsetPoint(float a, float b)
  • x a
  • y b
  • void Pointprint() const cout ltlt '' ltlt x ltlt
    ", " ltlt y ltlt ''
  • ostream operatorltlt(ostream output, const Point
    p)
  • p.print() // call print to output the
    object

5
Circle.h
  • // CIRCLE1.H
  • // Definition of class Circle
  • ifndef CIRCLE1_H
  • define CIRCLE1_H
  • include "point1.h"
  • class Circle public Point
  • friend ostream operatorltlt(ostream , const
    Circle )
  • public
  • // default constructor
  • Circle(float r 0.0, float x 0.0, float y
    0.0)
  • void setRadius(float)
  • float getRadius() const
  • virtual float area() const
  • virtual void printShapeName() const cout ltlt
    "Circle "
  • virtual void print() const
  • private
  • float radius // radius of Circle

6
Circle.cpp
  • // CIRCLE1.CPP
  • // Member function definitions for class Circle
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • include "circle1.h"
  • CircleCircle(float r, float a, float b)
  • Point(a, b) // call base-class constructor
  • radius r gt 0 ? r 0
  • void CirclesetRadius(float r) radius r gt 0
    ? r 0
  • float CirclegetRadius() const return radius
  • float Circlearea() const return 3.14159
    radius radius
  • void Circleprint() const
  • cout ltlt '' ltlt getX() ltlt ", " ltlt getY()
  • ltlt " Radius" ltlt setiosflags(iosshowpo
    int)

7
Cylindr1.h
  • // CYLINDR1.H
  • // Definition of class Cylinder
  • ifndef CYLINDR1_H
  • define CYLINDR1_H
  • include "circle1.h"
  • class Cylinder public Circle
  • friend ostream operatorltlt(ostream , const
    Cylinder )
  • public
  • // default constructor
  • Cylinder(float h 0.0, float r 0.0,
  • float x 0.0, float y 0.0)
  • void setHeight(float)
  • virtual float area() const
  • virtual float volume() const
  • virtual void printShapeName() const cout ltlt
    "Cylinder "
  • virtual void print() const
  • private

8
Cylindr1.cpp
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • include "cylindr1.h"
  • CylinderCylinder(float h, float r, float x,
    float y)
  • Circle(r, x, y) // call base-class
    constructor
  • height h gt 0 ? h 0
  • void CylindersetHeight(float h) height h gt
    0 ? h 0
  • float Cylinderarea() const // surface area of
    Cylinder
  • return 2 Circlearea()
  • 2 3.14159 CirclegetRadius()
    height
  • float Cylindervolume() const return
    Circlearea() height
  • void Cylinderprint() const
  • cout ltlt '' ltlt getX() ltlt ", " ltlt getY()
  • ltlt " Radius" ltlt setiosflags(iosshowpo
    int)

9
Main
  • main()
  • Point point(7, 11) // create
    a Point
  • Circle circle(3.5, 22, 8) // create
    a Circle
  • Cylinder cylinder(10, 3.3, 10, 10) // create
    a Cylinder
  • point.printShapeName() // static binding
  • cout ltlt point ltlt endl
  • circle.printShapeName() // static binding
  • cout ltlt circle ltlt endl
  • cylinder.printShapeName() // static binding
  • cout ltlt cylinder ltlt "\n\n"
  • cout ltlt setiosflags(iosshowpoint) ltlt
    setprecision(2)
  • Shape arrayOfShapes3 // array of
    base-class pointers
  • arrayOfShapes0 point
  • arrayOfShapes1 circle
  • arrayOfShapes2 cylinder
  • for (int i 0 i lt 3 i)
Write a Comment
User Comments (0)
About PowerShow.com