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

View File

@@ -0,0 +1,25 @@
/* 重载()的时钟.cpp */
#include <iostream>
using namespace std;
class Time {
private:
int hh, mm, ss;
public:
Time(int h = 0, int m = 0, int s = 0) : hh(h), mm(m), ss(s) {}
void operator()(int h, int m, int s) {
hh = h;
mm = m;
ss = s;
}
void ShowTime() { cout << hh << ":" << mm << ":" << ss << endl; }
};
int main() {
Time t1(12, 10, 11);
t1.ShowTime();
t1.operator()(23, 20, 34);
t1.ShowTime();
t1(10, 10, 10);
t1.ShowTime();
}

View File

@@ -0,0 +1,52 @@
/* 重载++的时钟.cpp */
/*
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算加时1秒但要使计时过程能够自动进位。
*/
#include <iostream>
using namespace std;
class Time {
public:
Time(int h = 0, int m = 0, int s = 0) {
hour = h;
minute = m;
second = s;
}
Time operator++();
Time operator++(int);
void showTime() {
cout << "当前时间为:" << hour << ":" << minute << ":" << second << endl;
}
private:
int hour, minute, second;
};
Time Time::operator++(int n) {
Time tmp = *this;
++(*this);
return tmp;
}
Time Time::operator++() {
++second;
if (second == 60) {
second = 0;
++minute;
if (minute == 60) {
minute = 0;
hour++;
if (hour == 24) {
hour = 0;
}
}
}
return *this;
}
int main(int argc, char const *argv[]) {
Time t(23, 59, 59);
++t;
t.showTime();
(t++).showTime();
t.showTime();
return 0;
}

View File

@@ -0,0 +1,13 @@
# please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_func`
# please run `bazel run //practical_exercises/10_day_practice/day4/const:obj_ref`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "obj_func",
srcs = ["obj_func.cpp"],
)
cc_binary(
name = "obj_ref",
srcs = ["obj_ref.cpp"],
)

View File

@@ -0,0 +1,40 @@
#include <iostream>
using namespace std;
class R {
public:
R(int r1, int r2) {
R1 = r1;
R2 = r2;
}
// const区分成员重载函数
void print();
void print() const;
private:
int R1, R2;
};
/*
常成员函数说明格式:类型说明符 函数名参数表const;
这里const是函数类型的一个组成部分因此在实现部分也要带const关键字。
const关键字可以被用于参与对重载函数的区分
通过常对象只能调用它的常成员函数
*/
void R::print() {
cout << "普通调用" << endl;
cout << R1 << ":" << R2 << endl;
}
//实例化也需要带上
void R::print() const {
cout << "常对象调用" << endl;
cout << R1 << ";" << R2 << endl;
}
int main() {
R a(5, 4);
a.print(); //调用void print()
//通过常对象只能调用它的常成员函数
const R b(20, 52);
b.print(); //调用void print() const
return 0;
}

View File

@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
void display(const double &r);
class A {
public:
A(int i, int j) {
x = i;
y = j;
}
private:
int x, y;
};
int main() {
double d(9.5);
display(d);
A const a(3, 4); // a是常对象不能被更新
return 0;
}
void display(const double &r)
//常引用做形参,在函数中不能更新 r所引用的对象。
{
cout << r << endl;
}

View File

@@ -1,24 +0,0 @@
#include<iostream>
using namespace std;
void display(const double& r);
class A
{
public:
A(int i,int j) {x=i; y=j;}
private:
int x,y;
};
int main()
{ double d(9.5);
display(d);
A const a(3,4); //a是常对象不能被更新
system("pause");
return 0;
}
void display(const double& r)
//常引用做形参,在函数中不能更新 r所引用的对象。
{ cout<<r<<endl; }

View File

