Upload
udara-manawadu
View
231
Download
0
Embed Size (px)
Citation preview
7/31/2019 CS288 - 03
1/15
DEPARTMENT OF MECHANICAL ENGINEERING
UNIVERSITY OF MORATUWA
CS2880
OBJECT ORIENTED PROGRAMMING USING C++
Manoj Ranaweera
Lecturer
Department of Mechanical Engineering
1
7/31/2019 CS288 - 03
2/15
EXAMPLE 02 - TEMPLATE FUNCTION
Define an overloaded function named Maximum to get
the maximum of 4 integers, 4 floating point numbers.
2
7/31/2019 CS288 - 03
3/15
3
int Max( int []);
float Max( float []);
void main()
{
int intVal[4];
float floatVal[4];
coutintVal[0];
cin>>intVal[1];
cin>>intVal[2];cin>>intVal[3];
coutfloatVal[0];
cin>>floatVal[1];
cin>>floatVal[2];
cin>>floatVal[3];
cout
7/31/2019 CS288 - 03
4/15
FUNCTIONTEMPLATES
Takes any type of input and returns any type
Templates are used when it is required to overload functionswhich differ only in data type
Example:
template
A TempFun( A a, C c) // if there is no return, just omit it
{
// statements
retunrn a;
}
4
7/31/2019 CS288 - 03
5/15
EXAMPLE
Repeat the above example using a function template
5
{
A max;
max = number [0];
for ( int i = 1 ; i< 4 ; i++)
{
if(number[i] > max)
max = number [i];
}
return max;
}
void main(){
int intVal[4];
float floatVal[4];coutintVal[0];
cin>>intVal[1];
cin>>intVal[2];
cin>>intVal[3];
coutfloatVal[0];
cin>>floatVal[1];
cin>>floatVal[2];
cin>>floatVal[3];
cout
7/31/2019 CS288 - 03
6/15
USERDEFINEDDATATYPES
What are built-in data types?
What is a user defined data type?
6
7/31/2019 CS288 - 03
7/15
ENUMERATION
What is enumeration?
How to define an enumeration?
enum TypeName {SYMBOL_1 , SYMBOL_2, }
Here TypeNameis the name of the new data type
SYMBOL_1, SYMBOL_2 etc. are the values which the new
data type TypeNamecan accept
7
English Meaning List, Record, Inventory
In C++, it is a collection of know symbols
with predefined values (or defaults)
7/31/2019 CS288 - 03
8/15
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
8
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
7/31/2019 CS288 - 03
9/15
ARRAYSOMEPOINTSTONOTE
9
enum defines only data
typesNOTvariables !!
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
7/31/2019 CS288 - 03
10/15
EXAMPLE 01
Write a program to get the marks of Mathematics subjects of 10
students along with their names. (Marks are given as integer valuesonly). 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
7/31/2019 CS288 - 03
11/15
IDENTIFIER ATTRIBUTES
Basic information provided by a variable definition
Name
Type (size in memory)
Value (optional)
Additional information provided Storage class
Scope
Linkage
11
7/31/2019 CS288 - 03
12/15
ENDNext Lecture
Pointers
12
7/31/2019 CS288 - 03
13/15
STORAGE CLASS
What it tells?
When and how long does the variable exist in memory
13
Storage Class
Static Automatic
Created once
Data retains throughout the
programInitialized to zero automatically,
unless otherwise initialized
Initialized only once
Use the keyword static
Created whenever the block is
executed
Destroyed after executionNo data retention
No special keyword is needed
(a keyword called auto exist,
though rarely being used)
7/31/2019 CS288 - 03
14/15
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 { }
File scope
Ex: Global variables, function declarations etc
etc
14
7/31/2019 CS288 - 03
15/15
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