🌎 欢迎大家关注公众号——空中飞马 后续踩坑记录我会上传到公众号
private 子类、外部不可访问
protected 子类可访问 外部不可访问
public 子类、外部可访问
My_Alice
今天的成果:
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
#include <iostream.h>
//struct Point
class Point
{
public:
//private:
//protected:
int x;
int y;
/* void init()
{
cin>>x>>y;
} */
Point()
{
x=0;
y=0;
}
Point(int a,int b)
{
x=a;
y=b;
}
~Point()
{
}
void outcry()
{
cout<<x<<endl<<y<<endl;
}
void outcry(int x,int y)
{
this->x=x;
this->y=y;
}
};
void main()
{
Point pt(3,0);
// cout<<pt.x<<endl<<pt.y<<endl;
// cout<<pt.x<<endl;
// pt.init();
pt.outcry( 5,5);
pt.outcry();
}
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
#include <iostream.h>
class Animal
{
public:
Animal(int shengao,int tizhong)
{
cout<<“I Love Animal”<<endl;
}
~Animal()
{
cout<<“Not Love Animal”<<endl;
}
void eat()
{
cout<<"eat"<<endl;
}
protected:
void sleep()
{
cout<<“sleep”<<endl;
}
private:
void jog()
{
cout<<“jog”<<endl;
}
};
class fish : public Animal
{
/* void test()
{
sleep();
jog();
}
*/
public:
fish() : Animal(200,100)
{
cout<<“I Love Fish”<<endl;
}
~fish()
{
cout<<“Not Love Fish”<<endl;
}
};
void main()
{
// Animal ann;
// ann.eat();
fish fhh;
// fhh.test();
//先构造父亲
}
结果为:
1
2
3
4
5
I Love Animal
I Love Fish
Not Love Fish
Not Love Animal
Press any key to continue
先构造基类
析构时子类先析构
函数的覆盖:发生在父类与子类之间
函数的重构:一个类之中