C++const成员

2017年12月1日20:04:59C++评论69阅读模式

1.const数据成员
在c++中不允许改变的数据成员,我们将它声明为const成员。
const成员的声明形式是
const+类型+变量名;
const成员必须用初始化表的形式进行初始化。
如:文章源自随机的未知-https://sjdwz.com/11097.html

class A
{
public:
A();
A(int m,int y);
void display();文章源自随机的未知-https://sjdwz.com/11097.html

private:
int x;
const int y;
};
A::A() :y(2)
{
cout << "-------************----------" << endl;
cout << "A的没有参数的构造函数被调用了" << endl;
x = 1;文章源自随机的未知-https://sjdwz.com/11097.html

cout << " -------************----------" << endl;
}
A::A(int m,int n) :y(n)
{
x = m;
cout << "-------************----------" << endl;
cout << "A有一个参数的构造函数被调用了" << endl;
cout << " -------************----------" << endl;
}文章源自随机的未知-https://sjdwz.com/11097.html

void A::display()
{
cout << "☆☆☆☆☆☆☆☆" << endl;
cout << "A中数据成员x的值是" << x << endl;
cout << "A中数据成员y的值是" << y << endl;
cout << "☆☆☆☆☆☆☆☆" << endl;
}文章源自随机的未知-https://sjdwz.com/11097.html

2.const成员函数
const函数的声明形式是
返回类型+函数名()+const;
const成员函数不能改变数据成员的值,也不能调用非const成员函数。文章源自随机的未知-https://sjdwz.com/11097.html

3.const对象
如果对象中的数据成员不允许被改变,那么久将它声明称const对象
声明格式是
const+类名+对象名;
类的声明和定义如下:文章源自随机的未知-https://sjdwz.com/11097.html

#include "stdafx.h"
#include<iostream>
#include<string>//写头文件的目的是为了提醒读者别忘了加string这个
using namespace std;
class Teacher
{
public:
Teacher(string name, int age, string course);
Teacher(Teacher &t);
void set(int age, string course);
void display()const;
private:
const string teaname;
int teacAge;
string teacCourse;
};
Teacher::Teacher(string name, int age, string course) :teaname(name)
{
teacAge = age;
teacCourse = course;
}
Teacher::Teacher(Teacher &t) : teaname(t.teaname)
{
teacAge = t.teacAge;
teacCourse = t.teacCourse;
}
void Teacher::set(int age, string name)
{
teacAge = age;
teacCourse = name;
}
void Teacher::display()const
{
cout << "教师姓名\t" << teaname << endl;
cout << "教师年龄\t" << teacAge << endl;
cout << "教授科目\t" << teacCourse << endl;
cout << endl;
}文章源自随机的未知-https://sjdwz.com/11097.html

主函数中有如下代码:文章源自随机的未知-https://sjdwz.com/11097.html

Teacher teac1("Zhang", 59, "操作系统");
const Teacher teac2(teac1);
teac1.display();
teac2.display();文章源自随机的未知-https://sjdwz.com/11097.html

这里用了一个复制构造函数
Teacher teac2(teac1);
便是调用了复制构造函数文章源自随机的未知-https://sjdwz.com/11097.html

Teacher::Teacher(Teacher &t) : teaname(t.teaname)
{
teacAge = t.teacAge;
teacCourse = t.teacCourse;
}文章源自随机的未知-https://sjdwz.com/11097.html

先不用关注它
————————————————
本文在未建站时所写,排版会有些问题,大家可以去访问原文链接查看原文
原文链接:https://blog.csdn.net/qq_41243472/article/details/78691908文章源自随机的未知-https://sjdwz.com/11097.html

欢迎关注本站微信公众号:随机的未知 如果喜欢本文,欢迎点赞,收藏,转发,打赏。
C++最后更新:2022-2-23
  • 本文由 发表于 2017年12月1日20:04:59
  • 转载请注明:来源:随机的未知 本文链接https://sjdwz.com/11097.html
教程

详解MySQL索引

索引介绍 索引是帮助MySQL高效获取数据的数据结构。在数据之外,数据库系统还维护着一个用来查找数据的数据结构,这些数据结构指向着特定的数据,可以实现高级的查找算法。 本文以MySQL常用的B+Tre...
java

详解java接口interface

引言 接口这个词在生活中我们并不陌生。 在中国大陆,我们可以将自己的家用电器的插头插到符合它插口的插座上; 我们在戴尔,惠普,联想,苹果等品牌电脑之间传输数据时,可以使用U盘进行传输。 插座的普适性是...
java

Java中的抽象类和抽象方法

引言 实例图片 如上图,二维图形类有三个子类,分别是正方形类,三角形类,圆形类; 我们都知道要求正方形的面积,直接使用面积公式边长的平方即可,同理三角形的是底乘高除以2,圆的面积是**乘以半径的平方。...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定