Oops
Oops
h>
void f1(int a)
{
printf("\n Inside int blcok");
}
void f1(float a)
{
printf("\n Inside float blcok");
}
void f1(double a)
{
printf("\n Inside double blcok");
}
int main()
{ f1(1);
f1(1.2);
f1(1.2f);
f1((int)1.2);
f1(1.2);
return 0;
} 1
Function overloading :
Function having same name but differs either in different number of
arguments or type of arguments or order of arguments such process of
writing function is called function overloading .
Functions which is taking part in function overloading such functions are
called as overloaded functions.
Sunbeam Infotech 3
cin and cout
• C++ provides an easier way
for input and output.
• The output:
– cout << “Hello C++”;
• The input:
– cin >> var;
Sunbeam Infotech 4
#include<stdio.h> #include<iostream.h>
int main() int main()
{ {
printf(“ hellow world”); cout<<“hellow world”;
} }
#include<stdio.h> #include<iostream.h>
int main() int main()
{ int num; { int num;
scanf(“%d”,&num); cin>>num;
} }
Sunbeam Infotech 7
What is object?
• Object is an instance of class
• Entity that has physical existence,
can store data, send and receive
message to communicate with
other objects
• Object has
– Data members (state of object)
– Member function (behavior of
object)
– Unique address(identity of object)
Sunbeam Infotech 8
What is object?
• The values stored in data members of the
object called as ‘state’ of object.
• Data members of class represent state of
object.
TComplex c1, c2;
c1 c2
real 10 real 30
Imag 20 imag 40
1000 2000
Sunbeam Infotech 9
Behavior is how object acts & reacts, when its
state is changed & operations are done
Behavior is decided by the member functions.
Operations performed are also known as
messages c1 c2
Input() 10 30
display()
Sum() 20 40
1000 2000
Sunbeam Infotech 12
Member Functions
• Member functions are generally
declared as public members of class.
• Constructor : Initialize Object
• Destructor : De-initialize Object
• Mutators : Modifies state of the
object
• Inspectors : Don’t Modify state of
object
• Facilitator : Provide facility like IO
Sunbeam Infotech 13
Constructor
• We can have constructors
with
– No argument : initialize data
member to default values
– One or more arguments :
initialize data member to values
passed to it
– Argument of type of object :
initialize object by using the
values of the data members of
the passed object. It is called as
copy constructor.
Sunbeam Infotech 14
Copy Constructor
• TComplex c1(c2);
• This statement gives call to copy
constructor. State of c1 become same as
that of c2.
• If we don’t write, compiler provides
default copy constructor.
• Generally, we implement copy
constructor when we have pointer as
data member which is pointing to
dynamically allocated memory.
Sunbeam Infotech 15
Constructor
• Constructor is a member function
of class having same name as that
of class and don’t have any return
type.
• Constructor get automatically
called when object is created i.e.
memory is allocated to object.
• If we don’t write any constructor,
compiler provides a default
constructor.
Sunbeam Infotech 16
Destructor
• Destructor is a member
function of class having same
name as that of class preceded
with ~ sign and don’t have any
return type and arguments.
• Destructor get automatically
called when object is going to
destroy i.e. memory is to be
de-allocated.
Sunbeam Infotech 17
Destructor
Sunbeam Infotech 21
#include<iostream.h>
int sum (int a=0, int b=0, int c=0, int d=0)
{ return a+b+c+d;
}
int main()
{ int ans=sum();
cout<<"sum="<<ans<<endl;
ans=sum(10);
cout<<"sum="<<ans<<endl;
ans=sum(10, 20);
cout<<"sum="<<ans<<endl;
ans=sum(10, 20, 30);
cout<<"sum="<<ans<<endl;
ans=sum(10, 20, 30, 40);
cout<<"sum="<<ans<<endl;
return 0;
} Sunbeam Infotech 22
Inline functions
• C++ provides a keyword inline that
makes the function as inline function.
• Inline functions get replaced by compiler
at its call statement. It ensures faster
execution of function just like macros.
• Advantage of inline functions over
macros: inline functions are type-safe.
• Inline is a request made to compiler.
• Every function may not be replace by
complier , rather it avoids replacement
in certain cases like function containing
switch , loop or recursion may not be
replaced
Sunbeam Infotech 23