← Back to Index
Research & Engineering Archive

OOPC 1. ProgrammingBasics

By Jingnan Huang · September 15, 2025 · 5886 Words

OOPC1.ProgrammingBasics
#

Last Edit: 9/15/25

1.2. Programming Basics
#

1.2.1. Basic Structure of a C++ Program
#

##include <iostream>
using namespace std;

int main() {
    cout << "Hello world!" << endl;
    return 0;
}

std Standard Library 标准库
#


##include <iostream>

int main() {
    std::cout << "Hello World!" << std::endl;
    return 0;
}

1.2.1.1. A program to input a value
#

##include <iostream>     // 引入输入输出库 iostream
using namespace std;    // 使用标准命名空间 std

int main(void) {        // 主函数,程序从这里开始
    int value = 0;      // 定义一个整数变量 value,初始值为 0

    cout << "Enter a value: ";   // cout (输出) → 在屏幕上打印提示
    cin >> value;                // cin (输入) → 等待用户输入,把值存到 value

    cout << "The value entered is " << value << endl;  
    // 再次用 cout 打印出刚刚输入的值,并换行

    return 0;          // 程序结束,返回 0
}

« & »
#

  1. cout «
    • cout (输出流,character output stream) → 往屏幕输出
    • << 箭头朝左,意思是把数据推出去(out of the page,往屏幕方向)
    • 可以记成:
      • cout << "Hello"; = 把 "Hello" 推到屏幕
  2. cin »
    • cin (输入流,character input stream) → 从键盘输入
    • >> 箭头朝右,意思是把数据吸进来(into the page,流进变量)
    • 可以记成:
      • cin >> value; = 从键盘读入一个值,放到 value 变量里

1.2.2. Common Data Representations
#

int i, j;   // 声明了两个 int 类型的变量 i 和 j

1.2.2.1. Integers
#

  1. int (整型,integer)
    • 通常占 32 bits,也就是 4 bytes
    • 因为 1 bit 用来存储正负号,所以剩下 31 bits 存数字
    • 能表示的范围:\(-2^{31} \quad 到 \quad 2^{31}-1\)
  2. short (短整型,short integer)
    • 通常占 16 bits
    • 1 bit 存符号,剩下 15 bits 存数字。
    • 范围:\(−1-2^{15} \quad 到 \quad 2^{15}-1\)
  3. long (长整型,long integer)
    • 占用比 32 bits 更多的空间(通常 64 bits)
    • 能表示的数范围比 int 更大
    • 范围取决于编译器和机器,但肯定比 int 大
int participants = 60;   // 声明一个整型变量 participants,赋值 60

1.2.2.2 Floating-point Numbers
#

  1. float (单精度浮点型)
    • 占 32 位 (32 bits),
    • 精度大约 7 decimal digits of precision,包括小数点前和小数点后的总位数
  2. double (双精度浮点型)
    • 64 bits
    • 精度大约 15 位十进制有效数字
  3. long double (长双精度浮点型)
    • 通常占用比 64 位更多的存储空间
    • 精度比 double 更高

1.2.2.3. Boolean
#

1.2.2.4. Characters
#

1.2.2.5. Arrays
#

int arr[7] = {1, 2, 3, 4, 5, 6, 7};

1.2.2.6. Strings
#

char h[6] = "Hello";
##include <iostream>
##include <string>
using namespace std;

int main(void) {
    string prePhrase = "This course is ";
    string postPhrase = " Programming Fundamentals!", blank = "______";

    cout << "Fill in the blank of the following sentence" << endl;
    cout << prePhrase << blank << postPhrase << endl;

    cin >> blank;   // 从用户输入读入 blank

    if (blank == "ECE244") {   // 判断输入是否等于 "ECE244"
        cout << "Correct!" << endl;
        string sentence = prePhrase + blank + postPhrase;  // 拼接字符串
        cout << sentence << endl;
    } else {
        cout << "Incorrect!" << endl;
    }
    return 0;
}

1.2.3 Operators
#

Arithmetic operators (算术运算符)
#

Assignment operators (赋值运算符)
#

注意它和 << 不一样,<< 专门用在 cont 里,表示把右边的东西送到输出流

Increment and decrement operators (自增和自减运算符)
#

Relational operators (关系运算符)
#

Logical operators (逻辑运算符)
#

sizeof() operator (求字节数运算符)
#

1.2.4. Control Structures (控制结构)
#

1.3 Functions
#

1.3.1. n choose k example (组合数示例)
#

问题:

$$ \binom{n}{k} = \frac{n!}{k!(n-k)!} $$

1.3.1.1 Defining a function
#

##include <iostream>
using namespace std;

// Function Prototypes
int factorial(int n);
int nChooseK(int n, int k);

