Function or Method with class in C++

Function with Class in C++:
  • Already discussing about function, If you know details about function then click the below link.
  • If you need to know about the class, then click the below link to learn detais of class in C++.
  • Two different ways to defining the member functions.
  • There are,
  • Function definition inside the class
  • Function definition outside the class
Function definition inside the class:
Syntax:
  1. <header files>
    class class_name //class declaration
    {
              Access specifiers:
              Variable_declaration;
              Return_type Method_name( parameter list)
              {
              //Statements
              }
    };
    void main()
    {
    //object declaration
    Class_name object_name;
    //accessing member function using object
    object_name .function_name();
    }
Example:
  1. #include<iostream.h>
    #include<conio.h>
    class calculate
    {
    int a,b;
    public:
    void get_data()//Function Definition inside the class
    {
    cout<<"\n Enter two values: ";
    cin>>a>>b;
    operation(a,b);
    }
    void operation(int x,int y) //Function Definition inside the class
    {
    cout<<"\n Additions of two value is: "<<x+y;
    cout<<"\n Subtractions of two value is: "<<x-y;
    cout<<"\n Muliplications of two value is: "<<x*y;
    cout<<"\n Divitions of two value is: "<<x/y;
    }
    };
    void main()
    {
    clrscr();
    calculate c;
    c.get_data();
    getch();
    }
Output:
Function definition outside the class:
  • Declare the function in the class and it function is calling outside the class.
  • If the method call in outside of the class by using scope resolution operator.
  • It’s like double colon (::).
Syntax:
  1. <header files>
    class class_name //class declaration
    {
              Access specifiers:
              Variable_declaration;
              Return_type Method_name( parameter list);
    };
    void main()
    {
    //object declaration
    Class_name object_name;
    //accessing member function using object
    object_name .function_name();
    }
    Return_type class_name :: Method_name( parameter list)
    {
              //Statement
    }
Example:
  1. #include<iostream.h>
    #include<conio.h>
    class calculate
    {
    int a,b;
    public:
    void get_data();
    void operation(int x,int y);
    };
    void main()
    {
    clrscr();
    calculate c;
    c.get_data();
    getch();
    }
    void calculate :: get_data()//Function Definition outside the class
    {
    cout<<"\n Enter two values: ";
    cin>>a>>b;
    operation(a,b);
    }
    void calculate :: operation(int x,int y) //Function Definition outside the class
    {
    cout<<"\n Additions of two value is: "<<x+y;
    cout<<"\n Subtractions of two value is: "<<x-y;
    cout<<"\n Multiplications of two value is: "<<x*y;
    cout<<"\n Divisions of two value is: "<<x/y;
    }
Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me