我是新手,自学C++,用的是DevC++,指教一下,C++不是说公有部分是成员方法,私有部分是成员数据吧,那怎么错呢,难道我理解错误,请高手指导一下吧
#include"iostream"
using namespace std;
class a
{
int a,b,c;
public:
c=a+b;
private:
a=2;
a=3;
}
int main()
{
cout<<c<<endl;
system("pause");
}
我是新手,自学C++,用的是DevC++ 编程,指教一下,
答案:3 悬赏:20
解决时间 2021-02-20 20:41
- 提问者网友:低唤何为爱
- 2021-02-19 23:38
最佳答案
- 二级知识专家网友:萝莉姐姐鹿小北
- 2021-02-20 00:23
恩,你理解错误
公有和私有都可以是数据或者函数
c = a +b, 不可以这么用的,需要放在函数里面
#include"iostream"
using namespace std;
class A///类型名别跟变量名弄一样的
{
private:///如果不写,默认为private
int a,b,c;
public:
A()/////构造函数,自动调用的, 赋值都放在函数里面
{
a = 2;
b = 3;
c=a+b;
}
void Display()
{
cout << a << '\t' << b << '\t' << c << endl;
}
};////////////////分号是必须的
int main()
{
A test;
test.Display();
system("pause");
return 0;////尽量写上返回值
}
公有和私有都可以是数据或者函数
c = a +b, 不可以这么用的,需要放在函数里面
#include"iostream"
using namespace std;
class A///类型名别跟变量名弄一样的
{
private:///如果不写,默认为private
int a,b,c;
public:
A()/////构造函数,自动调用的, 赋值都放在函数里面
{
a = 2;
b = 3;
c=a+b;
}
void Display()
{
cout << a << '\t' << b << '\t' << c << endl;
}
};////////////////分号是必须的
int main()
{
A test;
test.Display();
system("pause");
return 0;////尽量写上返回值
}
全部回答
- 1楼网友:一只傻青衣
- 2021-02-20 01:38
int main()里面最后加个return 0;试一试!
- 2楼网友:邪性洒脱
- 2021-02-20 00:50
你好!
错得不是一点点
我的回答你还满意吗~~
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