c语言 关键字const_C ++ const关键字| 查找输出程序| 套装1

c语言 关键字const

Program 1:

程序1:

#include <iostream>
using namespace std;
void fun(int& A) const
{
A = 10;
}
int main()
{
int X = 0;
fun(X);
cout << X;
return 0;
}

Output:

输出:

[Error] non-member function 'void fun(int)' cannot have const qualifier.

Explanation:

说明:

The above code will generate an error because we cannot define, the non-member function as a const. Without a const keyword the above code will work fine.

上面的代码将产生错误,因为我们无法将非成员函数定义为const 。 如果没有const关键字,则上面的代码可以正常工作。

Program 2:

程式2:

#include <iostream>
using namespace std;
int main()
{
const int X = 0;
int* ptr;
ptr = &X;
*ptr = 10;
cout << X;
return 0;
}

Output:

输出:

error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive] ptr = &X;

Explanation:

说明:

The above program will generate an error in C++, C++ does not allow modification in a constant using pointer, but we can modify the value of a constant in C language. The below program in C language will work fine.

上面的程序在C ++中会产生错误,C ++不允许使用指针修改常量,但是我们可以在C语言中修改常量的值。 下面的C语言程序可以正常运行。

#include <stdio.h>
int main()
{
const int X = 0;
int* ptr;
ptr = &X;
*ptr = 10;
printf("%d", X);
return 0;
}

Program may run on C language compiler, but it is not a standard that we can change the constant. In C language compiler – it can be changed through the pointer.

程序可以在C语言编译器上运行,但是我们不能更改常量不是标准。 在C语言编译器中–可以通过指针进行更改。

Program 3:

程式3:

#include <iostream>
using namespace std;
class Sample {
const int A;
const int B;
public:
Sample(): A(10), B(20)
{
}
void print()
{
cout << A << " " << B;
}
};
int main()
{
Sample S;
S.print();
return 0;
}

Output:

输出:

10 20

Explanation:

说明:

The above code will print "10 20" on the console screen.

上面的代码将在控制台屏幕上显示“ 10 20”

Let's understand the program step by step.

让我们逐步了解程序。

Here we created a class Sample that contains two const data members A and B. As we know that we can assign the values of constant at the time of declaration. But here we use the concept of member initialize list.

在这里,我们创建了一个Sample类,其中包含两个const数据成员AB。 众所周知,我们可以在声明时分配常量的值。 但是这里我们使用成员初始化列表的概念。

Sample ():A(10),B(20)
{
}

We can assign value to const data members using the member initialize list. We can initialize members by a colon (:) with members and value in the constructor.

我们可以使用成员初始化列表为const数据成员分配值。 我们可以初始化一个冒号成员:在构造函数中成员和值()。

Here we also defined a print() member function, which is used to print values of data members.

在这里,我们还定义了一个print()成员函数,该函数用于打印数据成员的值。

Recommended posts

推荐的帖子

  • C++ const Keyword | Find output programs | Set 2

    C ++ const关键字| 查找输出程序| 套装2

  • C++ Operators | Find output programs | Set 1

    C ++运算符| 查找输出程序| 套装1

  • C++ Operators | Find output programs | Set 2

    C ++运算符| 查找输出程序| 套装2

  • C++ Reference Variable| Find output programs | Set 1

    C ++参考变量| 查找输出程序| 套装1

  • C++ Reference Variable| Find output programs | Set 2

    C ++参考变量| 查找输出程序| 套装2

  • C++ Conditional Statements | Find output programs | Set 1

    C ++条件语句| 查找输出程序| 套装1

  • C++ Conditional Statements | Find output programs | Set 2

    C ++条件语句| 查找输出程序| 套装2

  • C++ Switch Statement | Find output programs | Set 1

    C ++转换语句| 查找输出程序| 套装1

  • C++ Switch Statement | Find output programs | Set 2

    C ++转换语句| 查找输出程序| 套装2

  • C++ goto Statement | Find output programs | Set 1

    C ++ goto语句| 查找输出程序| 套装1

  • C++ goto Statement | Find output programs | Set 2

    C ++ goto语句| 查找输出程序| 套装2

  • C++ Looping | Find output programs | Set 1

    C ++循环| 查找输出程序| 套装1

  • C++ Looping | Find output programs | Set 2

    C ++循环| 查找输出程序| 套装2

  • C++ Looping | Find output programs | Set 3

    C ++循环| 查找输出程序| 套装3

  • C++ Looping | Find output programs | Set 4

    C ++循环| 查找输出程序| 套装4

  • C++ Looping | Find output programs | Set 5

    C ++循环| 查找输出程序| 套装5

