Beruflich Dokumente
Kultur Dokumente
Recap of Day 3
• Method Overriding
• Pointer to objects
• Base class pointers pointing to Derived class
• Virtual Functions and Dynamic Binding
• Abstract classes
• Different types of Inheritance
• Namespaces
• Exception Handling
• Templates
– Generic Functions and Classes
• Introduction to STL
• Motivation
• Consider both the header files contain class with the name String but with
different implementation.
• If you include both the headers in an application, there will be two classes with
the same name String. Hence there will be a name clash.
• So Compiler will throw an error. Even if you resolve this error, you will be
ending up with using only one header file.
• So, now How to include both the header files and make use of both the String
classes?
// My Application
#include "vendor1.h"
#include "vendor2.h“
…Some Code ….
// My Application
#include "vendor1.h"
#include "vendor2.h“
…Some Code ….
Nesting of
namespace NS1{ namespace int main(int argc,char**arg)
int iEmpNo; { Correct
namespace NS2{ using namespace NS1;
usage
float fSalary; iEmpNo=1000;
} fSalary=20222.99;
} NS2 :: fSalary=20222.99;
} //End of main
/* File1.cpp*/ /*File2.cpp*/
namespace { extern int iCount;
int iCount;
} void fnReadCount(){
void fnDisplayCount(){ scanf(“%d”,&iCount);
iCount=99; }
printf(“%d”,iCount);
}
External
reference not
Equivalent to found; ERROR
static in C
• Errors such as division by zero, array-index out of bounds, under flow, etc.,
belong to the synchronous type of exceptions.
• Errors such as keyboard interrupt or any other type of hardware failure belong
to the asynchronous type of exceptions
• Execution of throw statement will transfer the control to the catch statement of
calling function
class MyException {
private: try{
int m_iExpNo; if(codn) {
char m_acExpName[25]; MyExeption oMexp(101,”TSN-Err”);
public: throw oMexp;
MyException(int iD,char acNam[]){ }
m_iExpNo=iD; }
strcpy(m_acExpName,acNam);
} catch(MyException oExeObject){
void DisplayError() { oExecObject.DisplayError();
printout(“%s”,m_acExpName); }
}
};
int iTemp = rA; Can we use this function for swapping two
floating-point numbers or two objects?
rA = rB;
• Eliminate code duplication for different types and thus make the software
development easier and more manageable.
// Function Template int main(int argc,char**argv) {
int iSum;
//Generic data type T, return type T, function float fSum;
//name fnAdd, parameters oT1,oT2 of type T iSum=fnAdd(12,4);
fSum=fnAdd(12.5,3.4);
template <class T>
T fnAdd(T oT1,T oT2) { return 0;
T oTSum; }
oTSum= oT1+oT2;
return oTSum;
}
• The compiler will perform an error analysis only when an instantiation takes
place.
• STL is a generic library which provides many algorithms and Data Structures
used in Computer programming.
• STL contains many container classes, which contain the other objects.
Examples: vector, list, deque, etc.
• Each of these are template classes and can be instantiated to contain any kind
of objects.
• In STL there are three foundational items namely containers, algorithms and
iterators.
• Containers
– are objects that hold other objects and there are several different types.
– Sequence container for example vector, list, queues … etc.
– Associative containers which allow efficient retrieval of values based on
keys.
– Each container class defines a set of functions that may be applied to the
container.
– For example, a list container includes functions that insert, delete and
merge elements. A stack includes functions that push and pop values.
• Algorithms
– Algorithms act on containers.
– They provide the means by which you will manipulate the contents of
containers including initialization, sorting, searching and transforming the
contents of the containers.
• Iterators
– are objects that act more or less like pointers.
– They give you the ability to cycle through the contents of a container in
much the same way that you would use a pointer to cycle through an array.
– You can increment and decrement iterators.
– The STL also supports reverse iterators. Reverse iterators are either
bidirectional or random-access iterators.
• STL also includes a large collection of useful algorithms that can operate on
containers
• Namespaces
• Exception Handling
• Templates
– Generic Functions and Classes
• Introduction to STL