Defining member functions
We may define member functions either within the class or outside the class definitions. If member functions are defined in the class specification, then these are defined as ordinary functions.
The syntax for defining the member function within class specification:
return-type function-name ( arguments if any)
{ Body of the function }
In case of exceeding the class definition, we should use the scope resolution operator (::) with the membership option (i.e., class name).
The syntax for defining the member functions outside of the class:
return-type class-name :: function-name (arguments if any)
{ Body of the function }
Example Program:
#include <iostream> using namespace std; class student { private: int rno; char name[10]; float marks; public: void get() { cout<<"\n enter the student details: "; cout<<"\n enter rno,name,marks:"; cin>>rno>>name>>marks; } void put(); }; void student::put() { cout<<"\nname : "<<name<<endl; cout<<"roll no: "<<rno<<endl; cout<<"marks: "<<marks<<endl; } int main() { student s; s.get(); s.put(); return 0; }
Output:
