Commit fb82929b authored by Clark Lin's avatar Clark Lin
Browse files

Merge branch 'learning' into 'main'

Merge Basic and OOP

See merge request !2
parents 2b9afd97 70a987cf
# Exclude all files
*
# Include all folders
!**/
# Include all cpp file under all folder
!**/*.cpp
# Include .gitignore itself
!.gitignore
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// 申明
cout << "Declaration" << endl;
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0] << endl;
// 修改
cout << "Modificaiton" << endl;
cars[0] = "Opel";
cout << cars[0] << endl;
// 循环
cout << "for loop" << endl;
string vehicles[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << vehicles[i] << endl;
}
// 循环2
cout << "for each loop" << endl;
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << endl;
}
// 省略大小
cout << "Omit size during declaration" << endl;
string pc[3] = {"Lenovo", "HP", "Dell"}; // Also three arrays
for (int i = 0; i < sizeof(pc) / sizeof(string); i++) {
cout << pc[i] << endl;
}
// 获取大小
cout << "Get size" << endl;
cout << sizeof(pc) / sizeof(string) << endl;
// 多维数组
cout << "Loop multi-dimension" << endl;
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
cout << letters[0][2] << endl;
// 循环多维数组
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << letters[i][j] << " ";
}
cout << endl;
}
}
\ No newline at end of file
#include <iostream>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
cout << area << " | defined by #define";
cout << NEWLINE;
// return 0;
const int CONS_LENGTH = 7;
const int CONS_WIDTH = 8;
const char CONS_NEWLINE = '\n';
// int area;
area = CONS_LENGTH * CONS_WIDTH;
cout << area << " | defined by const";
cout << NEWLINE;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// 输入数字
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum << endl;
// 输入字符串
string firstName, lastName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Type your last name: ";
cin >> lastName; // get user input from the keyboard
cout << "Your name is: " << firstName << " " << lastName << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// We put "1" to indicate there is a ship.
bool ships[4][4] = {
{ 0, 1, 1, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 1, 0 }
};
// Keep track of how many hits the player has and how many turns they have played in these variables
int hits = 0;
int numberOfTurns = 0;
// Allow the player to keep going until they have hit all four ships
while (hits < 4) {
int row, column;
cout << "Selecting coordinates\n";
// Ask the player for a row
cout << "Choose a row number between 0 and 3: ";
cin >> row;
// Ask the player for a column
cout << "Choose a column number between 0 and 3: ";
cin >> column;
// Check if a ship exists in those coordinates
if (ships[row][column]) {
// If the player hit a ship, remove it by setting the value to zero.
ships[row][column] = 0;
// Increase the hit counter
hits++;
// Tell the player that they have hit a ship and how many ships are left
cout << "Hit! " << (4-hits) << " left.\n\n";
} else {
// Tell the player that they missed
cout << "Miss\n\n";
}
// Count how many turns the player has taken
numberOfTurns++;
}
cout << "Victory!\n";
cout << "You won in " << numberOfTurns << " turns";
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// while loop
cout << "while loop:" << endl;
int i = 0;
while (i < 5) {
cout << i << endl;
i++;
}
// do while loop
i = 0;
cout << "do while loop:" << endl;
do {
cout << i << endl;
i++;
}
while (i < 5);
// for loop
cout << "for loop:" << endl;
for (int i = 0; i < 5; i++) {
cout << i << endl;
}
// for each loop
cout << "for each loop:" << endl;
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << endl;
}
// break
cout << "break:" << endl;
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << endl;
}
// continue
cout << "continue:" << endl;
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << endl;
}
}
\ No newline at end of file
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
// 解决控制台中文输出乱码
SetConsoleOutputCP(CP_UTF8);
/****************************************
* 算术运算符
* **************************************/
int a = 21;
int b = 10;
int c;
c = a + b;
cout << "Line 1 - c 的值是 " << c << endl ;
c = a - b;
cout << "Line 2 - c 的值是 " << c << endl ;
c = a * b;
cout << "Line 3 - c 的值是 " << c << endl ;
c = a / b;
cout << "Line 4 - c 的值是 " << c << endl ;
c = a % b;
cout << "Line 5 - c 的值是 " << c << endl ;
c = a++;
cout << "Line 6 - c 的值是 " << c << endl ;
c = a--;
cout << "Line 7 - c 的值是 " << c << endl ;
/****************************************
* 关系运算符
* **************************************/
if ( a == b ) {
cout << "Line 1 - a 等于 b" << endl ;
} else {
cout << "Line 1 - a 不等于 b" << endl ;
}
if ( a < b ) {
cout << "Line 2 - a 小于 b" << endl ;
} else {
cout << "Line 2 - a 不小于 b" << endl ;
}
if ( a > b ) {
cout << "Line 3 - a 大于 b" << endl ;
} else {
cout << "Line 3 - a 不大于 b" << endl ;
} /* 改变 a 和 b 的值 */
a = 5;
b = 20;
if ( a <= b ) {
cout << "Line 4 - a 小于或等于 b" << endl ;
}
if ( b >= a ) {
cout << "Line 5 - b 大于或等于 a" << endl ;
}
/****************************************
* 逻辑运算符
* **************************************/
a = 5;
b = 20;
if ( a && b ) {
cout << "Line 1 - 条件为真"<< endl ;
}
if ( a || b ) {
cout << "Line 2 - 条件为真"<< endl ;
}
/* 改变 a 和 b 的值 */
a = 0;
b = 10;
if ( a && b ) {
cout << "Line 3 - 条件为真"<< endl ;
} else {
cout << "Line 4 - 条件不为真"<< endl ;
}
if ( !(a && b) ) {
cout << "Line 5 - 条件为真"<< endl ;
}
/****************************************
* 位运算符
* **************************************/
a = 60; // 60 = 0011 1100
b = 13; // 13 = 0000 1101
c = 0;
c = a & b; // 12 = 0000 1100
cout << "Line 1 - c 的值是 " << c << endl ;
c = a | b; // 61 = 0011 1101
cout << "Line 2 - c 的值是 " << c << endl ;
c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - c 的值是 " << c << endl ;
c = ~a; // -61 = 1100 0011
cout << "Line 4 - c 的值是 " << c << endl ;
c = a << 2; // 240 = 1111 0000
cout << "Line 5 - c 的值是 " << c << endl ;
c = a >> 2; // 15 = 0000 1111
cout << "Line 6 - c 的值是 " << c << endl ;
/****************************************
* 赋值运算符
* **************************************/
a = 21;
c = a;
cout << "Line 1 - = 运算符实例,c 的值 = : " <<c<< endl ;
c += a;
cout << "Line 2 - += 运算符实例,c 的值 = : " <<c<< endl ;
c -= a;
cout << "Line 3 - -= 运算符实例,c 的值 = : " <<c<< endl ;
c *= a;
cout << "Line 4 - *= 运算符实例,c 的值 = : " <<c<< endl ;
c /= a;
cout << "Line 5 - /= 运算符实例,c 的值 = : " <<c<< endl ;
c = 200;
c %= a;
cout << "Line 6 - %= 运算符实例,c 的值 = : " <<c<< endl ;
// a = 11 = 0000 1011
c <<= 2;
// a = 44 = 0010 1100
cout << "Line 7 - <<= 运算符实例,c 的值 = : " <<c<< endl ;
c >>= 2;
cout << "Line 8 - >>= 运算符实例,c 的值 = : " <<c<< endl ;
c &= 2;
cout << "Line 9 - &= 运算符实例,c 的值 = : " <<c<< endl ;
c ^= 2;
cout << "Line 10 - ^= 运算符实例,c 的值 = : " <<c<< endl ;
c |= 2;
cout << "Line 11 - |= 运算符实例,c 的值 = : " <<c<< endl ;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
string food = "Pizza"; // A food variable of type string
cout << "Output value and address by variable and reference" << endl;
cout << food << endl; // Outputs the value of food (Pizza)
cout << &food << endl; // Outputs the memory address of food (0x6dfed4)
// 定义指针
cout << "Output value and address by pointer" << endl;
string* ptr = &food;
cout << *ptr << endl;
cout << ptr << endl;
// 修改指针指向内存的值
cout << "Modify value by pointer" << endl;
*ptr = "Hamburger";
// 指针指向变量的值也被改变了
cout << food << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
string food = "Pizza";
string &meal = food;
cout << food << endl; // Outputs Pizza
cout << meal << endl; // Outputs Pizza
// Get memory address
cout << &food << endl; // Outputs like 0x6dfed4
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting << endl;
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName << endl;
fullName = firstName.append(" ").append(lastName);
cout << fullName << endl;
cout << "The length of the txt string is: " << fullName.length() << endl;
// 竖着打印
for (int i = 0; i < fullName.length(); i++) {
cout << fullName[i] << endl;
}
// 转义字符
cout << "We are the so-called \"Vikings\" from the north." << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
// Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;
// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
// Print members of myStructure
cout << myStructure.myNum << endl;
cout << myStructure.myString << endl;
}
\ No newline at end of file
#include <iostream>
using namespace std;
int main() {
char mode;
cout << "Please choose mode (A/B/C): ";
cin >> mode;
switch(mode) {
case 'A':
cout << "Mode: " << mode << endl;
break;
case 'B':
cout << "Mode: " << mode << endl;
break;
case 'C':
cout << "Mode: " << mode << endl;
break;
default:
cout << "Invalid mode!" << endl;
}
}
\ No newline at end of file
...@@ -42,7 +42,7 @@ int main(){ ...@@ -42,7 +42,7 @@ int main(){
* char : -128 到 127 或者 0 到 255 * char : -128 到 127 或者 0 到 255
* unsigned char : 0 到 255 * unsigned char : 0 到 255
*******************************************/ *******************************************/
char char_min, char_max, char_A, char_overflow; char unsigned char_min, char_max, char_A, char_overflow;
char_min = int(0); char_min = int(0);
char_max = int(255); char_max = int(255);
char_A = int(65); char_A = int(65);
......
#include <iostream>
#include <Windows.h>
void func(void);
static int count = 10; // 全局变量
int count_in_main; // 定义一个变量,这个变量将被外部cpp程序引用
extern void write_extern(); // 引用外部cpp程序的函数
int main() {
// 解决控制台中文输出乱码
SetConsoleOutputCP(CP_UTF8);
// register 存储类用于定义存储在寄存器中而不是 RAM 中的局部变量。这意味着变量的最大尺寸等于寄存器的大小(通常是一个词),且不能对它应用一元的 '&' 运算符(因为它没有内存位置)。
// 寄存器只用于需要快速访问的变量,比如计数器。
// 还应注意的是,定义 'register' 并不意味着变量将被存储在寄存器中,它意味着变量可能存储在寄存器中,这取决于硬件和实现的限制。
register int miles;
// 在多次调用func的过程中,func中的局部变量i在程序的生命周期内保存,不需要在每次它进入和离开作用域时进行创建和销毁。
// 因此,使用 static 修饰局部变量可以在函数调用之间保持局部变量的值。
while (count--) {
func();
}
// !!! 由于使用到extern函数,编译的时候需要将这个cpp文件和VariableStorageDescSupport.cpp一起编译
// 编译命令 g++.exe .\VariableStorageDesc.cpp .\VariableStorageDescSupport.cpp -o .\VariableStorageDesc
count_in_main = 5;
write_extern();
return 0;
}
void func(void) {
static int i = 5; // 局部静态变量,不会在每次被调用时进行创建和销毁。
i++;
std::cout << "变量 i 为 " << i;
std::cout << " , 变量 count 为 " << count << std::endl;
}
\ No newline at end of file
#include <iostream>
extern int count_in_main; // 引用外部程序VariableStorageDesc的全局变量count_in_main
void write_extern(void)
{
std::cout << "count_in_main is " << count_in_main << ". 此处是调用外部cpp文件VariableStorageDescSupport.cpp输出的结果。" << std::endl;
}
\ No newline at end of file
#include <iostream>
#include <Windows.h>
using namespace std;
class Box {
// 类成员可以被定义为 public、private 或 protected。默认情况下是定义为 private。
// public: 公有成员在程序中类的外部是可访问的。您可以不使用任何成员函数来设置和获取公有变量的值
// private: 私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
// protected: 保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的
// 实际操作中,我们一般会在私有区域定义数据,在公有区域定义相关的函数,以便在类的外部也可以调用这些函数
// private: 私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
// 构造函数,它会在每次创建类的新对象时执行。
// 构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。
// 构造函数可用于为某些成员变量设置初始值。
// protected: 保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的
protected:
double volumn; // 体积
// protected: 保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员在派生类(即子类)中是可访问的
public:
// 构造函数,它会在每次创建类的新对象时执行。
// 构造函数的名称与类的名称是完全相同的,并且不会返回任何类型,也不会返回 void。
// 构造函数可用于为某些成员变量设置初始值。
Box() {
cout << "Box构造函数执行" << endl;
};
// 析构函数,在每次删除所创建的对象时执行。
~Box() {
cout << "Box析构函数执行" << endl;
};
// 类成员函数,计算体积
double getVolume() {
this->volumn = length * breadth * height;
return this->volumn;
};
void setLength(double length) {
this->length = length;
};
void setBreadth(double breadth) {
this->breadth = breadth;
};
void setHeight(double height) {
this->height = height;
};
};
int main() {
// 解决控制台中文输出乱码
SetConsoleOutputCP(CP_UTF8);
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
double volume = 0.0; // 用于存储体积
// box 1 详述
Box1.setHeight(5.0);
Box1.setLength(6.0);
Box1.setBreadth(7.0);
// box 2 详述
Box2.setHeight(10.0);
Box2.setLength(12.0);
Box2.setBreadth(13.0);
// box 1 的体积
volume = Box1.getVolume();
cout << "Box1 的体积:" << volume << endl;
// box 2 的体积
volume = Box2.getVolume();
cout << "Box2 的体积:" << volume << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
public:
// 构造函数定义
Box (double l, double b, double h) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
// 保存第一个对象的地址
ptrBox = &Box1;
// 现在尝试使用成员访问运算符来访问成员
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
// 保存第二个对象的地址
ptrBox = &Box2;
// 现在尝试使用成员访问运算符来访问成员
cout << "Volume of Box2: " << ptrBox->Volume() << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
private:
double price;
public:
Box(double price) {
this->price = price;
}
void show() {
cout << "Price is " << this->price << endl;
}
};
int main() {
// 对于普通类型的对象来说,它们之间的复制是很简单的,例如:
// int a = 100;
// int b = a;
// 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。
Box a(100);
Box b = a; // 注意这里的对象初始化要调用拷贝构造函数,而非赋值
a.show();
b.show();
/*
* 从以上代码的运行结果可以看出,系统为对象 B 分配了内存并完成了与对象 A 的复制过程。
* 就类对象而言,相同类型的类对象是通过拷贝构造函数来完成整个复制过程的。
* 拷贝构造函数是一种特殊的构造函数,它在创建对象时,
* 是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:
* 通过使用另一个同类型的对象来初始化新创建的对象。
* 复制对象把它作为参数传递给函数。
* 复制对象,并从函数返回这个对象。
*/
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Adder {
public:
// 构造函数
Adder(int i = 0) {
total = i;
}
// 数据封装是一种把数据和操作数据的函数捆绑在一起的机制,
// 外部调用累加操作时,只需要提供累加的数量,具体的实现逻辑封装在类的公有函数中
// 对外的接口
void addNum(int number) {
total += number;
}
// 数据封装是一种把数据和操作数据的函数捆绑在一起的机制,
// 外部调用取累加值,不需要了解实际逻辑,具体的实现逻辑封装在类的公有函数中
// 对外的接口
int getTotal() {
return total;
};
private:
// 对外隐藏的数据
// 数据抽象是一种仅向用户暴露接口而把具体的实现细节隐藏起来的机制。
// 对于数量的和,定义为私有成员,使它不直接对外可见
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() <<endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
private:
double width;
public:
double length;
// 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。
// 尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。
friend void printWidth(Box box) {
// 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员
cout << "Width of box : " << box.width <<endl;
};
void setWidth(double width) {
this->width = width;
};
};
// 程序的主函数
int main() {
Box box;
// 使用成员函数设置宽度
box.setWidth(10.0);
// 使用友元函数输出宽度
printWidth(box);
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment