CPP Programming
About Lesson

Defining Member Functions:

Member functions can be defined in two places:

  1. outside the class definition
  2. inside class definition

When the function is defined outside the class definition its header should be prefixed with its class name followed by scope resolution operator to indicate membership of the function to its class.

General format of function definition outside class definition:

return-type class-name::function-name(arguments list)

{

function body

}

Several different classes can use the same function name.

Inside the Class definition:

When a function is defined inside a class, it is treated as an inline function.  But only small functions are defined inside the class definition.

#include

class item
{
            int number;
            float cost;
public:
            void getdata(int a,float b);
            void putdata(void)
            {
                        cout<<”number:”<<number<<”n”;
                        cout<<”cost:”<<cost<<”n”;
            }
};

void item::getdata(int a,float b)
{
          number=a;
            cost=b;

}

void main()

{

            item x;

            cout<<”nobject x”<<”n”;

            x.getdata(100,299.95);

            x.putdata();

            item y;

            cout<<”nobject y”<<”n”;

            y.getdata(200,175.50);

            y.putdata();

}

You cannot copy content of this page