Compare commits

...

3 Commits

31 changed files with 934 additions and 0 deletions

View File

@ -3,3 +3,5 @@
# 千锋 C++阶段
#### day1: C++简介, ::作用域, namespace 命名空间, using 声明, using 编译指令, 类型转换, struct 类型加强, bool 关键字, 三目运算符增强, const 增强
#### day2: const 和#define 的区别, 引用(reference) 【重要】, 内联函数, 函数的默认值参数, 函数的占位参数, 函数重载和 extern "c", 类与对象的概念, 面向对象程序设计案例

View File

@ -36,5 +36,9 @@ int main()
p = p->next;
}
free(fn1);
free(fn2);
free(fn3);
free(head);
return 0;
}

78
day2/ce1.cpp Normal file
View File

@ -0,0 +1,78 @@
// Classroom exercises: 课堂练习
// ce
// 类的定义和使用
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
public:
void init(const char *iname, int iage);
void setName(const char sname[]);
void setAge(int sage);
const char *getname();
const int getAge();
const void show();
private:
char name[32];
int age;
};
void Person::init(const char *iname, int iage)
{
strcpy(this->name, iname);
if (iage > 0 && iage < 100)
this->age = iage;
else
{
this->age = 0;
cout << "年龄超出范围,拒绝赋值!" << endl;
}
}
void Person::setName(const char sname[])
{
strcpy(this->name, sname);
}
void Person::setAge(int sage)
{
if (sage > 0 && sage < 100)
this->age = sage;
else
{
cout << "年龄超出范围,拒绝赋值!" << endl;
}
}
const char *Person::getname()
{
return this->name;
}
const int Person::getAge()
{
return this->age;
}
const void Person::show()
{
cout << "name = " << this->name << ", age = " << this->age << endl;
}
int main()
{
Person p1;
p1.setAge(20);
p1.setName("flykhan");
p1.show();
Person p2;
p2.init("hello", 0);
p2.show();
return 0;
}

30
day2/d1.cpp Normal file
View File

@ -0,0 +1,30 @@
// 引用的使用
// 引用的本质是指针常量,所以引用的空间大小和指针相同,都是 8 个字节
// 引用的本质是指针常量,所以引用的指向不能改变,但是指向的值可以改变
// 引用的本质是指针常量,所以引用在定义时必须初始化,且不能再引用其他变量
// int a = 10;
// int &aa = a; // int * const aa = &a;
#include <iostream>
using namespace std;
int main()
{
// 定义引用时,必须给初始化值,另外,定义之后引用不能再引用其他变量
int a = 10;
int &b = a; // 对 a 的引用
b += 20; // 相当于 a += 20
cout << "a: " << a << " b: " << b << endl;
int &c = a;
cout << "a: " << a << " b: " << b << " c: " << c << endl;
int &d = b;
cout << "a: " << a << " b: " << b << " c: " << c << " d: " << d << endl;
int x = 100;
c = x;
cout << "c: " << c << endl;
return 0;
}

30
day2/d10.cpp Normal file
View File

@ -0,0 +1,30 @@
#include <iostream>
using namespace std;
void f(int a, int)
{
cout << "f(int, int )" << endl;
}
void f(double, int)
{
cout << "f(double, int)" << endl;
}
void f(int a)
{
cout << "f(int)" << endl;
}
void f(double a)
{
cout << "f(double)" << endl;
}
int main()
{
f(1);
f(1.0, 2);
return 0;
}

6
day2/d11/area.c Normal file
View File

@ -0,0 +1,6 @@
#include "area.h"
double S(double r)
{
return r * r * 3.1415926;
}

15
day2/d11/area.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __AREA_H__
#define __AREA_H__
// 条件编译
#if __cplusplus
extern "C"
{
#endif
double S(double r);
#if __cplusplus
}
#endif
#endif

12
day2/d11/main.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <iostream>
#include "area.h"
using namespace std;
int main()
{
double r = 2.5;
double s = S(r);
cout << "S(2.5) = " << s << endl;
return 0;
}

26
day2/d12.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
using namespace std;
// 增强的结构体c 中结构体中只有变量)
struct Person
{
int pid;
int age;
void show() // C++ 的结构体中可以加函数
{
// 可以访问同一结构体内的成员变量
cout << "pid = " << pid << ", age = " << age << endl;
}
};
int main()
{
Person p1 = {1001, 21};
p1.show();
Person p2 = {1002, 19};
p2.show();
return 0;
}

44
day2/d13.cpp Normal file
View File

