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,33 @@
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:param`
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:noctor`
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:cseq`
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:ctor`
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:ctor_d`
# please run `bazel run //practical_exercises/10_day_practice/day5/ctor_dtor:seq`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "param",
srcs = ["param.cpp"],
)
cc_binary(
name = "noctor",
srcs = ["noctor.cpp"],
)
cc_binary(
name = "cseq",
srcs = ["cseq.cpp"],
)
cc_binary(
name = "ctor",
srcs = ["ctor.cpp"],
)
cc_binary(
name = "ctor_d",
srcs = ["ctor_d.cpp"],
)
cc_binary(
name = "seq",
srcs = ["seq.cpp"],
)

View File

@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;
/*
先构造成员
再构造自身(调用构造函数)
*/
class A {
public:
A() { cout << "Constructing A" << endl; }
~A() { cout << "Destructing A" << endl; }
};
class B {
public:
B() { cout << "Constructing B" << endl; }
~B() { cout << "Destructing B" << endl; }
};
class C {
public:
C() { cout << "Constructing C" << endl; }
~C() { cout << "Destructing C" << endl; }
B b;
A a;
};
int main() {
C c;
}
/*
执行结果:
Constructing B
Constructing A
Constructing C
Destructing C
Destructing A
Destructing B
*/

View File

@@ -0,0 +1,29 @@
#include <iostream>
using namespace std;
class Base {
private:
int x;
public:
Base(int a) {
x = a;
cout << "Base constructor x=" << x << endl;
}
~Base() { cout << "Base destructor..." << endl; }
};
class Derived : public Base {
private:
int y;
public:
Derived(int a, int b) : Base(a) { //派生类构造函数的初始化列表
y = b;
cout << "Derived constructor y=" << y << endl;
}
~Derived() { cout << "Derived destructor..." << endl; }
};
int main() {
Derived d(1, 2);
return 0;
}

View File

@@ -0,0 +1,44 @@
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "Constructing A" << endl; }
~A() { cout << "Destructing A" << endl; }
};
class B {
public:
B() { cout << "Constructing B" << endl; }
~B() { cout << "Destructing B" << endl; }
};
class C {
public:
C() { cout << "Constructing C" << endl; }
~C() { cout << "Destructing C" << endl; }
};
class D : public C {
public:
D() { cout << "Constructing D" << endl; }
~D() { cout << "Destructing D" << endl; }
B b;
A a;
C c;
};
int main() {
D d;
}
/*
执行结果:
Constructing C
Constructing B
Constructing A
Constructing C
Constructing D
Destructing D
Destructing C
Destructing A
Destructing B
Destructing C
*/

View File

@@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "Constructing A" << endl; }
~A() { cout << "Destructing A" << endl; }
};
class B : public A {
public:
~B() { cout << "Destructing B" << endl; }
};
int main() {
B b;
}

View File

@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
class Point {
protected:
int x, y;
public:
Point(int a, int b = 0) {
x = a;
y = b;
cout << "constructing point(" << x << "," << y << ")" << endl;
}
};
class Line : public Point {
protected:
int len;
public:
Line(int a, int b, int l) : Point(a, b) { //构造函数初始化列表
len = l;
cout << "Constructing Line,len ..." << len << endl;
}
};
int main() {
Line L1(1, 2, 3);
}

View File

@@ -0,0 +1,41 @@
// Eg6-12.cpp
#include <iostream>
using namespace std;
class A {
int x;
public:
A(int i = 0) {
x = i;
cout << "A-----" << x << endl;
}
};
class B {
int y;
public:
B(int i) {
y = i;
cout << "B-----" << y << endl;
}
};
class C {
int z;
public:
C(int i) {
z = i;
cout << "C-----" << z << endl;
}
};
class D : public B {
public:
C c1, c2;
A *a1 = new A(10);
A a0, a4;
D() : a4(4), c2(2), c1(1), B(1) { cout << "D-----5" << endl; }
};
int main() {
D d;
}

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

