Object Oriented Programming Mansoor Ahmed Bughio

Slides:



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

Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Vectors, lists and queues
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Template Implicit function overload. Function overload Function overload double ssqq(double & a, double & b) { return(a*b);} float ssqq(float & a, float.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
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.
Chapter 1: An Overview of Computers and Programming Languages
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Dr\Ali-Alnajjar.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Subtracting Integers Algebraically. How to Subtract Integers Algebraically 1.Rewrite the problem  Keep the first number the same  Change the problem.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
1 Original Source : and Problem and Problem Solving.ppt.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
1 CSC241: Object Oriented Programming Lecture No 25.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Introduction to Object Oriented Programming Chapter 10.
Exception Handling How to handle the runtime errors.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Data Structure Lecture 1.  The Logical Organization of Data is called data structures  organize data  more efficient programs.  More powerful computers.
Inequalities and Absolute Value
Aim: How do we solve absolute value inequalities?
Chapter 1.2 Introduction to C++ Programming
Understanding The Basics
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
#define #include<iostream> using namespace std; #define GO
OBJECT ORIENTED PROGRAMMING
Command Line Arguments
1 Introduction to Algebra: Integers.
Sec. 1-5: Subtracting Real Numbers
Multiplying Real Numbers
Pointers and Pointer-Based Strings
Chapter 2 ERROR ANALYSIS
Lecture 4-7 Classes and Objects
Dynamic Memory Allocation Reference Variables
Chapter 17 Templates and Exceptions Part 2
Subtracting Integers 8/23/16.
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Absolute Values of Products in Open Sentences
Starting Out with C++: From Control Structures through Objects
Templates Class Templates
CHAPTER 2 Arrays and Vectors.
Pointers and Pointer-Based Strings
CHAPTER 2 Arrays and Vectors.
Section 1.4 Integers and Absolute Value
TOPIC: FUNCTION OVERLOADING
Introduction to Programming - 1
Introduction to Algorithms and Programming COMP151
OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class
Presentation transcript:

Object Oriented Programming Mansoor Ahmed Bughio

Templates and Exceptions

This chapter introduces two advanced C++ features: templates and exceptions. Templates make it possible to use one function or class to handle many different data types. Exceptions provide a convenient, uniform way to handle errors that occur within classes

The template concept can be used in two different ways: with functions and with classes. We’ll look at function templates first. then go on to class templates, and finally to exceptions.

Function Templates Suppose you want to write a function that returns the absolute value of two numbers. As you no doubt remember from high school algebra, the absolute value of a number is its value with- out regard to its sign: The absolute value of 3 is 3, and the absolute value of –3 is also 3. Absolute value “the magnitude of a real number without regard to its sign” Ordinarily this function would be written for a particular data type: int abs(int n) //absolute value of ints { return (n<0) ? -n : n; //if n is negative, return -n }

Here the function is defined to take an argument of type int and to return a value of this same type. But now suppose you want to find the absolute value of a type long. You will need to write a completely new function: long abs(long n) //absolute value of longs { return (n<0) ? -n : n; } And again, for type float: float abs(float n) //absolute value of floats

The body of the function is written the same way in each case, but they are completely different functions because they handle arguments and return values of different types. Rewriting the same function body over and over for different types is time-consuming and wastes space in the listing. Also , if you find you’ve made an error in one such function , you’ll need to remember to correct it in each function body

#include<iostream> example #include<conio.h> using namespace std; template <class v> //function template v abs(v n) { return (n < 0) ? -n : n; } int main() { int int1 = 5; int int2 = -6; long lon1 = 70000L; cout << " \nabs="<< int1 << "="<< abs(int1 ); //abs(long) cout << " \nabs="<< int2 << "="<< abs(int2); //abs(long) cout << " \nabs="<< lon1 << "="<< abs(lon1); //abs(long)