FUNCTIONS WITH DEFAULT ARGUMENTS

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Introduction to C Programming
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Design The goal is to design a modular solution, using the techniques of: Decomposition Abstraction Encapsulation In Object Oriented Programming this is.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
C++ fundamentals.
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.
1 CSC241: Object Oriented Programming Lecture No 07.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Data Structure Dr. Mohamed Khafagy. Abstraction.Abstraction is a technique in which we construct a model of an entity based upon its essential Characteristics.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 CSC241: Object Oriented Programming Lecture No 25.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Overview of C++ Polymorphism
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Chapter # 2 Part 2 Programs And data
Chapter VII: Arrays.
Functions + Overloading + Scope
Programming with ANSI C ++
C++ Templates.
Abstract Data Types Programmer-created data types that specify
FUNCTIONS In C++.
Inheritance & Polymorphism
Review: Two Programming Paradigms
Lecture 2 of Computer Science II
Templates.
The dirty secrets of objects
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 11 C Parameters Richard Gesick.
Classes and Objects Encapsulation
نوع داده هاي انتزاعي Abstract Data Types
Building Java Programs
Andy Wang Data Structures, Algorithms, and Generic Programming
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Chapter # 2 Part 2 Programs And data
Introduction to Data Structure
Overview of C++ Polymorphism
Java Programming Language
Building Java Programs
Data Structures and ADTs
Oriented Design and Abstract Data Type
Lists CMSC 202, Version 4/02.
Templates CMSC 202, Version 4/02.
Presentation transcript:

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

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. VS. Specification remains the same….Implementation is totally different ALSO: smart phones have additional interfaces

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

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)

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.

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

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

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

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)

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

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

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

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

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)

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

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

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)

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

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

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

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

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 }