C + +: How member functions of a class are defined

Source: Internet
Author: User
Tags define function function prototype gety


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.