c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

c ++查找字符串

Program 1:

程序1:

#include <iostream>
using namespace std;
class Sample {
int X;
int* const PTR = &X;
public:
void set(int x);
void print();
};
void Sample::set(int x)
{
*PTR = x;
}
void Sample::print()
{
cout << *PTR - EOF << " ";
}
int main()
{
Sample S;
S.set(10);
S.print();
return 0;
}

Output:

输出:

11

Explanation:

说明:

In the above program, we created class Sample that contain member X and a constant pointer PTR that contains the address of X, here we cannot relocate the pointer but we can change the value of X using pointer PTR.

在上面的程序,我们创建的类样品含有成员X和包含X的地址,在这里我们不能搬迁的指针,但我们可以用指针PTR改变X的值恒定的指针PTR。

Here, we defined two member functions set() and print() outside the class.

在这里,我们在类外部定义了两个成员函数set()print()

The set() function set the value to the data member X. and print() member function uses cout statement.

set()函数将值设置为数据成员X。print()成员函数使用cout语句。

cout << *PTR-EOF << " ";

The value of EOF is -1 then 10- -1 = 11.

EOF的值为-1,然后10- -1 = 11

Thus, 11 will be printed on the console screen.

因此,将在控制台屏幕上打印11

Program 2:

程式2:

#include <iostream>
using namespace std;
class Sample {
int X;
int* const PTR = &X;
public:
void set(int x);
void print();
};
void Sample::set(int x)
{
*PTR = x;
}
void Sample::print()
{
cout << *PTR - EOF << " ";
}
int main()
{
Sample* S;
S->set(10);
S->print();
return 0;
}

Output:

输出:

Segmentation fault (core dumped)
OR
Runtime error

Explanation:

说明:

The above code will generate a runtime error because in the above program we created the pointer of the class but, we did not initialize pointer PTR with any object of the Sample class. If we try to access the member of the class Sample then it will generate a runtime error.

上面的代码将产生运行时错误,因为在上面的程序中我们创建了该类的指针,但没有使用Sample类的任何对象初始化指针PTR 。 如果我们尝试访问Sample类的成员,则它将生成运行时错误

Program 3:

程式3:

#include <iostream>
using namespace std;
class Sample {
int X;
int* const PTR = &X;
public:
void set(int x);
void print();
};
void Sample::set(int x)
{
*PTR = x;
}
void Sample::print()
{
cout << *PTR - EOF << " ";
}
int main()
{
Sample* S = &(Sample());
S->set(10);
S->print();
return 0;
}

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:25:27: error: taking address of temporary [-fpermissive]
Sample* S = &(Sample());
^

Explanation:

说明:

The above program will generate an error due to the below statement used in the main() function:

由于main()函数中使用了以下语句,因此上述程序将生成错误:

Sample *S = &(Sample());

Here we created a pointer of the class and tried to initialize with the object of the class, this is not the correct way. The correct way is,

这里我们创建了一个类的指针,并试图用该类的对象进行初始化,这不是正确的方法。 正确的方法是

Sample OB;
Sample *S = &OB;

Recommended posts

推荐的帖子

  • C++ Class and Objects | Find output programs | Set 1

    C ++类和对象| 查找输出程序| 套装1

  • C++ Class and Objects | Find output programs | Set 2

    C ++类和对象| 查找输出程序| 套装2

  • C++ Class and Objects | Find output programs | Set 3

    C ++类和对象| 查找输出程序| 套装3

  • C++ Class and Objects | Find output programs | Set 5

    C ++类和对象| 查找输出程序| 套装5

  • 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

  • C++ Default Argument | Find output programs | Set 1

    C ++默认参数| 查找输出程序| 套装1

  • C++ Default Argument | Find output programs | Set 2

    C ++默认参数| 查找输出程序| 套装2

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

    C ++数组| 查找输出程序| 套装1

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

    C ++数组| 查找输出程序| 套装2

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

    C ++数组| 查找输出程序| 套装3

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

    C ++数组| 查找输出程序| 套装4

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

    C ++数组| 查找输出程序| 套装5

翻译自: https://www.includehelp.com/cpp-tutorial/class-and-objects-find-output-programs-set-4.aspx

c ++查找字符串

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

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

相关文章

ASP.NET 泛型类型 Dictionary操作