@@ -1,41 +0,0 @@
#include<iostream>
using namespace std;
class R
{ public:
R(int r1, int r2){R1=r1;R2=r2;}
//const区分成员重载函数
void print();
void print() const;
private:
int R1,R2;
};
/*
常成员函数说明格式:类型说明符 函数名参数表const;
这里const是函数类型的一个组成部分因此在实现部分也要带const关键字。
const关键字可以被用于参与对重载函数的区分
通过常对象只能调用它的常成员函数
*/
void R::print()
{
cout<<"普通调用"<<endl;
cout<<R1<<":"<<R2<<endl;
}
//实例化也需要带上
void R::print() const
{
cout<<"常对象调用"<<endl;
cout<<R1<<";"<<R2<<endl;
}
int main()
{
R a(5,4);
a.print(); //调用void print()
//通过常对象只能调用它的常成员函数
const R b(20,52);
b.print(); //调用void print() const
system("pause");
return 0;
}

View File

@@ -0,0 +1,16 @@
# please run `bazel run //practical_exercises/10_day_practice/day4/copy_ctor:clock`
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "clock",
hdrs = ["clock.h"],
)
cc_binary(
name = "main",
srcs = ["clock.cpp"],
deps = [
":clock",
],
)

View File

@@ -0,0 +1,38 @@
#include "clock.h"
#include <iostream>
using namespace std;
Clock::Clock(int NewH, int NewM, int NewS) {
this->Hour = NewH;
this->Minute = NewM;
this->Second = NewS;
}
Clock::Clock(Clock &c) {
this->Hour = c.Hour;
this->Minute = c.Minute;
this->Second = c.Second;
}
void Clock::SetTime(int NewH, int NewM, int NewS) {
//加不加this指针都一样
this->Hour = NewH;
this->Minute = NewM;
this->Second = NewS;
}
void Clock::ShowTime() {
cout << this->Hour << endl;
cout << this->Minute << endl;
cout << this->Second << endl;
}
//析构函数
Clock::~Clock() {}
int main(int argc, char const *argv[]) {
Clock c(0, 0, 0);
c.SetTime(10, 20, 30);
c.ShowTime();
//拷贝构造函数调用
Clock c1(c);
c1.ShowTime();
c1.SetTime(90, 98, 99);
c1.ShowTime();
return 0;
}

View File

@@ -0,0 +1,26 @@
#ifndef CLOCK
#define CLOCK
class Clock {
public:
Clock(int NewH, int NewM, int NewS);
Clock(
Clock &
c); //拷贝构造函数,如果不加,编译器会自动生成一个拷贝构造函数,因此加不加都可以直接使用。
void SetTime(int NewH, int NewM, int NewS);
void ShowTime();
~Clock(); //析构函数,编译器会自动产生一个默认的析构函数。
private:
int Hour, Minute, Second;
};
#endif
/*
#ifndef 标识符
程序段1
#else
程序段2
#endif
如果“标识符”未被定义过则编译程序段1否则编译程序段2。
*/

View File

@@ -0,0 +1,13 @@
# please run `bazel run //practical_exercises/10_day_practice/day4/friend:class`
# please run `bazel run //practical_exercises/10_day_practice/day4/friend:func`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "class",
srcs = ["class.cpp"],
)
cc_binary(
name = "func",
srcs = ["func.cpp"],
)

View File