翻译自: https://www.includehelp.com/cpp-tutorial/const-keyword-find-output-programs-set-1.aspx

c语言 关键字const

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541918.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【喜报】JEEWX荣获“2016 年度码云新增热门开源软件排行榜”第一名!

为什么80%的码农都做不了架构师&#xff1f;>>> 2016 年度码云新增项目排行榜 TOP 50 正式出炉&#xff01;根据 2016 年在码云上新增开源项目的 Watch、Star、Fork 数量以及其他角度的统计&#xff0c;JEEWX捷微管家荣获“2016 年度码云新增热门开源软件排行榜”第…

java 二叉树特点_疯狂java笔记之树和二叉树

树的概述树是一种非常常用的数据结构&#xff0c;树与前面介绍的线性表&#xff0c;栈&#xff0c;队列等线性结构不同&#xff0c;树是一种非线性结构1.树的定义和基本术语计算机世界里的树&#xff0c;是从自然界中实际的树抽象而来的&#xff0c;它指的是N个有父子关系的节点…

编辑距离 dp_使用动态编程(DP)编辑距离

编辑距离 dpProblem: You are given two strings s1 and s2 of length M and N respectively. You can perform following operations on the string. 问题&#xff1a;给您两个长度分别为M和N的字符串s1和s2 。 您可以对字符串执行以下操作。 Insert a character at any posi…

tomcat +apache 配置集群

2019独角兽企业重金招聘Python工程师标准>>> APACHE2.2.25TOMCAT6.0.37配置负载均衡 目标: 使用 apache 和 tomcat 配置一个可以应用的 web 网站&#xff0c;要达到以下要求&#xff1a; 1. Apache 做为 HttpServer &#xff0c;后面连接多个 tomcat 应用实例&…

java双缓存机制_详解JVM类加载机制及类缓存问题的处理方法

前言大家应该都知道&#xff0c;当一个Java项目启动的时候&#xff0c;JVM会找到main方法&#xff0c;根据对象之间的调用来对class文件和所引用的jar包中的class文件进行加载(其步骤分为加载、验证、准备、解析、初始化、使用和卸载)&#xff0c;方法区中开辟内存来存储类的运…

oracle中dbms_并发和由于DBMS中的并发导致的问题

oracle中dbms并发 (Concurrency) The ability of a database system which handles simultaneously or a number of transactions by interleaving parts of the actions or the overlapping this is called concurrency of the system. 数据库系统通过交织部分操作或重叠操作来…

什么是mvc?

什么是MVCMVC 是一种设计模式&#xff0c;它将应用划分为3 个部分&#xff1a;数据&#xff08;模型&#xff09;、展现层&#xff08;视图&#xff09;和用户交互层&#xff08;控制器&#xff09;。换句话说&#xff0c;一个事件的发生是这样的过程&#xff1a;1&#xff0e;…

mysql的安装和基本命令_MySQL安装以及简单命令用法

MYSQL:关系型数据库存储引擎:负责将逻辑层的概念转化为物理层机制&#xff0c;在物理层完成物理机制。支持事务&#xff1a;transaction必须满足的条件&#xff1a;ACID(一致性,持久性,原子性,隔离性)锁&#xff1a;并发访问随机访问&#xff1a;数据在磁盘上是随机存储的安装&…

