C++ Programming Functions

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Operator overloading redefine the operations of operators
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב ' Classes – - המשך.
C++ data types. Structs vs. Classes C++ Classes.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
1 CSE 303 Lecture 21 Classes and Objects in C++ slides created by Marty Stepp
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Object Oriented Programming (OOP) Lecture No. 10.
Copyright  Hannu Laine C++-programming Part 9 Hannu Laine.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 2 ADT and C++ Classes (I) Instructor: Zhigang Zhu Department of Computer Science City.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 C++ Programming Classes ● Class definition and implementation ● Constructors, data members ● Member Functions ● Structuring C + + code.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Topic: Classes and Objects
User-Written Functions
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Review What is an object? What is a class?
Programming with ANSI C ++
FUNCTIONS In C++.
A First C++ Class – a Circle
C++ Programming Functions
Constructor & Destructor
System Programming Practical Session 8 C++ classes.
Class: Special Topics Copy Constructors Static members Friends this
CSC212 Data Structure - Section EF
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
This.
Initialization List.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Constructors and Destructors
Reference Parameters.
Operator overloading Dr. Bhargavi Goswami
References, const and classes
Constructors and Destructors
C++ Constructor Insanity CSE 333 Summer 2018
Chapter 9 - Object-Oriented Programming: Inheritance
Guidelines for Writing Functions
C++ Constructor Insanity CSE 333 Autumn 2018
C++ Constructor Insanity CSE 333 Winter 2019
CPS120: Introduction to Computer Science
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Constructors & Destructors
Object Oriented Programming (OOP) Lecture No. 10
This.
SPL – PS3 C++ Classes.
Presentation transcript:

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

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

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

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);

... 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);

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

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;

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;

... 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 ); }

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

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;

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;