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
+8
View File
@@ -0,0 +1,8 @@
# please run `bazel run basic_content/virtual/set1:emp`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "emp",
srcs = ["emp.cpp"],
copts = ["-std=c++11"]
)
Binary file not shown.
+32 -44
View File
@@ -1,52 +1,40 @@
#include<iostream>
#include <iostream>
using namespace std;
class Employee {
public:
virtual void raiseSalary() { cout << 0 << endl; }
class Employee
{
public:
virtual void raiseSalary()
{
cout<<0<<endl;
}
virtual void promote() { /* common promote code */
}
};
virtual void promote()
{ /* common promote code */ }
};
class Manager : public Employee {
virtual void raiseSalary() { cout << 100 << endl; }
class Manager: public Employee {
virtual void raiseSalary()
{
cout<<100<<endl;
}
virtual void promote() { /* Manager specific promote */
}
};
class Engineer : public Employee {
virtual void raiseSalary() { cout << 200 << endl; }
virtual void promote()
{ /* Manager specific promote */ }
};
class Engineer: public Employee {
virtual void raiseSalary()
{
cout<<200<<endl;
}
virtual void promote() { /* Manager specific promote */
}
};
virtual void promote()
{ /* Manager specific promote */ }
};
// Similarly, there may be other types of employees
// We need a very simple function to increment salary of all employees
// Note that emp[] is an array of pointers and actual pointed objects can
// be any type of employees. This function should ideally be in a class
// like Organization, we have made it global to keep things simple
void globalRaiseSalary(Employee *emp[], int n)
{
for (int i = 0; i < n; i++)
emp[i]->raiseSalary(); // Polymorphic Call: Calls raiseSalary()
// according to the actual object, not
// according to the type of pointer
}
int main(){
Employee *emp[]={new Manager(),new Engineer};
globalRaiseSalary(emp,2);
return 0;
// Similarly, there may be other types of employees
// We need a very simple function to increment salary of all employees
// Note that emp[] is an array of pointers and actual pointed objects can
// be any type of employees. This function should ideally be in a class
// like Organization, we have made it global to keep things simple
void globalRaiseSalary(Employee *emp[], int n) {
for (int i = 0; i < n; i++)
emp[i]->raiseSalary(); // Polymorphic Call: Calls raiseSalary()
// according to the actual object, not
// according to the type of pointer
}
int main() {
Employee *emp[] = {new Manager(), new Engineer};
globalRaiseSalary(emp, 2);
return 0;
}
+8
View File
@@ -0,0 +1,8 @@
# please run `bazel run basic_content/virtual/set2:default_arg`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "default_arg",
srcs = ["default_arg.cpp"],
copts = ["-std=c++11"]
)
+16 -27
View File
@@ -7,33 +7,22 @@
* @date 2019-07-24
*/
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class Base
{
public:
virtual void fun ( int x = 10 )
{
cout << "Base::fun(), x = " << x << endl;
}
};
class Base {
public:
virtual void fun(int x = 10) { cout << "Base::fun(), x = " << x << endl; }
};
class Derived : public Base
{
public:
virtual void fun ( int x=20 )
{
cout << "Derived::fun(), x = " << x << endl;
}
};
int main()
{
Derived d1;
Base *bp = &d1;
bp->fun(); // 10
return 0;
}
class Derived : public Base {
public:
virtual void fun(int x = 20) { cout << "Derived::fun(), x = " << x << endl; }
};
int main() {
Derived d1;
Base *bp = &d1;
bp->fun(); // 10
return 0;
}
+57
View File
@@ -0,0 +1,57 @@
# please run `bazel run //basic_content/virtual/set3:copy_consrtuct`
# please run `bazel run //basic_content/virtual/set3:full_virde`
# please run `bazel run //basic_content/virtual/set3:inline_virtual`
# please run `bazel run //basic_content/virtual/set3:vir_con`
# please run `bazel run //basic_content/virtual/set3:vir_de`
# please run `bazel run //basic_content/virtual/set3:virtual_function`
# please run `bazel run //basic_content/virtual/set3:virtual_function1`
# please run `bazel run //basic_content/virtual/set3:virtual_inline`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "copy_consrtuct",
srcs = ["copy_consrtuct.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "full_virde",
srcs = ["full_virde.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "inline_virtual",
srcs = ["inline_virtual.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "vir_con",
srcs = ["vir_con.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "vir_de",
srcs = ["vir_de.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "virtual_function",
srcs = ["virtual_function.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "virtual_function1",
srcs = ["virtual_function1.cpp"],
copts = ["-std=c++11"]
)
cc_binary(
name = "virtual_inline",
srcs = ["virtual_inline.cpp"],
copts = ["-std=c++11"]
)
+21 -33
View File
@@ -1,41 +1,29 @@
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class Base
{
public:
class Base {
public:
};
};
class Derived : public Base {
public:
Derived() { cout << "Derived created" << endl; }
class Derived : public Base
{
public:
Derived()
{
cout << "Derived created" << endl;
}
Derived(const Derived &rhs) {
cout << "Derived created by deep copy" << endl;
}
Derived(const Derived &rhs)
{
cout << "Derived created by deep copy" << endl;
}
~Derived() { cout << "Derived destroyed" << endl; }
};
~Derived()
{
cout << "Derived destroyed" << endl;
}
};
int main() {
Derived s1;
int main()
{
Derived s1;
Derived s2 = s1; // Compiler invokes "copy constructor"
// Type of s1 and s2 are concrete to compiler
Derived s2 = s1; // Compiler invokes "copy constructor"
// Type of s1 and s2 are concrete to compiler
// How can we create Derived1 or Derived2 object
// from pointer (reference) to Base class pointing Derived object?
return 0;
}
// How can we create Derived1 or Derived2 object
// from pointer (reference) to Base class pointing Derived object?
return 0;
}
+18 -23
View File
@@ -10,30 +10,25 @@
* @version v1
* @date 2019-07-24
*/
#include<iostream>
#include <iostream>
using namespace std;
using namespace std;
class base {
public:
base()
{ cout<<"Constructing base \n"; }
virtual ~base()
{ cout<<"Destructing base \n"; }
};
class base {
public:
base() { cout << "Constructing base \n"; }
virtual ~base() { cout << "Destructing base \n"; }
};
class derived: public base {
public:
derived()
{ cout<<"Constructing derived \n"; }
~derived()
{ cout<<"Destructing derived \n"; }
};
class derived : public base {
public:
derived() { cout << "Constructing derived \n"; }
~derived() { cout << "Destructing derived \n"; }
};
int main(void)
{
derived *d = new derived();
base *b = d;
delete b;
return 0;
}
int main(void) {
derived *d = new derived();
base *b = d;
delete b;
return 0;
}
+24 -28
View File
@@ -1,35 +1,31 @@
#include <iostream>
#include <iostream>
using namespace std;
class Base
{
public:
inline virtual void who()
{
cout << "I am Base\n";
}
virtual ~Base() {}
class Base {
public:
inline virtual void who() { cout << "I am Base\n"; }
virtual ~Base() {}
};
class Derived : public Base
{
public:
inline void who() // 不写inline时隐式内联
{
cout << "I am Derived\n";
}
class Derived : public Base {
public:
inline void who() // 不写inline时隐式内联
{
cout << "I am Derived\n";
}
};
int main()
{
// 此处的虚函数 who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。
Base b;
b.who();
int main() {
// 此处的虚函数
// who(),是通过类(Base)的具体对象(b)来调用的,编译期间就能确定了,所以它可以是内联的,但最终是否内联取决于编译器。
Base b;
b.who();
// 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。
Base *ptr = new Derived();
ptr->who();
// 此处的虚函数是通过指针调用的,呈现多态性,需要在运行时期间才能确定,所以不能为内联。
Base *ptr = new Derived();
ptr->who();
// 因为Base有虚析构函数(virtual ~Base() {}),所以 delete 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。
delete ptr;
// 因为Base有虚析构函数(virtual ~Base() {}),所以 delete
// 时,会先调用派生类(Derived)析构函数,再调用基类(Base)析构函数,防止内存泄漏。
delete ptr;
return 0;
}
return 0;
}
+2 -2
View File
@@ -9,5 +9,5 @@
* @date 2019-07-24
*/
virtual static void fun() { }
static void fun() const { }
virtual static void fun() {}
static void fun() const {}
+109 -157
View File
@@ -1,206 +1,158 @@
/**
* @file vir_con.cpp
* @brief inline之外使
* @brief
* inline之外使
*
*
*
* vtable是在编译阶段就已经建立的vptr是在运行阶段实例化对象时才产生的 vptr vptr来访问vtablevptr还没产生
* vtable是在编译阶段就已经建立的vptr是在运行阶段实例化对象时才产生的
* vptr
* vptr来访问vtablevptr还没产生
*
* 使
*
* Virtual Copy Constructor
* Virtual Copy
* Constructor
*
*
*
*
*
* @author
* @version v1
* @date 2019-07-24
*/
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
//// LIBRARY SRART
class Base
{
public:
Base() { }
//// LIBRARY SRART
class Base {
public:
Base() {}
virtual // Ensures to invoke actual object destructor
~Base() { }
virtual // Ensures to invoke actual object destructor
~Base() {}
virtual void ChangeAttributes() = 0;
virtual void ChangeAttributes() = 0;
// The "Virtual Constructor"
static Base *Create(int id);
// The "Virtual Constructor"
static Base *Create(int id);
// The "Virtual Copy Constructor"
virtual Base *Clone() = 0;
};
// The "Virtual Copy Constructor"
virtual Base *Clone() = 0;
};
class Derived1 : public Base
{
public:
Derived1()
{
cout << "Derived1 created" << endl;
}
class Derived1 : public Base {
public:
Derived1() { cout << "Derived1 created" << endl; }
Derived1(const Derived1& rhs)
{
cout << "Derived1 created by deep copy" << endl;
}
Derived1(const Derived1 &rhs) {
cout << "Derived1 created by deep copy" << endl;
}
~Derived1()
{
cout << "~Derived1 destroyed" << endl;
}
~Derived1() { cout << "~Derived1 destroyed" << endl; }
void ChangeAttributes()
{
cout << "Derived1 Attributes Changed" << endl;
}
void ChangeAttributes() { cout << "Derived1 Attributes Changed" << endl; }
Base *Clone()
{
return new Derived1(*this);
}
};
Base *Clone() { return new Derived1(*this); }
};
class Derived2 : public Base
{
public:
Derived2()
{
cout << "Derived2 created" << endl;
}
class Derived2 : public Base {
public:
Derived2() { cout << "Derived2 created" << endl; }
Derived2(const Derived2& rhs)
{
cout << "Derived2 created by deep copy" << endl;
}
Derived2(const Derived2 &rhs) {
cout << "Derived2 created by deep copy" << endl;
}
~Derived2()
{
cout << "~Derived2 destroyed" << endl;
}
~Derived2() { cout << "~Derived2 destroyed" << endl; }
void ChangeAttributes()
{
cout << "Derived2 Attributes Changed" << endl;
}
void ChangeAttributes() { cout << "Derived2 Attributes Changed" << endl; }
Base *Clone()
{
return new Derived2(*this);
}
};
Base *Clone() { return new Derived2(*this); }
};
class Derived3 : public Base
{
public:
Derived3()
{
cout << "Derived3 created" << endl;
}
class Derived3 : public Base {
public:
Derived3() { cout << "Derived3 created" << endl; }
Derived3(const Derived3& rhs)
{
cout << "Derived3 created by deep copy" << endl;
}
Derived3(const Derived3 &rhs) {
cout << "Derived3 created by deep copy" << endl;
}
~Derived3()
{
cout << "~Derived3 destroyed" << endl;
}
~Derived3() { cout << "~Derived3 destroyed" << endl; }
void ChangeAttributes()
{
cout << "Derived3 Attributes Changed" << endl;
}
void ChangeAttributes() { cout << "Derived3 Attributes Changed" << endl; }
Base *Clone()
{
return new Derived3(*this);
}
};
Base *Clone() { return new Derived3(*this); }
};
// We can also declare "Create" outside Base.
// But is more relevant to limit it's scope to Base
Base *Base::Create(int id)
{
// Just expand the if-else ladder, if new Derived class is created
// User need not be recompiled to create newly added class objects
// We can also declare "Create" outside Base.
// But is more relevant to limit it's scope to Base
Base *Base::Create(int id) {
// Just expand the if-else ladder, if new Derived class is created
// User need not be recompiled to create newly added class objects
if( id == 1 )
{
return new Derived1;
}
else if( id == 2 )
{
return new Derived2;
}
else
{
return new Derived3;
}
}
//// LIBRARY END
if (id == 1) {
return new Derived1;
} else if (id == 2) {
return new Derived2;
} else {
return new Derived3;
}
}
//// LIBRARY END
//// UTILITY SRART
class User
{
public:
User() : pBase(0)
{
// Creates any object of Base heirarchey at runtime
//// UTILITY SRART
class User {
public:
User() : pBase(0) {
// Creates any object of Base heirarchey at runtime
int input;
int input;
cout << "Enter ID (1, 2 or 3): ";
cin >> input;
cout << "Enter ID (1, 2 or 3): ";
cin >> input;
while( (input != 1) && (input != 2) && (input != 3) )
{
cout << "Enter ID (1, 2 or 3 only): ";
cin >> input;
}
while ((input != 1) && (input != 2) && (input != 3)) {
cout << "Enter ID (1, 2 or 3 only): ";
cin >> input;
}
// Create objects via the "Virtual Constructor"
pBase = Base::Create(input);
}
// Create objects via the "Virtual Constructor"
pBase = Base::Create(input);
}
~User()
{
if( pBase )
{
delete pBase;
pBase = 0;
}
}
~User() {
if (pBase) {
delete pBase;
pBase = 0;
}
}
void Action()
{
// Duplicate current object
Base *pNewBase = pBase->Clone();
void Action() {
// Duplicate current object
Base *pNewBase = pBase->Clone();
// Change its attributes
pNewBase->ChangeAttributes();
// Change its attributes
pNewBase->ChangeAttributes();
// Dispose the created object
delete pNewBase;
}
// Dispose the created object
delete pNewBase;
}
private:
Base *pBase;
};
private:
Base *pBase;
};
//// UTILITY END
//// UTILITY END
//// Consumer of User (UTILITY) class
int main()
{
User *user = new User();
//// Consumer of User (UTILITY) class
int main() {
User *user = new User();
user->Action();
delete user;
}
user->Action();
delete user;
}
+20 -25
View File
@@ -10,32 +10,27 @@
* @date 2019-07-24
*/
// CPP program without virtual destructor
// causing undefined behavior
#include<iostream>
// CPP program without virtual destructor
// causing undefined behavior
#include <iostream>
using namespace std;
using namespace std;
class base {
public:
base()
{ cout<<"Constructing base \n"; }
~base()
{ cout<<"Destructing base \n"; }
};
class base {
public:
base() { cout << "Constructing base \n"; }
~base() { cout << "Destructing base \n"; }
};
class derived: public base {
public:
derived()
{ cout<<"Constructing derived \n"; }
~derived()
{ cout<<"Destructing derived \n"; }
};
class derived : public base {
public:
derived() { cout << "Constructing derived \n"; }
~derived() { cout << "Destructing derived \n"; }
};
int main(void)
{
derived *d = new derived();
base *b = d;
delete b;
return 0;
}
int main(void) {
derived *d = new derived();
base *b = d;
delete b;
return 0;
}
+19 -19
View File
@@ -2,32 +2,32 @@
* @file virtual_function.cpp
* @brief
*
* int main()Base类的友元 ptr无法访问私有函数
* public private---->
* int main()Base类的友元
* ptr无法访问私有函数 public
* private---->
* @author
* @version v1
* @date 2019-07-24
*/
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
class Derived;
class Derived;
class Base {
private:
virtual void fun() { cout << "Base Fun"; }
friend int main();
};
class Base {
private:
virtual void fun() { cout << "Base Fun"; }
friend int main();
};
class Derived: public Base {
public:
void fun() { cout << "Derived Fun"; }
};
class Derived : public Base {
public:
void fun() { cout << "Derived Fun"; }
};
int main()
{
Base *ptr = new Derived;
ptr->fun();
return 0;
int main() {
Base *ptr = new Derived;
ptr->fun();
return 0;
}
@@ -1,22 +1,21 @@
#include<iostream>
using namespace std;
#include <iostream>
using namespace std;
class Derived;
class Derived;
class Base {
public:
virtual void fun() { cout << "Base Fun"; }
// friend int main();
};
class Base {
public:
virtual void fun() { cout << "Base Fun"; }
// friend int main();
};
class Derived: public Base {
private:
void fun() { cout << "Derived Fun"; }
};
class Derived : public Base {
private:
void fun() { cout << "Derived Fun"; }
};
int main()
{
Base *ptr = new Derived;
ptr->fun();
return 0;
int main() {
Base *ptr = new Derived;
ptr->fun();
return 0;
}
+23 -32
View File
@@ -1,6 +1,6 @@
/**
* @file virtual_inline.cpp
* @brief
* @brief
*
*
* @author
@@ -8,37 +8,28 @@
* @date 2019-07-24
*/
#include <iostream>
using namespace std;
class Base
{
public:
virtual void who()
{
cout << "I am Base\n";
}
};
class Derived: public Base
{
public:
void who()
{
cout << "I am Derived\n";
}
};
#include <iostream>
using namespace std;
class Base {
public:
virtual void who() { cout << "I am Base\n"; }
};
class Derived : public Base {
public:
void who() { cout << "I am Derived\n"; }
};
int main()
{
// note here virtual function who() is called through
// object of the class (it will be resolved at compile
// time) so it can be inlined.
Base b;
b.who();
int main() {
// note here virtual function who() is called through
// object of the class (it will be resolved at compile
// time) so it can be inlined.
Base b;
b.who();
// Here virtual function is called through pointer,
// so it cannot be inlined
Base *ptr = new Derived();
ptr->who();
// Here virtual function is called through pointer,
// so it cannot be inlined
Base *ptr = new Derived();
ptr->who();
return 0;
}
return 0;
}
+8
View File
@@ -0,0 +1,8 @@
# please run `bazel run basic_content/virtual/set4:rtti`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "rtti",
srcs = ["rtti.cpp"],
copts = ["-std=c++11"]
)
Binary file not shown.
+29 -25
View File
@@ -1,34 +1,38 @@
/**
* @file rtti.cpp
* @brief 使Java的instanceofC#asis运算符类似C++dynamic_cast函数用于动态转型C风格的强制类型转换和C++ reinterpret_castdynamic_cast提供了类型安全检查(Capability Query)dynamic_cast
* @brief
* 使Java的instanceofC#asis运算符类似C++dynamic_cast函数用于动态转型C风格的强制类型转换和C++
* reinterpret_castdynamic_cast提供了类型安全检查(Capability
* Query)dynamic_cast
* @author
* @version v1
* @date 2019-07-24
*/
// CPP program to illustrate
// // Run Time Type Identification
#include<iostream>
#include<typeinfo>
using namespace std;
class B { virtual void fun() {} };
class D: public B { };
// CPP program to illustrate
// Run Time Type Identification
#include <iostream>
#include <typeinfo>
using namespace std;
class B {
virtual void fun() {}
};
class D : public B {};
int main()
{
B *b = new D; // 向上转型
B &obj = *b;
D *d = dynamic_cast<D*>(b); // 向下转型
if(d != NULL)
cout << "works"<<endl;
else
cout << "cannot cast B* to D*";
try {
D& dobj = dynamic_cast<D&>(obj);
cout << "works"<<endl;
} catch (bad_cast bc) { // ERROR
cout<<bc.what()<<endl;
}
return 0;
int main() {
B *b = new D; // 向上转型
B &obj = *b;
D *d = dynamic_cast<D *>(b); // 向下转型
if (d != NULL)
cout << "works" << endl;
else
cout << "cannot cast B* to D*";
try {
D &dobj = dynamic_cast<D &>(obj);
cout << "works" << endl;
} catch (bad_cast bc) { // ERROR
cout << bc.what() << endl;
}
return 0;
}
+14 -24
View File
@@ -1,35 +1,25 @@
// 在使用时需要注意:被转换对象obj的类型T1必须是多态类型,即T1必须公有继承自其它类,或者T1拥有虚函数(继承或自定义)。若T1为非多态类型,使用dynamic_cast会报编译错误。
// A为非多态类型
// A为非多态类型
class A{
class A {};
// B为多态类型
class B {
public:
virtual ~B() {}
};
//B为多态类型
// D为非多态类型
class B{
class D : public A {};
public: virtual ~B(){}
// E为非多态类型
};
//D为非多态类型
class D: public A{
};
//E为非多态类型
class E : private A{
};
//F为多态类型
class F : private B{
}
class E : private A {};
// F为多态类型
class F : private B {}