将数组转换为JavaScript中的对象

Lets say you have the following array, 假设您有以下数组&#xff0c; const nums [1, 2, 3, 4, 5];console.log(nums);Output 输出量 (5) [1, 2, 3, 4, 5]We know that nums is an array and we can see in the output that we get an array. Lets convert it into an ob…

docker集群运行在calico网络上

2019独角兽企业重金招聘Python工程师标准>>> ##网络及版本信息 docker1 centos7 192.168.75.200 docker2 centos7 192.168.75.201 物理网络 192.168.75.1/24 Docker version 1.10.3, build 3999ccb-unsupported &#xff0c;安装过程略 # calicoctl version Version…

python批量雷达图_python批量制作雷达图

老板要画雷达图&#xff0c;但是数据好多组怎么办&#xff1f;不能一个一个点excel去画吧&#xff0c;那么可以利用python进行批量制作&#xff0c;得到样式如下&#xff1a;首先制作一个演示的excel&#xff0c;评分为excel随机数生成&#xff1a;1 INT((RAND()4)*10)/10加入标…

JavaScript中带有示例的Math.log()方法

JavaScript | Math.log()方法 (JavaScript | Math.log() Method) Math.log() is a function in math library of JavaScript that is used to return the value of natural Log i.e. (base e) of the given number. It is also known as ln(x) in mathematical terms. Math.log…

SUI踩坑记录

SUI踩坑记录 最近做了个项目选型了SUI和vue做单页应用。下面记录一下踩坑经历SUI 介绍 sui文档&#xff1a;http://m.sui.taobao.org/SUI Mobile 是一套基于 Framework7 开发的UI库。它非常轻量、精美&#xff0c;只需要引入我们的CDN文件就可以使用&#xff0c;并且能兼容到 i…

java 写入xml文件_java读写xml文件

要读的xml文件李华姓名>14年龄>学生>张三姓名>16年龄>学生>学生花名册>package xml;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.Iterator;import java.util.Vector;import javax.xml.pa…

JavaScript中带有示例的Math.max()方法

JavaScript | Math.max()方法 (JavaScript | Math.max() Method) Math.max() is a function in math library of JavaScript that is used to return the greatest value of all the passed values to the method. Math.max()是JavaScript数学库中的函数&#xff0c;用于将所有…

java 修饰符默认_Java和C#默认访问修饰符

C#中&#xff1a;针对下面几种类型内部成员的访问修饰符&#xff1a;enum的默认访问修饰符&#xff1a;public。class的默认为private。interface默认为public。struct默认为private。其中&#xff1a;public可以被任意存取&#xff1b;protected只可以被本类和其继承子类存取&…

JavaScript中带有示例的Math.abs()方法

JavaScript | Math.abs()方法 (JavaScript | Math.abs() Method) Math operations in JavaScript are handled using functions of math library in JavaScript. In this tutorial on Math.abs() method, we will learn about the abs() method and its working with examples.…

人脸识别python face_recognize_python2.7使用face_recognition做人脸识别

偶然看到一篇文章&#xff0c;说是可以实时人脸识别&#xff0c;很有兴趣就自己按照文章开始动手人脸识别&#xff0c;但是实现过程中遇到了几个问题这里做个总结&#xff0c;希望可以帮助到大家安装face_recognition这个之前需要先安装编译dlib&#xff0c;如果没有安装dlib&a…

c# reverse_清单 .Reverse()方法,以C#为例

c# reverseC&#xff03;List <T> .Reverse()方法 (C# List<T>.Reverse() Method) List<T>.Reverse() method is used to reverse the all list elements. List <T> .Reverse()方法用于反转所有列表元素。 Syntax: 句法&#xff1a; void List<T&…