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类,并在类末尾使用分号前的两个对象A和B。
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 ++查找字符串