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,38 @@
# please run `bazel run //practical_exercises/10_day_practice/day3:static_member1`
# please run `bazel run //practical_exercises/10_day_practice/day3:swap`
# please run `bazel run //practical_exercises/10_day_practice/day3:pratice`
# please run `bazel run //practical_exercises/10_day_practice/day3:static_member2`
# please run `bazel run //practical_exercises/10_day_practice/day3:predeclare`
# please run `bazel run //practical_exercises/10_day_practice/day3:inline`
# please run `bazel run //practical_exercises/10_day_practice/day3:static_data`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "static_member1",
srcs = ["static_member1.cpp"],
)
cc_binary(
name = "swap",
srcs = ["swap.cpp"],
)
cc_binary(
name = "pratice",
srcs = ["pratice.cpp"],
)
cc_binary(
name = "static_member2",
srcs = ["static_member2.cpp"],
)
cc_binary(
name = "predeclare",
srcs = ["predeclare.cpp"],
)
cc_binary(
name = "inline",
srcs = ["inline.cpp"],
)
cc_binary(
name = "static_data",
srcs = ["static_data.cpp"],
)

View File

@@ -0,0 +1,15 @@
/* 内联函数.cpp */
#include <iostream>
using namespace std;
//函数声明
inline double CalArea(double radius);
int main(int argc, char const *argv[]) {
double r(3.0);
double area;
area = CalArea(r);
cout << area << endl;
return 0;
}
//加关键字inline
inline double CalArea(double radius) { return 3.14 * radius * radius; }

View File

@@ -0,0 +1,52 @@
/* 函数综合练习题.cpp */
/*
一圆型游泳池如图所示现在需在其周围建一圆型过道并在其四周围上栅栏。栅栏价格为35元/米过道造价为20元/平方米。
过道宽度为3米游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
图形描述:大圆嵌套小圆:
小圆在大圆中间,小圆为游泳池,大圆与小圆间隔为过道。
*/
#include <iostream>
using namespace std;
const float PI = 3.14159;
const float FencePrice = 35;
const float ConcretePrice = 20;
class Circle {
private:
float radius;
public:
Circle(float r);
float Circumference() const;
float Area() const;
};
Circle::Circle(float r) { radius = r; }
// 计算圆的周长
float Circle::Circumference() const { return 2 * PI * radius; }
// 计算圆的面积
float Circle::Area() const { return PI * radius * radius; }
int main(int argc, char const *argv[]) {
float radius;
float FenceCost, ConcreteCost;
// 提示用户输入半径
cout << "Enter the radius of the pool: ";
cin >> radius;
// 声明 Circle 对象
Circle Pool(radius);
Circle PoolRim(radius + 3);
// 计算栅栏造价并输出
FenceCost = PoolRim.Circumference() * FencePrice;
cout << "Fencing Cost is ¥" << FenceCost << endl;
// 计算过道造价并输出
ConcreteCost = (PoolRim.Area() - Pool.Area()) * ConcretePrice;
cout << "Concrete Cost is ¥" << ConcreteCost << endl;
return 0;
}

View File

@@ -1,3 +1,4 @@
/* 类前向声明.cpp */
/*
使
使
@@ -5,35 +6,35 @@
*/
//第一种
#include<iostream>
class Fred; //前向引用声明
#include <iostream>
class Fred; //前向引用声明
class Barney {
Fred x; //错误类Fred的声明尚不完善
Fred x; //错误类Fred的声明尚不完善
};
class Fred {
Barney y;
Barney y;
};
//第二种
class Fred; //前向引用声明
class Fred; //前向引用声明
class Barney {
public:
void method()
{
x->yabbaDabbaDo(); //错误Fred类的对象在定义之前被使用
}
private:
Fred* x; //正确经过前向引用声明可以声明Fred类的对象指针
};
public:
void method() {
x->yabbaDabbaDo(); //错误Fred类的对象在定义之前被使用
}
private:
Fred *x; //正确经过前向引用声明可以声明Fred类的对象指针
};
class Fred {
public:
void yabbaDabbaDo();
private:
Barney* y;
};
public:
void yabbaDabbaDo();
private:
Barney *y;
};
/*
使使

View File

@@ -0,0 +1,44 @@
/* 静态数据成员.cpp */
/*
学习知识:
静态数据成员
用关键字static声明
该类的所有对象维护该成员的同一个拷贝
必须在类外定义和初始化,用(::)来指明所属的类。
*/
#include <iostream>
using namespace std;
class Point {
public:
Point(int xx = 0, int yy = 0) {
X = xx;
Y = yy;
countP++;
}
Point(Point &p);
int GetX() { return X; }
int GetY() { return Y; }
void GetC() { cout << " Object id=" << countP << endl; }
private:
int X, Y;
//静态数据成员,必须在外部定义和初始化,内部不能直接初始化!
static int countP;
};
Point::Point(Point &p) {
X = p.X;
Y = p.Y;
countP++;
}
//必须在类外定义和初始化,用(::)来指明所属的类。
int Point::countP = 0;
int main() {
Point A(4, 5);
cout << "Point A," << A.GetX() << "," << A.GetY();
A.GetC();
Point B(A);
cout << "Point B," << B.GetX() << "," << B.GetY();
B.GetC();
return 0;
}

View File

