When a member function is defined inside the class then it is function treated as?

Confusion arises because inline has two effects:

  1. It tells the compiler that the function code can be expanded where the function is called, instead of effectively being called.
  2. It tells the compiler that the function definition can be repeated.

Point 1. is "archaic" in the sense that the compiler can in fact do what it likes in order to optimize code. It will always "inline" machine code if it can and find convenient to do and it will never do that if it cannot.

Point 2. is the actual meaning of the term: if you define (specify the body) a function in the header, since a header can be included in more sources, you must tell the compiler to inform the linker about the definition duplicates, so that they can be merged.

Now, by the language specification, free functions (not defined in class bodies) are by default not defined as inline, so defining in a header a thing like

void myfunc() {}

if the header is included in more sources, then linked in a same output, the linker will report a multiple definition error, hence the need to define it as

inline void fn() {}

For class members, the default is the opposite: if you just declare them, they will not be inlined. If you define them, they will be inline.

So a header should look like

//header file class myclass { public: void fn1() {} //defined into the class, so inlined by default void fn2(); }; inline void myclass::fn2() {} //defined outside the class, so explicit inline is needed

And if myclass::fn2() definition goes into a proper source, must lose the inline keyword.

Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class. The definition of member functions can be inside or outside the definition of class.

If the member function is defined inside the class definition it can be defined directly, but if its defined outside the class, then we have to use the scope resolution :: operator along with class name alng with function name.

For example:

class Cube { public: int side; /* Declaring function getVolume with no argument and return type int. */ int getVolume(); };

If we define the function inside class then we don't not need to declare it first, we can directly define the function.

class Cube { public: int side; int getVolume() { return side*side*side; //returns volume of cube } };

But if we plan to define the member function outside the class definition then we must declare the function inside class definition and then define it outside.

class Cube { public: int side; int getVolume(); } // member function defined outside class definition int Cube :: getVolume() { return side*side*side; }

The main function for both the function definition will be same. Inside main() we will create object of class, and will call the member function using dot . operator.

Calling Class Member Function in C++

Similar to accessing a data member in the class, we can also access the public member functions through the class object using the dot operator (.).

Below we have a simple code example, where we are creating an object of the class Cube and calling the member function getVolume():

int main() { Cube C1; C1.side = 4; // setting side value cout<< "Volume of cube C1 = "<< C1.getVolume(); }

Volume of cube C1 = 16

Similarly we can define the getter and setter functions to access private data members, inside or outside the class definition.

In this article, you will learn about C++ class and functions. You will learn about different ways of defining member functions of the class.

The functions associated with a class are called member functions of that class.

Member functions must be declared inside the class but they can be defined either inside the class or outside the class.

Different ways of defining member functions of a class

There are two ways in which the member functions can be defined :

  • Inside the class definition
  • Outside the class definition

C++ class and functions: Inside the class definition

As the name suggests, here the functions are defined inside the class.

Functions defined inside the class are treated as inline functions automatically if the function definition doesn’t contain looping statements or complex multiple line operations.

Example

#include <iostream> using namespace std; class car { private: int car_number; char car_model[10]; public: void getdata() { cout<<"Enter car number: "; cin>>car_number; cout<<"\n Enter car model: "; cin>>car_model; } void showdata() { cout<<"Car number is "<<car_number; cout<<"\n Car model is "<<car_model; } }; // main function starts int main() { car c1; c1.getdata(); c1.showdata(); return 0; }


Output

Enter car number : 9999 Enter car model : Sedan Car number is 9999 Car model is Sedan

Here in above program getdata() and showdata() are the member function defined inside the class.

C++ class and functions: Outside the class definition

As the name suggests, here the functions are defined outside the class however they are declared inside the class.

Functions should be declared inside the class to bound it to the class and indicate it as it’s member but they can be defined outside of the class.

To define a function outside of a class, scope resolution operator :: is used.

Syntax for declaring function outside of class

class class_name { ........ ........ public: return_type function_name (args); //function declaration }; //function definition outside class return_type class_name :: function_name (args) { ...........; // function definition }

Example

#include <iostream> using namespace std; class car { private: int car_number; char car_model[10]; public: void getdata(); //function declaration void showdata(); }; // function definition void car::getdata() { cout<<"Enter car number: "; cin>>car_number; cout<<"\n Enter car model: "; cin>>car_model; } void car::showdata() { cout<<"Car number is "<<car_number; cout<<"\n Car model is "<<car_model; } // main function starts int main() { car c1; c1.getdata(); c1.showdata(); return 0; }

Output

Enter car number : 9999 Enter car model : Sedan Car number is 9999 Car model is Sedan

Here is this program, the functions showdata() and getdata() are declared inside the class and defined outside the class. This is achieved by using scope resolution operator ::.

When a member function is defined inside the class then it is treated?

If a member function is defined inside a class declaration, it is treated as an inline function, and there is no need to qualify the function name with its class name.

What is a function when it is defined inside a class?

Function defined inside a class is called a method.

Can we define member function inside the class?

Member Function: It is a function that can be declared as members of a class. It is usually declared inside the class definition and works on data members of the same class. It can have access to private, public, and protected data members of the same class.

When the function is defined outside a class it is treated as?

Member functions of the class can be defined at two places. 1) Outside the class definition. 2) Inside the class definition. Irrespective of the place of definition, the function performs the same operation. Hence, code for the function body is identical in both the cases.

Toplist

Neuester Beitrag

Stichworte