SlideShare a Scribd company logo
CLASS & OBJECT
CLASS & OBJECT

Class: A Class is a user defined data type to
Class: A Class is a user defined data type to
implement an abstract object. Abstract means
implement an abstract object. Abstract means
to hide the details. A Class is a combination of
to hide the details. A Class is a combination of
data and functions.
data and functions.

Data is called as data members and functions
Data is called as data members and functions
are called as member functions.
are called as member functions.

Abstract data type:-
Abstract data type:-

A data type that separates the logical properties from
A data type that separates the logical properties from
the implementation details called Abstract Data
the implementation details called Abstract Data
Type(ADT).
Type(ADT).

An abstract data type is a set of object and an
An abstract data type is a set of object and an
associated set of operations on those objects.
associated set of operations on those objects.

ADT supports data abstraction, encapsulation and data
ADT supports data abstraction, encapsulation and data
hiding.
hiding.
 Examples of ADT are:-
Examples of ADT are:-

Boolean
Boolean
 Integer
Integer built in ADT
built in ADT

Array
Array

Stack
Stack

Queue
Queue
 Tree search structure
Tree search structure User defined
User defined
ADT
ADT

Boolean {operations are AND,OR,NOT and
Boolean {operations are AND,OR,NOT and
values are true and false}
values are true and false}

Queues{operations are create , dequeue,inqueue and
Queues{operations are create , dequeue,inqueue and
values are queue elements}
values are queue elements}
Class definition
Class definition
 A class definition begins with the keyword
A class definition begins with the keyword
class
class.
.
 The body of the class is contained within a set
The body of the class is contained within a set
of braces,
of braces, { } ;
{ } ; (notice the semi-colon).
(notice the semi-colon).
class class_name
{
….
….
….
};
Class body (data member +
methods
methods)
Any valid
identifier
 Within the body, the keywords
Within the body, the keywords private:
private: and
and
public:
public: specify the access level of the
specify the access level of the
members of the class.
members of the class.
 the default is
the default is private
private.
.
 Usually, the data members of a class are
Usually, the data members of a class are
declared in the
declared in the private:
private: section of the class and
section of the class and
the member functions are in
the member functions are in public:
public: section.
section.

Data member or member functions may be public,
Data member or member functions may be public,
private or protected.
private or protected.

Public means data members or member functions
Public means data members or member functions
defining inside the class can be used at outside the
defining inside the class can be used at outside the
class.( in different class and in main function)
class.( in different class and in main function)

Member access specifiers
Member access specifiers

public:
public:

can be accessed outside the class directly.
can be accessed outside the class directly.

The public stuff is
The public stuff is the interface
the interface.
.

private:
private:

Accessible only to member functions of class
Accessible only to member functions of class

Private members and methods are for internal
Private members and methods are for internal use only.
use only.

Private means data members and member functions
Private means data members and member functions
can’t be used outside the class.
can’t be used outside the class.

Protected means data member and member
Protected means data member and member
functions can be used in the same class and its
functions can be used in the same class and its
derived class (at one level) (not inmain function).
derived class (at one level) (not inmain function).
PRIVATE
PUBLIC
class class_name
{
private:
…
…
…
public:
…
…
…
};
Public members or methods
private members or methods
 This class example shows how we can
This class example shows how we can
encapsulate (gather) a circle information into
encapsulate (gather) a circle information into
one package (unit or class)
one package (unit or class)
class Circle
{
private:
double radius;
public:
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
No need for others classes to access
and retrieve its value directly. The
class methods are responsible for
that only.
They are accessible from outside
the class, and they can access the
member (radius)
Class Example (Problem)
Class Example (Problem)
#include<iostream.h>
#include<iostream.h>
#include<stdio.h>
#include<stdio.h>
class student
class student
{
{
int rollno;
int rollno;
char name[20];
char name[20];
};
};
void main()
void main()
{
{
student s;
student s;
cout<<“enter the rollno.:”;
cout<<“enter the rollno.:”;
cin>>s.rollno;
cin>>s.rollno;
cout<<“enter the name:”;
cout<<“enter the name:”;
gets(s.name);
gets(s.name);
cout<<“rollno:”<<s.rollno;
cout<<“rollno:”<<s.rollno;
cout<<“nname:”;
cout<<“nname:”;
puts(s.name);
puts(s.name);
}
}
Class Example (Solution)
Class Example (Solution)
#include<iostream.h>
#include<iostream.h>
#include<stdio.h>
#include<stdio.h>
class student
class student
{
{
public:
public:
int rollno;
int rollno;
char name[20];
char name[20];
};
};
void main()
void main()
{
{
student s;
student s;
cout<<“enter the rollno.:”;
cout<<“enter the rollno.:”;
cin>>s.rollno;
cin>>s.rollno;
cout<<“enter the name:”;
cout<<“enter the name:”;
gets(s.name);
gets(s.name);
cout<<“rollno:”<<s.rollno;
cout<<“rollno:”<<s.rollno;
cout<<“nname:”;
cout<<“nname:”;
puts(s.name);
puts(s.name);
}
}
Implementing class methods
Implementing class methods

There are two ways:
There are two ways:
1.
1. Member functions defined outside class
Member functions defined outside class

Using Binary scope resolution operator (
Using Binary scope resolution operator (::
::)
)

“
“Ties” member name to class name
Ties” member name to class name

Uniquely identify functions of particular class
Uniquely identify functions of particular class

Different classes can have member functions with same
Different classes can have member functions with same
name
name

Format for defining member functions
Format for defining member functions
ReturnType ClassName
ReturnType ClassName::
::MemberFunctionName
MemberFunctionName()
()
{
{
…
…
}
}
Member Function Defining Inside the Class
Member Function Defining Inside the Class
#include<iostream.h>
#include<iostream.h>
#include<stdio.h>
#include<stdio.h>
class student
class student
{
{
int rollno;
int rollno;
char name[20];
char name[20];
public:
public:
void getdata()
void getdata()
{
{
cout<<“enter the rollno.:”;
cout<<“enter the rollno.:”;
cin>>rollno;
cin>>rollno;
cout<<“enter the name:”;
cout<<“enter the name:”;
gets(name);
gets(name);
}
}
void putdata()
void putdata()
{
{
cout<<“rollno:”<<rollno;
cout<<“rollno:”<<rollno;
cout<<“nname:”;
cout<<“nname:”;
puts(name);
puts(name);
}
}
};
};
Data Members (Private : in this example)
Member Functions (Public: in this example)
Calling member function
void main()
{
student s;
s.getdata();
s.putdata();
}
Member Function
Member Function
Defining Outside the Class
Defining Outside the Class
#include<iostream.h>
#include<iostream.h>
#include<stdio.h>
#include<stdio.h>
class student
class student
{
{
int rollno;
int rollno;
char name[20];
char name[20];
public:
public:
void getdata();
void getdata();
void putdata();
void putdata();
};
};
void student :: getdata()
void student :: getdata()
{
{
cout<<“enter the rollno.:”;
cout<<“enter the rollno.:”;
cin>>rollno;
cin>>rollno;
cout<<“enter the name:”;
cout<<“enter the name:”;
gets(name);
gets(name);
}
}
void student: :: putdata()
void student: :: putdata()
{
{
cout<<“rollno:”<<rollno;
cout<<“rollno:”<<rollno;
cout<<“nname:”;
cout<<“nname:”;
puts(name);
puts(name);
}
}
void main()
void main()
{
{
student s;
student s;
s.getdata();
s.getdata();
s.putdata();
s.putdata();
}
}
Characteristics of member
Characteristics of member
function
function

Different classes have same function name. the
Different classes have same function name. the
“membership label” will resolve their scope.
“membership label” will resolve their scope.

Member functions can access the private data of the
Member functions can access the private data of the
class .a non member function cannot do this.(friend
class .a non member function cannot do this.(friend
function can do this.)
function can do this.)

A member function can call another member function
A member function can call another member function
directly, without using the dot operator.
directly, without using the dot operator.
Accessing Class Members
Accessing Class Members

Operators to access class members
Operators to access class members

Identical to those for
Identical to those for struct
structs
s

Dot member selection operator (
Dot member selection operator (.
.)
)

Object
Object

Reference to object
Reference to object

Arrow member selection operator (
Arrow member selection operator (->
->)
)

Pointers
Pointers
Static members
Static members

The data and functions of the class may be declared static in the
The data and functions of the class may be declared static in the
class declaration.
class declaration.

The static data members have similar properties to the C static
The static data members have similar properties to the C static
variable.
variable.

The static data members is initialized with zero when the first
The static data members is initialized with zero when the first
object of its class is created. No other initialization is permitted.
object of its class is created. No other initialization is permitted.

Only one copy of that member is created for the entire class and
Only one copy of that member is created for the entire class and
is shared by all the objects of that class, no matter how many
is shared by all the objects of that class, no matter how many
objects are created.
objects are created.

It is visible only within the class, but its lifetime is the entire
It is visible only within the class, but its lifetime is the entire
program.
program.
Static member function
Static member function

Like static data members we can also declare static
Like static data members we can also declare static
member functions.
member functions.

A static function can have access to only other static
A static function can have access to only other static
members(functions or variables) declared in the
members(functions or variables) declared in the
same class.
same class.

A static member function can be called using the
A static member function can be called using the
class (instead of its objects) as folows
class (instead of its objects) as folows

Class name:: function name.
Class name:: function name.
Example of static members
Example of static members
#inlcude<iostream.h>
#inlcude<iostream.h>
Class test
Class test
{
{
Int code;
Int code;
Static int count;
Static int count;
Public:
Public:
Void setcode()
Void setcode()
{
{
Code=++count;
Code=++count;
}
}
Void showcode()
Void showcode()
{
{
Cout<<“object number “<<code<<endl;
Cout<<“object number “<<code<<endl;
}
}
Static void showcount()
Static void showcount()
{
{
Cout<<“count :”<<count;
Cout<<“count :”<<count;
}
}
};
};
Int test::count;
Int test::count;
Int main()
Int main()
{
{
test t1,t2;
test t1,t2;
t1.setcode();
t1.setcode();
t2.setcode();
t2.setcode();
test::showcount();
test::showcount();
test t3;
test t3;
t3.setcode();
t3.setcode();
test::showcount();
test::showcount();
t1.showcode();
t1.showcode();
t2.showcode();
t2.showcode();
t3.showcode();
t3.showcode();
Return 0;
Return 0;
}
}
Class inside a function
Class inside a function
 When a class declared within a function, it is known as
When a class declared within a function, it is known as
local class
local class.
.
 A local class is known only to that function and
A local class is known only to that function and
unknown outside it.
unknown outside it.
 All member functions must be defined within the class
All member functions must be defined within the class
declaration.
declaration.
 The local class may not use local variables of the
The local class may not use local variables of the
function in which it is declared except static and extern
function in which it is declared except static and extern
local variables declared within the function.
local variables declared within the function.
 No static variables may be declared inside a local class.
No static variables may be declared inside a local class.
 Due to these restrictions local class is not popular in C++
Due to these restrictions local class is not popular in C++
programming.
programming.
Objects
Objects

An object is an instance of a class.
An object is an instance of a class.

An object is a class variable.
An object is a class variable.

Is can be uniquely identified by its name.
Is can be uniquely identified by its name.
 Every object have a state which is represented by the
Every object have a state which is represented by the
values of its attributes. These state are changed by
values of its attributes. These state are changed by
function which applied on the object.
function which applied on the object.
State identity and behavior of
State identity and behavior of
objects
objects
 Every object have
Every object have identity , behaviour and state
identity , behaviour and state.
.
 The identity of object
The identity of object is defined by its name, every
is defined by its name, every
object is unique and can be differentiated from other
object is unique and can be differentiated from other
objects.
objects.
 The behavior of
The behavior of an object is represented by the
an object is represented by the
functions which are defined in the object’s class. These
functions which are defined in the object’s class. These
function show the set of action for every objects.
function show the set of action for every objects.
 The state of objects
The state of objects are referred by the data stored
are referred by the data stored
within the object at any time moment.
within the object at any time moment.
Creating an object of a Class
Creating an object of a Class

Declaring a variable of a class type creates an
Declaring a variable of a class type creates an object
object. You
. You
can have many variables of the same type (class).
can have many variables of the same type (class).

Also known as Instantiation
Also known as Instantiation

Once an object of a certain class is instantiated, a new
Once an object of a certain class is instantiated, a new
memory location is created for it to store its data members
memory location is created for it to store its data members
and code
and code

You can instantiate many objects from a class type.
You can instantiate many objects from a class type.

Ex) Circle c; Circle *c;
Ex) Circle c; Circle *c;
Class item
Class item
{
{
………
……….
.
,,,,,,,,,,,,,
,,,,,,,,,,,,,
}x,y,z;
}x,y,z;
We have to declared objects close to the place where they are needed
We have to declared objects close to the place where they are needed
because it makes easier to identify the objects.
because it makes easier to identify the objects.
Object types
Object types
 There are four types of objects
