MEMBER FUNCTIONS
Defining member functions
Data
members of a class must be declared within the body of the class. Member
functions can be defined in Two ways:
Inside
the class Outside the class
§ Member
functions inside the class body:
§ This is
similar to a normal function definition expect that it is enclosed within the
body of a class.
§ These are
considered as inline by default.
§ In some
implementations member function a having loops like for, do, while etc. are not
treated as inline function.
#include<iostream.h>
Class date
{
private:
int day;
int
month;
int
year;
public: void
set(int d, int m, int y)
{
day = d;
month=
m; year = y;
}
void show()
{ cout<< day<<“-” month<<“-” year<<endl; }
};
void main()
{
Date d1, d2;
// creating two
objects D1.set(15,8,2011); d2,.set(26,1,2011);
Cout<<“
independence day”;
D1.show();
Cout<<“
republic day”;
D2.show();
}
Inline is
actually just a request, not a command, to the compiler. The compiler can
choose to ignore it.
Also,
some compilers may not inline all types of functions.
For
example, it is common for a compiler not to inline a recursive function. Inline
functions may be class member functions.
For
example, this is a perfectly valid C++ program
#include
<iostream>
class myclass
{
int a, b; public:
void init(int
i, int j);
void show();
};
// Create an
inline function.
inline void
myclass::init(int i, int j)
{
a = i; b = j;
}
// Create
another inline function. inline void myclass::show()
{
cout << a
<< " " << b << "\n";
}
int main()
{
myclass x;
x.init(10, 20); x.show(); return 0;
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.