Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Functions

Similar presentations


Presentation on theme: "C++ Programming Functions"— Presentation transcript:

1 C++ Programming Functions
Declaration and definition of functions Arguments Overloading Optional arguments Constant Functions Operator Functions

2 Function Declaration = specification of the identifier and the type of function The contract that the function must fulfil (Job to be done by the function) Definition = implementation The specifications of the contract (How to do the job) Point.cxx float Point::Distance(Point point) { /// Calculate the distance to a point float x_diff = m_x – point.m_x; float y_diff = m_y – point.m_y; return sqrt(x_diff*x_diff + (x_diff*x_diff); } Function identifier (name) Point.h float Distance(Point point); Return type + Argument = Function type

3 Arguments Passing arguments can be done in several ways: By value:
By reference: By pointer (address): By constant reference: By pointer type constant: Type value Type& value Type* value const Type& value const Type* value

4 Arguments ... The passage of arguments can be done in several ways:
By value The object "point" sent to the function is copied locally to a new object that is then used by the function The original object can not be changed by the function Appropriate for objects of simple types (primary) because the procedure to copy the object can be costly (in time) By constant reference The same object "point" whose reference is passed into the function is used by the function The declaration const guarantees that the object will not be changed by the function Avoids the need to copy the object, it is therefore more appropriate for the more complex types of objects (for example classes) float Distance(Point point); void Handle(int number); float Distance(const Point& point); void Handle(const int& number);

5 ... Arguments If the function is designed to change the subject:
By reference The same object "point" whose reference is sent to the function is then used by the function, and it may be modified By address (pointer) The same object "point" whose pointer is sent to function is then used by the function, and it may be modified float Distance(Point& point); void Handle(int& number); float Distance(Point* point); void Handle(int* number);

6 Arguments Passing arguments can be done in several ways: By value:
By reference: By pointer (address): By constant reference: By pointer type constant: Object can be modified: NO YES Type value Type& value Type* value const Type& value const Type* value

7 Overloading Several functions with the same name can coexist if the argument list differs: Functions can not differ only by their return type Usage The function selection is done by the compiler class Point { float Distance(Point point); // Distance from a point float Distance(Line line); // Distance from a line float Distance(Point p1, Point p2); // Distance from a line // defined by two points p1, p2 double Distance(Line line); // Distance from a line } Point point(3,4); Point centre(0,0); Line line(Point(1,2), Point(3,3)); std::cout << point.Distance(centre) << std::endl; std::cout << point.Distance(ligne) << std::endl;

8 Optional Arguments ... It is possible to give an argument its default value: The default arguments cannot introduce ambiguity - the default constructor (without arguments) is already declared through the constructor with default arguments Usage: class Point { Point( float x = 0, float y = 0 ); Point(); float Distance( Point point = Point(0, 0) ); } Point point(3,4); std::cout << point.Distance() << std::endl; Point center; std::cout << point.Distance(center) << std::endl;

9 ... Optional Arguments The arguments with default values must always be placed to the right of the arguments with no default class Point { Point( float x = 0, float y = 0 ); Point( float x, float y = 0 ); Point( float x = 0, float y ); }

10 Constant Functions The declaration of member functions of the class as const guarantees that the function will not change the data members of the class Functions that should not change the state of the class: Functions to access the state of the object Any other situations as chosen by the programmer class Point { public: // Functions to construct the object Point(); Point(float x, float y); // Functions to handle the object float Distance(Point point) const; // Functions to access the state of the object float GetX() const; float GetY() const; private: // Data members float m_x; float m_y; }; Our definition of class Point after applying const where appropriate

11 Operator Functions We can look at the operator as a shortcut to a function call: c = a + b; seems clearer that c = Add (a, b); In C++, operators are specified (declared) and implemented (defined) as functions, but used as operators Declaration: Usage: Definition: In C++, the programmer can redefine most operators, but not all (eg. operator . or operator ::) Point operator + (const Point& p1, const Point& p2); Point operator + (const Point& p1, const Point& p2) { return Point ( p1.GetX() + p2.GetX(), p1.GetY() + p2.GetY() ); } Point p1(3,4); Point p2(1,5); Point p3 = p1 + p2; std::cout << p3 << std::endl;

12 I/O Operators The operators <<,>> can send an object into a stream of input or output data Declaration: Implementation Usage: std::ostream& operator << (std::ostream& s, const Point& point); std::ostream& operator << (std::ostream& s, const Point& point) { s << "Point: " << point.GetX() << “, “ << point.GetY(); return s; } Point p5(10, 15); std::cout << p5 << std::endl;


Download ppt "C++ Programming Functions"

Similar presentations


Ads by Google