@ -0,0 +1,44 @@
// 类
#include <iostream>
#include <cstring>
using namespace std;
namespace SA
{
class Student
{
public:
int sid = 14; // 默认值设定,需要 c++11 标准以上
char name[32] = "hello";
private:
float score = 90;
public:
void showScore(); // 声明类方法
void addScore(float n) { this->score = n; }
};
// 实现类中的方法(函数)
void Student::showScore()
{
cout << "sid = " << this->sid << ", name = " << this->name << ", score = " << this->score << endl;
}
};
using namespace SA;
int main()
{
Student s1;
strcpy(s1.name, "flykhan");
s1.sid = 1001;
s1.addScore(100);
s1.showScore();
Student s2;
s2.showScore();
return 0;
}

81
day2/d14.cpp Normal file
View File

@ -0,0 +1,81 @@
// 立方体类与应用
#include <iostream>
using namespace std;
class Cube
{
public:
void init(int l, int w, int h)
{
this->a = l;
this->b = w;
this->c = h;
}
int S(); // 面积
int V(); // 体积
void show();
bool isEqual(Cube &tmp);
private:
int a; // 长
int b; // 宽
int c; // 高
};
int Cube::S()
{
return 2 * (a * b + a * c + b * c);
}
int Cube::V()
{
return a * b * c;
}
void Cube::show()
{
cout << "S = " << S() << ", V = " << V() << endl;
}
bool Cube::isEqual(Cube &tmp)
{
// if (this->a == tmp.a && this->b == tmp.b && this->c == tmp.c)
if (this->S() == tmp.S() && this->V() == tmp.V())
return true;
else
return false;
}
bool equalCube(Cube &c1, Cube &c2)
{
if (c1.S() == c2.S() && c1.V() == c2.V())
return true;
return false;
}
const char *resBool(bool b) // bool 数字值转换人类可读
{
if (b != 0)
return "true";
return "false";
}
int main()
{
Cube c1, c2, c3;
c1.init(2, 3, 4);
cout << "c1: " << endl;
c1.show();
c2.init(3, 4, 5);
cout << "c2: " << endl;
c2.show();
c3.init(4, 2, 3);
cout << "Cube::isEqual(c2) = " << resBool(c1.isEqual(c2)) << endl;
cout << "equalCube(c1, c2) = " << resBool(equalCube(c1, c3)) << endl;
return 0;
}

83
day2/d15.cpp Normal file
View File

@ -0,0 +1,83 @@
// 点和圆的关系
#include <iostream>
#include <cmath>
using namespace std;
enum Pos_e
{
onCircle = 4, // 点在圆上
inCircle, // 点在圆内
outCircle // 点在圆外
};
class Point
{
public:
int x, y;
};
class AdvCircle
{
public:
int init(int x0, int y0, int r)
{
this->r = r;
o.x = x0;
o.y = y0;
}
Pos_e at(Point &point);
private:
int r;
Point o; // 圆心点
};
Pos_e AdvCircle::at(Point &point)
{
int distence = pow((point.x - this->o.x), 2) + pow((point.y - this->o.y), 2);
int rr = r * r;
if (distence == rr)
{
return onCircle;
}
else if (distence < rr)
{
return inCircle;
}
else
return outCircle;
}
string posToString(Pos_e pos)
{
switch (pos)
{
case onCircle:
return "在圆上";
break;
case inCircle:
return "在圆内";
break;
case outCircle:
return "在圆外";
break;
default:
break;
}
}
int main()
{
AdvCircle c1;
c1.init(0, 0, 2);
Point p1;
p1.x = 1;
p1.y = 1;
cout << "c1.at(p1) " << posToString(c1.at(p1)) << endl;
return 0;
}

23
day2/d2.cpp Normal file
View File

@ -0,0 +1,23 @@
// 引用相当于变量的别名
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
// 给 arr 设置一个别名(引用)
// 先定义 5 个元素数组类型的别名
typedef int ArrRef[5];
ArrRef &a = arr;
// 通过别名操作数组
for (int i = 0; i < 5; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}

21
day2/d2_2.cpp Normal file
View File

@ -0,0 +1,21 @@
// 数组引用的方法二
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
// 直接定义 5 个元素数组类型的引用
int(&a)[5] = arr;
// 通过别名操作数组
for (int i = 0; i < 5; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}

23
day2/d3.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <iostream>
using namespace std;
// 引用传递: 会改变实参的值
// 值传递: 不会改变实参的值
void cumsum(int &total, int n) // total 是引用传递n 是值传递
{
total += n;
}
int main()
{
int total = 0;
int n = 0;
while (cin >> n)
{
cumsum(total, n);
cout << "total = " << total << endl;
if (total > 100)
break;
}
}

