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 ++查找字符串

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

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

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; publi ...

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

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

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

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private int ...

  4. c ++查找字符串_C ++数组| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { char* STR[] = ...

  5. c ++查找字符串_C ++异常处理| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { try { int num1 ...

  6. c ++查找字符串_C ++结构| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; str ...

  7. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int A, B; fr ...

  8. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample1 { int A, B; f ...

  9. c ++查找字符串_C ++结构| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { typedef struct ...

最新文章

  1. Beam Search
  2. 视频监控为校园安全插上“隐形的翅膀”
  3. Java中的输入输出流
  4. 7、Reverse Integer(python)
  5. 存储器的保护(二)——《x86汇编语言:从实模式到保护模式》读书笔记19
  6. C# list删除 另外list里面的元素_在Python 中 List 操作 9种例子详细了解
  7. matlab 十字路口左转
  8. Spring Boot学习总结(14)——Spring Boot常见面试题汇总
  9. java实心菱形_java打印出实心菱形与空心菱形
  10. HDFS 上传文件的不平衡,Balancer问题是过慢
  11. 获取Resources文件下图片的精灵格式
  12. 大数据hadoop常见端口
  13. 二级MYSQL的语法整理_MySQL 常用命令及语法整理
  14. 电影:美国队长:复仇者先锋
  15. php爬取ins图片_python爬取【追新番】日剧资源
  16. UART嵌入式通信协议(以AVR单片机为例)
  17. HTML播放器快进不显示进度条,MediaSource播放视频,快进(直接点击进度条)的时候怎么知道range范围呢...
  18. 上海老百姓的胆量 VS 大牌店营业员的势利
  19. elang 游戏 生成全局id
  20. 【软件测试】:“用户登录”功能测试用例设计方法

热门文章

  1. java调用kettle例子_Kettle API - Java调用示例
  2. java实时记录在线人数
  3. Es6学习笔记(7)----数组的扩展
  4. 你真的了解css像素嘛?
  5. 采访田飞师兄有感 ——by 李皈颖
  6. Cannot find module '@babel/plugin-proposal-class-properties'
  7. Vue--- 一点车项目
  8. iOS开发之Masonry框架-使用方法须知
  9. canvas 图片反色
  10. 【转】MFC学习总结