REVIEW OF DATA TYPE ABSTRACT DATA TYPE WHY
REVIEW OF: ü DATA TYPE ü ABSTRACT DATA TYPE ü WHY C++ CLASSES ü FUNCTION OVERLOADING ü TEMPLATES ü FUNCTIONS WITH DEFAULT ARGUMENTS CS 201 Data structures Mehreen Saeed Slid No. 1
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
ABSTRACT DATA TYPE VS. Specification remains the same…. Implementation is totally different ALSO: smart phones have additional interfaces Slid No. 3
WHY C++ CLASSES? ? ? • Ideal way to 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 Slid No. 4
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; if (a<b) max = b; return max; } 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 Slid No. 5
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 main 3() { double arr. Double[] = {9. 8, 7. 6, 3. 2, 10. 5}; int m = largest(4, 5); double m 1 = largest(4. 5, 7. 6); m 1 = largest(arr. Double, 4); } Slid No. 6
CLASS TEMPLATES (Parameterized types) template <class type> class Array. Of. Elements { public: Array. Of. Elements(); void Allocate(int length); void Add(type element); private: int Actual. Size; int Allocated. Size; type *arr; }; template <class type> Array. Of. Elements<type>: : Array. Of. Elements() { Actual. Size = 0; Allocated. Size = 0; } Based on the parameter type, a class is generated template <class type> void Array. Of. Elements<type>: : Add(type element) { //write code here } template <class type> void Array. Of. Elements<type>: : Allocate(int size) { //write code here } Slid No. 7
void main 3() { Array. Of. Elements<int> int. Array; Array. Of. Elements<float> float. Array; int. Array. Allocate(10); int. Array. Add(5); float. Array. Allocate(20); float. Array. Add(10. 5); } Slid No. 8
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 Use. Matrix() { Matrix <int, 10> M; M. Initialize(); } NOTE: You can also use “typename” instead of “class” for specifying template parameters Slid No. 9
FUNCTIONS WITH DEFAULT ARGUMENTS void Initialize. Array(int *arr, int size = 10, int value =0) { int i; for (i =0; i<size; ++i) arr[i] = value; } void Caller. Function() { int arr. Example[10] = {0}; Initialize. Array(arr. Example); Initialize. Array(arr. Example, 5, 1); } Slid No. 10
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; }; Slid No. 11
class point METHODS WITH DEFAULT { ARGUMENTS…another public: example 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 Set. Color(int red=255, int green=255, int blue=255); private: int x, y; }; class triangle { private: point *p; }; void point: : Set. Color(int red, int green, int blue) { //. . do something here } Slid No. 12
- Slides: 12