Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS WITH DEFAULT ARGUMENTS

Similar presentations


Presentation on theme: "FUNCTIONS WITH DEFAULT ARGUMENTS"— Presentation transcript:

1 FUNCTIONS WITH DEFAULT ARGUMENTS
REVIEW OF: DATA TYPE ABSTRACT DATA TYPE WHY C++ CLASSES FUNCTION OVERLOADING TEMPLATES FUNCTIONS WITH DEFAULT ARGUMENTS

2 DATA TYPES VS. ABSTRACT DATA TYPE VS. DATA STRUCTURES
Data type: Collection of data along with corresponding set of operations….everything with well defined purpose Abstract Data type can only be accessed by a program via the interface Interface is kept completely separate from its implementation Changing the implementation doesn’t change the program Data structures: Actual implementation of an abstract data type

3 ABSTRACT DATA TYPE VS. VS.
Specification remains the same….Implementation is totally different ALSO: smart phones have additional interfaces

4 DATA TYPES VS. DATA STRUCTURES
Data type: Collection of data along with corresponding set of operations….everything with well defined purpose Data structures: Actual implementation of an abstract data type

5 ABSTRACTION All operations take place through the buttons on the TV…the circuitry is not visible to us ( so we can’t see the implementation part) We don’t know HOW the TV works But we can look at the instructions manual and find out WHAT a TV can do. The manual doesn’t tell us HOW it does it…It SPECIFIES what a TV can do Encapsulation: Internal representation is HIDDEN from us Data abstraction: Separation between specification and implementation (distinction between WHAT & HOW)

6 SPECIFICATION & INTERFACE
Can only operate the TV through the buttons or the remote…this is called the INTERFACE The manuals tell us which function a button will perform… This is the SPECIFICATION We now have a new high tech TV … better picture quality, less power consumption etc etc …. the interface (on off buttons etc) and specifications (what on off buttons do) are the same but internal working is changed….Of course it will have additions to the interface like extra channels etc.

7 DATA ABSTRACTION Data abstraction: Technique in which we construct a model of an entity based on its important characteristics and totally ignore the unimportant details Data abstraction: Separation between specification of a data object and its implementation A GOOD QUESTION…. What’s the benefit of data abstraction????? We can handle complex systems like this System is more manageable System is more understandable

8 ABSTRACTION VS. IMPLEMENTATION
int x = 15 Implementation: a memory location with 15 stored as a binary number, i.e., 0111 int TwoDMatrix[5][2] How is this implemented??? What is this from a user’s point of view??? An algorithm is an abstract idea and a program is its implementation

9 For example: int is a data type
Objects: {0,+1,-1,…} Operators: {+,-,*,/} Objects Operations ABSTRACT DATA TYPE Abstract idea behind a data type without getting jumbled up in its implementation details ADT is defined only in terms of operations that are defined on a data type and the mathematical constraints on the effects of those operations An abstract data type specifies the values of the type, but not how those values are represented as collections of bits, An ADT specifies operations on those values in terms of their inputs, outputs, and effects rather than as particular algorithms or program code. An ADT is accessed via the interface ADT Interface Implementation Interface Program

10 ABSTRACT DATA TYPE Abstract Data type can only be accessed by a program via the interface Interface is kept completely separate from its implementation Changing the implementation doesn’t change the program (provided the interface has not changed)

11 EXAMPLE ADT: Natural number
Object: ordered subset of integers, starting at zero onwards OPERATIONS Zero : Set the number to zero IsZero : check if the number is zero Add(x,y) : (x,y are two natural numbers): return x+y Divide(x,y) : x, y are two natural numbers, return x/y An error will occur if y is zero The above is an ADT … how you implement it is entirely up to you. You can represent the number using a ‘float’, ‘short’ or ‘int’…but these are internal details hidden to the user of the ADT

12 C++ EXAMPLE WITH NATURAL NUMBERS
class NaturalNumber { public: NaturalNumber(); NaturalNumber Add(NaturalNumber &x,NaturalNumber &y); void Zero(); NaturalNumber &operator = (int x); NaturalNumber &operator = (float x); private: unsigned int number; }; void main() { NaturalNumber a,b,c; b=11; a = 10; c = a.Add(a,b); } We don’t care about how the implementation is done …as long as its correct