@@ -1,4 +1,4 @@
#include<iostream>
#include <iostream>
using namespace std;
/*
访
@@ -9,39 +9,33 @@ using namespace std;
B类是A类的友元B类的成员函数就可以访问A类的私有和保护数据
A类的成员函数却不能访问B类的私有
*/
class A{
class A {
friend class B;
public:
void Display(){
cout<<x<<endl;
}
private:
int x;
};
class B
{ public:
void Set(int i);
void Display();
private:
A a;
};
void B::Set(int i)
{
a.x=i;
}
void B::Display()
{
a.Display();
}
int main(int argc, char const *argv[])
{
B b;
b.Set(10);
b.Display();
public:
void Display() { cout << x << endl; }
system("pause");
return 0;
private:
int x;
};
class B {
public:
void Set(int i);
void Display();
private:
A a;
};
void B::Set(int i) { a.x = i; }
void B::Display() { a.Display(); }
int main(int argc, char const *argv[]) {
B b;
b.Set(10);
b.Display();
return 0;
}
/*

View File

@@ -0,0 +1,27 @@
//使用友元函数计算两点间距离
#include <cmath>
#include <iostream>
using namespace std;
class Point {
public:
Point(int x = 0, int y = 0) : X(x), Y(y) {}
int GetX() { return X; }
int GetY() { return Y; }
friend float Distance(Point &a, Point &b);
private:
int X, Y; //私有数据成员
};
//通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
float Distance(Point &a, Point &b) {
double dx = a.X - b.X;
double dy = a.Y - b.Y;
return sqrt(dx * dx + dy * dy);
}
int main() {
Point p1(3.0, 5.0), p2(4.0, 6.0);
cout << "两点距离为:" << Distance(p1, p2) << endl;
return 0;
}

View File

@@ -1,31 +0,0 @@
//使用友元函数计算两点间距离
#include <iostream>
#include <cmath>
using namespace std;
class Point{
public:
Point(int x=0,int y=0):X(x),Y(y){}
int GetX(){
return X;
}
int GetY(){
return Y;
}
friend float Distance(Point &a,Point &b);
private:
int X,Y;//私有数据成员
};
//通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
float Distance(Point &a, Point &b){
double dx = a.X-b.X;
double dy = a.Y-b.Y;
return sqrt(dx*dx+dy*dy);
}
int main()
{
Point p1(3.0,5.0),p2(4.0,6.0);
cout<<"两点距离为:"<<Distance(p1,p2)<<endl;
system("pause");
return 0;
}

View File

@@ -1,47 +0,0 @@
#include<iostream>
#include"clock.h"
using namespace std;
Clock::Clock(int NewH,int NewM,int NewS)
{
this->Hour=NewH;
this->Minute=NewM;
this->Second=NewS;
}
Clock::Clock(Clock &c)
{
this->Hour=c.Hour;
this->Minute=c.Minute;
this->Second=c.Second;
}
void Clock::SetTime(int NewH,int NewM,int NewS)
{
//加不加this指针都一样
this->Hour=NewH;
this->Minute=NewM;
this->Second=NewS;
}
void Clock::ShowTime()
{
cout<<this->Hour<<endl;
cout<<this->Minute<<endl;
cout<<this->Second<<endl;
}
//析构函数
Clock::~Clock()
{
}
int main(int argc, char const *argv[])
{
Clock c(0,0,0);
c.SetTime(10,20,30);
c.ShowTime();
//拷贝构造函数调用
Clock c1(c);
c1.ShowTime();
c1.SetTime(90,98,99);
c1.ShowTime();
system("pause");
return 0;
}

View File

@@ -1,26 +0,0 @@
#ifndef CLOCK
# define CLOCK
class Clock
{
public:
Clock(int NewH,int NewM,int NewS);
Clock(Clock &c);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECBAAF>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼Ӳ<CBBC><D3B2>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>á<EFBFBD>
void SetTime(int NewH,int NewM,int NewS);
void ShowTime();
~Clock();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ĭ<EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private:
int Hour,Minute,Second;
};
#endif
/*
#ifndef <20><>ʶ<EFBFBD><CAB6>
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
#else
<20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2
#endif
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>
*/

View File

@@ -1,25 +0,0 @@
#include <iostream>
using namespace std;
class Time{
private:
int hh,mm,ss;
public:
Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){}
void operator()(int h,int m,int s) {
hh=h;
mm=m;
ss=s;
}
void ShowTime(){
cout<<hh<<":"<<mm<<":"<<ss<<endl;
}
};
int main(){
Time t1(12,10,11);
t1.ShowTime();
t1.operator()(23,20,34);
t1.ShowTime();
t1(10,10,10);
t1.ShowTime();
system("pause");
}

View File

@@ -1,53 +0,0 @@
/*
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算加时1秒但要使计时过程能够自动进位。
*/
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0){
hour = h;
minute = m;
second = s;
}
Time operator++();
Time operator++(int);
void showTime(){
cout<<"当前时间为:"<<hour<<":"<<minute<<":"<<second<<endl;
}
private:
int hour,minute,second;
};
Time Time::operator++(int n){
Time tmp=*this;
++(*this);
return tmp;
}
Time Time::operator++(){
++second;
if(second==60){
second=0;
++minute;
if(minute==60){
minute=0;
hour++;
if(hour==24){
hour=0;
}
}
}
return *this;
}
int main(int argc, char const *argv[])
{
Time t(23,59,59);
++t;
t.showTime();
(t++).showTime();
t.showTime();
system("pause");
return 0;
}