方法一:
class R{
public:
R(int r1,int r2):r1(r1),r2(r2){}
void print();
void print()const ;
private:
int r1,r2;
};
void R::print(){
cout<<r1<<":"<<r2<<endl;
}
void R::print() const{
cout<<r1<<";"<<r2<<endl;
}
int main(){
system("color 4e");
R a(5,4);
a.print();
const R b(20,52) ;
b.print();
system("pause");
return 0;
}
方法二:
class R{ //常对象及常成员函数 31
public:
R(int r1,int r2)
{
r1=r1;
r2=r2;
}
void print();
void print()const ;
private:
int r1,r2;
};
void R::print(){
cout<<r1<<":"<<r2<<endl;
}
void R::print() const{
cout<<r1<<";"<<r2<<endl;
}
int main(){
system("color 4e");
R a(5,4);
a.print();
const R b(20,52) ;
b.print();
system("pause");
return 0;
}
c++下面的两种函数的写法有什么区别?第二种写法为什么不对?应怎么改才对?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 小灸舞 2016-04-20 09:54关注
方法二的构造函数应该改成
R(int r1,int r2) { this->r1=r1; this->r2=r2; }
你写成r1=r1;编译器会把两个r1都当成形参的r1
这个和初始化参数列表不同解决 无用评论 打赏 举报
悬赏问题
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?
- ¥15 c++头文件不能识别CDialog