Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Module10-Methods - Member Functions - Inclasses

Member functions or methods are used to access and modify class data members. They can be defined inside or outside the class. Defining them inside does not require prior declaration, while defining outside does. Inline member functions are similar to macros and avoid function call overhead by copying code during compilation. They must be small for efficiency and large functions are better defined outside the class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module10-Methods - Member Functions - Inclasses

Member functions or methods are used to access and modify class data members. They can be defined inside or outside the class. Defining them inside does not require prior declaration, while defining outside does. Inline member functions are similar to macros and avoid function call overhead by copying code during compilation. They must be small for efficiency and large functions are better defined outside the class.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

www.sakshieducation.

com

Methods (Member Functions) in classes

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

using namespace std;


w

class Account {

private: // private access specifier


w

int acc_no;
float balance;

public: // public access specifier


void acc_details()// function definition inside the class
{
cout << "enter your account number:";
cin >> acc_no;
cout << "enter your balence:";

www.sakshieducation.com
www.sakshieducation.com

cin >> balance;


cout << "Balance:" << balance << endl;
}

float deposit();//function declaration for outside class functions definition


float withdraw();//function declaration for outside class functions definition
};

float Account::deposit()// function definition outside the class


{
float amount;

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

cout << "Balance after withdrawing:" << obj1.withdraw() << endl;


system("pause");
return 0;
a

}
.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

3. Friend member function


4. Static member functions
hi

5. Const member functions


ks

Simple member functions


a

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

Return_type Function_Name(argument list)


w

statements;

www.sakshieducation.com
www.sakshieducation.com

Inline member functions

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:

inline void function_name()


at
uc
{
return 0;
ed

}
hi

Important points about inline functions:


ks

• 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

the code which affects the speed also.


• The large functions must be defined outside the class definition using scope resolution ‘::’
w

operator, because if we define such function inside the class then they become inline
w

function automatically which affects the performance.


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

Example for inline functions inside the class:

#include <iostream>

using namespace std;

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

Figure2: Output of the program


w
w

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.

Example for inline functions outside the class:

www.sakshieducation.com
www.sakshieducation.com

#include <iostream>

using namespace std;

class A
{
private:
int x;
public:
void get_value();
void print_value();

m
};

inline void A :: get_value() // inline function outside the class

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

Figure3: Output of the program


w

Friend member function

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

Example program working with friend function:


ks

#include <iostream>
a

using namespace std;


.s

class class1
{
w

private:
int a;
public:
w

void get_value()
{
w

cout << "Enter A value:" ;


cin >> a;
}
friend void print_value(class1); // friend function declaration
};

void print_value(class1 obj1) // friend function defination


{
cout << "a=" << obj1.a << endl;
}

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

in the friend function definition.


a

#include <iostream>
.s

using namespace std;

class class1; // forward declaration


w

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

friend void Addition(class1, class2); // friend function declaration

};

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

Figure5: Output of the program

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

Static member functions


a
.s

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

using namespace std;

class rectangle
hi

{
private:
ks

int Length, Width;


static int count; // static member declaration
public:
void get_value(int, int );
a

void print_values();
void area();
.s

static int get_count() // static member function defination


w

{
return count;
}
w

};
w

void rectangle :: get_value(int l, int w)


{
Length = l;
Width = w;
count ++;
}

void rectangle :: print_values()


{
cout << "Length is:" << Length << endl;

www.sakshieducation.com
www.sakshieducation.com

cout << "Width is:" << Width << endl;


}

void rectangle :: area()


{
cout << "The Area is:" << Length * Width << endl;
}

int rectangle::count = 0; // static member initialization

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

Figure6: Output of the program

Const member functions

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

int Length, Width;

public:
ks

void get_value(int, int );

void print_values();
a

void area() const; // Const member function declaration


.s

};
w

void rectangle :: get_value(int l, int w)


{
w

Length = l;
Width = w;
}
w

void rectangle :: print_values()


{
cout << "Length is:" << Length << endl;
//Length = 10; ‐‐‐> Here we can access the object and can be
change the member variable
//cout << "Length is:" << Length << endl;
cout << "Width is:" << Width << endl;

www.sakshieducation.com
www.sakshieducation.com

void rectangle :: area() const // const member function defination


{
//Length = 2; ‐‐‐‐> Here we cannot change the member variables becz it is
const member function
cout << "The Area is:" << Length * Width << endl;
}

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

Figure7: Output of the program


hi
a ks
.s
w
w
w

www.sakshieducation.com

You might also like