C Notes
C Notes
UNIT-1
Object Oriented Programming
Programming language
A programming language is a formal computer language or constructed language
designed to communicate instructions to a machine, particularly a computer.
Programming languages can be used to create programs to control the behavior
of a machine or to express algorithms.
Features of a Programming Language
users and professional programmers to specify the results they want without
specifying how to solve the problem.
Examples:FORTRAN,C++,COBOL,ALGOL etc
Non-procedural language is concerned with the WHAT not the HOW. Non-proc
languages are those languages where you specify what conditions the answer
should satisfy, but not how to obtain it.
Examples: SQL, Visual Basic, etc
Non-procedural:
1)Non-Procedural language directs the computer what to do not and how to do
2)It is easy to learn
3)It requires a few non-procedural instructions.
4) It can be used professional and non-technical users
Programming Paradigm
Paradigm can also be termed as method to solve some problem or do some task.
Programming paradigm is an approach to solve problem using some programming
language or also we can say it is a method to solve a problem using tools and
techniques that are available to us following some approach. There are lots for
programming language that are known but all of them need to follow some
strategy when they are implemented and this methodology/strategy is
paradigms. Apart from varieties of programming language there are lots of
paradigms to fulfill each and every demand. They are discussed below:
Class
When you define a class, you define a blueprint for an object. This doesn't
actually define any data, but it does define what the class name means, that is,
what an object of the class will consist of and what operations can be performed
on such an object.
Difference between Class and Object
Class Object
Class is a container which
1 collection of variables and object is a instance of class
methods.
Sufficient memory space will be allocated
No memory is allocated at the
2 for all the variables of class at the time of
time of declaration
declaration.
One class definition should For one class multiple objects can be
3
exist only once in the program. created.
#include<iostream.h>
#include<conio.h>
class Employee
{
public:
int salary // data member
void sal()
{
6
Abstraction
Data abstraction refers to, providing only essential information to the outside
world and hiding their background details, i.e., to represent the needed
information in program without presenting the details.
Let's take one real life example of a TV, which you can turn on and off, change
the channel, adjust the volume, and add external components such as speakers,
VCRs, and DVD players, BUT you do not know its internal details, that is, you do
not know how it receives signals over the air or through a cable, how it translates
them, and finally displays them on the screen.
Thus, we can say a television clearly separates its internal implementation from
its external interface and you can play with its interfaces like the power button,
channel changer, and volume control without having zero knowledge of its
internals.
Now, if we talk in terms of C++ Programming, C++ classes provides great level
of data abstraction. They provide sufficient public methods to the outside world
to play with the functionality of the object and to manipulate object data, i.e.,
state without actually knowing how class has been implemented internally.
Encapsulation
Encapsulation is placing the data and the functions that work on that data in the
same place. While working with procedural languages, it is not always clear
which functions work on which variables but object-oriented programming
provides you framework to place the data and the relevant functions together in
the same object.
7
It is the mechanism that binds together code and data in manipulates, and keeps
both safe from outside interference and misuse. In short, it isolates a particular
code and data from all other codes and data.
A well-defined interface controls the access to that particular code and data. The
act of placing data and the operations that perform on that data in the same
class. The class then becomes the 'capsule' or container for the data and
operations.
Storing data and functions in a single unit (class) is encapsulation. Data cannot be
accessible to the outside world and only those functions which are stored in the
class can access it.
Inheritance
One of the most useful aspects of object-oriented programming is code
reusability. As the name suggests Inheritance is the process of forming a new
class from an existing class that is from the existing class called as base class, new
class is formed called as derived class.
This is a very important concept of object-oriented programming since this
feature helps to reduce the code size.
It is the process by which one object acquires the properties of another object.
This supports the hierarchical classification. Without the use of hierarchies, each
object would need to define all its characteristics explicitly. However, by use of
inheritance, an object need only define those qualities that make it unique within
its class. It can inherit its general attributes from its parent. A new sub-class
inherits all of the attributes of all of its ancestors
Polymorphism
The ability to use an operator or function in different ways in other words giving
different meaning or functions to the operators or functions is called
polymorphism. Poly refers to many. That is a single function or an operator
functioning in many ways different upon the usage is called polymorphism.
Polymorphism means the ability to take more than one form. An operation may
exhibit different behaviors in different instances. The behavior depends on the
data types used in the operation.
It is a feature that allows one interface to be used for a general class of actions.
The specific action is determined by the exact nature of the situation. In general,
polymorphism means "one interface, multiple methods", This means that it is
possible to design a generic interface to a group of related activities. This helps
reduce complexity by allowing the same interface to be used to specify a general
class of action. It is the compiler's job to select the specific action (that is,
method) as it applies to each situation.
Dynamic Binding
functions. For functions, it means that matching the call with the right function
definition by the compiler. It takes place either at compile time or at runtime.
Early Binding (compile-time time polymorphism) As the name indicates,
compiler (or linker) directly associate an address to the function call. It replaces
the call with a machine language instruction that tells the mainframe to leap to
the address of the function.
Late Binding : (Run time polymorphism) In this, the compiler adds code that
identifies the kind of object at runtime then matches the call with the right
function definition (Refer this for details). This can be achieved by declaring
a virtual function.
Message Passing
Objects communicate with one another by sending and receiving information to
each other. A message for an object is a request for execution of a procedure
and therefore will invoke a function in the receiving object that generates the
desired results. Message passing involves specifying the name of the object, the
name of the function and the information to be sent.
Message Passing is passing objects as parameters (to a method)
class A
{
public void Method(Object obj)
{
// Method does somthing
}
}
class B
{
Object obj1 = new Object();
A a = new A();
a.Method1(obj1);
}
In the above example a.Method1() is Method invocation
passing parameter (obj1) to Method1 is message passing
Benefits of OOP
C+ of OOP's
Through inheritance, we can eliminate redundant code and extend the use
of existing classes which is not possible in procedure oriented approach.
We can build programs from the standard working modules that
communicate with one another, rather than having to start writing the
code from scratch which happens procedure oriented approach. This leads
to saving of development time and higher productivity.
9
give example
Features of c++
C++ is object oriented programming language and it is a very simple and easy
language, It is the enhanced form of C programming language. this language
have following features and here we discuss some important features of C++.
10
Boolean: Boolean data type is used for storing boolean or logical values. A
boolean variable can store either true or false. Keyword used for boolean
data type is bool.
Floating Point: Floating Point data type is used for storing single precision
floating point values or decimal values. Keyword used for floating point
data type is float. Float variables typically requires 4 byte of memory space.
Double Floating Point: Double Floating Point data type is used for storing
double precision floating point values or decimal values. Keyword used for
double floating point data type is double. Double variables typically
requires 8 byte of memory space.
void: Void means without any value. void datatype represents a valueless
entity. Void data type is used for those function which does not returns a
value.
Derived data types in C++
Derived Data Types: Data types that are derived from the built-in data types are
known as derived data types. The various derived data types provided by C++
are arrays, junctions, references and pointers.
We have four types of derived-defined data types in C++
1. Array
2. Function
3. Pointer
4.Reference
Array An array is a set of elements of the same data type that are referred to by
the same name. All the elements in an array are stored at contiguous (one after
another) memory locations and each element is accessed by a unique index or
subscript value. The subscript value indicates the position of an element in an
array. To declare an array in C++, the programmer specifies the type of the
elements and the number of elements required by an array as follows −
type arrayName [ arraySize ];
Read the & in these declarations as reference. Thus, read the first
declaration as "r is an integer reference initialized to i"
Following example makes use of references
#include <iostream.h>
int main () {
// declare simple variables
int i;
// declare reference variables
int& r = i;
i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
return 0;
}
Pointer A pointer is a variable that can store the memory address of another
variable. Pointers allow to use the memory dynamically. That is, with the help of
pointers, memory can be allocated or de-allocated to the variables at run-time,
thus, making a program more efficient. The general form of a pointer variable
declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-
name is the name of the pointer variable. the asterisk is being used to
designate a variable as a pointer. Following are the valid pointer declaration
−
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
The actual data type of the value of all pointers, whether integer, float,
character, or otherwise, is the same, a long hexadecimal number that
represents a memory address.
User-defined data types
We have four types of user-defined data types in C++
1. struct
2. union
3. Enum
4. class
15
Struct In C++ language Array is also a user defined data type but Array hold or
store only similar type of data, If we want to store different-different type of data.
In this case we need to defined separate variable for each type of data. To resolve
this problem we use Structure in C++ Programming, It can store different-different
data type in single variable. Example: Suppose we want to store Student
record, then we need to store....
Student Name
Roll number
Class
Address
For store Student name and Address we need character data type, for
store Roll number and class we need integer data type.
If we use Structure then we use single variable for all data like belowe
example.
Example
struct stu
{
char student_name[10];
char address[20];
int roll_no[5];
int class[5];
};
Union
A union is a special data type available in C that allows storing different data types in
the same memory location. You can define a union with many members, but only one
member can contain a value at any given time. Unions provide an efficient way of using
the same memory location for multiple purposes.
Defining a Union: To define a union, you must use the unionstatement in the same way
as you did while defining a structure. The union statement defines a new data type with
more than one member for your program. The format of the union statement is as
follows:
union [union name]
{
member definition;
member definition;
...
member definition;
};
Enum
An enumeration is a user-defined data type that consists of integral constants. To define
an enumeration, keyword enum is used.
class
A class is used to specify the form of an object and it combines data
representation and methods for manipulating that data into one neat package.
The data and functions within a class are called members of the class. A class
definition starts with the keyword class followed by the class name; and
the class body, enclosed by a pair of curly braces. A class definition must be
followed either by a semicolon or a list of declarations. For example, we
defined the Box data type using the keyword class as follows −
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
2) Assignment Operators
Assignments operators in C++ are: =, +=, -=, *=, /=, %=
There are following assignment operators supported by C++ language −
Show Examples
Operator Description Example
3) Logical Operators
Logical Operators are used with binary variables. They are mainly used in
conditional statements and loops for evaluating a condition.
Logical operators in C++ are: &&, ||, !
Assume variable A holds 1 and variable B holds 0, then −
Show Examples
Operator Description Example
4) Relational operators
We have six relational operators in C++: ==, !=, >, <, >=, <=
Assume variable A holds 10 and variable B holds 20, then −
Show Examples
Operator Description Example
5) Bitwise Operators
There are six bitwise Operators: &, |, ^, ~, <<, >>
Operator Description Example
& Binary AND Operator copies a bit to the (A & B) will give 12 which
result if it exists in both operands. is 0000 1100
~ Binary Ones Complement Operator is unary (~A ) will give -61 which
and has the effect of 'flipping' bits. is 1100 0011 in 2's
complement form due to a
signed binary number.
1 sizeof
sizeof operator returns the size of a variable. For example, sizeof(a),
where ‘a’ is integer, and will return 4.
2 Condition ? X : Y
Conditional operator (?). If Condition is true then it returns value of X
otherwise returns value of Y.
3 ,
Comma operator causes a sequence of operations to be performed. The
value of the entire comma expression is the value of the last expression of
the comma-separated list.
5 Cast
Casting operators convert one data type to another. For example,
int(2.2000) would return 2.
20
6 &
Pointer operator & returns the address of a variable. For example &a; will
give actual address of the variable.
7 *
Pointer operator * is pointer to a variable. For example *var; will pointer
to a variable var.
sizeof operator
The sizeof is a keyword, but it is a compile-time operator that determines the
size, in bytes, of a variable or data type.
The sizeof operator can be used to get the size of classes, structures, unions and
any other user defined data type.
The syntax of using sizeof is as follows −
sizeof (data type)
Where data type is the desired data type including classes, structures, unions and
any other user defined data type.
member operator
The . (dot) operator and the -> (arrow) operator are used to reference individual
members of classes, structures, and unions.
The dot operator is applied to the actual object. The arrow operator is used with
a pointer to an object. For example, consider the following structure −
struct Employee {
char first_name[16];
int age;
} emp;
The (.) dot operator
To assign the value "zara" to the first_name member of object emp, you would
write something as follows −
strcpy(emp.first_name, "zara");
The (->) arrow operator
If p_emp is a pointer to an object of type Employee, then to assign the value
"zara" to the first_name member of object emp, you would write something as
follows −
strcpy(p_emp->first_name, "zara");
The -> is called the arrow operator. It is formed by using the minus sign followed
by a greater than sign.
comma operator
The purpose of comma operator is to string together several expressions. The
value of a comma-separated list of expressions is the value of the right-most
21
int main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
Output:
Value of global x is 0
Value of local x is 10
int main()
{
A a;
a.fun();
return 0;
}
Inline function
C++ inline function is powerful concept that is commonly used with
classes. If a function is inline, the compiler places a copy of the code of that
function at each point where the function is called at compile time.
Any change to an inline function could require all clients of the function to
be recompiled because compiler would need to replace all the code once
again otherwise it will continue with old functionality.
To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function. The compiler
can ignore the inline qualifier in case defined function is more than a line.
A function definition in a class definition is an inline function definition, even
without the use of the inline specifier.
Following is an example, which makes use of inline function to return max
of two numbers −
#include <iostream>
inline int Max(int x, int y) {
return (x > y)? x : y;
}
return 0;
}
Remember, inlining is only a request to the compiler, not a command. Compiler can
ignore the request for inlining. Compiler may not perform inlining in such circumstances
like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement doesn’t exist in
function body.
5) If a function contains switch or goto statement.
Inline functions provide following advantages:
1) Function call overhead doesn’t occur.
2) It also saves the overhead of push/pop variables on the stack when function is called.
3) It also saves overhead of a return call from a function.
4) When you inline a function, you may enable compiler to perform context specific
optimization on the body of function. Such optimizations are not possible for normal
function calls. Other optimizations can be obtained by considering the flows of calling
context and the called context.
5) Inline function may be useful (if it is small) for embedded systems because inline can
yield less code than the function call preamble and return.
Inline function disadvantages:
1) The added variables from the inlined function consumes additional registers, After in-
lining function if variables number which are going to use register increases than they
may create overhead on register variable resource utilization. This means that when
inline function body is substituted at the point of function call, total number of variables
24
used by the function also gets inserted. So the number of register going to be used for
the variables will also get increased. So if after function inlining variable numbers
increase drastically then it would surely cause an overhead on register utilization.
2) If you use too many inline functions then the size of the binary executable file will be
large, because of the duplication of same code.
3) Too much inlining can also reduce your instruction cache hit rate, thus reducing the
speed of instruction fetch from that of cache memory to that of primary memory.
4) Inline function may increase compile time overhead if someone changes the code
inside the inline function then all the calling location has to be recompiled because
compiler would require to replace all the code once again to reflect the changes,
otherwise it will continue with old functionality.
5) Inline functions may not be useful for many embedded systems. Because in
embedded systems code size is more important than speed.
6) Inline functions might cause thrashing because inlining might increase size of the
binary executable file. Thrashing in memory causes performance of computer to
degrade.
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n"; //call is replaced by 3*3*3
return 0;
} //Output: The cube of 3 is: 27
Example of inline function
#include <iostream>
int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
} //Output: The cube of 3 is: 27
If you follow the rule of array initialization, then you can write the above
statement as follows −
char greeting[] = "Hello";
Actually, you do not place the null character at the end of a string constant.
The C++ compiler automatically places the '\0' at the end of the string
when it initializes the array. Let us try to print above-mentioned string −
26
#include <iostream>
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following
result −
Greeting message: Hello
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2;
greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in
string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in
string s1.
Following example makes use of few of the above-mentioned functions −
#include <iostream>
#include <cstring>
int main () {
UNIT-2
Access modifiers
Data hiding is one of the important features of Object Oriented Programming
which allows preventing the functions of a program to access directly the
internal representation of a class type. The access restriction to the class
members is specified by the labeled public, private, and protected sections
within the class body. The keywords public, private, and protected are called
access specifiers.
A class can have multiple public, protected, or private labeled sections. Each
section remains in effect until either another section label or the closing right
brace of the class body is seen. The default access for members and classes is
private.
class Base {
public:
protected:
private:
};
The public members:
A public member is accessible from anywhere outside the class but within a
program. You can set and get the value of public variables without any member
function as shown in the following example:
class abc
{
public: // public access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
};
Void main()
{
Abc o1;
30
O1.x=10; //valid
O1.diplay();//valid
}
private members:
A private member variable or function cannot be accessed, or even viewed from
outside the class. Only the class and friend functions can access private members.
class abc
{
private: // private access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
};
Void main()
{
Abc o1;
O1.x=10; //invalid
O1.diplay();//invalid
}
The heap: This is unused memory of the program and can be used to
allocate the memory dynamically when program runs.
Many times, you are not aware in advance how much memory you will need to
store particular information in a defined variable and the size of required
memory can be determined at run time.
You can allocate memory at run time within the heap for the variable of a given
type using a special operator in C++ which returns the address of the space
allocated. This operator is called new operator.
If you are not in need of dynamically allocated memory anymore, you can
use delete operator, which de-allocates memory previously allocated by new
operator.
int main ()
{
int* pvalue = NULL; // Pointer initialized with null
pvalue = new int; // Request memory for the variable
return 0;
}
this pointer
Every object in C++ has access to its own address through an important pointer
called this pointer. The this pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this may be used to refer to the
invoking object.
The ‘this’ pointer is passed as a hidden argument to all nonstatic member
function calls and is available as a local variable within the body of all nonstatic
functions. ‘this’ pointer is a constant pointer that holds the memory address of
the current object. ‘this’ pointer is not available in static member functions as
static member functions can be called without any object
Friend functions do not have a this pointer, because friends are not members of
a class. Only member functions have a this pointer.
#include<iostream>
using namespace std;
UNIT-4
Exception Handling
Exception handling is a programming language construct designed to handle the
occurrence of exceptions, special conditions that change the normal flow of
program execution.
Purpose
The purpose of exception handling is to provide means to detect and
report an exceptional circumstance so that appropriate action can be
taken.
Exceptions are erroneous events like a division be zero, an arithmetic or
array overflow and exhaustion of free heap space which occur at runtime.
In case these errors are not trapped they cause abrupt halt, without any
indication to the user about what happened
The C++ language provides Type-safe integrated approach, for coping with
the anomalous situations that arise while executing a program called
exceptions
Types of Exceptions
Synchronous exceptions:
◦ That occur during runtime and can be generated using the throw
expression.
◦ Only these type of errors can be handled using exception handlers in
C++.
◦ Control does not come back to the point from where the exception is
thrown. E.g. Errors like “out of range Index” and “overflow”
Asynchronous exceptions:
◦ That occur due to keyboard or mouse interrupt.
◦ C++ programs cannot directly handle these types of exceptions using
the try and catch statements.
◦ A separate handler that automatically calls the routine associated
with the keyboard and mouse interrupts has to be created
Exception handling mechanism
It includes a separate error handling code that performs following tasks
◦ Find the problem (Hit the exception)
◦ Inform that an error has occurred(Throw the exception)
◦ Receive the error information(Catch the exception)
◦ Take corrective actions (Handle the exception
Mechanism
try Block: A demarcated block of program statements used to preface a
block of statements which may generate exceptions. It is also called the
guarded section
throw: An expression called throw, which indicates to the program to jump
to statements at another location. This location in the program should be
34
Syntax
try
{
…………. //Block of statements which detect and throw
exception
throw exception;
}
catch (type arg) //catches exception
{
// block of statement that handles the exception of type
specified
……..
}
The try, throw and catch mechanism
The execution of program containing the try, catch and throw statements
proceeds as follows
The control reaches the try statement by normal sequential execution. The
guarded section within the try block is executed.
If no exception is thrown during the execution of guarded section, the catch
clauses following the try block are not executed. Execution continues from
the statement after the last catch clause following the try block in which
exception was thrown.
If an exception occurs, the program searches for a catch clause that can
handle the exception of the type thrown.
If a matching handler is not found, the predefined runtime handler
terminate() is called.
35
Throwing mechanism
When an exception that is desired to be handled is detected, it is thrown
using the throw statement in one of the following forms
◦ throw(exception);
◦ throw exception;
◦ throw; //used for rethrowing an exception
The operand exception may be of any type including constants.
When an exception is thrown, it will be caught by the catch statement
associated with the try block i.e. control exits the current try block and is
transferred to catch block after the try block.
Catching mechanism
Syntax for catch block
catch(type arg)
{
//statements for managing exceptions
}
type indicates the type of exceptions that catch block handles. The catch
statement catches an exception whose type matches with the type of catch
statement. When it is caught code in the catch block is executed
Multiple catch blocks
Syntax
try
{
//try block
}
catch(type1 arg)
{
//catch block 1
}
catch(type2 arg)
{
//catch block 1
}
catch(typeN arg)
{
//catch block N
}
• When exception is thrown, the exception handlers are searched in order for
an appropriate match.
36
Rethrowing an Exception
A handler may rethrow the exception without processing it.
In such cases we may use the throw statement without parameters
It is also possible to nest try-catch blocks within more external try blocks. In these
cases, we have the possibility that an internal catch block forwards the exception
to its external level. This is done with the expression throw; with no arguments.
For example:
1 try {
2 try {
3 // code here
4 }
5 catch (int n) {
6 throw;
7 }
8 }
9 catch (...) {
10 cout << "Exception occurred";
11 }
Templates
Templates are a feature of the C++ programming language that allow functions
and classes to operate with generic types.
It enables us to define generic classes and functions and provide support for
generic programming.
Templates are blueprints of a function or a class that can be applied to different
data types.
The essence of templates as the instantiation of functions and the classes
themselves at runtime according to requirements. This means that the programar
does not have to know the exact utilization context at the coding stage. The end
user can create user defined classes at the time of implementation. A template
may be considered a type of macro. When an object of a specific type is defined
for actual use, the template definition for that class is substituted with the
required data type. Since a template is defined with a parameter that would be
replaced by a specified data type at the time of actual use of the class or function,
the templates are some times called parametrized classes or functions.
39
{
//public
T1 var1;
T2 var2;
};
Example
#include<iostream.h>
#include<conio.h>
template<class T1,class T2>
class xyz
{
public :
T1 a;
T2 b;
xyz(T1 x, T2 y)
{
a=x;
b=y;
cout<<"\na="<<a;
cout<<”\nb=”<<b;
}
};
int main()
{
xyz<int,int> o1(10,20);
xyz<float,float> o2(10.45,2,5);
xyz<int,float> o3(10,20.8);
xyz<float,int> o1(10.8,20);
getch();
return 0;
}
Template function/Generic function:
A template function may be defined as an unbounded set of overloaded
functions. This means that all the possible parameters to the function are not
known in advance and a copy of function has to be created as and when
necessary.
Template functions are created using the keyword template.
Syntax
Template<class T1, class T2, >
void function_name(T1 parameter1, T2 parameter 2)
{
...
41
}
Example
#include<iostream.h>
#include<conio.h>
template<class T>
T max(T a, T b)
{ return a>b?a:b;
}
int main()
{
cout<<"\nmax('A','a')="<<max('A','a');
cout<<"\nmax(5,3)="<<max(5,3);
cout<<"\nmax(14.2,14.02)="<<max(14.2,14.02);
getch();
return 0;
}
Overloading of templates
Template function may be overloaded either by template function or
ordinary function of its name
Resolution is accomplished as follows
◦ Call an ordinary function that has an exact match
◦ Call a template that could be created with an exact match
◦ Try normal overloading resolution to ordinary functions and call the
one that matches
template <class T>
void display(T x)
{
cout<<"Template Display:"<<x<<"\n";
}
void display(int x)
{
cout<<"explicit Display:"<<x<<"\n";
}
void display(char x)
{
cout<<"explicit Display:"<<x<<"\n";
}
int main()
{
display(100);
display(23.4);
42
display('f');
getch();
}
OUTPUT
Explicit display 100
Template display 23.4
Explicit display f