c ++查找字符串

Program 1:

程序1:

#include <iostream>
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 <iostream>
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 const keyword with class declaration. The const keyword can be used with data members and member functions of the class.

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

Program 3:

程式3:

#include <iostream>
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 declare member function inside the class and defined outside the class. But we use the colon operator instead of scope resolution operator ("::" ).

上面的程序将产生错误,因为在上面的程序中,我们在类内部声明成员函数 ,并在类外部定义成员函数 。 但是我们使用冒号运算符而不是范围解析运算符(“ ::”) 。

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 4

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

  • 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-3.aspx

c ++查找字符串

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

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

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

  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. FPGA之道(20)FPGA设计的上板调试与项目总结
  2. requests payload_python+Requests接口自动化测试之传递 URL 参数
  3. Java-Java I/O流解读之Object Serialization and Object Streams
  4. 云南省计算机一级理论知识试卷,云南省计算机一级考试模拟试题理论题型
  5. Java Signal实例
  6. ASP.NET Web API中的Controller
  7. Linux安装SNMP
  8. vue底部跳转_详解Vue底部导航栏组件
  9. 前端学习(1049):todolist正在进行和已经完成阶段2
  10. C语言向文件写入内容并读取显示
  11. 【二】Java变量与常量
  12. 干货!博应用APP推广的三大步骤五大原则
  13. 接口测试如何生成随机的参数值
  14. 9张图看懂AI人工智能现状!从事AI职业女性稀缺!
  15. Halcon学习之缺陷检测-凸点检测
  16. php手册中的tokenizer详细总结,基本看它就够了
  17. 行走在拉萨、林芝的路上
  18. 不错的学习金字塔模型
  19. Synchronized 用法总结
  20. spark日志中 Tid是什么

热门文章

  1. python期末项目书怎么写_自己写了一部书怎么出版
  2. 安装 PyTorch C++ API libtorch 及一个最小例子
  3. oracle中修改多个字段默认值_利用VBA代码在已有的数据表中删除、添加、修改字段...
  4. java mongodb 插入数据_mongoDB 插入数据 用java实现
  5. transporter上传卡正在交付_【iOS】Xcode11使用Transporter将APP上传到App Store,卡在正在验证APP...
  6. python框架是干什么的_django框架是干什么的
  7. linux流式访问日志,流式实时日志分析系统的实现原理
  8. php system 255,GitHub - dwg255/OA-SYS: OA办公系统开源项目
  9. Docker容器网络解析
  10. Angular性能优化之脏检测