Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
code_shenbing
cpp_10_defineClass
提交
fc29a5c9
C
cpp_10_defineClass
项目概览
code_shenbing
/
cpp_10_defineClass
通知
2
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cpp_10_defineClass
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
fc29a5c9
编写于
4月 08, 2023
作者:
W
wsb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加静态成员的使用示例!
上级
5967fc38
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
62 addition
and
3 deletion
+62
-3
CMakeLists.txt
CMakeLists.txt
+1
-1
main.cpp
main.cpp
+25
-2
staticDemo.cpp
staticDemo.cpp
+19
-0
staticDemo.h
staticDemo.h
+17
-0
未找到文件。
CMakeLists.txt
浏览文件 @
fc29a5c9
...
@@ -3,4 +3,4 @@ project(cpp_10_defineClass)
...
@@ -3,4 +3,4 @@ project(cpp_10_defineClass)
set
(
CMAKE_CXX_STANDARD 11
)
set
(
CMAKE_CXX_STANDARD 11
)
add_executable
(
cpp_10_defineClass main.cpp Student.cpp Student.h Book.cpp Book.h friendFunc.cpp friendFunc.h ThisDemo.cpp ThisDemo.h
)
add_executable
(
cpp_10_defineClass main.cpp Student.cpp Student.h Book.cpp Book.h friendFunc.cpp friendFunc.h ThisDemo.cpp ThisDemo.h
staticDemo.cpp staticDemo.h
)
main.cpp
浏览文件 @
fc29a5c9
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include "Book.h"
#include "Book.h"
#include "friendFunc.h"
#include "friendFunc.h"
#include "ThisDemo.h"
#include "ThisDemo.h"
#include "staticDemo.h"
//使用online关键字申明一个内联函数:执行内容简单,使用inline修饰,凡是出现内联函数调用的地方编译器均会赋值副本。整体上是以空间换取时间。
//使用online关键字申明一个内联函数:执行内容简单,使用inline修饰,凡是出现内联函数调用的地方编译器均会赋值副本。整体上是以空间换取时间。
inline
int
max
(
int
a
,
int
b
){
inline
int
max
(
int
a
,
int
b
){
...
@@ -60,8 +61,30 @@ int main() {
...
@@ -60,8 +61,30 @@ int main() {
ThisDemo
thisDemo
;
ThisDemo
thisDemo
;
thisDemo
.
printAge
(
23
);
thisDemo
.
printAge
(
23
);
//类指针的使用示范: 申明一个类指针,把book变量的地址赋予它
Book
*
bookPointer
=&
book
;
//使用类指针访问类的成员
bookPointer
->
disValue
(
book
);
//创建多个对象,看看静态成员的内存地址是否会变化
staticDemo
demo
,
demo1
,
demo2
,
demo3
;
int
a
=
demo
.
count
+
1
;
int
b
=
demo1
.
count
+
2
;
int
c
=
demo2
.
count
+
3
;
int
d
=
demo3
.
count
+
4
;
cout
<<
"count的指针地址:"
<<&
(
demo
.
count
)
<<
","
<<&
(
demo1
.
count
)
<<
","
<<&
(
demo2
.
count
)
<<
","
<<&
(
demo3
.
count
)
<<
endl
;
//输出结果为: 0x7ff7e4779090,0x7ff7e4779090,0x7ff7e4779090,0x7ff7e4779090 ,可以发现四个对象的count始终只有一个副本
cout
<<
"值变化:"
<<
a
<<
","
<<
b
<<
","
<<
c
<<
","
<<
d
<<
","
<<
endl
;
demo
.
printCount
();
demo1
.
printCount
();
demo2
.
printCount
();
demo3
.
printCount
();
//也可以直接访问public修饰的静态成员
staticDemo
::
count
=
100
;
staticDemo
::
printCount
();
return
0
;
return
0
;
}
}
staticDemo.cpp
0 → 100644
浏览文件 @
fc29a5c9
//
// Created by 11010 on 2023/4/8.
//
#include "staticDemo.h"
#include <iostream>
using
namespace
std
;
//在外部使用类访问修饰符:: 初始化静态成员变量
int
staticDemo
::
count
=
0
;
void
staticDemo
::
printCount
()
{
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
count
++
;
}
cout
<<
"总次数为:"
<<
count
<<
endl
;
}
staticDemo.h
0 → 100644
浏览文件 @
fc29a5c9
//
// Created by 11010 on 2023/4/8.
//
#ifndef CPP_10_DEFINECLASS_STATICDEMO_H
#define CPP_10_DEFINECLASS_STATICDEMO_H
class
staticDemo
{
public:
//使用static申明成员变量与函数: 静态成员为类所共享,且静态成员会一致存在,无论创建多少个类的对象均只有一个内存副本。
static
int
count
;
static
void
printCount
();
};
#endif //CPP_10_DEFINECLASS_STATICDEMO_H
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录