protected void Page_Load(object sender, EventArgs e){//泛型Dictionary Dictionary<string, string> dit new Dictionary<string, string>();dit.Add("13", "张三");dit.Add("22", "李四");Response.Write("总数…

独立看门狗---STM32----HAL

基本概念 看门狗解决的问题是什么&#xff1f; 在系统跑飞&#xff08;程序异常执行&#xff09;的情况&#xff0c;是系统复位&#xff0c;程序重新执行。 独立看门狗适应用于需要看门狗作为一个在主程序之外能够完全独立工作&#xff0c;并且对时间精度要求低的场合。 工…

实训09.09:简单的彩票系统(注册信息)

package wsq; import java.util.Scanner;//本文件负责注册用户信息 /*用户注册信息:1.要求设置账号和密码,使用字符串数组2.账号名不能重复3.密码需要输入两次,两次密码输入一致4.满足账号名不重复.且两次密码一致,即为注册成功!!将信息添加到字符串数组中String[][] users ne…

【转】JAVA生成缩略图

方法1&#xff1a;[第一种方法比后一种生成的缩略图要清晰] import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.WritableRaster;import java.awt.*;import java.awt.geom.AffineTransform;import jav…

javascript写入_如何在JavaScript中写入HTML元素?

javascript写入写入HTML元素 (Writing into an HTML element) To write string/text into an HTML element, we use the following things: 要将字符串/文本写入HTML元素&#xff0c;我们使用以下内容&#xff1a; There must be an HTML element like paragraph, span, div e…

大话设计模式之设计模式遵循的七大原则

最近几年来&#xff0c;人们踊跃的提倡和使用设计模式&#xff0c;其根本原因就是为了实现代码的复用性&#xff0c;增加代码的可维护性。设计模式的实现遵循了一些原则&#xff0c;从而达到代码的复用性及增加可维护性的目的&#xff0c;设计模式对理解面向对象的三大特征有很…

IIC通信---EEPROM24C02---STMF4

IIC通信协议 IIC是同步半双工通信&#xff0c;一个数据线SDA和一个时钟SCL线&#xff0c;可以接受和发送数据。在CPU与被控IC之间、IC与IC之间进行双向传送。 空闲状态 IIC总线的SDA和SCL两条信号线同时处于高电平时&#xff0c;规定为总线的空闲状态。 起始信号 当SCL为高…

实训09.08:简单的算法练习

/*final 关键字 修饰的变量值 后期不可更改 相当于定义常量常量 &#xff1a;不可更改*/final int a 10;//a 20; 报错的值不可更改&#xff01;/*输入函数* */System.out.println("请输入数字&#xff1a;");Scanner scanner new Scanner(System.in);int b…

让自己闪亮

转载于:https://www.cnblogs.com/Gigabyte/archive/2009/01/03/you_can_shine.html

Java中的wait()和sleep()方法之间的区别

Java中的wait()和sleep()方法 (wait() and sleep() methods in Java) First, we will see how wait() method differs from sleep() method in Java? 首先&#xff0c;我们将看到wait()方法与Java中的sleep()方法有何不同&#xff1f; wait()方法 (wait() Method) This metho…

离线使用iPhone SDK文档的方法

在使用Xcode进行iPhone编程时&#xff0c;有时需要参考iPhone SDK的文档&#xff0c;不过每次ControlClick后&#xff0c;Xcode都会试图连接Internet&#xff0c;进行在线读取。有什么方法能够把资料下载到硬盘上进行离线阅读吗&#xff1f; 答案是肯定的。首先去Xcode的Prefer…

远程连接sql server 2000服务器的解决方案

远程连接sql server 2000服务器的解决方案2007-04-07 11:29远程连接sql server 2000服务器的解决方案   一 看ping 服务器IP能否ping通。   这个实际上是看和远程sql server 2000服务器的物理连接是否存在。如果不行&#xff0c;请检查网络&#xff0c;查看配置&#xff0c…

实训09.10:HTML简单表格设计

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>燕雨简历</title></head><body><table border"" cellspacing"" cellpadding"" width"400px" height"6…

LCD显示实验----STM32f4--HAL

步骤 LCD初始化 LCD_Init(); //LCD初始化此函数在lcd.c文件里面 2. 设置LCD背景颜色 LCD_Clear(WHITE);此函数在lcd.c文件里面 3. 设置字体颜色 POINT_COLORRED; 写入要显示的字体 LCD_ShowString(10,80,240,24,24,"LTDC TEST");LCD_ShowSt…

JavaScript | 使用提示从用户输入值

Example 1) Input name and print 示例1)输入名称和打印 Code (JS & HTML): 代码(JS和HTML)&#xff1a; <!DOCTYPE html><HTML><HEAD><SCRIPT>var name prompt("Enter Your name:");var msg "Welcome "name;//alert(msg)…

一个游戏程序员的学习资料 (zz)

一个游戏程序员的学习资料//z 2012-4-19 14:39:51 PM IS2120CSDN想起写这篇文章是在看侯杰先生的《深入浅出MFC》时, 突然觉得自己在大学这几年关于游戏编程方面还算是有些心得&#xff0c;因此写出这篇小文,介绍我眼中的游戏程序 员的书单与源代码参考。一则是作为自己今后两年…

项目管理中工作分解结构模型(WBSM)的应用

摘要 本文根据工作分解结构(WBS)的工作特点&#xff0c;运用系统工程的思想理论方法&#xff0c;构建了工作分解结构模型&#xff0c;并提出了模型算法;该模型方法的建立使得WBS工作更加简单可靠、思路清晰、基于更加可靠的科学基础之上。 1、工作分解结构模型(WBSM)方法工作程…

实训09.11:java重点内容介绍

package test;/** * OP:面向对象的简称* 类&#xff1a;同一特征的属性* * 类的定义&#xff1a;具有相同特征和行为的事物的抽象。&#xff08;不具体化&#xff09;* 对象&#xff08;实例对象&#xff09;&#xff1a;具体真实存在的实例。* 类是对象的实例&#xff1a;* *…

SPI通信原理---STM32F4--HAL

SPI接口原理 SPI是一种高速全双工同步通信&#xff0c;在芯片管脚上占用四根线&#xff0c;主要应用在EEPROM、FLASH、实时时钟、AD转换器&#xff0c;还有数字信号处理器和数字信号解码器之间。 SPI接口使用4根线通信。 MISO&#xff1a;主设备数据输入&#xff0c;从设备数…

Linux 引导管理器 grub2 使用简介

转自&#xff1a;杜昌彬的空间 首先向其致敬&#xff01;有改动。 grub是Linux系统即其他类unix系统的主流bootloder&#xff0c;由于grub原来版本的设计存在很大缺陷&#xff0c;与以前的grub很不相同&#xff0c;其使用和配置也发生很大变化。现在很多Linux发行版本都使用了…