मराठी
महाराष्ट्र राज्य शिक्षण मंडळएचएससी विज्ञान (संगणक विज्ञान) इयत्ता १२ वी

Explain how member functions of a class can be defined outside the class definition and inside class definition with an example in C++. - Computer Science 1

Advertisements
Advertisements

प्रश्न

Explain how member functions of a class can be defined outside the class definition and inside class definition with an example in C++.

Describe how member functions of class can be defined outside class definition and inside class definition.

स्पष्ट करा
सविस्तर उत्तर
Advertisements

उत्तर

Member functions of the class can be defined at two places outside the class definition and inside the class definition

Irrespective of the place of definition, the function performs the same operation. Hence, code for the function body is identical in both the cases. Only function header is changed.

(i) Outside the class definition:

a) The general form of member function definition outside the class definition is:

return-type class-name :: function-name (Argument declaration)
{
function body
}

b) The member function incorporates an identity label or membership label (i.e., class-name : :)

c) This label tells the compiler the class to which the function belongs and restricts the scope of that function the objects of the class ‘class-name’ specified in the header line.

Example:

//class definition
class try1
{
    public:
    void display (void);
};
    //member function definition outside class
    void try 1 :: display (void)
{
    cout<<"Programming is fun";
}

ii) Inside the class definition
a)
Another method for defining a member function is to replace the function declaration by the actual function definition.

Example:

class try1
{
    public:
    void display (void)
    {
    cout<<"Programming is fun";
    }
};

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

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2017-2018 (March)
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×