support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,23 @@
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:public`
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:protected_inherit`
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:protected`
# please run `bazel run //practical_exercises/10_day_practice/day5/inherit_access:private`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "public",
srcs = ["public.cpp"],
)
cc_binary(
name = "protected_inherit",
srcs = ["protected_inherit.cpp"],
)
cc_binary(
name = "protected",
srcs = ["protected.cpp"],
)
cc_binary(
name = "private",
srcs = ["private.cpp"],
)

View File

@@ -0,0 +1,31 @@
#include <iostream>
using namespace std;
class base {
int x;
public:
void setx(int n) { x = n; }
int getx() { return x; }
void showx() { cout << x << endl; }
};
//派生类
class derived : public base {
int y;
public:
void sety(int n) { y = n; }
void sety() { y = getx(); }
void showy() { cout << y << endl; }
};
//派生类不可直接访问基类的private成员可通过基类的共有成员函数访问
int main() {
derived obj;
obj.setx(10);
obj.sety(20);
obj.showx();
obj.showy();
obj.sety();
obj.showx();
obj.showy();
}

View File

@@ -0,0 +1,33 @@
/*
基类中protected的成员
类内部:可以访问
类的使用者:不能访问
类的派生类成员:可以访问
*/
#include <iostream>
class B {
private:
int i;
protected:
int j;
public:
int k;
};
class D : public B {
public:
void f() {
i = 1; // cannot access 派生类不可访问基类私有成员
j = 2; //派生类可以访问基类保护成员
k = 3;
}
};
int main() {
B b;
b.i = 1; // cannot access 私有成员,类的使用者不能访问
b.j = 2; // cannot access 保护成员,类的使用者不能访问
b.k = 3;
return 0;
}

View File

@@ -0,0 +1,40 @@
/*
派生方式为protected的继承称为保护继承在这种继承方式下
基类的public成员在派生类中会变成protected成员
基类的protected和private成员在派生类中保持原来的访问权限
*/
#include <iostream>
using namespace std;
class Base {
int x;
protected:
int getx() { return x; }
public:
void setx(int n) { x = n; }
void showx() { cout << x << endl; }
};
class Derived : protected Base {
int y;
public:
void sety(int n) { y = n; }
void sety() { y = getx(); } //访问基类的保护成员
void showy() { cout << y << endl; }
};
int main() {
Derived obj;
obj.setx(10); //错误
obj.sety(20);
obj.showx(); //错误,
obj.showy();
}
/*
解释:
如最上面文字所示保护继承会将基类的public变为protected而对于protected成员
外部去使用保护成员的时候会报错所以setx与showx访问错误而对于派生类则可直接访问基类的保护成员
在派生类中y=getx()可正常访问!
*/

View File

@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
class Base {
int x;
public:
void setx(int n) { x = n; }
int getx() { return x; }
void showx() { cout << x << endl; }
};
//私有继承
//基类的中的public成员在派生类中是private, private成员在派生类中不可访问。
class derived : private Base {
int y;
public:
void sety(int n) { y = n; }
void sety() { y = getx(); }
void showy() { cout << y << endl; }
};
int main() {
derived obj;
obj.setx(10); // cannot access
obj.sety(20);
obj.showx(); // cannot access
obj.showy();
}

View File

@@ -0,0 +1,48 @@
一、公有继承
1.基类中protected的成员
类内部:可以访问,
类的使用者:不能访问,
类的派生类成员:可以访问,
2.派生类不可访问基类的private成员
3.派生类可访问基类的protected成员
4.派生类可访问基类的public成员
基类 public继承 派生类
public -> public
protected -> protected
private -> 不可访问
二、私有继承
派生类也不可访问基类的private成员
基类 private继承 派生类
public -> private
protected -> private
private -> 不可访问
三、保护继承
派生方式为protected的继承称为保护继承在这种继承方式下
基类的public成员在派生类中会变成protected成员
基类的protected和private成员在派生类中保持原来的访问权限
注意点当采用保护继承的时候由于public成员变为protected成员因此类的使用者不可访问而派生类可访问
基类 protected继承 派生类
public -> protected
protected -> protected
private -> 不可访问
四、派生类对基类成员的访问形式
1.通过派生类对象直接访问基类成员
2.在派生类成员函数中直接访问基类成员
3.通过基类名字限定访问被重载的基类成员名