目录
写一个有默认参数的函数,把声明和定义分开,并在主函数内成功调用
#include <iostream>
using namespace std;
class Stu
{
private:
int age=10;
char sex='m';
float tall=180.1 ;
public:
double score;
int get_age();
char get_sex();
float get_high();
void set_m(int a,char c,float b);
};
/*获取私有成员*/
int Stu::get_age()
{
return age;
}
char Stu::get_sex()
{
return sex;
}
float Stu::get_high()
{
return tall;
}
/*设置私有成员*/
void Stu::set_m(int a,char c,float b)
{
age=a;
sex=c;
tall=b;
}
int main()
{
Stu s1;
s1.set_m(18,'w',171.2);
cout << "age=" << s1.get_age() << endl;
cout << "sex=" << s1.get_sex() << endl;
cout << "tall=" << s1.get_high() << endl;
return 0;
}
#include <iostream>
using namespace std;
struct A
{
int add(int a,int b,int c);//声明
};
/*定义*/
int A::add(int a,int b,int c=100)
{
return a+b+c;
}
int main()
{
A s1;
int num1,num2;
num1=s1.add(2,7);
cout << "num1=" << num1 << endl;
num2=s1.add(3,37,16);
cout << "num2=" << num2 << endl;
return 0;
}