There are four types of objects
1.
1. External (global )objects
External (global )objects
1.
1. This object have the existence throughout the lifetime of the program and
This object have the existence throughout the lifetime of the program and
having file –scope.
having file –scope.
2.
2. Automatic(local)objects
Automatic(local)objects
1.
1. Persistent and visible only throughout the local scope in which they are
Persistent and visible only throughout the local scope in which they are
created.
created.
3.
3. Static objects
Static objects
1.
1. Persistent throughout a program but only visible within their local scope.
Persistent throughout a program but only visible within their local scope.
4.
4. Dynamic objects
Dynamic objects
1.
1. Lifetime may be controlled within a particular scope.
Lifetime may be controlled within a particular scope.
Memory Allocation of Object
Memory Allocation of Object
class student
class student
{
{
int rollno;
int rollno;
char name[20];
char name[20];
int marks;
int marks;
};
};
student s;
student s;
rollno – 2 bytes
name- 20 bytes
marks- 2 bytes
24 bytes s
Array of objects
Array of objects

The array of class type variable is known as array of
The array of class type variable is known as array of
object.
object.

We can declare array of object as following way:-
We can declare array of object as following way:-
Class _name object [length];
Class _name object [length];
Employee manager[3];
Employee manager[3];
1.
1. We can use this array when calling a member function
We can use this array when calling a member function
2.
2. Manager[i].put data();
Manager[i].put data();
3.
3. The array of object is stored in memory as a multi-
The array of object is stored in memory as a multi-
dimensional array.
dimensional array.
Object as function arguments
Object as function arguments

This can be done in two ways:-
This can be done in two ways:-

A copy of entire object is passed to the function.
A copy of entire object is passed to the function.
 ( pass by value)
( pass by value)
 Only the address of the object is transferred to the
Only the address of the object is transferred to the
function. (pass by reference)
function. (pass by reference)
( pass by value)
( pass by value)
 A copy of the object is passed to the function, any
A copy of the object is passed to the function, any
changes made to the object inside the function do not
changes made to the object inside the function do not
affect the object used to call function.
affect the object used to call function.
 When an address of object is passed, the called
