您的位置:首页 > 其它

第六周实验报告(一)

2012-03-26 18:48 169 查看
* (程序头部注释开始)
* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称:学会在类中使用指针

* 作 者:齐艳红

 * 完成日期: 2011年 3月 26日*

版 本 号:

 * 对任务及求解方法的描述部分

 *问题描述:“cout”: 未声明的标识符,“C::getX”: 不能将“this”指针从“const C”转换为“C &”

 * 程序输出: 5

* 程序头部的注释结束*/

#include<iostream>

using namespace std;

class C

{

private:

int x;

public:

C(int x)

{this->x = x;}

int getX() const

{return x;}

};

void main()

{
const C c(5);

cout<<c.getX()<<endl;

system("pause");
}

#include<iostream>

using namespace std;

class C

{

private:

int x;

public:

C(int x)

{this->x = x;}

int getX()

{return x;}

};

void main()

{
C c(5);

cout<<c.getX()<<endl;

system("pause");
}



总结:在public中定义的是int getX(),而输出时是const C````,两者对不起来,出现错误不能指向const,上面给出两种法案。我觉得第二种好,因为常成员函数只能引用本类中的数据成员,而不能修改它们·······
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c 任务