@@ -5,28 +5,31 @@
*/
#include <iostream>
using namespace std;
class Base{
int x;
class Base {
int x;
protected:
int getx(){ return x; }
int getx() { return x; }
public:
void setx(int n){ x=n; }
void showx(){ cout<<x<<endl; }
void setx(int n) { x = n; }
void showx() { cout << x << endl; }
};
class Derived:protected Base{
int y;
class Derived : protected Base {
int y;
public:
void sety(int n){ y=n; }
void sety(){ y=getx();} //访问基类的保护成员
void showy(){ cout<<y<<endl; }
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();
system("pause");
int main() {
Derived obj;
obj.setx(10); //错误
obj.sety(20);
obj.showx(); //错误,
obj.showy();
}
/*

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,8 @@
# please run `bazel run //practical_exercises/10_day_practice/day5/rela:rela`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "rela",
srcs = ["rela.cpp"],
)

View File

@@ -0,0 +1,55 @@
/* 基类与派生类(重要).cpp */
#include <iostream>
using namespace std;
class A {
int a;
public:
void setA(int x) { a = x; }
int getA() { return a; }
};
class B : public A {
int b;
public:
void setB(int x) { b = x; }
int getB() { return b; }
};
void f1(A a, int x) { a.setA(x); }
void f2(A *pA, int x) { pA->setA(x); }
void f3(A &rA, int x) { rA.setA(x); }
int main() {
A a1, *pA;
B b1;
a1.setA(1);
b1.setA(2);
//把派生类对象赋值给基类对象。
a1 = b1;
cout << a1.getA() << endl;
cout << b1.getA() << endl;
a1.setA(10);
cout << a1.getA() << endl;
cout << b1.getA() << endl;
//把派生类对象的地址赋值给基类指针。
pA = &b1;
pA->setA(20);
cout << pA->getA() << endl;
cout << b1.getA() << endl;
//用派生类对象初始化基类对象的引用。
A &ra = b1;
ra.setA(30);
cout << pA->getA() << endl;
cout << b1.getA() << endl;
b1.setA(7);
cout << b1.getA() << endl;
f1(b1, 100);
cout << "1111111111" << endl;
cout << b1.getA() << endl; // 7
f2(&b1, 200);
cout << b1.getA() << endl;
f3(b1, 300);
cout << b1.getA() << endl;
return 0;
}

View File

@@ -0,0 +1,8 @@
# please run `bazel run //practical_exercises/10_day_practice/day5/rule:direct`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "direct",
srcs = ["direct.cpp"],
)

View File

@@ -0,0 +1,26 @@
/* 直接基类与间接基类.cpp */
//当同时存在直接基类和间接基类时,每个派生类只负责其直接基类的构造。
#include <iostream>
using namespace std;
class A {
int x;
public:
A(int aa) {
x = aa;
cout << "Constructing A" << endl;
}
~A() { cout << "Destructing A" << endl; }
};
class B : public A {
public:
B(int x) : A(x) { cout << "Constructing B" << endl; }
};
class C : public B {
public:
C(int y) : B(y) { cout << "Constructing C" << endl; }
};
int main() {
C c(1);
}

View File

@@ -0,0 +1,23 @@
# please run `bazel run //practical_exercises/10_day_practice/day5/virtual:example2`
# please run `bazel run //practical_exercises/10_day_practice/day5/virtual:init`
# please run `bazel run //practical_exercises/10_day_practice/day5/virtual:seq`
# please run `bazel run //practical_exercises/10_day_practice/day5/virtual:example1`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "example2",
srcs = ["example2.cpp"],
)
cc_binary(
name = "init",
srcs = ["init.cpp"],
)
cc_binary(
name = "seq",
srcs = ["seq.cpp"],
)
cc_binary(
name = "example1",
srcs = ["example1.cpp"],
)

View File

@@ -0,0 +1,17 @@
/* 例1.cpp */
#include <iostream>
using namespace std;
class A {
public:
void vf() { cout << "I come from class A" << endl; }
};
class B : public A {};
class C : public A {};
class D : public B, public C {};
int main() {
D d;
d.vf(); // error
return 0;
}

View File

@@ -0,0 +1,17 @@
/* 例2.cpp */
#include <iostream>
using namespace std;
class A {
public:
void vf() { cout << "I come from class A" << endl; }
};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
int main() {
D d;
d.vf(); // okay
return 0;
}

View File

@@ -0,0 +1,39 @@
/* 派生类初始化.cpp */
#include <iostream>
using namespace std;
class A {
int a;
public:
A(int x) {
a = x;
cout << "Virtual Bass A..." << endl;
}
};
class B : virtual public A {
public:
B(int i) : A(i) { cout << "Virtual Bass B..." << endl; }
};
class C : virtual public A {
int x;
public:
C(int i) : A(i) {
cout << "Constructing C..." << endl;
x = i;
}
};
class ABC : public C, public B {
public:
//虚基类由最终派生类初始化
ABC(int i, int j, int k)
: C(i), B(j), A(i) // L1这里必须对A进行初始化
{
cout << "Constructing ABC..." << endl;
}
};
int main() {
ABC obj(1, 2, 3);
return 0;
}

View File

@@ -0,0 +1,33 @@
/* 虚基类调用次序(重要).cpp */
//重要!!!
#include <iostream>
using namespace std;
class A {
int a;
public:
A() { cout << "Constructing A" << endl; }
};
class B {
public:
B() { cout << "Constructing B" << endl; }
};
class B1 : virtual public B, virtual public A {
public:
B1(int i) { cout << "Constructing B1" << endl; }
};
class B2 : public A, virtual public B {
public:
B2(int j) { cout << "Constructing B2" << endl; }
};
class D : public B1, public B2 {
public:
D(int m, int n) : B1(m), B2(n) { cout << "Constructing D" << endl; }
A a;
};
int main() {
D d(1, 2);
return 0;
}

View File

@@ -1,55 +0,0 @@
#include <iostream>
using namespace std;
class A {
int a;
public:
void setA(int x){ a=x; }
int getA(){ return a;}
};
class B:public A{
int b;
public:
void setB(int x){ b=x; }
int getB(){ return b;}
};
void f1(A a, int x)
{ a.setA(x); }
void f2(A *pA, int x)
{ pA->setA(x); }
void f3(A &rA, int x)
{ rA.setA(x); }
int main(){
A a1,*pA;
B b1,*pB;
a1.setA(1);
b1.setA(2);
//把派生类对象赋值给基类对象。
a1=b1;
cout<<a1.getA()<<endl;
cout<<b1.getA()<<endl;
a1.setA(10);
cout<<a1.getA()<<endl;
cout<<b1.getA()<<endl;
//把派生类对象的地址赋值给基类指针。
pA=&b1;
pA->setA(20);
cout<<pA->getA()<<endl;
cout<<b1.getA()<<endl;
//用派生类对象初始化基类对象的引用。
A &ra=b1;
ra.setA(30);
cout<<pA->getA()<<endl;
cout<<b1.getA()<<endl;
b1.setA(7);
cout<<b1.getA()<<endl;
f1(b1,100);
cout<<"1111111111"<<endl;
cout<<b1.getA()<<endl; //7
f2(&b1,200);
cout<<b1.getA()<<endl;
f3(b1,300);
cout<<b1.getA()<<endl;
system("pause");
return 0;
}

View File

@@ -1,40 +0,0 @@
//Eg6-12.cpp
#include <iostream>
using namespace std;
class A {
int x;
public:
A(int i=0) {
x=i;
cout << "A-----"<<x<<endl;
}
};
class B {
int y;
public:
B(int i) {
y=i;
cout << "B-----"<<y<<endl;
}
};
class C {
int z;
public:
C(int i) {
z=i;
cout << "C-----"<<z<<endl;
}
};
class D : public B{
public:
C c1, c2;
A *a1 = new A(10);
A a0,a4;
D( ) : a4(4),c2(2),c1(1),B(1) {
cout << "D-----5"<<endl;
}
};
int main(){
D d;
system("pause");
}

View File

@@ -1,43 +0,0 @@
#include<iostream>
using namespace std;
/*
先构造成员
再构造自身(调用构造函数)
*/
class A {
public:
A() { cout<<"Constructing A"<<endl;}
~A(){ cout<<"Destructing A"<<endl;}
};
class B {
public:
B() { cout<<"Constructing B"<<endl;}
~B(){ cout<<"Destructing B"<<endl;}
};
class C
{
public:
C() { cout<<"Constructing C"<<endl;}
~C(){ cout<<"Destructing C"<<endl;}
B b;
A a;
};
int main()
{
C c;
system("pause");
}
/*
执行结果:
Constructing B
Constructing A
Constructing C
Destructing C
Destructing A
Destructing B
*/

View File

@@ -1,15 +0,0 @@
#include <iostream>
using namespace std;
class A {
public:
A(){ cout<<"Constructing A"<<endl; }
~A(){ cout<<"Destructing A"<<endl; }
};
class B:public A {
public:
~B(){ cout<<"Destructing B"<<endl; }
};
int main(){
B b;
system("pause");
}

View File

@@ -1,27 +0,0 @@
#include <iostream>
using namespace std;
class Base{
private:
int x;
public:
Base(int a){
x=a;
cout<<"Base constructor x="<<x<<endl;
}
~Base(){ cout<<"Base destructor..."<<endl; }
};
class Derived:public Base{
private:
int y;
public:
Derived(int a,int b):Base(a){ //派生类构造函数的初始化列表
y=b;
cout<<"Derived constructor y="<<y<<endl;
}
~Derived(){ cout<<"Derived destructor..."<<endl; }
};
int main(){
Derived d(1,2);
system("pause");
return 0;
}

View File

@@ -1,46 +0,0 @@
#include<iostream>
using namespace std;
class A {
public:
A() { cout<<"Constructing A"<<endl;}
~A(){ cout<<"Destructing A"<<endl;}
};
class B {
public:
B() { cout<<"Constructing B"<<endl;}
~B(){ cout<<"Destructing B"<<endl;}
};
class C {
public:
C() { cout<<"Constructing C"<<endl;}
~C(){ cout<<"Destructing C"<<endl;}
};
class D:public C
{
public:
D() { cout<<"Constructing D"<<endl;}
~D(){ cout<<"Destructing D"<<endl;}
B b;
A a;
C c;
};
int main()
{
D d;
system("pause");
}
/*
执行结果:
Constructing C
Constructing B
Constructing A
Constructing C
Constructing D
Destructing D
Destructing C
Destructing A
Destructing B
Destructing C
*/

View File

@@ -1,24 +0,0 @@
#include <iostream>
using namespace std;
class Point{
protected:
int x,y;
public:
Point(int a,int b=0) {
x=a; y=b;
cout<<"constructing point("<<x<<","<<y<<")"<<endl;
}
};
class Line:public Point{
protected:
int len;
public:
Line(int a,int b,int l):Point(a,b) { //构造函数初始化列表
len=l;
cout<<"Constructing Line,len ..."<<len<<endl;
}
};
int main(){
Line L1(1,2,3);
system("pause");
}

View File

@@ -1,36 +0,0 @@
/*
基类中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;
system("pause");
return 0;
}

View File

@@ -1,30 +0,0 @@
#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();
system("pause");
}

View File

@@ -1,25 +0,0 @@
#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

@@ -1,18 +0,0 @@
#include<iostream>
using namespace std;
class A {
public:
void vf() {
cout<<"I come from class A"<<endl; }
};
class B: public A{};
class C: public A{};
class D: public B, public C{};
int main()
{
D d;
d.vf (); // error
system("pause");
return 0;
}

View File

@@ -1,18 +0,0 @@
#include<iostream>
using namespace std;
class A {
public:
void vf() {
cout<<"I come from class A"<<endl; }
};
class B: virtual public A{};
class C: virtual public A{};
class D: public B, public C{};
int main()
{
D d;
d.vf (); // okay
system("pause");
return 0;
}

View File

@@ -1,33 +0,0 @@
#include <iostream>
using namespace std;
class A {
int a;
public:
A(int x) {
a=x;
cout<<"Virtual Bass A..."<<endl;
}
};
class B:virtual public A {
public:
B(int i):A(i){ cout<<"Virtual Bass B..."<<endl; }
};
class C:virtual public A{
int x;
public:
C(int i):A(i){
cout<<"Constructing C..."<<endl;
x=i;
}
};
class ABC:public C, public B {
public:
//虚基类由最终派生类初始化
ABC(int i,int j,int k):C(i),B(j),A(i) //L1这里必须对A进行初始化
{ cout<<"Constructing ABC..."<<endl; }
};
int main(){
ABC obj(1,2,3);
system("pause");
return 0;
}

View File

@@ -1,31 +0,0 @@
//重要!!!
#include <iostream>
using namespace std;
class A {
int a;
public:
A(){ cout<<"Constructing A"<<endl; }
};
class B {
public:
B(){ cout<<"Constructing B"<<endl;}
};
class B1:virtual public B ,virtual public A{
public:
B1(int i){ cout<<"Constructing B1"<<endl; }
};
class B2:public A,virtual public B {
public:
B2(int j){ cout<<"Constructing B2"<<endl; }
};
class D: public B1, public B2 {
public:
D(int m,int n): B1(m),B2(n){ cout<<"Constructing D"<<endl; }
A a;
};
int main(){
D d(1,2);
system("pause");
return 0;
}

View File

@@ -1,24 +0,0 @@
//当同时存在直接基类和间接基类时,每个派生类只负责其直接基类的构造。
#include <iostream>
using namespace std;
class A {
int x;
public:
A(int aa) {
x=aa;
cout<<"Constructing A"<<endl;
}
~A(){ cout<<"Destructing A"<<endl; }
};
class B:public A {
public:
B(int x):A(x){ cout<<"Constructing B"<<endl; }
};
class C :public B{
public:
C(int y):B(y){ cout<<"Constructing C"<<endl; }
};
int main(){
C c(1);
system("pause");
}