Improvements - Constructor - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Improvements - Constructor

Description:

Example: C obtained by rotating point A by 90 degree clockwise. X. Y. A (-1, 0.8) Point Class (con'd) Two modification member functions: (con'd) Rotating function ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 21
Provided by: haimen
Category:

less

Transcript and Presenter's Notes

Title: Improvements - Constructor


1
Constructors
  • Improvements - Constructor
  • Parameter passing

Problem Solving With C
2
Example Point Class
  • This is an ADT to store and manipulate the
    location of a single point on a two-dimensional
    plane.

3
Point Class (cond)
  • Two modification member functions
  • Shifting function
  • Example B obtained by shifting point A by 1.3
    units along the X axis and by -1.4 units along
    the Y axis

Y
A (-1, 0.8)
X
4
Point Class (cond)
  • Two modification member functions
  • Shifting function
  • Example B obtained by shifting point A by 1.3
    units along the X axis and by -1.4 units along
    the Y axis

Y
A (-1, 0.8)
X
B (0.3, -0.8)
5
Point Class (cond)
  • Two modification member functions (cond)
  • Rotating function
  • Example C obtained by rotating point A by 90
    degree clockwise.

Y
A (-1, 0.8)
X
6
Point Class (cond)
  • Two modification member functions (cond)
  • Rotating function
  • Example C obtained by rotating point A by 90
    degree clockwise.

Y
C (0.8, 1.0)
A (-1, 0.8)
X
7
Point Class (cond)
  • Two constant member functions
  • return X coordinate of the point. double
    get_x() const
  • return Y coordinate of the point double
    get_y() const

8
Point Class (cond)
  • Class Point
  • public
  • void shift(double x_ammount, double y_ammount)
  • void rotate90()
  • double get_x() const return x
  • double get_y() const return y
  • private
  • double x // x coordinate of this point
  • double y // y coordinate of this point

Inline functions
9
Improvements Constructor
  • A member function that is automatically called to
    initialize an object when the object is declared

10
Improvements Constructor
  • A member function that is automatically called to
    initialize an object when the object is declared
  • The name of the constructor must be the same as
    the name of the class

11
Improvements Constructor
  • A member function that is automatically called to
    initialize an object when the object is declared
  • The name of the constructor must be the same as
    the name of the class
  • It does not have any return value

12
Constructor (cond)
  • Two kinds of constructors
  • Default constructor constructor with no
    parameters
  • Constructor with parameters
  • Example Point Class

13
Constructor (cond)
  • Example Point Class

Class Point ... //Constructor Point( )
//or Point(double initial_x, double initial_y)

14
Constructor (cond)
  • Example Point Class
  • Application
  • Point pointA
  • Point pointB(5, 10)

15
Constructor (cond)
  • Example Point Class
  • Application
  • Point pointA
  • Point pointB(5, 10)

If the constructor has parameters, then the
values must be given after the object name.
16
Constructor (cond)
  • A good technique using constructor with default
    parameters
  • Example Point Class
  • PointPoint(double initial_x 0,
  • double initial_y 0)

17
Constructor (cond)
int main( ) Point pointA(5, 10) Point
pointB(5) Point pointC ... return
0
  • When you declare an object, what happen to these
    declarations?

18
Constructor (cond)
  • When you declare an object, what happen to these
    declarations?

void main() Point pointA(5, 10) Point
pointB(5) Point pointC ...
Initial_x 5, initial_y 10
Initial_x 5 initial_y 0
Initial_x 0, initial_y 0
19
Constructor (cond)
  • What if you write a class without constructors?

20
Constructor (cond)
  • What if you write a class without constructors?
  • Automatic default constructor will be called
Write a Comment
User Comments (0)
About PowerShow.com