Presentation is loading. Please wait.

Presentation is loading. Please wait.

P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++

Similar presentations


Presentation on theme: "P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++"— Presentation transcript:

1 p Improvements - Constructor p Parameter passing Constructors Problem Solving With C++

2 Example: Point Class p This is an ADT to store and manipulate the location of a single point on a two- dimensional plane. XY A (-1, 0.8)

3 Point Class (con’d) p Two modification member functions: p Shifting function p Example: B obtained by shifting point A by 1.3 units along the X axis and by -1.4 units along the Y axis X Y A (-1, 0.8)

4 Point Class (con’d) p Two modification member functions: p Shifting function p Example: B obtained by shifting point A by 1.3 units along the X axis and by -1.4 units along the Y axis X Y A (-1, 0.8) B (0.3, -0.8)

5 Point Class (con’d) p Two modification member functions: (con’d) p Rotating function p Example: C obtained by rotating point A by 90 degree clockwise. X Y A (-1, 0.8)

6 Point Class (con’d) p Two modification member functions: (con’d) p Rotating function p Example: C obtained by rotating point A by 90 degree clockwise. X Y A (-1, 0.8) C (0.8, 1.0)

7 Point Class (con’d) p Two constant member functions : p return X coordinate of the point. double get_x() const; p return Y coordinate of the point double get_y() const;

8 Point Class (con’d) Class Point { public: public: void shift(double x_ammount, double y_ammount); void rotate90(); double get_x() const {return x;} double get_y() const {return y;} private: private: double x; // x coordinate of this point double y; // y coordinate of this point double y; // y coordinate of this point}; Inline functions

9 Improvements: Constructor p A member function that is automatically called to initialize an object when the object is declared

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

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

12 Constructor (con’d) p Two kinds of constructors p Default constructor: constructor with no parameters p Constructor with parameters p Example: Point Class

13 Constructor (con’d) p Example: Point Class Class Point { …... //Constructor Point( ); //or Point(double initial_x, double initial_y); …… };

14 Constructor (con’d) p Example: Point Class p Application Point pointA; Point pointA; Point pointB(5, 10); Point pointB(5, 10);

15 Constructor (con’d) p Example: Point Class p Application Point pointA; Point pointA; Point pointB(5, 10); Point pointB(5, 10); If the constructor has parameters, then the values must be given after the object name.

16 Constructor (con’d) p A good technique: using constructor with default parameters p Example: Point Class Point::Point(double initial_x = 0, Point::Point(double initial_x = 0, double initial_y = 0); double initial_y = 0);

17 Constructor (con’d) p When you declare an object, what happen to these declarations? int main( ) { Point pointA(5, 10); Point pointB(5); Point pointC; …... return 0; };

18 Constructor (con’d) p 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 = 0, initial_y = 0 Initial_x = 0, initial_y = 0 Initial_x = 5 initial_y = 0 Initial_x = 5 initial_y = 0

19 Constructor (con’d) p What if you write a class without constructors?

20 Constructor (con’d) p What if you write a class without constructors? Automatic default constructor will be called


Download ppt "P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++"

Similar presentations


Ads by Google