// Main Function
int main(void) {
  int n, k;
  cout << "Enter the value of n: " << endl;
  cin >> n;
  cout << "Enter the value of k: " << endl;
  cin >> k;
  cout << "The number of ways to choose " << k;
  cout << " from " << n << " is ";
  cout << nChooseK(n, k) << endl;
  return 0;
}

// Function Implementations
int factorial(int n) {
  int result = 1;
  for (int i = 1; i <= n; i++) {
    result *= i;
  }
  return result;
}

int nChooseK(int n, int k) {
  return factorial(n) / (factorial(k) * factorial(n - k));
}

1.3.2 Pass-by-value
#

简单说就是 Function 的内部对外部 Variable 的调用算作局部的调用,它并不会影响到全局的外部函数,只是复制了他的一份

image.png

1.4 Passing Input Parameters to Functions
#

1.4.1.1 Pass-By-Pointer
#

int a;
int* ptr = &a;

void swapByPointers(int* pa, int* pb) {
    int temp = *pa;  // temp 取到 a 的值
    *pa = *pb;       // a 的位置写入 b 的值
    *pb = temp;      // b 的位置写回 temp
}

image.png

image.png

到这里都和 C 语言的逻辑,代码一模一样

1.4.1.2 Pass-By-Reference 引用传递
#

int a = 5;
int& ra = a;   // ra 是 a 的引用

##include <iostream>
using namespace std;

int main(void) {
    int a = 5, b = 12;
    int& ra = a;   // ra 是 a 的引用

    cout << "Point 1: ra = " << ra << ", a = " << a << endl;

    ra = 10;       // 改变 ra 就是改变 a
    cout << "Point 2: ra = " << ra << ", a = " << a << endl;

    ra = b;        // 注意:这是把 b 的值赋给 a,而不是让 ra 引用 b
    cout << "Point 3: ra = " << ra << ", a = " << a << endl;

    return 0;
}
Point 1: ra = 5, a = 5
Point 2: ra = 10, a = 10
Point 3: ra = 12, a = 12

##include <iostream>
using namespace std;

void swapByRef(int& ra, int& rb) {
    int temp = ra;
    ra = rb;
    rb = temp;
}

int main(void) {
    int a = 5, b = 10;
    cout << "Before swapping: a = " << a << ", b = " << b << endl;
    swapByRef(a, b);
    cout << "After swapping: a = " << a << ", b = " << b << endl;
    return 0;
}
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Summary
#

1.5. Program Organization and Separate Compilation
#

1.5.1 How do we split code into separate files?
#

1.5.1.1 Compilation of a single file
#

##include <iostream>
using namespace std;

void printNum(int x);
int userInputNum();

int main(void) {
    int num = userInputNum();
    printNum(num);
    return 0;
}

void printNum(int x) {
    cout << "The number is " << x << endl;
}

int userInputNum() {
    int x;
    cout << "Enter a number: ";
    cin >> x;
    return x;
}

1.5.1.2 Split Code into Multiple Files
#

Hidden Segment
#

image.png

1.5.2 How are multiple files compiled?
#

g++ main.cpp print.cpp input.cpp -o main

Compiler Steps
#

  1. Preprocessing(预处理)
    • #include "file_name" 替换成对应文件的内容
    • 处理完后 .h 文件就不再需要了
  2. Generating object files(生成目标文件)
    • 每个 .cpp 会被编译成一个 .o 文件(目标文件)
    • .o 文件里是机器码,但单独的 .o 文件不能运行
    • 比如 main.cppmain.o,里面有 main 函数的机器码,但调用 printNumuserInputNum 时,只是“引用”,没有具体实现
  3. Linking object files(链接目标文件)
    • 把所有 .o 文件合并成一个可执行文件。
    • 链接器会找到 print.oinput.o 里的函数实现,把它们和 main.o 拼接在一起
    • 生成的 main 可执行文件就能运行

image.png

1.5.2.1. Separate Compilation
#

g++ -c print.cpp
g++ main.o print.o input.o -o main
g++ -c print.cpp
g++ -c main.cpp
g++ main.o print.o input.o -o main

1.5.3 Why header Files ?
#

Case Without header files
#

Case when #include cpp files
#


1.5.4 What Happens if we include a header file multiple times ?
#

##include "a.h"
##include "b.h"

Header Guards(头文件保护)
#

##ifndef FILE_NAME_H   // 如果没有定义这个宏
##define FILE_NAME_H   // 定义宏
// 头文件内容
##endif                // 结束
  1. 第一次包含头文件时,宏 FILE_NAME_H 还没定义,于是内容会展开,并且定义 FILE_NAME_H
  2. 再次包含时,发现 FILE_NAME_H 已经定义,于是直接跳过,不会重复展开

Ex.
#

##ifndef MATH_UTILS_H   // 如果没有定义过 MATH_UTILS_H
##define MATH_UTILS_H   // 定义宏,防止重复包含

// 函数声明
int add(int a, int b);
int multiply(int a, int b);

##endif  // MATH_UTILS_H

Naming Strategy
#