Presentation is loading. Please wait.

Presentation is loading. Please wait.

תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך.

Similar presentations


Presentation on theme: "תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך."— Presentation transcript:

1 תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך

2 Constructors: point Initialization The program starts by activating the initialize member function for p1. #include #include “point.h" int main( ) { point p1: point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp First improvement: automatic initialization without activating the initialize function

3 Constructors: point Initialization class point { public: void initialize(double init_x, double init_y); void shift(double dx, double dy); double get_x() const; double get_y( ) const; private: double x; double y; }; We can provide a normal member function initialize x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

4 Constructors: point Initialization class point { public: point(double init_x, double init_y); void shift(double dx, double dy); double get_x() const; double get_y( ) const; private: double x; double y; }; Or use a constructor that is automatically called x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp -function name same as class name - no return type, even no “void” !

5 Constructors: Implementation void point::initialize(double init_x, double init_y) { x = init_x; y = init_y; } For the most part, the constructor is no different than any other member functions. We only need to replace initialize with point x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

6 Constructors: Implementation point::point(double init_x, double init_y) { x = init_x; y = init_y; } For the most part, the constructor is no different than any other member functions. But there are some special features about constructor... x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp

7 Constructors Constructor is a member function which – the name must be the same as the class name – automatically called whenever a variable of the class is declared – arguments must be given after the variable name (when declared in user file) A way to improve the initialize function – by providing an initialization function that is guaranteed to be called

8 Constructors: point Initialization Automatically called when declared. Parameters after the object names #include #include “point.h" int main( ) { point p1: point p2; p1. initialize(-1.0, 0.8); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp First improvement: automatic initialization without explicitly activating an initialize function

9 Constructors: point Initialization Automatically called when declared. Parameters after the object names #include #include “point.h" int main( ) { point p1(-1.0, 0.8): point p2(0.3, 0.6); x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp First improvement: automatic initialization without explicitly activating an initialize function

10 Default Constructors Automatically called when declared. Parameters after the object names NO parameters after the object name p2 #include #include “point.h" int main( ) { point p1(-1.0, 0.8); point p2; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp Can we want define an object with no parameters, not even a pair of parentheses?

11 Default Constructors class point { public: point(); point(double init_x, double init_y); … private: double x; double y; }; We could provide a second constructor with no parameters x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp Implementation point::point() { x = 0.0; y = 0.0; }

12 Constructors: Function Overloading You may declare as many constructors as you like – one for each different way of initializing an object Each constructor must have a distinct parameter list so that the compiler can tell them part Question: How many default constructors are allowed?

13 Constructors: automatic default constructor What happens if you write a class without any constructors? The compiler automatically creates a simple default constructor – which only calls the default constructors for the member variables that are objects of some other classes Programming Tip :Always provide your own constructors, and better with a default constructor

14 Default arguments A default argument is a value that will be used for an argument –When a programmer does not provide an actual argument when calling a function Default arguments may be listed in the prototype of a function – Syntax: Type_name var_name = default_value

15 Default arguments – rules The default argument is only specified once – in the prototype – not in the implementation No need to specify all the arguments as default but the default must be rightmost in the parameter list In a call, arguments with default may be omitted from the right end. Example of a prototype: int date_check (int year, int month = 1, int date =1); int date_check (int year, int month = 1, int date =1);

16 Default arguments – Examples Prototype: int date_check (int year, int month = 1, int date =1); int date_check (int year, int month = 1, int date =1); Usage in the calling function date_check(2002); // uses default for both month and date date_check(2002, 9); // uses default for date =1 date_check(2002, 9, 5); // does not use defaults

17 Default Constructor revisited A default constructor can be provided by using default arguments Instead of defining two constructors, we can define just one constructor with default arguments for all of its arguments class point { public: point(double init_x=0.0, double init_y =0.0); … };

18 Default Constructor revisited In using the class, we can have three declarations The implementation of the constructor with default arguments is the same as the usual one... point a(-1, 0.8); // uses the usual constructor with // two arguments point b(-1); // uses –1 for the first, // but use default for the second point c; // uses default arguments for both; // default constructor: // no arguments, no parentheses!

19 Value Semantics of a Class Value semantics determines how values are copied from one object to another Consists of two operations in C++ – The assignment operator – The copy constructor

