找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1774|回复: 9

C++问题

[复制链接]
发表于 2009-11-3 03:22:27 | 显示全部楼层 |阅读模式 IP:江苏扬州
人们为什么说C++难!难在哪里啊?
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void display() const;
void change(int n,float s) const;
private:
int num;
float score;
};
void Stundet::change(int n,float s)
{num=n;score=s;}
void Student::display()
{cout<<num<<" "<<score<<endl;}
int main()
{
const Student stud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return 0;
}
发表于 2009-11-3 03:22:29 | 显示全部楼层 IP:江苏扬州
const 函数调用const成员
回复

使用道具 举报

发表于 2009-11-3 03:22:31 | 显示全部楼层 IP:江苏扬州
请把程序写出来
回复

使用道具 举报

发表于 2009-11-3 03:22:32 | 显示全部楼层 IP:江苏扬州
以下是引用心动音符在2006-6-11 16:10:01的发言:
人们为什么说C++难!难在哪里啊?
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void display() const;
void change(int n,float s); //去掉const因为你在定义时要改变num,score的值.
private:
int num;
float score;
};
void Stundet::change(int n,float s)//Student 写错了
{num=n;score=s;}
void Student::display() //少了const
{cout<<num<<" "<<score<<endl;}
int main()
{
const Student stud(101,78.5); //应该把const去掉,因为你下面要调用chang()函数.
stud.display();
stud.change(101,80.5);//常对象只能调用常成员.
stud.display();
return 0;
}
这题错哪了
回复

使用道具 举报

发表于 2009-11-3 03:22:33 | 显示全部楼层 IP:江苏扬州
谢谢 没想到那么多的错啊 我发现C++好灵活,一不小心就错
回复

使用道具 举报

发表于 2009-11-3 03:22:34 | 显示全部楼层 IP:江苏扬州
const Student stud(101,78.5);
const 是题目要求
回复

使用道具 举报

发表于 2009-11-3 03:22:35 | 显示全部楼层 IP:江苏扬州
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void change(int ,float ) const;
void change(int n,float s)
{num=n;score=s;}
void display() const;
void display()
{cout<<num<<" "<<score<<endl;}
private:
mutable int num;
mutable float score;
};
int main()
{
const Student stud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return 0;
}
我把程序改成这样 但是还是不行
题目要求 要把对象定义成const,而且要用change 函数修改成员值。
回复

使用道具 举报

发表于 2009-11-3 03:22:36 | 显示全部楼层 IP:江苏扬州
附件: 只有本站会员才能下载或查看附件,请您 登录 或 注册
回复

使用道具 举报

发表于 2009-11-3 03:22:37 | 显示全部楼层 IP:江苏扬州
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s) const
{num=n;score=s;}
void display() const
{cout<<num<<" "<<score<<endl;}
private:
mutable int num;
mutable float score;
};
int main()
{
const Student stud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return 0;
}
原来是这样啊
回复

使用道具 举报

发表于 2009-11-3 03:22:39 | 显示全部楼层 IP:江苏扬州
哈哈总于搞定了 呵呵 自言自语 自言自语 大家不用理我
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 13:26 , Processed in 0.217059 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表