When an address of object is passed, the called
function works directly on the actual object used in
function works directly on the actual object used in
the call. Means that any change made in side the
the call. Means that any change made in side the
function will reflect in the actual object.
function will reflect in the actual object.
(pass by reference)
Passing Object
Passing Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
void sum (Complex A, Complex B);
void sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
void Complex : : sum ( complex A, complex B)
void Complex : : sum ( complex A, complex B)
{
{
real = A.real + B.real;
real = A.real + B.real;
imag= A.imag + B.imag;
imag= A.imag + B.imag;
}
}
void main( )
void main( )
{
{
Complex X,Y,Z;
Complex X,Y,Z;
X.getdata( );
X.getdata( );
Y.getdata( );
Y.getdata( );
Z.sum(X,Y);
Z.sum(X,Y);
Z.putdata( );
Z.putdata( );
}
}
Passing Object
Passing Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
void sum (Complex A, Complex B);
void sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
void Complex : : sum ( Complex A, Complex B)
void Complex : : sum ( Complex A, Complex B)
{
{
real = A.real + B.real;
real = A.real + B.real;
imag= A.imag + B.imag;
imag= A.imag + B.imag;
}
}
void main( )
void main( )
{
{
Complex X,Y,Z;
Complex X,Y,Z;
X.getdata( );
X.getdata( );
Y.getdata( );
Y.getdata( );
Z.sum(X,Y);
Z.sum(X,Y);
Z.putdata( );
Z.putdata( );
}
}
X Y Z
Passing Object
Passing Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
void sum (Complex A, Complex B);
void sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
void Complex : : sum ( Complex A, Complex B)
void Complex : : sum ( Complex A, Complex B)
{
{
real = A.real + B.real;
real = A.real + B.real;
imag= A.imag + B.imag;
imag= A.imag + B.imag;
}
}
void main( )
void main( )
{
{
Complex X,Y,Z;
Complex X,Y,Z;
X.getdata( );
X.getdata( );
Y.getdata( );
Y.getdata( );
Z.sum(X,Y);
Z.sum(X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
Passing Object
Passing Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
void sum (Complex A, Complex B);
void sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
void Complex : : sum ( Complex A, Complex B)
void Complex : : sum ( Complex A, Complex B)
{
{
real = A.real + B.real;
real = A.real + B.real;
imag= A.imag + B.imag;
imag= A.imag + B.imag;
}
}
void main( )
void main( )
{
{
Complex X,Y,Z;
Complex X,Y,Z;
X.getdata( );
X.getdata( );
Y.getdata( );
Y.getdata( );
Z.sum(X,Y);
Z.sum(X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
5
6
7
8
A B
Passing Object
Passing Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
void sum(Complex A, Complex B);
void sum(Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
void Complex : : sum ( Complex A, Complex B)
void Complex : : sum ( Complex A, Complex B)
{
{
real = A.real + B.real;
real = A.real + B.real;
imag= A.imag + B.imag;
imag= A.imag + B.imag;
}
}
void main( )
void main( )
{
{
Complex X,Y,Z;
Complex X,Y,Z;
X.getdata( );
X.getdata( );
Y.getdata( );
Y.getdata( );
Z.sum(X,Y);
Z.sum(X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
12
14
X Y Z
5
6
7
8
A B
+
+
=
=
Passing Object
Passing Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
void sum (Complex A, Complex B);
void sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
void complex : : sum ( Complex A, Complex B)
void complex : : sum ( Complex A, Complex B)
{
{
real = A.real + B.real;
real = A.real + B.real;
imag= A.imag + B.imag;
imag= A.imag + B.imag;
}
}
void main( )
void main( )
{
{
Complex X,Y,Z;
Complex X,Y,Z;
X.getdata( );
X.getdata( );
Y.getdata( );
Y.getdata( );
Z.sum(X,Y);
Z.sum(X,Y);
Z.putdata( );
Z.putdata( );
}
}
12 + 14 i
12 + 14 i
5
6
7
8
12
14
X Y Z
5
6
7
8
A B
+
+
=
=
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
X Y Z
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
12
14
temp
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
12
14
temp
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
12
14
X Y Z
7
8
B
12
14
temp
Returning Object
Returning Object
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
Complex sum (Complex B);
Complex sum (Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex Complex : : sum (Complex B)
Complex Complex : : sum (Complex B)
{
{
Complex temp;
Complex temp;
temp.real=real + B.real;
temp.real=real + B.real;
temp.imag= imag + B.imag;
temp.imag= imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= X.sum (Y);
Z= X.sum (Y);
Z.putdata( );
Z.putdata( );
}
}
12 + 14 i
12 + 14 i
5
6
7
8
12
14
X Y Z
7
8
B
12
14
temp
C++ garbage collection
C++ garbage collection

In c++ the garbage collection task is accomplised by
In c++ the garbage collection task is accomplised by
mark and sweep algorithm.
mark and sweep algorithm.
 In this approach the garbage collector periodically
In this approach the garbage collector periodically
examines every single pointer in our program and
examines every single pointer in our program and
find that the memory is still in use. At the end of the
find that the memory is still in use. At the end of the
cycle, any memory that has not been marked is
cycle, any memory that has not been marked is
deemed to be not in use and is freed.
deemed to be not in use and is freed.
Difference between static and
Difference between static and
dynamic memory allocation
dynamic memory allocation
Static memory allocation Dynamic memory allocation
Static memory is allocated automatically
by compiler when definition statements
are encountered.
Dynamic memory is allocated only when
there is explicit call to malloc, calloc or
realloc function.
To make static memory allocation , the
amount of the memory space to be
reserved should be known at the run time.
Amount of memory to be reserved can be
given at the run time.
In static memory allocation sometimes
memory wastage occurs because memory
is already known and it can not change.
Memory wastage is avoided due to
memory allocation occur at run time.
Memory allocated at the compile time has
static lifetime.
Memory allocated at run time has
dynamic lifetime.
Its is faster it is slower
Friend function
Friend function

C++ allows a way through which a function can access the private data of a
C++ allows a way through which a function can access the private data of a
class.
class.

Such a function need not be a class member, it may be member function of
Such a function need not be a class member, it may be member function of
another class or may be non member function.
another class or may be non member function.

This function is called FRIEND FUNCTION. The declaration should be
This function is called FRIEND FUNCTION. The declaration should be
preceded by keyword FRIEND.
preceded by keyword FRIEND.
Class PQr
Class PQr
{
{
Private:
Private:
………
………
Public:
Public:
……
……
……
……
Friend void abc();
Friend void abc();
};
};

The function is defined elsewhere
The function is defined elsewhere
in the program like normal
in the program like normal
function.
function.

Function definition does not use
Function definition does not use
either keyword FRIEND or scope
either keyword FRIEND or scope
operator.
operator.

Functions that are declared with
Functions that are declared with
FRIEND keyword are known as
FRIEND keyword are known as
friend functions.
friend functions.

A function can declared as friend in number of class.
A function can declared as friend in number of class.

A friend function has full access right to access the
A friend function has full access right to access the
private members of class.
private members of class.
 Member function of one class can be friend of
Member function of one class can be friend of
another class.
another class.
Characteristics
Characteristics

It is not in the scope of the class in which it has been
It is not in the scope of the class in which it has been
declared as friend.
declared as friend.

it is not in the scope of class so it cannot be called
it is not in the scope of class so it cannot be called
using object of that class.
using object of that class.

It can be invoked like normal function ,without
It can be invoked like normal function ,without
object.
object.

It can be declared either in public or private part with
It can be declared either in public or private part with
out affecting its meaning.
out affecting its meaning.
 Usually, it has the objects as arguments.
Usually, it has the objects as arguments.
 Unlike member function, it cannot access the member
Unlike member function, it cannot access the member
names directly and has to use an object name and dot
names directly and has to use an object name and dot
membership operator with each name. like
membership operator with each name. like

A.h
A.h
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
X Y Z
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
5
6
A
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
5
6
A
+
+
=
=
temp
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
X Y Z
7
8
B
5
6
A
12
14
+
+
=
=
temp
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
5
6
7
8
12
14
X Y Z
7
8
B
5
6
A
12
14
+
+
=
=
temp
Friend Function
Friend Function
#include<iostream.h>
#include<iostream.h>
class Complex
class Complex
{
{
float real, imag;
float real, imag;
public:
public:
void getdata( );
void getdata( );
void putdata( );
void putdata( );
friend Complex sum (Complex A, Complex B);
friend Complex sum (Complex A, Complex B);
};
};
void Complex : : getdata( )
void Complex : : getdata( )
{
{
cout<<“enter real part:”;
cout<<“enter real part:”;
cin>>real;
cin>>real;
cout<<“enter imaginary part:”;
cout<<“enter imaginary part:”;
cin>>imag;
cin>>imag;
}
}
void Complex : : putdata( )
void Complex : : putdata( )
{
{
if (imag>=0)
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
cout<<real<<“+”<<imag<<“i”;
else
else
cout<<real<<imag<<“i”;
cout<<real<<imag<<“i”;
}
}
Complex sum (Complex A, Complex B)
Complex sum (Complex A, Complex B)
{
{
Complex temp;
Complex temp;
temp.real=A.real + B.real;
temp.real=A.real + B.real;
temp.imag= A.imag + B.imag;
temp.imag= A.imag + B.imag;
return temp;
return temp;
}
}
void main ( )
void main ( )
{
{
Complex X, Y, Z;
Complex X, Y, Z;
X.Getdata( );
X.Getdata( );
Y. getdata( );
Y. getdata( );
Z= sum (X,Y);
Z= sum (X,Y);
Z.putdata( );
Z.putdata( );
}
}
12 + 14 i
12 + 14 i
5
6
7
8
12
14
X Y Z
7
8
B
5
6
A
12
14
+
+
=
=
temp
 We can also declare all the member functions of one
We can also declare all the member functions of one
class as the friend functions of another class. In this
class as the friend functions of another class. In this
case the first class is known as FRIEND class.
case the first class is known as FRIEND class.
 This can be specified as follows :-
This can be specified as follows :-
Class z
Class z
{
{
…………
…………..
..
……
……
Friend class x;
Friend class x;
};
};
A function friend in two classes
A function friend in two classes
Void max(XYZ m,ABC n)
Void max(XYZ m,ABC n)
{
{
If(m.x>=n.a)
If(m.x>=n.a)
Cout<<m.x;
Cout<<m.x;
Else
Else
Cout<<n.a;
Cout<<n.a;
}
}
Int main ()
Int main ()
{
{
ABC abc;
ABC abc;
abc..setvalue(10);
abc..setvalue(10);
XYZ xyz;
XYZ xyz;
Xyz.setvalue(20);
Xyz.setvalue(20);
max(xyz,abc);
max(xyz,abc);
Return 0;
Return 0;
}
}
#include<iostream.h>
#include<iostream.h>
Class ABC;
Class ABC;
Class XYZ
Class XYZ
{ int x;
{ int x;
Public:
Public:
Void setvalue(int i)
Void setvalue(int i)
{ x=i ; }
{ x=i ; }
Friend void max(XYZ,ABC);
Friend void max(XYZ,ABC);
};
};
Class ABC
Class ABC
{
{ int a;
int a;
Public:
Public:
void setvalue(int i)
void setvalue(int i)
{
{ a=i ;}
a=i ;}
Friend void max(XYZ,ABC);
Friend void max(XYZ,ABC);
};
};
Pass by reference
Pass by reference
#include<iostream.h>
#include<iostream.h>
Class class_2;
Class class_2;
Class class_1
Class class_1
{
{ int value1;
int value1;
Public:
Public:
void indata(int a)
void indata(int a)
{
{ value1=a;
value1=a; }
}
Void display()
Void display()
{
{ cout<<value1<<“n”;
cout<<value1<<“n”; }
}
Friend void exchange(class_1 &,
Friend void exchange(class_1 &,
class_2 &);
class_2 &);
};
};
Class class_2
Class class_2
{
{ int value2;
int value2;
public:
public:
void indata( int a)
void indata( int a)
{
{ value2=a;
value2=a; }
}
Void display()
Void display()
{
{ cout<<value2<<“n”;
cout<<value2<<“n”; }
}
Friend void exchange(class_1
Friend void exchange(class_1
&,class_2 &);
&,class_2 &);
};
};
Contd…
Void exchange(class_1 & x,class_2
Void exchange(class_1 & x,class_2
& y)
& y)
{
{ int temp=x.value1;
int temp=x.value1;
x.value1=y.value2;
x.value1=y.value2;
y.value2=temp;
y.value2=temp;
}
}
Int main()
Int main()
{
{
class_1 c1;
class_1 c1;
Class_2 c2;
Class_2 c2;
C1.indata(100);
C1.indata(100);
C2.indata(200);
C2.indata(200);
Cout<<“values before exchange”<<“
Cout<<“values before exchange”<<“
n”;
n”;
C1.dispaly();
C1.dispaly();
C2.display();
C2.display();
Exchange(c1,c2);
Exchange(c1,c2);
Cout<<“values after exchange”<<“n”;
Cout<<“values after exchange”<<“n”;
C1.display();
C1.display();
C2.display();
C2.display();
Return 0;
Return 0;
}
}
Inline function
Inline function

Inline is a function that expanded in a line when it is invoked.
Inline is a function that expanded in a line when it is invoked.

The compiler replaces the function call by its corresponding
The compiler replaces the function call by its corresponding
code.
code.

Syntax:
Syntax:

Inline return type function name
Inline return type function name

{
{

Function body
Function body

}
}

Improve the execution speed.
Improve the execution speed.

Reduces the memory requirement of function execution.
Reduces the memory requirement of function execution.

All inline function must be defined before they called.
All inline function must be defined before they called.

The speed benefits of inline function diminish as the function
The speed benefits of inline function diminish as the function
grows in size.
grows in size.

A function definition in a class definition is an inline function
A function definition in a class definition is an inline function
definition, even without the use of the
definition, even without the use of the inline
inline specifier.
specifier.

Where inline may not work
Where inline may not work

For functions returning values , if a loop, switch, goto statements.
For functions returning values , if a loop, switch, goto statements.

Functions not returning values, if return exists.
Functions not returning values, if return exists.

If function contain static variables.
If function contain static variables.

If inline functions are recursive
If inline functions are recursive

When function call becomes small compare to function execution.
When function call becomes small compare to function execution.

#include<iostream.h>
#include<iostream.h>

Inline float add(float x , float y)
Inline float add(float x , float y)

{
{

Return (x+y);
Return (x+y);

}
}

Inline float sub(float p , float q )
Inline float sub(float p , float q )

{
{
Return(p-q);
Return(p-q);
}
}
Int main()
Int main()
{
{
Float a=12.34;
Float a=12.34;
Float b=3.6
Float b=3.6
Cout<<add(a,b)<<endl;
Cout<<add(a,b)<<endl;
cout<<sub(a,b)<<endl;
cout<<sub(a,b)<<endl;
Return 0;
Return 0;
}
}
Default arguments
Default arguments

A default argument is a value given in the function
A default argument is a value given in the function
declaration that the compiler automatically inserts if the
declaration that the compiler automatically inserts if the
caller does not provide a value for that argument in the
caller does not provide a value for that argument in the
function call.
function call.

Syntax:
Syntax:
return_type f(…, type x = default_value,…);
Default arguments
Default arguments

Default values are specified when the function is declared.
Default values are specified when the function is declared.

We must add default values from right to left ,we can not
We must add default values from right to left ,we can not
provide a default value to a particular arguments in the
provide a default value to a particular arguments in the
middle of argument list.
middle of argument list.

Default arguments are useful in situations where some
Default arguments are useful in situations where some
arguments always have the same value.
arguments always have the same value.
Default Arguments
Default Arguments
(Examples)
(Examples)
 double
double pow
pow(
(double
double x,
x, int
int n=2)
n=2)

// computes and returns x
// computes and returns xn
n
The default value of the 2nd
argument is 2.
This means that if the programmer calls pow(x), the
compiler will replace that call with pow(x,2), returning
x2
Default Arguments
Default Arguments
(Rules)
(Rules)

Once an argument has a default value, all the arguments
Once an argument has a default value, all the arguments
after it must have default values.
after it must have default values.

Once an argument is defaulted in a function call, all the
Once an argument is defaulted in a function call, all the
remaining arguments must be defaulted.
remaining arguments must be defaulted.
int f(int x, int y=0, int n)
// illegal
int f(int x, int y=0, int n=1)
// legal
Examples:-
Examples:-

Int mul(int I,int j=6,int l=9); legal
Int mul(int I,int j=6,int l=9); legal

Int mul(int I,int j=6,int l); illegal
Int mul(int I,int j=6,int l); illegal

Int mul(int I=0,int j,int l=9); illegal
Int mul(int I=0,int j,int l=9); illegal

Int mul(int I=0,int j=6,int l=9); legal
Int mul(int I=0,int j=6,int l=9); legal

Advantages:-
Advantages:-

We can default arguments to add new parameters to the
We can default arguments to add new parameters to the
existing functions.
existing functions.

Default arguments can be used to combine similar
Default arguments can be used to combine similar
functions into one.
functions into one.
Function overloading
Function overloading

When using more than one functions with same name and
When using more than one functions with same name and
with different arguments in a program is known as
with different arguments in a program is known as
function overloading or function polymorphism.
function overloading or function polymorphism.

Function overloading is part of polymorphism.
Function overloading is part of polymorphism.
Function overloading
Function overloading

Function would perform different operations depending
Function would perform different operations depending
on the argument list in function call.
on the argument list in function call.

Correct function to be invoked is determined by checking
Correct function to be invoked is determined by checking
the number and type of arguments but not on return type
the number and type of arguments but not on return type
of function.
of function.

Examples;-
Examples;-

Int area(int,int);
Int area(int,int);

Int area(int ,float);
Int area(int ,float);
Function overloading
Function overloading

Examples :-
Examples :-

Int add(int a, int b);
Int add(int a, int b);

Int add(int a, int b, int c);
Int add(int a, int b, int c);

Double add(double x, double y);
Double add(double x, double y);

Double add(int p ,double q);
Double add(int p ,double q);

Double add(double p, int q);
Double add(double p, int q);

Function calls
Function calls

Add(5,19);
Add(5,19);

Add(16,7.9);
Add(16,7.9); Add(12.4,3.5);
Add(12.4,3.5);
 Add (4,12,23);
Add (4,12,23); Add(3.4,7)
Add(3.4,7)
Function prototype 1
Function prototype 2
Function prototype 3
Function prototype4
Function prototype 5
Function overloading
Function overloading

A function call first match the prototype having the same
A function call first match the prototype having the same
number and type of actual arguments and then calls the
number and type of actual arguments and then calls the
appropriate function for execution…
appropriate function for execution…
Function overloading
Function overloading

A function match includes following steps:-
A function match includes following steps:-
1.
1. Compiler first try to find exact match in which the types
Compiler first try to find exact match in which the types
of actual arguments are the same.
of actual arguments are the same.
2.
2. If exact match not found, compiler uses the integral
If exact match not found, compiler uses the integral
promotions to the actual arguments like char to int, float
promotions to the actual arguments like char to int, float
to double.
to double.
Function overloading
Function overloading
3.
3. When either of them fail then compiler uses built in
When either of them fail then compiler uses built in
conversion to the actual arguments and then uses the
conversion to the actual arguments and then uses the
function whose match is unique.
function whose match is unique.
4.
4. If all of the steps fail then the compiler will try user
If all of the steps fail then the compiler will try user
defined conversions in combination with integral
defined conversions in combination with integral
promotions and built in conversions to find a unique
promotions and built in conversions to find a unique
match.
match.

More Related Content

Similar to cpp class unitdfdsfasadfsdASsASass 4.ppt (20)

PPTX
C++ppt. Classs and object, class and object
secondakay
 
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
PPTX
class c++
vinay chauhan
 
PDF
Implementation of oop concept in c++
Swarup Boro
 
PPTX
Class and object
prabhat kumar
 
PPTX
Classes
Sheheryar Gull
 
PPTX
Classes and objects
Shailendra Veeru
 
PPTX
Lecture 2 (1)
zahid khan
 
PDF
Class object
Dr. Anand Bihari
 
PDF
Class and object
Prof. Dr. K. Adisesha
 
PDF
C++ Notes
MOHAMED RIYAZUDEEN
 
PPT
Class objects oopm
Shweta Shah
 
PPTX
C++ classes
Zahid Tanveer
 
PPT
classes & objects.ppt
BArulmozhi
 
PPTX
Classes and objects till 16 aug
shashank12march
 
PPT
c++ lecture 1
sai kumar
 
PPT
c++ lecture 1
sai kumar
 
PPT
c++ introduction
sai kumar
 
PPT
Mca 2nd sem u-2 classes & objects
Rai University
 
PPT
Bca 2nd sem u-2 classes & objects
Rai University
 
C++ppt. Classs and object, class and object
secondakay
 
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
class c++
vinay chauhan
 
Implementation of oop concept in c++
Swarup Boro
 
Class and object
prabhat kumar
 
Classes and objects
Shailendra Veeru
 
Lecture 2 (1)
zahid khan
 
Class object
Dr. Anand Bihari
 
Class and object
Prof. Dr. K. Adisesha
 
Class objects oopm
Shweta Shah
 
C++ classes
Zahid Tanveer
 
classes & objects.ppt
BArulmozhi
 
Classes and objects till 16 aug
shashank12march
 
c++ lecture 1
sai kumar
 
c++ lecture 1
sai kumar
 
c++ introduction
sai kumar
 
Mca 2nd sem u-2 classes & objects
Rai University
 
Bca 2nd sem u-2 classes & objects
Rai University
 

More from nandemprasanna (18)

PPTX
CSS propertievcvcbccccbvcccccvcbvcvbcs.pptx
nandemprasanna
 
PPT
RADRAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1.ppt
nandemprasanna
 
PPTX
1707325642974_Classes fffand objects.pptx
nandemprasanna
 
PPTX
ilovepdf_mergebvhfhbxcbcgsdgsdvcxcvd.pptx
nandemprasanna
 
PPT
LECT3A (1).PPThhdfghdfhdfghdhdhdfsfdfgsfd
nandemprasanna
 
PPT
4_25655_SE291_2020_1__2_1_Lecture 3 - Software Process Models (1).ppt
nandemprasanna
 
PPT
Intro to Multimediartgtwertrwtwetwttw.ppt
nandemprasanna
 
PPTX
Multimedia.pptxgdsssssssssssssssssssssssssssss
nandemprasanna
 
PPTX
Internet programming computers presentation.pptx
nandemprasanna
 
PPTX
versionsofwindows1-20000000413054842.pptx
nandemprasanna
 
PPTX
MSWORD programming computers fundamentales.pptx
nandemprasanna
 
PPTX
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
nandemprasanna
 
PPT
COMPUTERS STRUCTURES OS YHE Sblock diagram.ppt
nandemprasanna
 
PPTX
introductiontocprogramming datatypespp.pptx
nandemprasanna
 
PPT
datatypesinformation datatypes finin5.ppt
nandemprasanna
 
PPTX
Desktop Publishing Example basics presentation
nandemprasanna
 
PPT
Computer-Networks,types of lan wan manNetwork.ppt
nandemprasanna
 
PPTX
operating system basic and windows versions and types of operating system
nandemprasanna
 
CSS propertievcvcbccccbvcccccvcbvcvbcs.pptx
nandemprasanna
 
RADRAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1.ppt
nandemprasanna
 
1707325642974_Classes fffand objects.pptx
nandemprasanna
 
ilovepdf_mergebvhfhbxcbcgsdgsdvcxcvd.pptx
nandemprasanna
 
LECT3A (1).PPThhdfghdfhdfghdhdhdfsfdfgsfd
nandemprasanna
 
4_25655_SE291_2020_1__2_1_Lecture 3 - Software Process Models (1).ppt
nandemprasanna
 
Intro to Multimediartgtwertrwtwetwttw.ppt
nandemprasanna
 
Multimedia.pptxgdsssssssssssssssssssssssssssss
nandemprasanna
 
Internet programming computers presentation.pptx
nandemprasanna
 
versionsofwindows1-20000000413054842.pptx
nandemprasanna
 
MSWORD programming computers fundamentales.pptx
nandemprasanna
 
functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx
nandemprasanna
 
COMPUTERS STRUCTURES OS YHE Sblock diagram.ppt
nandemprasanna
 
introductiontocprogramming datatypespp.pptx
nandemprasanna
 
datatypesinformation datatypes finin5.ppt
nandemprasanna
 
Desktop Publishing Example basics presentation
nandemprasanna
 
Computer-Networks,types of lan wan manNetwork.ppt
nandemprasanna
 
operating system basic and windows versions and types of operating system
nandemprasanna
 
Ad

Recently uploaded (9)

PPTX
The effects of innovative finishing materials and creative accessories on mar...
JulianaPerez73
 
PDF
Yellow and Orange Illustrative Self-Improvement Infographic Poster.pdf
ajmalshans064
 
PPTX
"Lighting the Future: Exploring Unique Pendant Light Designs"
Manaralights
 
PPTX
Mitochondrion.pptxHQYT8FFRFFFFFFFFFFFFFF
viojancarljoseph
 
PDF
S1Tu5 Par4 Pem3NanG, M0Dal Rec3H dIj4m1N M3nANg B4ny4k. Daft4r S3kar4ng & Lan...
hokimamad0
 
PDF
Top Trends in Dhawani Sets Fabric, Colours, and Styling Tips.pdf
Mahalekshmi Silks
 
PPTX
Creative fashion, bold streetwear, visual storytelling, and brand identity sh...
stiction Wear
 
PPTX
Mastering Men’s Style: The Power of a Tailored Wardrobe
Don Morphy
 
PPTX
Presentationsssssssssssssssssssssssssssssssssss.pptx
sarmanali014
 
The effects of innovative finishing materials and creative accessories on mar...
JulianaPerez73
 
Yellow and Orange Illustrative Self-Improvement Infographic Poster.pdf
ajmalshans064
 
"Lighting the Future: Exploring Unique Pendant Light Designs"
Manaralights
 
Mitochondrion.pptxHQYT8FFRFFFFFFFFFFFFFF
viojancarljoseph
 
S1Tu5 Par4 Pem3NanG, M0Dal Rec3H dIj4m1N M3nANg B4ny4k. Daft4r S3kar4ng & Lan...
hokimamad0
 
Top Trends in Dhawani Sets Fabric, Colours, and Styling Tips.pdf
Mahalekshmi Silks
 
Creative fashion, bold streetwear, visual storytelling, and brand identity sh...
stiction Wear
 
Mastering Men’s Style: The Power of a Tailored Wardrobe
Don Morphy
 
Presentationsssssssssssssssssssssssssssssssssss.pptx
sarmanali014
 
Ad

cpp class unitdfdsfasadfsdASsASass 4.ppt

  • 2.  Class: A Class is a user defined data type to Class: A Class is a user defined data type to implement an abstract object. Abstract means implement an abstract object. Abstract means to hide the details. A Class is a combination of to hide the details. A Class is a combination of data and functions. data and functions.  Data is called as data members and functions Data is called as data members and functions are called as member functions. are called as member functions.
  • 3.  Abstract data type:- Abstract data type:-  A data type that separates the logical properties from A data type that separates the logical properties from the implementation details called Abstract Data the implementation details called Abstract Data Type(ADT). Type(ADT).  An abstract data type is a set of object and an An abstract data type is a set of object and an associated set of operations on those objects. associated set of operations on those objects.  ADT supports data abstraction, encapsulation and data ADT supports data abstraction, encapsulation and data hiding. hiding.
  • 4.  Examples of ADT are:- Examples of ADT are:-  Boolean Boolean  Integer Integer built in ADT built in ADT  Array Array  Stack Stack  Queue Queue  Tree search structure Tree search structure User defined User defined ADT ADT  Boolean {operations are AND,OR,NOT and Boolean {operations are AND,OR,NOT and values are true and false} values are true and false}  Queues{operations are create , dequeue,inqueue and Queues{operations are create , dequeue,inqueue and values are queue elements} values are queue elements}
  • 5. Class definition Class definition  A class definition begins with the keyword A class definition begins with the keyword class class. .  The body of the class is contained within a set The body of the class is contained within a set of braces, of braces, { } ; { } ; (notice the semi-colon). (notice the semi-colon). class class_name { …. …. …. }; Class body (data member + methods methods) Any valid identifier
  • 6.  Within the body, the keywords Within the body, the keywords private: private: and and public: public: specify the access level of the specify the access level of the members of the class. members of the class.  the default is the default is private private. .  Usually, the data members of a class are Usually, the data members of a class are declared in the declared in the private: private: section of the class and section of the class and the member functions are in the member functions are in public: public: section. section.
  • 7.  Data member or member functions may be public, Data member or member functions may be public, private or protected. private or protected.  Public means data members or member functions Public means data members or member functions defining inside the class can be used at outside the defining inside the class can be used at outside the class.( in different class and in main function) class.( in different class and in main function)  Member access specifiers Member access specifiers  public: public:  can be accessed outside the class directly. can be accessed outside the class directly.  The public stuff is The public stuff is the interface the interface. .
  • 8.  private: private:  Accessible only to member functions of class Accessible only to member functions of class  Private members and methods are for internal Private members and methods are for internal use only. use only.  Private means data members and member functions Private means data members and member functions can’t be used outside the class. can’t be used outside the class.  Protected means data member and member Protected means data member and member functions can be used in the same class and its functions can be used in the same class and its derived class (at one level) (not inmain function). derived class (at one level) (not inmain function).
  • 11.  This class example shows how we can This class example shows how we can encapsulate (gather) a circle information into encapsulate (gather) a circle information into one package (unit or class) one package (unit or class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)
  • 12. Class Example (Problem) Class Example (Problem) #include<iostream.h> #include<iostream.h> #include<stdio.h> #include<stdio.h> class student class student { { int rollno; int rollno; char name[20]; char name[20]; }; }; void main() void main() { { student s; student s; cout<<“enter the rollno.:”; cout<<“enter the rollno.:”; cin>>s.rollno; cin>>s.rollno; cout<<“enter the name:”; cout<<“enter the name:”; gets(s.name); gets(s.name); cout<<“rollno:”<<s.rollno; cout<<“rollno:”<<s.rollno; cout<<“nname:”; cout<<“nname:”; puts(s.name); puts(s.name); } }
  • 13. Class Example (Solution) Class Example (Solution) #include<iostream.h> #include<iostream.h> #include<stdio.h> #include<stdio.h> class student class student { { public: public: int rollno; int rollno; char name[20]; char name[20]; }; }; void main() void main() { { student s; student s; cout<<“enter the rollno.:”; cout<<“enter the rollno.:”; cin>>s.rollno; cin>>s.rollno; cout<<“enter the name:”; cout<<“enter the name:”; gets(s.name); gets(s.name); cout<<“rollno:”<<s.rollno; cout<<“rollno:”<<s.rollno; cout<<“nname:”; cout<<“nname:”; puts(s.name); puts(s.name); } }
  • 14. Implementing class methods Implementing class methods  There are two ways: There are two ways: 1. 1. Member functions defined outside class Member functions defined outside class  Using Binary scope resolution operator ( Using Binary scope resolution operator (:: ::) )  “ “Ties” member name to class name Ties” member name to class name  Uniquely identify functions of particular class Uniquely identify functions of particular class  Different classes can have member functions with same Different classes can have member functions with same name name  Format for defining member functions Format for defining member functions ReturnType ClassName ReturnType ClassName:: ::MemberFunctionName MemberFunctionName() () { { … … } }
  • 15. Member Function Defining Inside the Class Member Function Defining Inside the Class #include<iostream.h> #include<iostream.h> #include<stdio.h> #include<stdio.h> class student class student { { int rollno; int rollno; char name[20]; char name[20]; public: public: void getdata() void getdata() { { cout<<“enter the rollno.:”; cout<<“enter the rollno.:”; cin>>rollno; cin>>rollno; cout<<“enter the name:”; cout<<“enter the name:”; gets(name); gets(name); } } void putdata() void putdata() { { cout<<“rollno:”<<rollno; cout<<“rollno:”<<rollno; cout<<“nname:”; cout<<“nname:”; puts(name); puts(name); } } }; }; Data Members (Private : in this example) Member Functions (Public: in this example) Calling member function void main() { student s; s.getdata(); s.putdata(); }
  • 16. Member Function Member Function Defining Outside the Class Defining Outside the Class #include<iostream.h> #include<iostream.h> #include<stdio.h> #include<stdio.h> class student class student { { int rollno; int rollno; char name[20]; char name[20]; public: public: void getdata(); void getdata(); void putdata(); void putdata(); }; }; void student :: getdata() void student :: getdata() { { cout<<“enter the rollno.:”; cout<<“enter the rollno.:”; cin>>rollno; cin>>rollno; cout<<“enter the name:”; cout<<“enter the name:”; gets(name); gets(name); } } void student: :: putdata() void student: :: putdata() { { cout<<“rollno:”<<rollno; cout<<“rollno:”<<rollno; cout<<“nname:”; cout<<“nname:”; puts(name); puts(name); } } void main() void main() { { student s; student s; s.getdata(); s.getdata(); s.putdata(); s.putdata(); } }
  • 17. Characteristics of member Characteristics of member function function  Different classes have same function name. the Different classes have same function name. the “membership label” will resolve their scope. “membership label” will resolve their scope.  Member functions can access the private data of the Member functions can access the private data of the class .a non member function cannot do this.(friend class .a non member function cannot do this.(friend function can do this.) function can do this.)  A member function can call another member function A member function can call another member function directly, without using the dot operator. directly, without using the dot operator.
  • 18. Accessing Class Members Accessing Class Members  Operators to access class members Operators to access class members  Identical to those for Identical to those for struct structs s  Dot member selection operator ( Dot member selection operator (. .) )  Object Object  Reference to object Reference to object  Arrow member selection operator ( Arrow member selection operator (-> ->) )  Pointers Pointers
  • 19. Static members Static members  The data and functions of the class may be declared static in the The data and functions of the class may be declared static in the class declaration. class declaration.  The static data members have similar properties to the C static The static data members have similar properties to the C static variable. variable.  The static data members is initialized with zero when the first The static data members is initialized with zero when the first object of its class is created. No other initialization is permitted. object of its class is created. No other initialization is permitted.  Only one copy of that member is created for the entire class and Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many is shared by all the objects of that class, no matter how many objects are created. objects are created.  It is visible only within the class, but its lifetime is the entire It is visible only within the class, but its lifetime is the entire program. program.
  • 20. Static member function Static member function  Like static data members we can also declare static Like static data members we can also declare static member functions. member functions.  A static function can have access to only other static A static function can have access to only other static members(functions or variables) declared in the members(functions or variables) declared in the same class. same class.  A static member function can be called using the A static member function can be called using the class (instead of its objects) as folows class (instead of its objects) as folows  Class name:: function name. Class name:: function name.
  • 21. Example of static members Example of static members #inlcude<iostream.h> #inlcude<iostream.h> Class test Class test { { Int code; Int code; Static int count; Static int count; Public: Public: Void setcode() Void setcode() { { Code=++count; Code=++count; } } Void showcode() Void showcode() { { Cout<<“object number “<<code<<endl; Cout<<“object number “<<code<<endl; } } Static void showcount() Static void showcount() { { Cout<<“count :”<<count; Cout<<“count :”<<count; } } }; }; Int test::count; Int test::count; Int main() Int main() { { test t1,t2; test t1,t2; t1.setcode(); t1.setcode(); t2.setcode(); t2.setcode(); test::showcount(); test::showcount(); test t3; test t3; t3.setcode(); t3.setcode(); test::showcount(); test::showcount(); t1.showcode(); t1.showcode(); t2.showcode(); t2.showcode(); t3.showcode(); t3.showcode(); Return 0; Return 0; } }
  • 22. Class inside a function Class inside a function  When a class declared within a function, it is known as When a class declared within a function, it is known as local class local class. .  A local class is known only to that function and A local class is known only to that function and unknown outside it. unknown outside it.  All member functions must be defined within the class All member functions must be defined within the class declaration. declaration.  The local class may not use local variables of the The local class may not use local variables of the function in which it is declared except static and extern function in which it is declared except static and extern local variables declared within the function. local variables declared within the function.  No static variables may be declared inside a local class. No static variables may be declared inside a local class.  Due to these restrictions local class is not popular in C++ Due to these restrictions local class is not popular in C++ programming. programming.
  • 23. Objects Objects  An object is an instance of a class. An object is an instance of a class.  An object is a class variable. An object is a class variable.  Is can be uniquely identified by its name. Is can be uniquely identified by its name.  Every object have a state which is represented by the Every object have a state which is represented by the values of its attributes. These state are changed by values of its attributes. These state are changed by function which applied on the object. function which applied on the object.
  • 24. State identity and behavior of State identity and behavior of objects objects  Every object have Every object have identity , behaviour and state identity , behaviour and state. .  The identity of object The identity of object is defined by its name, every is defined by its name, every object is unique and can be differentiated from other object is unique and can be differentiated from other objects. objects.  The behavior of The behavior of an object is represented by the an object is represented by the functions which are defined in the object’s class. These functions which are defined in the object’s class. These function show the set of action for every objects. function show the set of action for every objects.  The state of objects The state of objects are referred by the data stored are referred by the data stored within the object at any time moment. within the object at any time moment.
  • 25. Creating an object of a Class Creating an object of a Class  Declaring a variable of a class type creates an Declaring a variable of a class type creates an object object. You . You can have many variables of the same type (class). can have many variables of the same type (class).  Also known as Instantiation Also known as Instantiation  Once an object of a certain class is instantiated, a new Once an object of a certain class is instantiated, a new memory location is created for it to store its data members memory location is created for it to store its data members and code and code  You can instantiate many objects from a class type. You can instantiate many objects from a class type.  Ex) Circle c; Circle *c; Ex) Circle c; Circle *c; Class item Class item { { ……… ………. . ,,,,,,,,,,,,, ,,,,,,,,,,,,, }x,y,z; }x,y,z; We have to declared objects close to the place where they are needed We have to declared objects close to the place where they are needed because it makes easier to identify the objects. because it makes easier to identify the objects.
  • 26. Object types Object types  There are four types of objects There are four types of objects 1. 1. External (global )objects External (global )objects 1. 1. This object have the existence throughout the lifetime of the program and This object have the existence throughout the lifetime of the program and having file –scope. having file –scope. 2. 2. Automatic(local)objects Automatic(local)objects 1. 1. Persistent and visible only throughout the local scope in which they are Persistent and visible only throughout the local scope in which they are created. created. 3. 3. Static objects Static objects 1. 1. Persistent throughout a program but only visible within their local scope. Persistent throughout a program but only visible within their local scope. 4. 4. Dynamic objects Dynamic objects 1. 1. Lifetime may be controlled within a particular scope. Lifetime may be controlled within a particular scope.
  • 27. Memory Allocation of Object Memory Allocation of Object class student class student { { int rollno; int rollno; char name[20]; char name[20]; int marks; int marks; }; }; student s; student s; rollno – 2 bytes name- 20 bytes marks- 2 bytes 24 bytes s
  • 28. Array of objects Array of objects  The array of class type variable is known as array of The array of class type variable is known as array of object. object.  We can declare array of object as following way:- We can declare array of object as following way:- Class _name object [length]; Class _name object [length]; Employee manager[3]; Employee manager[3]; 1. 1. We can use this array when calling a member function We can use this array when calling a member function 2. 2. Manager[i].put data(); Manager[i].put data(); 3. 3. The array of object is stored in memory as a multi- The array of object is stored in memory as a multi- dimensional array. dimensional array.
  • 29. Object as function arguments Object as function arguments  This can be done in two ways:- This can be done in two ways:-  A copy of entire object is passed to the function. A copy of entire object is passed to the function.  ( pass by value) ( pass by value)  Only the address of the object is transferred to the Only the address of the object is transferred to the function. (pass by reference) function. (pass by reference)
  • 30. ( pass by value) ( pass by value)  A copy of the object is passed to the function, any A copy of the object is passed to the function, any changes made to the object inside the function do not changes made to the object inside the function do not affect the object used to call function. affect the object used to call function.  When an address of object is passed, the called When an address of object is passed, the called function works directly on the actual object used in function works directly on the actual object used in the call. Means that any change made in side the the call. Means that any change made in side the function will reflect in the actual object. function will reflect in the actual object. (pass by reference)
  • 31. Passing Object Passing Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); void sum (Complex A, Complex B); void sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } void Complex : : sum ( complex A, complex B) void Complex : : sum ( complex A, complex B) { { real = A.real + B.real; real = A.real + B.real; imag= A.imag + B.imag; imag= A.imag + B.imag; } } void main( ) void main( ) { { Complex X,Y,Z; Complex X,Y,Z; X.getdata( ); X.getdata( ); Y.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.sum(X,Y); Z.putdata( ); Z.putdata( ); } }
  • 32. Passing Object Passing Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); void sum (Complex A, Complex B); void sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } void Complex : : sum ( Complex A, Complex B) void Complex : : sum ( Complex A, Complex B) { { real = A.real + B.real; real = A.real + B.real; imag= A.imag + B.imag; imag= A.imag + B.imag; } } void main( ) void main( ) { { Complex X,Y,Z; Complex X,Y,Z; X.getdata( ); X.getdata( ); Y.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.sum(X,Y); Z.putdata( ); Z.putdata( ); } } X Y Z
  • 33. Passing Object Passing Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); void sum (Complex A, Complex B); void sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } void Complex : : sum ( Complex A, Complex B) void Complex : : sum ( Complex A, Complex B) { { real = A.real + B.real; real = A.real + B.real; imag= A.imag + B.imag; imag= A.imag + B.imag; } } void main( ) void main( ) { { Complex X,Y,Z; Complex X,Y,Z; X.getdata( ); X.getdata( ); Y.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.sum(X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z
  • 34. Passing Object Passing Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); void sum (Complex A, Complex B); void sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } void Complex : : sum ( Complex A, Complex B) void Complex : : sum ( Complex A, Complex B) { { real = A.real + B.real; real = A.real + B.real; imag= A.imag + B.imag; imag= A.imag + B.imag; } } void main( ) void main( ) { { Complex X,Y,Z; Complex X,Y,Z; X.getdata( ); X.getdata( ); Y.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.sum(X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 5 6 7 8 A B
  • 35. Passing Object Passing Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); void sum(Complex A, Complex B); void sum(Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } void Complex : : sum ( Complex A, Complex B) void Complex : : sum ( Complex A, Complex B) { { real = A.real + B.real; real = A.real + B.real; imag= A.imag + B.imag; imag= A.imag + B.imag; } } void main( ) void main( ) { { Complex X,Y,Z; Complex X,Y,Z; X.getdata( ); X.getdata( ); Y.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.sum(X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 12 14 X Y Z 5 6 7 8 A B + + = =
  • 36. Passing Object Passing Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); void sum (Complex A, Complex B); void sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } void complex : : sum ( Complex A, Complex B) void complex : : sum ( Complex A, Complex B) { { real = A.real + B.real; real = A.real + B.real; imag= A.imag + B.imag; imag= A.imag + B.imag; } } void main( ) void main( ) { { Complex X,Y,Z; Complex X,Y,Z; X.getdata( ); X.getdata( ); Y.getdata( ); Y.getdata( ); Z.sum(X,Y); Z.sum(X,Y); Z.putdata( ); Z.putdata( ); } } 12 + 14 i 12 + 14 i 5 6 7 8 12 14 X Y Z 5 6 7 8 A B + + = =
  • 37. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } }
  • 38. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } X Y Z
  • 39. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z
  • 40. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B
  • 41. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B
  • 42. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B 12 14 temp
  • 43. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B 12 14 temp
  • 44. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 12 14 X Y Z 7 8 B 12 14 temp
  • 45. Returning Object Returning Object #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); Complex sum (Complex B); Complex sum (Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex Complex : : sum (Complex B) Complex Complex : : sum (Complex B) { { Complex temp; Complex temp; temp.real=real + B.real; temp.real=real + B.real; temp.imag= imag + B.imag; temp.imag= imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= X.sum (Y); Z= X.sum (Y); Z.putdata( ); Z.putdata( ); } } 12 + 14 i 12 + 14 i 5 6 7 8 12 14 X Y Z 7 8 B 12 14 temp
  • 46. C++ garbage collection C++ garbage collection  In c++ the garbage collection task is accomplised by In c++ the garbage collection task is accomplised by mark and sweep algorithm. mark and sweep algorithm.  In this approach the garbage collector periodically In this approach the garbage collector periodically examines every single pointer in our program and examines every single pointer in our program and find that the memory is still in use. At the end of the find that the memory is still in use. At the end of the cycle, any memory that has not been marked is cycle, any memory that has not been marked is deemed to be not in use and is freed. deemed to be not in use and is freed.
  • 47. Difference between static and Difference between static and dynamic memory allocation dynamic memory allocation Static memory allocation Dynamic memory allocation Static memory is allocated automatically by compiler when definition statements are encountered. Dynamic memory is allocated only when there is explicit call to malloc, calloc or realloc function. To make static memory allocation , the amount of the memory space to be reserved should be known at the run time. Amount of memory to be reserved can be given at the run time. In static memory allocation sometimes memory wastage occurs because memory is already known and it can not change. Memory wastage is avoided due to memory allocation occur at run time. Memory allocated at the compile time has static lifetime. Memory allocated at run time has dynamic lifetime. Its is faster it is slower
  • 48. Friend function Friend function  C++ allows a way through which a function can access the private data of a C++ allows a way through which a function can access the private data of a class. class.  Such a function need not be a class member, it may be member function of Such a function need not be a class member, it may be member function of another class or may be non member function. another class or may be non member function.  This function is called FRIEND FUNCTION. The declaration should be This function is called FRIEND FUNCTION. The declaration should be preceded by keyword FRIEND. preceded by keyword FRIEND.
  • 49. Class PQr Class PQr { { Private: Private: ……… ……… Public: Public: …… …… …… …… Friend void abc(); Friend void abc(); }; };  The function is defined elsewhere The function is defined elsewhere in the program like normal in the program like normal function. function.  Function definition does not use Function definition does not use either keyword FRIEND or scope either keyword FRIEND or scope operator. operator.  Functions that are declared with Functions that are declared with FRIEND keyword are known as FRIEND keyword are known as friend functions. friend functions.
  • 50.  A function can declared as friend in number of class. A function can declared as friend in number of class.  A friend function has full access right to access the A friend function has full access right to access the private members of class. private members of class.  Member function of one class can be friend of Member function of one class can be friend of another class. another class.
  • 51. Characteristics Characteristics  It is not in the scope of the class in which it has been It is not in the scope of the class in which it has been declared as friend. declared as friend.  it is not in the scope of class so it cannot be called it is not in the scope of class so it cannot be called using object of that class. using object of that class.  It can be invoked like normal function ,without It can be invoked like normal function ,without object. object.
  • 52.  It can be declared either in public or private part with It can be declared either in public or private part with out affecting its meaning. out affecting its meaning.  Usually, it has the objects as arguments. Usually, it has the objects as arguments.  Unlike member function, it cannot access the member Unlike member function, it cannot access the member names directly and has to use an object name and dot names directly and has to use an object name and dot membership operator with each name. like membership operator with each name. like  A.h A.h
  • 53. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } }
  • 54. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } X Y Z
  • 55. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z
  • 56. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B 5 6 A
  • 57. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B 5 6 A + + = = temp
  • 58. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 X Y Z 7 8 B 5 6 A 12 14 + + = = temp
  • 59. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } 5 6 7 8 12 14 X Y Z 7 8 B 5 6 A 12 14 + + = = temp
  • 60. Friend Function Friend Function #include<iostream.h> #include<iostream.h> class Complex class Complex { { float real, imag; float real, imag; public: public: void getdata( ); void getdata( ); void putdata( ); void putdata( ); friend Complex sum (Complex A, Complex B); friend Complex sum (Complex A, Complex B); }; }; void Complex : : getdata( ) void Complex : : getdata( ) { { cout<<“enter real part:”; cout<<“enter real part:”; cin>>real; cin>>real; cout<<“enter imaginary part:”; cout<<“enter imaginary part:”; cin>>imag; cin>>imag; } } void Complex : : putdata( ) void Complex : : putdata( ) { { if (imag>=0) if (imag>=0) cout<<real<<“+”<<imag<<“i”; cout<<real<<“+”<<imag<<“i”; else else cout<<real<<imag<<“i”; cout<<real<<imag<<“i”; } } Complex sum (Complex A, Complex B) Complex sum (Complex A, Complex B) { { Complex temp; Complex temp; temp.real=A.real + B.real; temp.real=A.real + B.real; temp.imag= A.imag + B.imag; temp.imag= A.imag + B.imag; return temp; return temp; } } void main ( ) void main ( ) { { Complex X, Y, Z; Complex X, Y, Z; X.Getdata( ); X.Getdata( ); Y. getdata( ); Y. getdata( ); Z= sum (X,Y); Z= sum (X,Y); Z.putdata( ); Z.putdata( ); } } 12 + 14 i 12 + 14 i 5 6 7 8 12 14 X Y Z 7 8 B 5 6 A 12 14 + + = = temp
  • 61.  We can also declare all the member functions of one We can also declare all the member functions of one class as the friend functions of another class. In this class as the friend functions of another class. In this case the first class is known as FRIEND class. case the first class is known as FRIEND class.  This can be specified as follows :- This can be specified as follows :- Class z Class z { { ………… ………….. .. …… …… Friend class x; Friend class x; }; };
  • 62. A function friend in two classes A function friend in two classes Void max(XYZ m,ABC n) Void max(XYZ m,ABC n) { { If(m.x>=n.a) If(m.x>=n.a) Cout<<m.x; Cout<<m.x; Else Else Cout<<n.a; Cout<<n.a; } } Int main () Int main () { { ABC abc; ABC abc; abc..setvalue(10); abc..setvalue(10); XYZ xyz; XYZ xyz; Xyz.setvalue(20); Xyz.setvalue(20); max(xyz,abc); max(xyz,abc); Return 0; Return 0; } } #include<iostream.h> #include<iostream.h> Class ABC; Class ABC; Class XYZ Class XYZ { int x; { int x; Public: Public: Void setvalue(int i) Void setvalue(int i) { x=i ; } { x=i ; } Friend void max(XYZ,ABC); Friend void max(XYZ,ABC); }; }; Class ABC Class ABC { { int a; int a; Public: Public: void setvalue(int i) void setvalue(int i) { { a=i ;} a=i ;} Friend void max(XYZ,ABC); Friend void max(XYZ,ABC); }; };
  • 63. Pass by reference Pass by reference #include<iostream.h> #include<iostream.h> Class class_2; Class class_2; Class class_1 Class class_1 { { int value1; int value1; Public: Public: void indata(int a) void indata(int a) { { value1=a; value1=a; } } Void display() Void display() { { cout<<value1<<“n”; cout<<value1<<“n”; } } Friend void exchange(class_1 &, Friend void exchange(class_1 &, class_2 &); class_2 &); }; }; Class class_2 Class class_2 { { int value2; int value2; public: public: void indata( int a) void indata( int a) { { value2=a; value2=a; } } Void display() Void display() { { cout<<value2<<“n”; cout<<value2<<“n”; } } Friend void exchange(class_1 Friend void exchange(class_1 &,class_2 &); &,class_2 &); }; }; Contd…
  • 64. Void exchange(class_1 & x,class_2 Void exchange(class_1 & x,class_2 & y) & y) { { int temp=x.value1; int temp=x.value1; x.value1=y.value2; x.value1=y.value2; y.value2=temp; y.value2=temp; } } Int main() Int main() { { class_1 c1; class_1 c1; Class_2 c2; Class_2 c2; C1.indata(100); C1.indata(100); C2.indata(200); C2.indata(200); Cout<<“values before exchange”<<“ Cout<<“values before exchange”<<“ n”; n”; C1.dispaly(); C1.dispaly(); C2.display(); C2.display(); Exchange(c1,c2); Exchange(c1,c2); Cout<<“values after exchange”<<“n”; Cout<<“values after exchange”<<“n”; C1.display(); C1.display(); C2.display(); C2.display(); Return 0; Return 0; } }
  • 65. Inline function Inline function  Inline is a function that expanded in a line when it is invoked. Inline is a function that expanded in a line when it is invoked.  The compiler replaces the function call by its corresponding The compiler replaces the function call by its corresponding code. code.  Syntax: Syntax:  Inline return type function name Inline return type function name  { {  Function body Function body  } }
  • 66.  Improve the execution speed. Improve the execution speed.  Reduces the memory requirement of function execution. Reduces the memory requirement of function execution.  All inline function must be defined before they called. All inline function must be defined before they called.  The speed benefits of inline function diminish as the function The speed benefits of inline function diminish as the function grows in size. grows in size.  A function definition in a class definition is an inline function A function definition in a class definition is an inline function definition, even without the use of the definition, even without the use of the inline inline specifier. specifier.
  • 67.  Where inline may not work Where inline may not work  For functions returning values , if a loop, switch, goto statements. For functions returning values , if a loop, switch, goto statements.  Functions not returning values, if return exists. Functions not returning values, if return exists.  If function contain static variables. If function contain static variables.  If inline functions are recursive If inline functions are recursive  When function call becomes small compare to function execution. When function call becomes small compare to function execution.
  • 68.  #include<iostream.h> #include<iostream.h>  Inline float add(float x , float y) Inline float add(float x , float y)  { {  Return (x+y); Return (x+y);  } }  Inline float sub(float p , float q ) Inline float sub(float p , float q )  { { Return(p-q); Return(p-q); } } Int main() Int main() { { Float a=12.34; Float a=12.34; Float b=3.6 Float b=3.6 Cout<<add(a,b)<<endl; Cout<<add(a,b)<<endl; cout<<sub(a,b)<<endl; cout<<sub(a,b)<<endl; Return 0; Return 0; } }
  • 69. Default arguments Default arguments  A default argument is a value given in the function A default argument is a value given in the function declaration that the compiler automatically inserts if the declaration that the compiler automatically inserts if the caller does not provide a value for that argument in the caller does not provide a value for that argument in the function call. function call.  Syntax: Syntax: return_type f(…, type x = default_value,…);
  • 70. Default arguments Default arguments  Default values are specified when the function is declared. Default values are specified when the function is declared.  We must add default values from right to left ,we can not We must add default values from right to left ,we can not provide a default value to a particular arguments in the provide a default value to a particular arguments in the middle of argument list. middle of argument list.  Default arguments are useful in situations where some Default arguments are useful in situations where some arguments always have the same value. arguments always have the same value.
  • 71. Default Arguments Default Arguments (Examples) (Examples)  double double pow pow( (double double x, x, int int n=2) n=2)  // computes and returns x // computes and returns xn n The default value of the 2nd argument is 2. This means that if the programmer calls pow(x), the compiler will replace that call with pow(x,2), returning x2
  • 72. Default Arguments Default Arguments (Rules) (Rules)  Once an argument has a default value, all the arguments Once an argument has a default value, all the arguments after it must have default values. after it must have default values.  Once an argument is defaulted in a function call, all the Once an argument is defaulted in a function call, all the remaining arguments must be defaulted. remaining arguments must be defaulted. int f(int x, int y=0, int n) // illegal int f(int x, int y=0, int n=1) // legal
  • 73. Examples:- Examples:-  Int mul(int I,int j=6,int l=9); legal Int mul(int I,int j=6,int l=9); legal  Int mul(int I,int j=6,int l); illegal Int mul(int I,int j=6,int l); illegal  Int mul(int I=0,int j,int l=9); illegal Int mul(int I=0,int j,int l=9); illegal  Int mul(int I=0,int j=6,int l=9); legal Int mul(int I=0,int j=6,int l=9); legal  Advantages:- Advantages:-  We can default arguments to add new parameters to the We can default arguments to add new parameters to the existing functions. existing functions.  Default arguments can be used to combine similar Default arguments can be used to combine similar functions into one. functions into one.
  • 74. Function overloading Function overloading  When using more than one functions with same name and When using more than one functions with same name and with different arguments in a program is known as with different arguments in a program is known as function overloading or function polymorphism. function overloading or function polymorphism.  Function overloading is part of polymorphism. Function overloading is part of polymorphism.
  • 75. Function overloading Function overloading  Function would perform different operations depending Function would perform different operations depending on the argument list in function call. on the argument list in function call.  Correct function to be invoked is determined by checking Correct function to be invoked is determined by checking the number and type of arguments but not on return type the number and type of arguments but not on return type of function. of function.  Examples;- Examples;-  Int area(int,int); Int area(int,int);  Int area(int ,float); Int area(int ,float);
  • 76. Function overloading Function overloading  Examples :- Examples :-  Int add(int a, int b); Int add(int a, int b);  Int add(int a, int b, int c); Int add(int a, int b, int c);  Double add(double x, double y); Double add(double x, double y);  Double add(int p ,double q); Double add(int p ,double q);  Double add(double p, int q); Double add(double p, int q);  Function calls Function calls  Add(5,19); Add(5,19);  Add(16,7.9); Add(16,7.9); Add(12.4,3.5); Add(12.4,3.5);  Add (4,12,23); Add (4,12,23); Add(3.4,7) Add(3.4,7) Function prototype 1 Function prototype 2 Function prototype 3 Function prototype4 Function prototype 5
  • 77. Function overloading Function overloading  A function call first match the prototype having the same A function call first match the prototype having the same number and type of actual arguments and then calls the number and type of actual arguments and then calls the appropriate function for execution… appropriate function for execution…
  • 78. Function overloading Function overloading  A function match includes following steps:- A function match includes following steps:- 1. 1. Compiler first try to find exact match in which the types Compiler first try to find exact match in which the types of actual arguments are the same. of actual arguments are the same. 2. 2. If exact match not found, compiler uses the integral If exact match not found, compiler uses the integral promotions to the actual arguments like char to int, float promotions to the actual arguments like char to int, float to double. to double.
  • 79. Function overloading Function overloading 3. 3. When either of them fail then compiler uses built in When either of them fail then compiler uses built in conversion to the actual arguments and then uses the conversion to the actual arguments and then uses the function whose match is unique. function whose match is unique. 4. 4. If all of the steps fail then the compiler will try user If all of the steps fail then the compiler will try user defined conversions in combination with integral defined conversions in combination with integral promotions and built in conversions to find a unique promotions and built in conversions to find a unique match. match.