20 Value Semantics: assignment operator Automatic assignment operator – For a new class, C++ normally carries out assignment by simply copying each variable from the object on the right to that on the left –Our new class point can use automatic assignment operator point p1(-1.0, 0.8), p2; p2 = p1; cout << p2.get_x() <<“ “ << p2.get_y();

21 Value Semantics: copy constructor A copy constructor –is a constructor with one parameter whose data type is the same as the constructor’s class –initializes a new object as an exact copy of an existing object Examples: point p1(-1.0, 0.8); point p2 (p1); cout << p2.get_x() <<“ “ << p2.get_y(); point p1(-1.0, 0.8); point p2 = p1; cout << p2.get_x() <<“ “<< p2.get_y();

22 point p2 = p1; versus p2 = p1; – The assignment p2 = p1; merely copies p1 to the already existing object p2 using the assignment operator. –The syntax point p2 = p1; looks like an assignment statement, but actually a declaration that both declares a new object, and calls the copy constructor to initialize p2 as a copy of p1. p2 will be the same if the assignment operator and the copy constructor do the same things Value Semantics: discussion

23 Constructors, etc.– a summary Constructor is a member function – define your own constructors (including a default) – automatic default constructor Value semantics of a class – assignment operators and copy constructor – automatic assignment op and copy constructor

24 Class as type of parameter A class can be used as a function parameter, just like any other data type –Value parameters –Reference parameters –Const reference parameters –In fact you can also have const value parameters, even if this does not make many senses

25 Value parameters How many shifts to move p into the first quad Function implementation: int shifts_needed(point p) { int answer = 0; while ((p.get_x() <0) || (p.get_y()<0)) { p.shift(1,1); answer++; } return answer; } Calling program: point a(-1.5,-2.5); cout << a.get_x() << a.get_y() << endl; cout << shifts_needed(a) << endl; cout << a.get_x() << a.get_y() << endl; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp formal parameter actual argument -1.5, -2.5 3 1.5, 0.5 ? -1.5, -2.5 3 -1.5, -2.5

26 Value parameters A value parameter is declared by writing – type-name parameter-name Any change made to the formal parameter within the body of the function does not change the actual argument from the calling program The formal parameter is implemented as a local variable of the function and the copy constructor is used to initialize the formal parameter as a copy of the actual argument

27 Reference parameters Actually move p into the first quadrant Function implementation (almost the same except &): int shift_to_1st_quad(point& p) { int shifts; while ((p.get_x() <0) || (p.get_y()<0)) { p.shift(1,1); shifts++; } return shifts; } In calling program: point a(-1.5,-2.5); cout << a.get_x() << a.get_y() << endl; cout << shift_to_1st_quad(a) << endl; cout << a.get_x() << a.get_y() << endl; x -2 -1 0 1 2 -2 -1 0 1 2 2 1 0-2yp type_name & para_name NO & ! -1.5, -2.5 3 1.5, 0.5

28 Reference parameters A reference parameter is declared by writing – type-name& parameter-name Any use of the formal parameter within the body of the function will access the actual argument from the calling program; change made to the parameter in the body of the function will alter the argument The formal parameter is merely another name of the argument used in the body of the function!

29 const reference parameters A const reference parameter is declared by writing – const type-name& parameter-name A solution that provides the efficiency of a reference parameter along with the security of a value parameter.

30 Class as return value point middle(const point& p1, const point& p2) { double x_midpoint, y_midpoint; // Compute the x and y midpoints x_midpoint = (p1.get_x( ) + p2.get_x( )) / 2; y_midpoint = (p1.get_y( ) + p2.get_y( )) / 2; // Construct a new point and return it point midpoint(x_midpoint, y_midpoint); return midpoint; }

31 Class as return value The type of a function’s return value may be a class Often the return value will be stored in a local variable of the function (such as midpoint ) –not always (could be in a formal parameter) C++ return statement uses the copy constructor to copy the function’s return value to a temporary location before returning the value to the calling program

32 סיכום – מה ראינו עד עכשיו? הגדרת class – data & member functions מימוש ה -member functions הגדרת constructors ( בנאים ) –C ’ tor שמקבל פרמטרים –Default c ’ tor Copy c ’ tor – מתי נקרא ? כש -class מועבר כפרמטר לפונקציה by value כש -class מוחזר מפונקציה by value – כיצד מממשים copy c ’ tor הזכרנו את אופרטור ההשמה = להעתקה בין מחלקות אך עדיין לא ראינו איך מגדירים אותו


Download ppt "תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך."

Similar presentations


Ads by Google