*************
C++
topic: function
*************
Imagine a program is just like a school. Namespace is different colluges and class is different classes. A member function of a class can be defined at the same time it is declared. It is imperative to cultivate positive habits. Just separate the definitions from declaration, no matter how big the class is.
functions are basic units of a program. Functions are crucial. Reducing duplication is a mian function of functions.
what is dedclaration?
// add.h (声明)
#ifndef ADD_H
#define ADD_H
int add(int a, int b);
#endif
what is definition?
// add.cpp (定义)
#include "add.h"
int add(int a, int b) {
return a + b;
}
Declaration tells ya what to do. Definition tells how to do.
The basic form of a function is really simple.
返回类型 函数名(参数类型 参数名, 参数类型 参数名, ...) {
// 函数体
return 返回值; // 如果返回类型不是 void,则需要返回一个值
}
Take an example, calculate 13 + 38.
case 1: no functions
#include <iostream>
int main()
{
// 直接计算并输出结果
std::cout << "13 + 38 的结果是: " << 13 + 38 << std::endl;
return 0; // 添加返回值以保持程序完整性和规范性
}
case 2: no functions but seems more complicated
#include <iostream>
int main()
{
int a = 13;
int b = 38;
// 分解计算并输出
std::cout << "13 + 38 的结果是: " << (a + b) << std::endl;
return 0;
}
case 3: add function
#include <iostream>
// 定义一个函数来计算两个数的和
int multiply(int a, int b)
{
return a + b;
}
int main()
{
// 调用函数并输出结果
int result = multiply(13, 38);
std::cout << "13 + 38 的结果是: " << result << std::endl;
return 0; // 显式返回0,表示程序成功退出
}
It looks same. Functions will reduce the size of code when increasing calculating times.
Imagine calculate the sums of two integers several times.
// 重复代码(不使用函数)
int result1 = 13 + 38;
std::cout << "13 + 38 = " << result1 << std::endl;
int result2 = 5 + 7;
std::cout << "5 + 7 = " << result2 << std::endl;
int result3 = 2 + 9;
std::cout << "2 + 9 = " << result3 << std::endl;
// 定义一个函数来计算两个数的和
int add(int a, int b) {
return a + b;
}
// 使用函数
std::cout << "13 + 38 = " << add(13, 38) << std::endl;
std::cout << "5 + 7 = " << add(5, 7) << std::endl;
std::cout << "2 + 9 = " << add(2, 9) << std::endl;
Introduce CLASS. Class does one simple task.
definite class A calculate. Definite class B output the result.
#include <iostream>
// 定义类 A,用于封装公式
class A {
public:
// 定义公式:计算两个数的和
int formula(int a, int b) {
return a + b; // 公式:a + b
}
};
// 定义类 B,用于调用类 A 中的公式
class B {
private:
A formulaProvider; // 创建一个类 A 的对象,用于访问公式
public:
// 构造函数:初始化 formulaProvider
B() = default;
// 提供一个方法,调用公式并输出结果
void calculateAndPrint(int a, int b) {
// 调用 formulaProvider 的 formula 函数
int result = formulaProvider.formula(a, b);
// 输出结果
std::cout << a << " + " << b << " 的结果是: " << result << std::endl;
}
};
// 主函数
int main() {
// 创建类 B 的对象
B calculator;
// 调用类 B 的 calculateAndPrint 方法
calculator.calculateAndPrint(13, 38);
// 显示程序成功退出
return 0;
}
It uses more functions to calculate the sum of 13 and 38. It looks complex. Donnot worry about it because it is some easy function units.
Class A does one task. The responsibility of Class A is clear: to provide a formula. The main function of Class B is to call the formula defined in Class A and output the result.
Call the formula in class A, which has a specific way.
Name an object and then call the formula.
类 新创建的对象;
返回类型 函数名 = 对象名.函数名(输入值);
#include <iostream>
// 定义 Class A
class A {
public:
// 定义公式:计算两个数的和
int formula(int a, int b) {
return a + b; // 返回 a + b 的结果
}
};
int main() {
// 创建 Class A 的对象
A formulaCalculator;
// 按照指定格式调用函数
int result = formulaCalculator.formula(13, 38); // 返回类型 int,函数名 formula,对象名 formulaCalculator,输入值 13 和 38
// 输出结果
std::cout << "13 + 38 的结果是: " << result << std::endl;
return 0; // 显式返回 0,表示程序成功退出
}
Class B wants to call a formula in class A. It will be the same.
#include <iostream>
// 定义 Class A
class A {
public:
// 定义公式:计算两个数的和
int formula(int a, int b) {
return a + b;
}
};
// 定义 Class B,直接调用 Class A 的公式
class B {
private:
A formulaProvider; // 创建 Class A 的对象
public:
// 调用公式并输出结果
void calculateAndPrint(int a, int b) {
int result = formulaProvider.formula(a, b);
std::cout << a << " + " << b << " 的结果是: " << result << std::endl;
}
};
int main() {
// 创建 Class B 的对象
B calculator;
// 调用 Class B 的方法,间接调用 Class A 的公式
calculator.calculateAndPrint(13, 38);
return 0;
}
In a real Project, codes are devided into head and main.
Head declares the variables. Main carry out.
#ifndef A_H
#define A_H
// 定义类 A
class A {
public:
// 声明公式函数
int formula(int a, int b) {
return a + b; // 实现公式:返回 a + b
}
};
#endif // A_H
#ifndef B_H
#define B_H
#include "A.h"
#include <iostream> // 包含输入输出库
// 定义类 B
class B {
private:
A formulaProvider; // 声明一个 A 类的对象
public:
// 声明并实现 calculateAndPrint 函数
void calculateAndPrint(int a, int b) {
// 调用 A 类的公式
int result = formulaProvider.formula(a, b);
// 输出结果
std::cout << a << " + " << b << " 的结果是: " << result << std::endl;
}
};
#endif // B_H
#include "B.h"
int main() {
// 创建 B 类的对象
B calculator;
// 调用 B 类的 calculateAndPrint 方法
calculator.calculateAndPrint(13, 38);
return 0; // 显式返回 0,表示程序成功退出
}