Member Function’s Definition Outside Class

Member function’s definition outside Class

In this part you will learn: 1. Why we need to define function outside class 2. Syntax 3. Scope resolution operator 4. Program Defining Member function outside Class Till now, we have written many programs using class in the previous topic. In those classes, we had member functions too. Those member functions were defined right there because of their small size. But think about a huge project, with hundreds of member functions of a single class. Moreover the member functions we have defined are small ones, in real programming the size of a member function can be much greater. Won’t it make our program difficult to read and understand? Even finding a function definition would become difficult. C++ provide us with an option of defining member functions separately. That means, the programmer have to just declare the function in the class and he/she can define it anywhere in the code. By doing so, the readability of the code increases. Syntax Following is the syntax of defining a member function outside the class. class Class_Name { return_type Function_name(parameters); } return_type Class_Name_to_which_Function_belongs :: Function_name(parameters) { //function definition } In the above syntax we have used scope resolution operator ( :: ) Scope resolution operator (::) is a special type of operator used to access members of a class. Since we want to access member function of a class we will use scope resolution operator. Program with member Function definition outside Class In this example I have defined the member function of a Class. Basic Step:
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
These two are the most common and basic lines of a c++ program. The first one iostream is used to grant us most of the basic functions of C++ like Input and Output functions. The second one conio.h means console input output, the black screen we see is the console. Using namespace std; provides us an ease of using all the library functions without specifying that this function belongs to standard library. Without including it, we have to write ‘std’ with all the built-in functions. Class
  1. class fraction
  2. {
  3. private: //access specifier
  4. int num;
  5. int denom;
  6. public:
  7. void getfraction(){
  8. cout << "Fraction is: " << num << "/" << denom;
  9. }
  10. void setfraction(){
  11. cout << "Enter numerator: ";
  12. cin >> num;
  13. cout << "Enter denominator: ";
  14. cin >> denom;
  15. }
  16. fraction addfraction(fraction f);
  18. };
  19. fraction fraction :: addfraction(fraction f){
  20. fraction sum;
  21. sum.num = (this->num*f.denom) + (this->denom*f.num);
  22. sum.denom = this->denom*f.denom;
  24. return sum;
  25. }
In the above class, I have defined three functions for fractions namely getfraction, setfraction and addfraction. The first two are generally setters and getters, they are defined inside the class. The third function is declared in the class but its definition is outside the class. Note: In the current example member function definition is directly under the class definition, it can be anywhere in the program. The line to focus on is “fraction fraction :: addfraction(fraction f)” which means There is return type “fraction” for a class named “fraction” with the function name addfraction and parameter “fraction f”. Now the compiler will know which function we are defining, then we can define the function under it. The program will generate an error if we directly write this outside the class “fraction addfraction(fraction f)”, this is because the compiler will not know which class we are considering, we have to specify it using scope resolution operator.

Add new comment