@@ -0,0 +1,27 @@
/* 静态成员函数1.cpp */
/*
知识点:
静态成员函数
类外代码可以使用类名和作用域操作符来调用静态成员函数。
静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
*/
#include <iostream>
using namespace std;
class Application {
public:
static void f();
static void g();
private:
static int global;
};
int Application::global = 0;
void Application::f() { global = 5; }
void Application::g() { cout << global << endl; }
int main() {
Application::f();
Application::g();
return 0;
}

View File

@@ -0,0 +1,23 @@
/* 静态成员函数2.cpp */
#include <iostream>
using namespace std;
class A {
public:
static void f(A a);
private:
int x;
};
void A::f(A a) {
//静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
// cout<<x; //对x的引用是错误的
cout << a.x; //正确
}
int main(int argc, char const *argv[]) {
A a;
a.f(A());
return 0;
}

View File

@@ -0,0 +1,19 @@
/* 两数交换.cpp */
#include <iostream>
using namespace std;
void swap(int &a, int &b);
int main(int argc, char const *argv[]) {
int x1(5);
int x2(7);
swap(x1, x2);
cout << x1 << " " << x2 << endl;
return 0;
}
void swap(int &a, int &b) {
int t;
t = a;
a = b;
b = t;
}

View File

@@ -1,21 +0,0 @@
#include<iostream>
using namespace std;
void swap(int & a, int & b);
int main(int argc, char const *argv[])
{
int x1(5);
int x2(7);
swap(x1,x2);
cout<<x1<<" "<<x2<<endl;
system("pause");
return 0;
}
void swap(int & a, int & b)
{
int t;
t = a;
a = b;
b = t;
}

View File

@@ -1,18 +0,0 @@
#include<iostream>
using namespace std;
//函数声明
inline double CalArea(double radius);
int main(int argc, char const *argv[])
{
double r(3.0);
double area;
area = CalArea(r);
cout<<area<<endl;
system("pause");
return 0;
}
//加关键字inline
inline double CalArea(double radius)
{
return 3.14*radius*radius;
}

View File

@@ -1,61 +0,0 @@
/*
一圆型游泳池如图所示现在需在其周围建一圆型过道并在其四周围上栅栏。栅栏价格为35元/米过道造价为20元/平方米。
过道宽度为3米游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
图形描述:大圆嵌套小圆:
小圆在大圆中间,小圆为游泳池,大圆与小圆间隔为过道。
*/
#include<iostream>
using namespace std;
const float PI=3.14159;
const float FencePrice=35;
const float ConcretePrice=20;
class Circle
{
private:
float radius;
public:
Circle(float r);
float Circumference() const;
float Area() const;
};
Circle::Circle(float r)
{
radius=r;
}
// 计算圆的周长
float Circle::Circumference() const
{
return 2 * PI * radius;
}
// 计算圆的面积
float Circle::Area() const
{
return PI * radius * radius;
}
int main(int argc, char const *argv[])
{
float radius;
float FenceCost, ConcreteCost;
// 提示用户输入半径
cout<<"Enter the radius of the pool: ";
cin>>radius;
// 声明 Circle 对象
Circle Pool(radius);
Circle PoolRim(radius + 3);
// 计算栅栏造价并输出
FenceCost = PoolRim.Circumference() * FencePrice;
cout << "Fencing Cost is ¥" << FenceCost << endl;
// 计算过道造价并输出
ConcreteCost = (PoolRim.Area() - Pool.Area())*ConcretePrice;
cout << "Concrete Cost is ¥" << ConcreteCost << endl;
system("pause");
return 0;
}

View File

@@ -1,30 +0,0 @@
/*
知识点:
静态成员函数
类外代码可以使用类名和作用域操作符来调用静态成员函数。
静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
*/
#include<iostream>
using namespace std;
class Application
{
public:
static void f();
static void g();
private:
static int global;
};
int Application::global=0;
void Application::f()
{ global=5;}
void Application::g()
{ cout<<global<<endl;}
int main()
{
Application::f();
Application::g();
system("pause");
return 0;
}

View File

@@ -1,24 +0,0 @@
#include<iostream>
using namespace std;
class A
{
public:
static void f(A a);
private:
int x;
};
void A::f(A a)
{
//静态成员函数只能引用属于该类的静态数据成员或静态成员函数。
// cout<<x; //对x的引用是错误的
cout<<a.x; //正确
}
int main(int argc, char const *argv[])
{
A a;
a.f(A());
system("pause");
return 0;
}

View File

@@ -1,39 +0,0 @@
/*
学习知识:
静态数据成员
用关键字static声明
该类的所有对象维护该成员的同一个拷贝
必须在类外定义和初始化,用(::)来指明所属的类。
*/
#include <iostream>
using namespace std;
class Point
{
public:
Point(int xx=0, int yy=0) {X=xx; Y=yy; countP++; }
Point(Point &p);
int GetX() {return X;}
int GetY() {return Y;}
void GetC() {cout<<" Object id="<<countP<<endl;}
private:
int X,Y;
//静态数据成员,必须在外部定义和初始化,内部不能直接初始化!
static int countP;
};
Point::Point(Point &p)
{ X=p.X;
Y=p.Y;
countP++;
}
//必须在类外定义和初始化,用(::)来指明所属的类。
int Point::countP=0;
int main()
{ Point A(4,5);
cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
A.GetC();
Point B(A);
cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
B.GetC();
system("pause");
return 0;
}