-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstruct_function.cpp
More file actions
70 lines (62 loc) · 1.31 KB
/
struct_function.cpp
File metadata and controls
70 lines (62 loc) · 1.31 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
//
// Created by pengshuai on 2022/3/24.
//
#include<iostream>
#include<cstdio>
using namespace std;
struct Base {
int v1{};
private:
int v3{};
public: // 显示声明public
int v2{};
static void print(){
printf("%s\n","Base hello world");
};
Base() {
cout<<"Base construct"<<endl;
};
virtual ~Base() {
cout<<"Base deconstruct"<<endl;
};
};
typedef struct Base1 {
int v1;
// private: //error!
int v3;
public: //显示声明public
int v2;
static void print(){
printf("%s\n","hello world");
};
} B;
struct Derived:Base {
Derived() {
cout<<"Derived construct"<<endl;
};
~Derived() override {
cout<<"Derived deconstruct"<<endl;
};
public:
int v2 {};
static void print(){
printf("%s\n","Derived, hello world!");
};
};
//void Base(){
// printf("%s\n","I am Base func");
//}
//void B() {} //error! 符号 "B" 已经被定义为一个 "struct Base1" 的别名
int main() {
// Base base{}; // error
struct Base base{};
base.v1=1;
// base.v3=2; // error
Base::print();
printf("%d\n",base.v1);
// printf("%d\n",base.v3); // error
Base *b = new Derived();
b->print(); // todo:Base::print() 和 class 有点不一样
delete b;
return 0;
}