Member function declarations and definitions
statement:
It is a parameter statement portion tightly, and the type of the function return value type.
The void set (int w)
definition:
Function definition part is a sector for achieving the function
as: void SET (W int)
{
COUT W << << endl;
}
WhileThe definition of a member functionOnly need to add the class name and a double colon, others remain unchanged
void Human::set(int w)
{
需要实现的内容;
}
Example:
#include<iostream>
using namespace std;
class Human
{
public:
void set(int );//类成员函数的声明
int print(){
return weight;
}
private:
int weight;
};
int main()
{
Human Tom;
Tom.set(90);
cout<<"Tom的体重为:"<<Tom.print()<<endl;
Human Mike;
Mike.set(111);
cout<<"Mike的体重为:"<<Mike.print()<<endl;
return 0;
}
void Human::set(int w)//类成员函数的声明
{
if(w>0&&w<100)
weight=w;
else
{
cout<<"请将set里面的参数设置为0-100以内的数字,否则默认设置为0"<<endl;
weight=0;
}
}
Enter the result:
Inline functions:
Ordinary inline functions:
#include<iostream>
using namespace std;
inline int func(int);//使用inline将带有一个int变量并返回int值的函数
/*func说明为内联函数*/
int main()
{
int x;
cout<<"请输入一个数字:"<<endl;
cin>>x;//将输入的数字保留在x变量当中
cout<<"\n";
cout<<"输入的数字是:"<<func(x)<<endl;
/*将该变量作为参数传递到函数func(x)当中,由于该函数是内联函数,
它的所有代码都直接复制在该行的func(x)处,因此这里不会跳转到第17行去执行func函数体重的语句*/
return 0 ;
}
int func(int x)//17行
{
return x;//该行代码会直接被复制到 cout<<"输入的数字是:"<<func(x)<<endl;fuc(x)处
}
Union members within the function:
#include<iostream>
using namespace std;
class A
{
public:
inline void fuc(int ,int);//将fuc设置为内联函数
inline void print();////将print设置为内联函数
private:
int i,j;
};
int main()
{
A a;
a.fuc(3,2);//调用内联函数相当于之间在这将i=3,j=2写在这里
a.print();//调用print()内联函数等于将 cout<<"i*j is:"<<i*j<<endl;直接写在这里
}
void A::fuc(int x,int y)
{
i=x;
j=y;
}//fuc定义部分
void A::print()
{
cout<<"i*j is:"<<i*j<<endl;
}//print定义部分
#include<iostream>
using namespace std;
class A
{
public:
inline void fuc(int x,int y){i=x;j=y;}//将fuc设置为内联函数
inline void print(){cout<<"i*j is:"<<i*j<<endl;}////将print设置为内联函数
private:
int i,j;
};
int main()
{
A a;
a.fuc(3,2);//调用内联函数相当于之间在这将i=3,j=2写在这里
a.print();//调用print()内联函数等于将 cout<<"i*j is:"<<i*j<<endl;直接写在这里
}
Why would declare and define member functions separately:
The class declaration and definition is stored in the head portion of the file: