1. The first way to define a member function: Only the prototype of the member function is given in the class declaration, and the definition of the member function
Placed outside of the class.
Return value type class Name:: member function name (parameter table)
{
function body
}
classpoint{ Public: voidSetPoint (int,int);//function prototype for setting the member function of a coordinate point setpoint intGetx ();//function prototype of member function getx with X coordinate points intGety ();//function prototype of member function gety with Y coordinate point Private: intx, y; };voidPonit::setpoint (intAintb//define member functions outside the class SetPoint{x=A; Y=b;}intPoint::getx ()//define member functions outside the class Getx{ returnx;}intPoint:: Gety ()//define member functions outside the class Gety{ returny;}/*
2. The second way to define member functions is to define the member functions directly inside the class
classpoint{ Public: voidSetPoint (intAintb)//The member function setpoint is defined directly inside the class{x=A; Y=b; } intGetx ()//The member function GETX is defined directly inside the class { returnx; } intGety ()//The member function gety is defined directly inside the class { returny; } Private: intx, y; };
3, the third definition of member functions: The functions setpoint, getx and gety as inline functions, that is,
These functions are implicitly defined as inline member functions.
classpoint{ Public: InlinevoidSetPoint (int,int);//declaring member functions setpoint as inline functionsInlineintGetx ();//declaring member functions getx as inline functionsInlineintGety ();//declaring member functions getx as inline functions Private: intx, y; };inlinevoidPoint::setpoint (intAintb//define function SetPoint as inline function outside the class{x=A; Y=b;} InlineintPoint::getx ()//define function Getx as inline function outside the class{ returnx;} InlineintPoint::gety ()//define function Gety as inline function outside the class{ returny;}
C + +: How member functions of a class are defined