#include <iostream> using namespace std; /** * Objects * Data:the properties or status * Operation: the function * * * */ class Piont { private: // 私有成员 // 如果没有声明默认是 公有变量; double x, y; public: /** * 公有成员方法 * */ Piont(){}; // 定义 类Point的无参数构造函数 Piont(double a, double b) { x = a; y = b; }; // 类Piont有参数的构造函数 void Setxy(double a, double b) { x = a; y = b; }; // 成员函数用于重新设置数据成员 void Display() { cout << x << "\t" << y << endl; }; // 成员函数 用户输出指定格式的数据格式 }; int main(int argc, char const *argv[]) { Piont a; Piont b(1.1, 2.2); a.Setxy(3.3, 4.4); a.Display(); b.Display(); return 0; }
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <iostream>
using
namespace
std
;
/
*
*
*
Objects
*
Data:
the
properties
or
status
*
Operation
:
the
function
*
*
*
*
/
class
Piont
{
private
:
/
/
私有成员
/
/
如果没有声明默认是
公有变量;
double
x
,
y
;
public
:
/
*
*
*
公有成员方法
*
*
/
Piont
(
)
{
}
;
/
/
定义
类
Point的无参数构造函数
Piont
(
double
a
,
double
b
)
{
x
=
a
;
y
=
b
;
}
;
/
/
类
Piont有参数的构造函数
void
Setxy
(
double
a
,
double
b
)
{
x
=
a
;
y
=
b
;
}
;
/
/
成员函数用于重新设置数据成员
void
Display
(
)
{
cout
<<
x
<<
"\t"
<<
y
<<
endl
;
}
;
/
/
成员函数
用户输出指定格式的数据格式
}
;
int
main
(
int
argc
,
char
const
*
argv
[
]
)
{
Piont
a
;
Piont
b
(
1.1
,
2.2
)
;
a
.
Setxy
(
3.3
,
4.4
)
;
a
.
Display
(
)
;
b
.
Display
(
)
;
return
0
;
}
|
本文详细介绍了C++中类的定义与使用,包括构造函数、成员函数和数据成员的实现。通过具体示例,展示了如何创建类的实例,设置和获取属性值,以及输出指定格式的数据。
1629

被折叠的 条评论
为什么被折叠?



