c ++查找字符串

Program 1:

程序1:

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

Output:

输出:

main.cpp:6:5: error: expected ‘:’ before ‘int’
int A;
^~~
main.cpp:8:5: error: expected ‘:’ before ‘int’
int B;
^~~
main.cpp:11:5: error: expected ‘:’ before ‘void’
void init()
^~~~
main.cpp:17:5: error: expected ‘:’ before ‘void’
void print()
^~~~

Explanation:

说明:

The above code will generate errors because in the above program we did not use private and public modifiers properly.

上面的代码将产生错误,因为在上面的程序中我们没有正确使用private和public修饰符 。

In C++, we need to use the colon ":" operator to specify modifiers block. Here we did not use modifiers for each member individually, here we create blocks like,

在C ++中,我们需要使用冒号“ :”运算符来指定修饰符块。 在这里,我们没有为每个成员单独使用修饰符,在这里,我们创建如下块:

class ABC {
private:
int A;
int B;
public:
void fun();
};

Here A and B are private members and fun() is public in ABC class.

这里AB是私有成员,而fun()ABC类中是公共的。

Program 2:

程式2:

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

Output:

输出:

10 20

Explanation:

说明:

In the above program, we created a class Sample. Here we created two private members A and B. After that we used modifiers multiple times, In C++, last used modifiers decide the visibility of members so init() and print() function will be public type.

在上面的程序中,我们创建了Sample类。 在这里,我们创建了两个私人成员AB。 之后,我们多次使用修饰符,在C ++中,最后使用的修饰符决定成员的可见性,因此init()print()函数将成为公共类型。

Now, come to main() function, here we created of the object of Sample class and call init() and print() function then the "10 20" will be printed on the console screen.

现在,进入main()函数,在这里我们创建Sample类的对象,并调用init()print()函数,然后“ 10 20”将被打印在控制台屏幕上。

Program 3:

程式3:

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

Output:

输出:

10 20

Explanation:

说明:

In the above program, we created a Sample class that contains two data member A and B, they initialized with 10 and 20 respectively. Here we initial member at the time of declaration. It is also possible in C++.

在上面的程序中,我们创建了一个Sample类,其中包含两个数据成员AB ,它们分别用10和20初始化。 在这里,我们是宣布时的初始成员。 在C ++中也是可能的。

Here we did not mention any access modifier. In C++, if we did not any specify any modifier then by default it will be considered as a private type.

这里我们没有提及任何访问修饰符。 在C ++中,如果我们未指定任何修饰符,则默认情况下它将被视为私有类型。

In the main() function, we create object S of Sample class and then call print() member function of Sample class, which will print "10 20" on the console screen.

main()函数,我们创建Sample类的对象S,然后呼叫打印() 成员函数 Sample类的,其将打印“10 20”在控制台屏幕上。

Recommended posts

推荐的帖子

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

c ++查找字符串

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

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

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

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

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

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

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; 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. 【AJAX】JavaScript的面向对象
  2. 部署exchange2010三合一:之七:安装证书服务器
  3. iOS设计模式 - 备忘录
  4. CVE-2021-35211: SolarWinds Serv-U SSH 漏洞分析
  5. 作业三——原型化系统——外卖app
  6. IIS 7.5 Express概况
  7. 图片链接生成器软件_推荐10个小众但是黑科技十足的Windows软件
  8. 省钱攻略送上!戴尔官网OptiPlex商用台式机到手仅需2279元!速速抢购!
  9. 【算法】第76题 Minimum Window Substring
  10. 远程访问dmz和虚拟服务器的设置
  11. CCF201612-5 卡牌游戏(募集解题代码)
  12. 3640 交换机实验的一些摘要【待进一步更新】
  13. 架构 简述负载均衡和CDN技术
  14. 2011-12-21的告别信
  15. Unity3d 音效 音乐 大小控制
  16. HBase hmaster无法正常启动,日志报错Operation category READ is not supported in state standby.hdfs ha hbase配置修改
  17. 非线性回归算法--学习笔记
  18. [洛谷P4158][SCOI2009]粉刷匠(动态规划)
  19. 用div来代替table
  20. 基于jQuery实现tab选项卡【js实现页签切换】

热门文章

  1. android自定义alertdialog不现实输入法,自定义的dialog中的EditText无法弹出输入法解决方案...
  2. Kafka:集群部署
  3. Mac使用Homebrew安装Kafka
  4. 改变centos系统的时区
  5. PC介绍之PCIE、总线、内存、电源
  6. 【译】Googler如何解决编程问题
  7. IPv4地址分类及特征
  8. Maven的Settings.xml配置文件解释
  9. linux常用命令和配置
  10. [MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现