CS 202 Computer Science II Lab Fall 2009 October 22.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
User Defined Functions
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++ Karoly Bosa RISC SS2000.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Lecture 8. MIPS Instructions #4 – Branch Instructions #2
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
Functions Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences National Cheng.
CS 202 Computer Science II Lab Fall 2009 September 17.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
CS 202 Computer Science II Lab Fall 2009 September 10.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CS 202 Computer Science II Lab Fall 2009 October 1.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Templates.
C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions. Suppose,
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
TK1924 Program Design & Problem Solving Session 2011/2012
Functions + Overloading + Scope
Object-Oriented Design (OOD) and C++
FIGURE 4-10 Function Return Statements
Dissection of AVL tree operations
Method Mark and Lyubo.
FIGURE 4-10 Function Return Statements
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
CS150 Introduction to Computer Science 1
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS150 Introduction to Computer Science 1
FIGURE 4-10 Function Return Statements
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
See requirements for practice program on next slide.
In C Programming Language
CS150 Introduction to Computer Science 1
A simple function.
FIGURE 4-10 Function Return Statements
CS148 Introduction to Programming II
Presentation transcript:

CS 202 Computer Science II Lab Fall 2009 October 22

Today Topics Templates

Why Templates? One of the C++ Powerful Features One for All – Function Template : a code segment for a set of related functions – Class Template : a code segment for a set of related classes

Syntax ! template declarations; template: reserved word class: refer to any user-defined type or built-in type Type: refer to formal parameter to the template

Function Templates ! template function definition; template Type larger(Type x, Type y) { if (x>=y) return x; else return y; }

Class Templates ! template class declaration;

Class Templates Example! template class templateMath { public: type1 add (type1, type1); type1 mul (type1, type1); }; template type1 templateMath ::add (type1 a, type1 b) { return a+b; } template type1 templateMath ::mul(type1 a, type1 b) { return a*b; }

Class Templates Example! templateMath tM;

Class Templates Example! template type1 templateMath ::myPow (type1 a, int b) { return pow(a, b); }

Class Templates Example! template class templateMath { private: type1 myArray[input]; public: type1 add (type1, type1); type1 mul (type1, type1); type1 myPow (type1, int); void setMember(type1, int); type1 getMember(int); };

Class Templates Example! template type1 templateMath ::add (type1 a, type1 b) { return a+b; }

Class Templates Example! template void templateMath ::setMember(type1 a, int b) { if(b<0 || input<=b) return; myArray[b] = a; } template type1 templateMath ::getMember(int b) { if(b<0 || input<=b) return 0; return myArray[b]; }

Class Templates Example! templateMath tM;

Questions?