Member Function Definition

 

Member functions:

Member functions are the functions that are included within the class.

Member functions can be defined in two places:

·       Inside the class declaration

·       Outside the class declaration

Inside the class declaration:

class date

{

            private:

                        int day, month, year;

            public:

                        void getdata()

                        {

                                    cin>>day>>month>>year;

                        }

                        void putdata()

                        {

                                    cout<<day<<month<<year;

                        }

};

 

In this example to member functions getdata() and putdata() are defined inside the class declaration.

 

Outside the class declaration:

Syntax:

return_type class_name::member_function_name(arguments)

{

            // member function code

}

Ex:

class date

{

            private:

                        int day, month, year;

            public:

                        void getdata();

                        void putdata();

};

void date::getdata()

{

            cin>>day>>month>>year;

}

void date::putdata()

{

            cout<<day<<month<<year;

}

:: this operator is called scope resolution operator.

This operator is used to define member function outside the class.

This operator links the class name with the member function.

Comments

Popular posts from this blog

Default Constructors

Solve this Code: Problem 1

Basic Concepts of OOPs