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,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。
*/