28
day2/d4.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
#define N 10 // 定义常量
using namespace std;
typedef int TenArr[N];
TenArr &new_arr()
{
static int m[N] = {0}; // 全局静态区存储,程序结束后才释放
for (int i = 0; i < N; i++)
{
m[i] = i;
}
return m;
}
int main()
{
TenArr &p = new_arr();
int i = 0;
while (i < N)
{
cout << "p[" << i << "] = " << p[i++] << endl;
}
return 0;
}

57
day2/d5.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#define N 7
using namespace std;
void sort(int (&arr)[N])
{
for (int i = 0; i <= N - 1; i++)
{
for (int j = 0; j <= N - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
arr[j] ^= arr[j + 1];
arr[j + 1] ^= arr[j];
arr[j] ^= arr[j + 1];
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
}
}
}
}
void sort(int arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int nums[] = {1, 5, 2, 0, 9, 10, 7};
int size = sizeof(nums) / sizeof(nums[0]);
sort(nums);
// sort(nums, size);
for (int i = 0; i < size; i++)
{
cout << nums[i] << "\t";
}
cout << endl;
return 0;
}

29
day2/d6.cpp Normal file
View File

@ -0,0 +1,29 @@
// 指针的引用
// 为指针取个别名,相当于'指针的指针'
#include <iostream>
#include <cstdlib>
using namespace std;
struct STU
{
int sid;
float score;
};
void set_score(STU *&q, int sid, float score)
{
q->sid = sid;
q->score = score;
}
int main()
{
STU *p = (STU *)malloc(sizeof(STU));
set_score(p, 1, 99);
cout << "sid = " << p->sid << ", score = " << p->score << endl;
return 0;
}

30
day2/d6_2.cpp Normal file
View File

@ -0,0 +1,30 @@
// 指针的引用
// 为指针取个别名,相当于'指针的指针'
#include <iostream>
#include <cstdlib>
using namespace std;
struct STU
{
int sid;
float score;
};
// 指针的指针
void set_score(STU **q, int sid, float score)
{
(*q)->sid = sid;
(*q)->score = score;
}
int main()
{
STU *p = (STU *)malloc(sizeof(STU));
set_score(&p, 2, 98);
cout << "sid = " << p->sid << ", score = " << p->score << endl;
return 0;
}

28
day2/d7.cpp Normal file
View File

@ -0,0 +1,28 @@
// 常量引用
// 相当于 常量指针常量
// 用法: const 数据类型 &变量名 = 其他变量或常量
// 注意:
// 一般的引用不能赋值常量(字面量),但是 const 引用可以赋值常量
#include <iostream>
using namespace std;
int main()
{
int x = 10;
const int &x1 = x;
// error: assignment of read - only reference x1
x1 -= 5; // const 修饰的引用不能修改内容
cout << "x = " << x << endl;
const int &x2 = 50;
cout << "x2 = " << x2 << endl;
// error: invalid initialization of non-const reference of type int& from an rvalue of type int
int &x3 = 'a'; // 常量不能赋值给非常量引用
cout << "x3 = " << x3 << endl;
return 0;
}

26
day2/d8.cpp Normal file
View File

@ -0,0 +1,26 @@
// 内联函数
// 函数前面添加了 inline 关键字,则此函数为内联函数
// 内联函数只能在当前文件中使用,相当于函数前面加 static
// 内联函数一般用于替换 有参的宏,有参宏经常会出错,而且参数是无数据类型
// 每一次使用内联函数时,都会像有参宏一样,展开一次(内联函数不入栈,运行效率高,编译时会提前处理)
#include <iostream>
using namespace std;
#define ADD(x, y) x + y
// 内联函数
inline int add(int a, int b)
{
return a + b;
}
int main()
{
int ret1 = ADD(10, 20) * 10;
cout << "ret = " << ret1 << endl;
int ret2 = add(10, 20) * 10;
cout << "ret = " << ret2 << endl;
return 0;
}

30
day2/d9.cpp Normal file
View File

@ -0,0 +1,30 @@
// 函数的默认参数
#include <iostream>
using namespace std;
bool isSs(int n = 10) // 判断 n 是否为质数
{
int i = 2;
for (; i < n / 2; i++)
{
if (n % i == 0)
return false; // 0 为假
}
return true; // 非 0 为真
}
double area(double r = 1.0) // 求面积
{
return 3.14 * r * r;
}
int main()
{
cout << "isSS(7) is " << isSs(7) << endl;
cout << "isSS() is " << isSs() << endl;
cout << "area() is " << area() << endl;
cout << "area(2) is " << area(2) << endl;
return 0;
}

13
day2/homework/h1.cpp Normal file
View File

@ -0,0 +1,13 @@
// 找错
#include <iostream>
using namespace std;
int main()
{
const int n = 10;
const int &p = n;
// p++; // 错误,不能修改常量引用所引用的值
cout << "n=" << n << endl;
return 0;
}

