const double PI = 3.14159; - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

const double PI = 3.14159;

Description:

Overloaded operator has multiple meanings. Operator with more than one meaning ... define new operators by overloading symbols that are not already operators in C ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 17
Provided by: BlairT6
Category:

less

Transcript and Presenter's Notes

Title: const double PI = 3.14159;


1
  • const double PI 3.14159
  • class Sphere
  • public
  • Sphere()
  • Sphere(double initialRadius)
  • void SetRadius(double newRadius)
  • double GetRadius() const
  • double GetDiameter() const
  • double GetCircumference()
  • double GetArea() const
  • double GetVolume() const
  • void DisplayStatistics() const
  • private
  • double theRadius

2
Scope resolution operator
  • Sphere
  • Precede each member function name
  • Distinguishes it from any other function

3
Default Constructor
  • SphereSphere() theRadius (1.0)
  • Sphere unitSphere //invokes the default
    constructor

4
Constructor
  • SphereSphere() (double initialRadius)
    theRadius (initialRadius)
  • Sphere mySphere(5.1)
  • Invokes above constructor with the value 5.1

5
Initializer
  • Data member name followed by its initial value
    enclosed in parentheses
  • SphereSphere() theRadius(1.0)
  • Multiple initializers separated by commas
  • Initializer can only be used with constructors

6
Constructor defaults
  • If you omit all constructors-
  • Compiler-generated Default Constructor
  • No arguments
  • If you define a constructor with arguments, but
    no default
  • No default constructor
  • Cannot do
  • Sphere defaultSphere

7
sphere.cpp
  • SphereSphere() theRadius (1.0)
  • SphereSphere(double initialRadius)
  • if (initialRadius gt 0)
  • theRadius initialRadius
  • else
  • theRadius 1.0
  • void SphereSetRadius(double newRadius)
  • if (newRadius gt 0)
  • theRadius newRadius
  • else
  • theRadius 1.0

8
  • double SphereGetRadius() const
  • return theRadius
  • double SphereGetDiameter() const
  • return 2.0 theRadius
  • double SphereGetCircumference() const
  • return PI 2.0 theRadius
  • double SphereGetArea() const
  • return 4 PI theRadius theRadius
  • double SphereGetVolume() const
  • double radiusCubed theRadius theRadius
    theRadius

9
  • bool SphereEqual(Sphere otherSphere)
  • return (radius otherSphere.radius)
  • void SphereDisplayStatistics() const
  • cout ltlt "\nRadius " ltlt GetRadius()
  • ltlt "\nDiameter " ltlt GetDiameter()
  • ltlt "\nCircumference " ltlt
    GetCircumference()
  • ltlt "\nArea " ltlt GetArea()
  • ltlt "\nVolume " ltlt GetVolume()
  • ltlt endl

10
client
  • include ltiostream.hgt
  • include "Sphere.h"
  • int main()
  • Sphere unitSphere
  • Sphere mySphere(5.1)
  • unitSphere.DisplayStatistics()
  • mySphere.setRadius(4.2)
  • cout ltlt mySphere.GetDiameter() ltlt endl
  • if (unitSphere.Equal(mySphere))
  • cout ltlt "Spheres are equal" ltlt endl
  • return 0

11
Friend of a class
  • class Sphere
  • friend bool Equal(Sphere sphere1, Sphere
    sphere2)
  • //
  • bool Equal( / in / Sphere firstSphere,
  • / in / Sphere secondSphere)
  • return (firstSphere.radius
    secondSphere.radius)

12
Friend
  • Defined outside the scope of a class
  • Nonmember function
  • Has access to the private data members
  • Friend appears only in prototype
  • Allows more traditional syntax
  • Equal(sphere1,sphere2)
  • must use dot notation within implementation of
    Equal

13
Operator Overloading
  • Overloaded operator has multiple meanings
  • Operator with more than one meaning
  • integer addition, float addition
  • 2 3 //integer addition
  • 2.0 3.0 //float addition
  • C class instance only allowed assignment() and
    member selection(.)
  • operator function - allows built-in operators to
    apply to class instances
  • operator ltsymbolgt

14
Guidelines for Overloading Operators
  • You can overload any operator except
  • . . ? sizeof
  • You cannot define new operators by overloading
    symbols that are not already operators in C
  • You cannot change the standard precedence of a
    C operator or the number of its operands
  • At least one operand of an overloaded operator
    must be an instance of a class
  • A typical class should overload at least the
    assignment operator(), the equality( and !)
    and the relationsl(lt,lt,gt,gt) operators.
  • You cannot change the number of arguments for an
    overloaded method.
  • Model conventional use of operator

15
  • class Sphere
  • public //can be accessed by client
  • ...
  • bool operator (Sphere otherSphere) const
  • private //can be accessed by member functions
  • double theRadius //member variables
  • if (sphere1.Equal(sphere2))//using a member
    function
  • if (Equal(sphere1,sphere2) //using a friend
    function
  • if (sphere1sphere2) //using an operator
    function

16
I/O
  • istream cin //instance of class
  • ostream cout
  • cin.get(someChar) //invoke function
  • cin.ignore(200,'\n')
  • ifstream dataFile
  • dataFile.open("input.dat")
  • ltlt and gtgt are overloaded
Write a Comment
User Comments (0)
About PowerShow.com