Ticker

6/recent/ticker-posts

A C++ PROGRAM WITH CLASS WITH MEMBER FUNCTION

A C++ PROGRAM WITH CLASS 

# include 
class item // class declaration 

        int number; // private by default 
        float cost; 
    public : 
        void getdata(int a float b); 
        void putdata(void) // function defined here 
        
                cout <<înumber ì<< number << "\n"; 
                cout << cost: << cost << "\n" 
        
}; 
// member functions definition 
void item :: getdata(int a, float b) 

number = a; 
cost = b; 

// main program 
main() 

item x; // create object x 
x.getdata(100, 20.3); 
x.putdata(); 

In the above program we have shown that one member functions is inline and other is external member function. Here is the output of program. 

number : 100 

cost : 20.3

MAKING AN OUTSIDE FUNCTION INLINE

We can define a member function outside the class definition and still make it inline by just using the qualifier inline in the header line of function definition 

Ex - class item 
{ ---- 
    ---- 
public : 
        void getdata(int a, float b); 

inline void item ::getdata (int a, float b) 

        number = a; 
        cost = b; 
}

NESTING OF MEMBER FUNCTION

When a member function can be called by using its name inside another member function of the same class, it is known as nesting of member function. 

Ex - 

# include 
class set 

int m, n; 
public: 
        void input (void); 
        void display (void); 
        int largest(void); 
}; 
int set:: largest (void) 

        if (m >= n) 
                return (m); 
        else 
                return (n); 

void set : : input (void) 

        cout << ìinput values of m & n:".
        cin >> m >> n;
}
void set :: display(void)
{
        cout << ìlargest value = ì <<largest();
}
main()
{
        set a
        a.input();
        a.display();
}

PRIVATE MEMBER FUNCTION

i) Although we place all data items in a private section and all the functions in public, some situation may require certain function to be hidden from the outside calls. 

ii) We can place these functions in the private section. A private member function can only be called by another function that is a member of its class. Even an object cannot invoke a private function using the dot operator. 

iii) Consider following class: 

        class sample 
        
                int m; 
                void read(void); 
            public: 
                    void update(void); 
                    void write(void); 
    }; 

If s1 is an object of sample then 

s1.read(); // will not work , object cannot access private data. 

read() can be called by the function update() to update the value of m. 

void sample :: update (void) { 
        read(); // a simple call 
}

ALSO SEARCH:

"example of member function in C++"

"types of member function in C++"

"data members in C++"

"private member function in C++"

"classes in C++ example program"

"how to define member function outside the class in C++"

"example of member function in c"

"types of member function in c"

"data members in c"

"how to define member function outside the class in c"

"private member function in c"