You are on page 1of 15

DEPARTMENT OF MECHANICAL ENGINEERING

UNIVERSITY OF MORATUWA

CS2880 OBJECT ORIENTED PROGRAMMING USING C++

Manoj Ranaweera Lecturer Department of Mechanical Engineering 1

EXAMPLE 02 - TEMPLATE FUNCTION

Define an overloaded function named Maximum to get the maximum of 4 integers, 4 floating point numbers.

int Max( int []); float Max( float []); void main() { int intVal[4]; float floatVal[4]; cout<<"\n Enter 4 integer numbers"; cin>>intVal[0]; cin>>intVal[1]; cin>>intVal[2]; cin>>intVal[3]; cout<<"\n Enter 4 floating point numbers"; cin>>floatVal[0]; cin>>floatVal[1]; cin>>floatVal[2]; cin>>floatVal[3]; cout<<"\n Maximum integer value is "<<Max(intVal); cout<<"\n Maximum float value is "<<Max(floatVal); getch(); } int Max( int number4]) { int max; max = number[0]; for ( int i = 1 ; i< 4 ; i++) { if(number[i] > max) max = number [i]; } return max; } float Max(float number[4]) { float max; max = number [0]; for ( int i = 1 ; i< 4 ; i++) { if(number[i] > max) max = number [i]; } return max; }

FUNCTION TEMPLATES

Takes any type of input and returns any type Templates are used when it is required to overload functions which differ only in data type

Example:
template <typename A, typename C> A TempFun( A a, C c) // if there is no return, just omit it { // statements

retunrn a;
}

EXAMPLE

A Max( A number[4]) { A max; max = number [0]; for ( int i = 1 ; i< 4 ; i++) { if(number[i] > max) max = number [i]; } return max; } void main() {

Repeat the above example using a function template


int intVal[4]; float floatVal[4];
cout<<"\n Enter 4 integer numbers"; cin>>intVal[0]; cin>>intVal[1]; cin>>intVal[2]; cin>>intVal[3]; cout<<"\n Enter 4 floating point numbers"; cin>>floatVal[0]; cin>>floatVal[1]; cin>>floatVal[2]; cin>>floatVal[3];

cout<<"\n Maximum integer value is "<<Max(intVal); cout<<"\n Maximum float value is "<<Max(floatVal);
getch(); }

USER DEFINED DATA TYPES

What are built-in data types?

What is a user defined data type?

ENUMERATION

What is enumeration?
English Meaning List, Record, Inventory In C++, it is a collection of know symbols with predefined values (or defaults)

How to define an enumeration?


enum TypeName {SYMBOL_1 , SYMBOL_2, }

Here TypeName is the name of the new data type SYMBOL_1, SYMBOL_2 etc. are the values which the new data type TypeName can accept
7

ENUMERATION

Key points

Unless otherwise defined, first element is assigned zero Unless otherwise defined, any element is given one incremented value of previous element current_element = previous_element +1
enum Place {FIRST, SECOND, THIRD} 0 1 2

enum Place {FIRST =1 , SECOND, THIRD} 1 2 3

enum Place {FIRST =5, SECOND, THIRD =18} 5 6 18


8

ARRAY SOME POINTS TO NOTE


enum defines only data types NOT variables !!

enum Place {FIRST, SECOND, THIRD}

Defining variable of type Place Place var_1; Place var_2; Variables var_1 and var_2 can hold only FIRST, SECOND or THIRD only
9

EXAMPLE 01

Write a program to get the marks of Mathematics subjects of 10 students along with their names. (Marks are given as integer values only). Hold the marks and names in arrays. Define a function named GetCredit which takes the mark of a student as an input parameter and decide and returns the credit of the student based on the following given criterion. Define an enumeration named Result which can takes data A, B, C, D, and F only. Results must be printed on screen within main function after taking all 10 marks Criteria (Marks m)

(m>=75 grade A), (65<= m <75 grade B), (55<=m <65 grade C) (45 <=m < 55 grade D) and (m<45 grade F)

10

IDENTIFIER ATTRIBUTES

Basic information provided by a variable definition


Name Type (size in memory) Value (optional)

Additional information provided


Storage class Scope Linkage

11

END
Next Lecture Pointers

12

STORAGE CLASS

What it tells?

When and how long does the variable exist in memory


Storage Class

Static
Created once Data retains throughout the program Initialized to zero automatically, unless otherwise initialized Initialized only once Use the keyword static

Automatic
Created whenever the block is executed Destroyed after execution No data retention No special keyword is needed (a keyword called auto exist, though rarely being used)
13

SCOPE

Scope determines where the variable can be referenced Scope categories

Functional scope

Ex: Labels have functional scope. (only labels have the true functional scope. However, local variables in functions which are not defined in any block within the function have the functional scope)

Block scope

Ex: anything lies within a block bounded by curly brackets { }


Ex: Global variables, function declarations etc

File scope

etc

14

LINKAGE

It determines whether the variable is available only to the file it is defined or to other files as well Use extern keywords to make it available to other files extern must be used if the project has more than one source files NOT header files

15

You might also like