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,43 @@
# you can run some main program, just replace binary name
# such as: `bazel run basic_content/abstract:interesting_facts1`
# `bazel run basic_content/abstract:interesting_facts2`
# etc...
load("@rules_cc//cc:defs.bzl", "cc_binary")
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "interesting_facts1",
srcs = ["interesting_facts1.cpp"],
)
cc_binary(
name = "interesting_facts2",
srcs = ["interesting_facts2.cpp"],
)
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "interesting_facts3",
srcs = ["interesting_facts3.cpp"],
)
cc_binary(
name = "interesting_facts4",
srcs = ["interesting_facts4.cpp"],
)
cc_binary(
name = "interesting_facts5",
srcs = ["interesting_facts5.cpp"],
)
# Don't panic if you get compilation errors, this is what this code demonstrates, as expected.
cc_binary(
name = "pure_virtual",
srcs = ["pure_virtual.cpp"],
)
cc_binary(
name = "derived_full",
srcs = ["derived_full.cpp"],
)

View File

@@ -23,7 +23,7 @@ public:
抽象类只能作为基类来派生新类使用,不能创建抽象类的对象,抽象类的指针和引用->由抽象类派生出来的类的对象!
> 代码样例:[test.cpp](./test.cpp)、[pure_virtual.cpp](./pure_virtual.cpp)
> 代码样例:[abstract_base.h](./abstract_base.h)、[pure_virtual.cpp](./pure_virtual.cpp)
## 2.实现抽象类

View File

@@ -1,27 +1,28 @@
/**
* @file abstract.cpp
* @brief 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数
* @brief
* 抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数
* 如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类
* @author 光城
* @version v1
* @date 2019-07-20
*/
#include<iostream>
#include <iostream>
using namespace std;
class A {
public:
virtual void f() = 0; // 纯虚函数
void g(){ this->f(); }
A(){}
virtual void f() = 0; // 纯虚函数
void g() { this->f(); }
A() {}
};
class B:public A{
class B : public A {
public:
void f(){ cout<<"B:f()"<<endl;}
void f() { cout << "B:f()" << endl; }
};
int main(){
B b;
b.g();
return 0;
int main() {
B b;
b.g();
return 0;
}

View File

@@ -0,0 +1,21 @@
/**
* @file abstreact_base.cpp
* @brief
* C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数
* 纯虚函数:没有函数体的虚函数
* @author 光城
* @version v1
* @date 2019-07-20
*/
/**
* @brief 抽象类
*/
class AbstractBase {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};

View File

@@ -6,27 +6,24 @@
* @date 2019-07-20
*/
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
class Base {
int x;
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; } // 实现了fun()函数
};
public:
virtual void fun() = 0;
int getX() { return x; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
class Derived : public Base {
public:
void fun() { cout << "fun() called"; } // 实现了fun()函数
};
int main(void) {
Derived d;
d.fun();
return 0;
}

View File

@@ -6,23 +6,21 @@
* @date 2019-07-20
*/
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
/**
* @brief 抽象类至少包含一个纯虚函数
*/
class Test
{
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
class Test {
int x;
int main(void)
{
Test t; //error! 不能创建抽象类的对象
return 0;
}
public:
virtual void show() = 0;
int getX() { return x; }
};
int main(void) {
Test t; // error! 不能创建抽象类的对象
return 0;
}

View File

@@ -6,33 +6,28 @@
* @date 2019-07-20
*/
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
/**
* @brief 抽象类至少包含一个纯虚函数
*/
class Base
{
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
class Derived: public Base
{
public:
void show() { cout << "In Derived \n"; }
Derived(){}
};
int main(void)
{
//Base b; //error! 不能创建抽象类的对象
//Base *b = new Base(); error!
Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
bp->show();
return 0;
}
class Base {
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
class Derived : public Base {
public:
void show() { cout << "In Derived \n"; }
Derived() {}
};
int main(void) {
// Base b; //error! 不能创建抽象类的对象
// Base *b = new Base(); error!
Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
bp->show();
return 0;
}

View File

@@ -6,24 +6,23 @@
* @date 2019-07-20
*/
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
class Base
{
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
class Derived: public Base
{
public:
// void show() { }
};
int main(void)
{
Derived d; //error! 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象
return 0;
}
class Base {
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
class Derived : public Base {
public:
// void show() { }
};
int main(void) {
Derived
d; // error!
// 派生类没有实现纯虚函数,那么派生类也会变为抽象类,不能创建抽象类的对象
return 0;
}

View File

@@ -6,30 +6,29 @@
* @date 2019-07-20
*/
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
// An abstract class with constructor
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i) { x = i; }
};
// An abstract class with constructor
class Base {
protected:
int x;
class Derived: public Base
{
int y;
public:
Derived(int i, int j):Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " << y; }
};
public:
virtual void fun() = 0;
Base(int i) { x = i; }
};
int main(void)
{
Derived d(4, 5);
d.fun();
return 0;
}
class Derived : public Base {
int y;
public:
Derived(int i, int j) : Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " << y; }
};
int main(void) {
Derived d(4, 5);
d.fun();
return 0;
}

View File

@@ -7,24 +7,23 @@
* @version v1
* @date 2019-07-20
*/
#include<iostream>
#include <iostream>
using namespace std;
class Base {
public:
Base() { cout << "Constructor: Base" << endl; }
virtual ~Base() { cout << "Destructor : Base" << endl; }
class Base {
public:
Base() { cout << "Constructor: Base" << endl; }
virtual ~Base() { cout << "Destructor : Base" << endl; }
};
class Derived: public Base {
public:
Derived() { cout << "Constructor: Derived" << endl; }
~Derived() { cout << "Destructor : Derived" << endl; }
class Derived : public Base {
public:
Derived() { cout << "Constructor: Derived" << endl; }
~Derived() { cout << "Destructor : Derived" << endl; }
};
int main() {
Base *Var = new Derived();
delete Var;
return 0;
int main() {
Base *Var = new Derived();
delete Var;
return 0;
}

View File

@@ -2,33 +2,31 @@
* @file pure_virtual.cpp
* @brief 纯虚函数:没有函数体的虚函数
* 抽象类:包含纯虚函数的类
*
*
* @author 光城
* @version v1
* @date 2019-07-20
*/
#include<iostream>
#include <iostream>
using namespace std;
class A
{
class A {
private:
int a;
int a;
public:
virtual void show()=0; // 纯虚函数
virtual void show() = 0; // 纯虚函数
};
int main() {
/*
* 1. 抽象类只能作为基类来派生新类使用
* 2. 抽象类的指针和引用->由抽象类派生出来的类的对象!
*/
A a; // error 抽象类,不能创建对象
int main()
{
/*
* 1. 抽象类只能作为基类来派生新类使用
* 2. 抽象类的指针和引用->由抽象类派生出来的类的对象!
*/
A a; // error 抽象类,不能创建对象
A *a1; // ok 可以定义抽象类的指针
A *a1; // ok 可以定义抽象类的指针
A *a2 = new A(); // error, A是抽象类不能创建对象
A *a2 = new A(); // error, A是抽象类不能创建对象
}

View File

@@ -1,23 +0,0 @@
/**
* @file test.cpp
* @brief C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它!通过声明中赋值0来声明纯虚函数
* 纯虚函数:没有函数体的虚函数
* @author 光城
* @version v1
* @date 2019-07-20
*/
/**
* @brief 抽象类
*/
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};