13 ANOTHER EXAMPLE Class Student { char Name[10] int Age }
Student has name and age User perspective System view: 14 bytes or 112 bits

14 C++ Classes A class in C++ helps us represent and implement an ADT
Has two things….data and methods (called members of the class) Methods are operations/functions that can be performed on the data Types of access for a class…public, private, protected Public data members and methods can be accessed by others Private data members and methods cannot be accessed by others Protected data members and methods can only be accessed by the sub-classes of that class

15 HOW TO ACHIEVE ENCAPSULATION VIA C++ CLASS???
Declare all data members to be private/protected…users of the class cannot access the representation of data members Keep all the methods of the interface public……(all methods not part of the interface are kept private/protected)

16 FUNCTION OVERLOADING Functions with the same name but different set of parameters, e.g., float maximum(float a,float b) { float max = a; if (a<b) max = b; return max; } int maximum(int a,int b) int max = a; int maximum(int *a,const int size) { int max = a[0]; for (int i=0;i<size;++i) if (a[i] > max) max = a[i]; } return max; Which function is invoked???….Depends upon the parameters passed by the caller

17 FUNCTION TEMPLATES…GENERIC PROGRAMMING
No need to write the code for so many functions… template <class Type> Type largest(Type a,Type b) { Type max = a; if (a<b) max = b; return max; } template <class Type> Type largest(Type *a,int size) { Type max = a[0]; for (int i=0;i<size;++i) { if (a[i] > max) max = a[i]; } return max; void main3() { double arrDouble[] = {9.8,7.6,3.2,10.5}; int m = largest(4,5); double m1 = largest(4.5,7.6); m1 = largest(arrDouble,4); }

18 CLASS TEMPLATES (Parameterized types)
Based on the parameter type, a class is generated template <class type> class ArrayOfElements { public: ArrayOfElements(); void Allocate(int length); void Add(type element); private: int ActualSize; int AllocatedSize; type *arr; }; ArrayOfElements<type>::ArrayOfElements() ActualSize = 0; AllocatedSize = 0; } template <class type> void ArrayOfElements<type>::Add(type element) { //write code here } void ArrayOfElements<type>::Allocate(int size)

19 void main3() { ArrayOfElements<int> intArray; ArrayOfElements<float> floatArray; intArray.Allocate(10); intArray.Add(5); floatArray.Allocate(20); floatArray.Add(10.5); }

20 ANOTHER EXAMPLE WITH TEMPLATES
We can have any number of parameters we like in the two angular brackets< > template <typename type,const int x> class Matrix { public: void Initialize(){for (int i=0;i<x;++i) arr[i][i]=0;}; private: type arr[x][x]; }; In the user function void UseMatrix() { Matrix <int,10> M; M.Initialize(); } NOTE: You can also use “typename” instead of “class” for specifying template parameters

21 FUNCTIONS WITH DEFAULT ARGUMENTS
void InitializeArray(int *arr,int size = 10,int value =0) { int i; for (i =0;i<size;++i) arr[i] = value; } void CallerFunction() { int arrExample[10] = {0}; InitializeArray(arrExample); InitializeArray(arrExample,5); InitializeArray(arrExample,5,1); }

22 METHODS WITH DEFAULT ARGUMENTS
class point { public: point(int x,int y); ~point(); int Getx() const{return x;} int Gety() const{return y;} void Setx(int a=0){x = a;} void Sety(int a=0){y = a;} private: int x, y; };

23 METHODS WITH DEFAULT ARGUMENTS…another example
class point { public: point(int x,int y); ~point(); int Getx() const{return x;} int Gety() const{return y;} void Setx(int a=0){x = a;} void Sety(int a=0){y = a;} void SetColor(int red=255,int green=255,int blue=255); private: int x, y; }; class triangle point *p; void point::SetColor(int red,int green, int blue) { //....do something here }


Download ppt "FUNCTIONS WITH DEFAULT ARGUMENTS"

Similar presentations


Ads by Google