14
day2/homework/h2.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <iostream>
using namespace std;
int func1(int a, int, int b = 20)
{
return a + b;
}
int main()
{
cout << func1(10, 30) << endl;
cout << func1(10, 10, 100) << endl;
}

21
day2/homework/h3.cpp Normal file
View File

@ -0,0 +1,21 @@
// 1. 2. 创建一个名为 Rectangle 的类,具有成员变量 width 和 height以及成员函数 double calculateArea() ,用于计算矩形的面积。
#include <iostream>
using namespace std;
class Rectangle
{
public:
double calculateArea()
{
return width * height;
}
int width, height;
};
int main()
{
return 0;
}

20
day2/homework/h4.cpp Normal file
View File

@ -0,0 +1,20 @@
// 1. 2. 创建一个名为 Circle 的类,具有成员变量 radius 和 pi圆周率以及成员函数 double calculateArea() ,用于计算圆的面积。
#include <iostream>
using namespace std;
class Circle
{
public:
double radius, pi = 3.1415926;
double calculateArea()
{
return pi * radius * radius;
}
};
int main()
{
return 0;
}

24
day2/homework/h5.cpp Normal file
View File

@ -0,0 +1,24 @@
// 1. 2. 创建一个名为 BankAccount 的类,具有成员变量 accountNumber 和 balance以及成员函数 void deposit(double amount) 和 void withdraw(double amount) ,用于存款和取款操作。
#include <iostream>
using namespace std;
class BankAccount
{
public:
long accountNumber, balance;
void deposit(double amount)
{
this->balance += amount;
}
void withdraw(double amount)
{
this->balance -= amount;
}
};
int main()
{
return 0;
}

21
day2/homework/h6.cpp Normal file
View File

@ -0,0 +1,21 @@
// 创建一个名为 Student 的类,具有成员变量 name 和 grade以及成员函数 void study(char *course) ,用于表示某班级的某学生正在学习某一门课程。
#include <iostream>
using namespace std;
class Student
{
public:
char name[32]; // 学生姓名
char grade[32]; // 班级名
void study(char *course)
{
cout << this->grade << "班的" << this->name << "同学正在学习" << course << endl;
}
};
int main()
{
return 0;
}

35
day2/homework/h7.cpp Normal file
View File

@ -0,0 +1,35 @@
// 创建一个名为 Car 的类,具有成员变量 brand 和 speed以及成员函数 void accelerate() 和 void brake() ,用于加速和刹车操作。
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
string brand;
int speed;
void accelerate()
{
this->speed += 1;
cout << "加速 1 码" << endl;
}
void brake()
{
if (this->speed > 0)
{
this->speed -= 1;
cout << "减速中" << endl;
}
else
{
cout << "车已经停下了" << endl;
}
}
};
int main()
{
return 0;
}

22
day2/homework/h8.cpp Normal file
View File

@ -0,0 +1,22 @@
// 1. 2. 创建一个名为 Employee 的类,具有成员变量 name、id 和 salary以及成员函数 void displayInfo() ,用于显示员工的信息。
#include <iostream>
using namespace std;
class Employee
{
public:
string name;
int id;
int salary;
void displayinfo()
{
cout << "员工信息\nid: " << this->id << "\t姓名: " << this->name << "\t薪水: " << this->salary << endl;
}
};
int main()
{
return 0;
}

58
day2/homework/h9.cpp Normal file
View File

@ -0,0 +1,58 @@
// 创建一个名为 Book 的类,具有成员变量 title、author 和 year以及成员函数 void displayInfo() ,用于显示图书的信息。 创建Book类的数组至少存放5本书并设计全局函数实现Book对象按year从大到小排序并显示排序后信息。
#include <iostream>
using namespace std;
class Book
{
public:
string title, author;
int year;
// 使用 const 加 & 引用,避免不必要的拷贝
void init(const string &title, const string &author, int year)
{
this->title = title;
this->author = author;
this->year = year;
}
void displayinfo()
{
cout << "书名: " << this->title << " , 作者: " << this->author << " , 出版年份: " << this->year << endl;
}
};
void sortAndShow(Book book[], int n)
{
int i = 0;
for (; i < n; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (book[j].year < book[j + 1].year)
{
Book tmp = book[j];
book[j] = book[j + 1];
book[j + 1] = tmp;
}
}
}
for (i = 0; i < n; i++)
{
book[i].displayinfo();
}
}
int main()
{
Book books[5];
books[0].init("数学", "张三", 1998);
books[1].init("语文", "李四", 2009);
books[2].init("英语", "王五", 1987);
books[3].init("物理", "赵六", 2008);
books[4].init("化学", "郑七", 1982);
sortAndShow(books, 5);
return 0;
}