博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c ++查找字符串_C ++类和对象| 查找输出程序| 套装3
阅读量:2531 次
发布时间:2019-05-11

本文共 2122 字,大约阅读时间需要 7 分钟。

c ++查找字符串

Program 1:

程序1:

#include 
using namespace std;class Sample { int X;public: void set(int x) { X = x; } void print() { cout << X << " "; }} A, B;int main(){ A.set(10); B.set(20); A.print(); B.print(); return 0;}

Output:

输出:

10 20

Explanation:

说明:

In the above program, we created class Sample and two objects A and B using before semicolon at the end of the class.

在上面的程序中,我们创建了Sample类,并在类末尾使用分号前的两个对象AB。

In the main() function, we call set() and print() function, then it will print "10 20" on the console screen.

main()函数中,我们调用set()print()函数,然后它将在控制台屏幕上打印“ 10 20”

Program 2:

程式2:

#include 
using namespace std;const class Sample { int X;public: void set(int x) { X = x; } void print() { cout << X << " "; }};int main(){ Sample A; Sample B; A.set(10); B.set(20); A.print(); B.print(); return 0;}

Output:

输出:

main.cpp:4:1: error: ‘const’ can only be specified for objects and functions const class Sample { ^~~~~

Explanation:

说明:

The above program will generate an error because we cannot use the with class declaration. The const keyword can be used with data members and member functions of the class.

上面的程序将产生错误,因为我们不能在类声明中使用 。 const关键字可与类的数据成员和成员函数一起使用。

Program 3:

程式3:

#include 
using namespace std;class Sample { int X;public: void set(int x); void print();};void Sample : set(int x){ X = x;}void Sample : print(){ cout << X << " ";}int main(){ Sample A; Sample B; A.set(10); B.set(20); A.print(); B.print(); return 0;}

Output:

输出:

main.cpp:12:13: error: found ‘:’ in nested-name-specifier, expected ‘::’ void Sample : set(int x)             ^main.cpp:17:13: error: found ‘:’ in nested-name-specifier, expected ‘::’ void Sample : print()             ^

Explanation:

说明:

The above program will generate errors because in the above program we inside the class and defined outside the class. But we use the colon operator instead of .

上面的程序将产生错误,因为在上面的程序中,我们在类内部 ,并在类外部定义 。 但是我们使用冒号运算符而不是 。

Recommended posts

推荐的帖子

翻译自:

c ++查找字符串

转载地址:http://pdtzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>