Module10-Methods - Member Functions - Inclasses
Module10-Methods - Member Functions - Inclasses
com
Member functions or methods are used to work on the data members of the class. The methods
can be defined inside or outside of the class. If the methods are defined inside the class definition
then it can be defined directly, but if defined outside the class then we have to use scope
m
resolution operator ‘::’ along with class name and function name.
co
If we define the function inside the class then no need declare it first, we can directly define the
function inside the class as shown in below.
n.
io
at
uc
ed
hi
If we define the function outside the class definition then must declare the function inside the
class definition and then define the function outside the class as shown in below.
a ks
.s
w
w
w
The following examples illustrates the defining the member function inside the class and outside
the class.
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
ed
hi
ks
Example:
a
.s
#include <iostream>
w
class Account {
int acc_no;
float balance;
www.sakshieducation.com
www.sakshieducation.com
m
cout << "Enter the deposit amount:";
cin >> amount;
co
balance = balance + amount;
return balance;
n.
float Account::withdraw() // function definition outside the class
io
{
float amount;
cout << "Enter withdraw amount:";
at
cin >> amount;
balance = balance ‐ amount;
return balance;
uc
}
ed
int main()
{
Account obj1;
hi
obj1.acc_details();
cout << "Balace after depositing:" << obj1.deposit() << endl;
ks
}
.s
w
w
w
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
Figure1: Output of the program
io
Types of member functions
at
1. Simple member functions
uc
2. Inline member functions
ed
These are the simple basic member functions which are not required any special keyword
.s
to indicate as prefix. The syntax for simple member functions given below,
w
w
statements;
www.sakshieducation.com
www.sakshieducation.com
The Inline functions are enhancement feature provide in C++ programming language to
increase the execution time of a program. Inline functions are similar to macros. The
preprocessors are not used in C++ because it has some drawbacks. Inline functions are actual
functions, which are copy the code every where the function is defined during the compilation
m
time. All the member functions defined inside the class definition are by default inline functions,
but whereas outside the class, the function are inline by using ‘inline’ keyword with the function
co
name. If any change to an inline function then the program must be recompiled because the
compiler needs to replace all the code at each place otherwise it will continue with old
n.
functionality.
io
Syntax:
}
hi
• Inline functions increase the speed of the program by avoiding function calling overheads.
a
• Inline functions must be small to have better efficiency otherwise it increases the size of
.s
operator, because if we define such function inside the class then they become inline
w
• The compiler is unable to perform inlining if the function is too complicated, so must
avoid big looping conditions inside the inline functions.
• Inline functions are kept in the symbol table by the compiler and all the calls for such
function taken care at the compile time.
www.sakshieducation.com
www.sakshieducation.com
#include <iostream>
class A
{
private:
int x;
m
public:
void get_value() // default inline function in C++
{
co
cout << "Enter a Value:";
cin >> x;
}
n.
void print_value() // default inline function in C++
io
{
cout << x << endl;
}
at
};
int main()
{
uc
A obj;
obj.get_value();
obj.print_value();
ed
system("pause");
return 0;
}
hi
a ks
.s
In the above program ‘get_value()’ and ‘print_value()’ are by default inline function inside class
w
definition and they are defined to access the private data members of the class. If we want to use
inline functions outside the class then must be use the ‘inline’ keyword before the function name
as shown below.
www.sakshieducation.com
www.sakshieducation.com
#include <iostream>
class A
{
private:
int x;
public:
void get_value();
void print_value();
m
};
co
{
cout << "Enter a Value:";
cin >>x;
n.
}
io
inline void A :: print_value() // inline function outside the class
{
at
cout << x << endl;
}
uc
int main()
{
ed
A obj;
obj.get_value();
obj.print_value();
system("pause");
hi
return 0;
}
a ks
.s
w
w
A non member function cannot access an objects private and protected members and
sometimes it force programmers to write long and complex codes. This can be overcome by
www.sakshieducation.com
www.sakshieducation.com
using friend function and friend class. The friend member functions are not class member
functions but they are made to give private access to non class functions. These are the functions
that can be made friendly with both the classes, so that these functions can have access to the
private members of these classes.
If a function is defines as a friend function then the private and protected members of class
can be accessed from that function. The declaration of friend function should be done inside the
m
body of class either in private section or in public section starting with keyword friend. The
co
compiler knows the given function is a friend function by its keyword ‘friend’.
n.
Syntax:
class ClassName
io
{
at
private:
data members;
uc
public:
friend return_type FunctionName( arguments );
ed
};
hi
#include <iostream>
a
class class1
{
w
private:
int a;
public:
w
void get_value()
{
w
www.sakshieducation.com
www.sakshieducation.com
int main()
{
class1 obj1;
obj1.get_value();
print_value(obj1);
system("pause");
return 0;
}
m
co
n.
io
Figure4: Output of the program
at
In the above example we are getting some value from user using normal member function and
displaying this value on the screen using friend function.
uc
Example program working on objects of two different classes using friend function:
ed
In the below example the friend function for addition is declared in both the classes and getting
hi
values for ‘a’ form class1 and for ‘b’ class2 respectively, addition is performed on these numbers
ks
#include <iostream>
.s
class class2
w
{
private:
w
int b;
public:
void get_value(int y)
{
b = y;
}
void print_value()
{
cout << "b=" << b << endl;
www.sakshieducation.com
www.sakshieducation.com
};
class class1
{
private:
int a;
m
public:
void get_value(int x)
co
{
a = x;
}
n.
void print_value()
{
io
cout << "a=" << a << endl;
}
at
friend void Addition(class1, class2); // friend function declarartion
};
uc
void Addition(class1 obj1, class2 obj2) // friend function defination
{
cout << "Addition is:" << obj1.a + obj2.b << endl;
ed
int main()
{
hi
class1 obj1;
class2 obj2;
ks
obj1.get_value(10);
obj1.print_value();
obj2.get_value(20);
a
obj2.print_value();
Addition(obj1,obj2);
.s
system("pause");
return 0;
}
w
w
w
www.sakshieducation.com
www.sakshieducation.com
Friend class
A class can be friend of another class using ‘friend’ keyword as similar to friend function.
When a class is made a friend class then all the member functions of that class becomes friend
functions.
class1
m
{
co
public:
functions();
n.
friend class2; // class2 is friend class
};
io
class2
at
{
public:
uc
functions();
}
ed
hi
In the above example the member functions of class2 will be friend functions of class1 so that
any member functions of class2 can access the private data of class1.
ks
A function can be static member function by using ‘static’ keyword with function name.
w
These functions work for the class as whole rather than for a particular object of a class. It can be
w
called using the object and direct member access operator ‘.’ (Dot), but more typically the static
member function can be accessed itself using class name and scope resolution ‘::’ operator.
w
These functions cannot access ordinary data members and member functions, but only static data
members and static member functions.
• We cannot have the static member function and non static member function with same
name and type arguments.
www.sakshieducation.com
www.sakshieducation.com
• A static member function cannot be declared with const and volatile type qualifiers.
• A static member function cannot declare as a virtual function.
• A static member function cannot have this pointer.
m
Syntax:
co
static Return_data_type Function_Name()
n.
{
io
statements;
Example:
at
uc
#include <iostream>
ed
class rectangle
hi
{
private:
ks
void print_values();
void area();
.s
{
return count;
}
w
};
w
www.sakshieducation.com
www.sakshieducation.com
m
int main()
{
rectangle obj,obj2;
co
cout << "count=" << rectangle::get_count() << endl; // calling static member
function
obj.get_value(7,7);
n.
obj.print_values();
obj.area();
cout << "count=" << rectangle::get_count() << endl; // calling static member
io
function
obj.get_value(9,9);
obj.print_values();
at
obj.area();
cout << "count=" << rectangle::get_count() << endl; // calling static member
function
uc
system("pause");
return 0;
}
ed
hi
a ks
.s
w
w
w
Const keyword makes the variable constant, which means if the variable once defined then
there values will be constant and cannot be changed throughout the program. A function
www.sakshieducation.com
www.sakshieducation.com
becomes const when the const keyword is used with function declaration. Const keyword with
member functions can never modify the object or its related data members. It is useful to make as
many functions const as possible so that accidental changes to objects are avoided. When a
function is declared as const, then it can be called by any type of object but where as non const
functions can only be called by non const objects.
Syntax:
m
Return_type Function_Name const ()
co
{
n.
statements;
}
io
at
Example:
#include <iostream>
uc
using namespace std;
ed
class rectangle
{
private:
hi
public:
ks
void print_values();
a
};
w
Length = l;
Width = w;
}
w
www.sakshieducation.com
www.sakshieducation.com
int main()
m
{
rectangle obj;
obj.get_value(7,7);
co
obj.print_values();
obj.area();
system("pause");
n.
return 0;
}
io
at
uc
ed
www.sakshieducation.com