Programmingoneonone is known for sharing best programming knowledge.

Defining member functions in c++ programming

in this post, you will learn about how to defining member functions in c++ programming inside the class and outside the class with an example program.


defining member functions in c++ programming
learn c++ programming

There are two types of methods in c++ to defining member functions in the class.

  • defining the function inside the class.
  • defining the function outside the class.

defining function inside the class:

to access the function from outside the class always define a member function in public mode in a class.

Syntext:
              <return-type> <function-name><(parameters)>;

Example:
               class student
               {
                  public:

                   void getdata(int, float); 
                  // a function which is defined in the class in public mode.

                }

Note: you can also declare the member function with the definition of the member function inside the class. 

Example:
              class student
              {
                     int x;
                     float f;
                     public:

               /* a function which is declare with the definition of function 
                   inside the class */
               void getdata(int a, float c)
                {                                           
                                x=a;
                                f=c;
                    
                         cout<<"\n vlaue for the int and float is:"<< x << f;
                 }

               };

Note: always use the [;] semicolon at the end of the class.

defining the member function outside the class:

to define or declare a member function outside the class you first need to define a function inside the class. 

syntax:
            <return-type> <class-name>::<function-name><(parameter list)
            {

                    code for execution;
            }

Example:
                 void test::getdata(int x, float f)
                 {

                          a=x;
                          b=f;
                 }

Note:  here the [::] symbol is called the scope resolution operator. 

Example program for declaring a class and defining member functions in class and call it by objects:

  #include<iostream>

   class data
   {

       int x,y,z;  
  /* by default the all the member function or data member are in private scope. */     

       public:

         void getdata(void); // define a member function inside the class.
         void putdata(void); // define a member function inside the class.
    };

   // declare a getdata function outside the class.   
  void sum::getdata(void)  
    {
              cout<<"\nenter the value of X and Y:"
              cin>>x>>y;
    }

    // declare a putdata function outside the class.
    void sum::putdata(void) 
    {
       z=x+y;
       cout<<"\nAddition of:"<<a<<" and"<<b<<"="<<t;
     }

   int main()
    {
       data obj;          // create a object of the data class.
       obj.getdata(); // call the getdata function by object of the class.
       obj.putdata(); // call the putdata function by object of the class. 

        return 0;
    }

"Please share this knowledge as much possible as you can and also comment me your queries and questions related to this topic. I will really grateful to help you."

No comments:

Post a Comment