*************
C++
topic:code structure
*************
To have a better understanding of the c++ learning, have a glance at the code structure.
Take a very impressive example like I want to run a car factory. The bank's name is ElseWhere Car, ERCar for short. It means all the data and opreations are running in this protected space. Another example is that If you deposit $100 in ERBank, the balance changes in ERBank's account. And then you deposit $100 in other banks, the balance changes in other Bank's account. The object $100 can be same in different namespace.
![]() |
#include <iostream>
#include <string>
#include <vector>
// 命名空间:提供全局作用域的组织结构
namespace CarFactory {
};
Ok, For better understanding, I want to make a car, the design expection is class. The specific car made from this design is the object of the class. The brand and model and color are private, which other factory cannnot get.
![]() |
#include <iostream>
#include <string>
#include <vector>
// 命名空间:提供全局作用域的组织结构
namespace CarFactory {
// 类定义:汽车设计蓝图(抽象,不可实例化)
class CarDesignBlueprint {
private:
std::string brand; // 汽车品牌
std::string model; // 汽车型号
std::string color; // 汽车颜色
};
Users cannot change brand, model and color, but they need to know the information. so Program need to be visited outside.
![]() |
#include <iostream>
#include <string>
#include <vector>
// 命名空间:提供全局作用域的组织结构
namespace CarFactory {
// 类定义:汽车设计蓝图(抽象,不可实例化)
class CarDesignBlueprint {
private:
std::string brand; // 汽车品牌
std::string model; // 汽车型号
std::string color; // 汽车颜色
public:
// 构造函数:初始化设计蓝图
CarDesignBlueprint(const std::string& carBrand, const std::string& carModel, const std::string& carColor)
: brand(carBrand), model(carModel), color(carColor) {}
// 获取品牌、型号和颜色的方法
std::string getBrand() const { return brand; }
std::string getModel() const { return model; }
std::string getColor() const { return color; }
};
After designing, I need to buil a real car. Just define another calss named Car to make the program of build a real car. Alway pay attention to the concept thay class is only the method and data.
![]() |
#include <iostream>
#include <string>
#include <vector>
// 命名空间:提供全局作用域的组织结构
namespace CarFactory {
// 类定义:汽车设计蓝图(抽象,不可实例化)
class CarDesignBlueprint {
private:
std::string brand; // 汽车品牌
std::string model; // 汽车型号
std::string color; // 汽车颜色
public:
// 构造函数:初始化设计蓝图
CarDesignBlueprint(const std::string& carBrand, const std::string& carModel, const std::string& carColor)
: brand(carBrand), model(carModel), color(carColor) {}
// 获取品牌、型号和颜色的方法
std::string getBrand() const { return brand; }
std::string getModel() const { return model; }
std::string getColor() const { return color; }
};
// 类定义:根据设计蓝图制造的汽车实例
class Car : public CarDesignBlueprint {
public:
// 构造函数:根据设计蓝图创建具体的汽车
Car(const std::string& carBrand, const std::string& carModel, const std::string& carColor)
: CarDesignBlueprint(carBrand, carModel, carColor) {}
// 汽车的行为:启动
void startEngine() {
std::cout << "Starting " << getBrand() << " " << getModel() << " (Color: " << getColor() << ")...\n";
}
// 汽车的行为:加速
void accelerate() {
std::cout << getBrand() << " " << getModel() << " is accelerating...\n";
}
// 汽车的行为:刹车
void brake() {
std::cout << getBrand() << " " << getModel() << " is braking...\n";
}
// 汽车的行为:停车
void stopEngine() {
std::cout << getBrand() << " " << getModel() << " has stopped.\n";
}
};
} // namespace CarFactory
The next is very exciting because we will make a real car. The specific making happens in main code.
![]() |
#include <iostream>
#include <string>
#include <vector>
// 命名空间:提供全局作用域的组织结构
namespace CarFactory {
// 类定义:汽车设计蓝图(抽象,不可实例化)
class CarDesignBlueprint {
private:
std::string brand; // 汽车品牌
std::string model; // 汽车型号
std::string color; // 汽车颜色
public:
// 构造函数:初始化设计蓝图
CarDesignBlueprint(const std::string& carBrand, const std::string& carModel, const std::string& carColor)
: brand(carBrand), model(carModel), color(carColor) {}
// 获取品牌、型号和颜色的方法
std::string getBrand() const { return brand; }
std::string getModel() const { return model; }
std::string getColor() const { return color; }
};
// 类定义:根据设计蓝图制造的汽车实例
class Car : public CarDesignBlueprint {
public:
// 构造函数:根据设计蓝图创建具体的汽车
Car(const std::string& carBrand, const std::string& carModel, const std::string& carColor)
: CarDesignBlueprint(carBrand, carModel, carColor) {}
// 汽车的行为:启动
void startEngine() {
std::cout << "Starting " << getBrand() << " " << getModel() << " (Color: " << getColor() << ")...\n";
}
// 汽车的行为:加速
void accelerate() {
std::cout << getBrand() << " " << getModel() << " is accelerating...\n";
}
// 汽车的行为:刹车
void brake() {
std::cout << getBrand() << " " << getModel() << " is braking...\n";
}
// 汽车的行为:停车
void stopEngine() {
std::cout << getBrand() << " " << getModel() << " has stopped.\n";
}
};
} // namespace CarFactory
int main() {
// 根据设计蓝图制造具体的汽车实例(对象)
CarFactory::Car car1("Toyota", "Camry", "Red");
CarFactory::Car car2("Ford", "Mustang", "Black");
CarFactory::Car car3("BMW", "X5", "Silver");
// 每辆汽车都具备设计蓝图规定的属性和行为,但属性值不同
std::cout << "Cars in the factory:\n";
car1.startEngine();
car2.startEngine();
car3.startEngine();
car1.accelerate();
car2.brake();
car3.stopEngine();
return 0;
}
the output is here.
Cars in the factory:
Starting Toyota Camry (Color: Red)...
Starting Ford Mustang (Color: Black)...
Starting BMW X5 (Color: Silver)...
Toyota Camry is accelerating...
Ford Mustang is braking...
BMW X5 has stopped.
In a huge program, many files are written to realize the whole function of the car. In a factory, there are many departments. Every department take charge of one task. So is the code. To make the code readable, it is separated into head file, source file and main file. 头文件相当于饭店中的菜单,菜单上列出了餐厅提供的各种菜品(相当于函数、类、变量的声明),但是并不包含如何制作这些菜品的详细步骤(即实现代码)。顾客(编译器)通过菜单了解可以点哪些菜,而厨师(源文件)则根据菜单上的信息在厨房(编译过程)中制作这些菜品。这样,不同的顾客可以共享同一份菜单,而厨师可以根据菜单为不同的顾客制作相同的菜品。
I get a need to make a calculator.
Head file is only the declaration of the class and functions. Head files tell the compiler what the function/class is capable of. In other words, it specifies the type of parameters it takes and the type of value it returns.
calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <iostream>
using namespace std;
namespace jisuanqi {
class Calculator {
public:
Calculator(int a, int b); // 构造函数
// 运算方法
int add() const; // 加法
int subtract() const; // 减法
int multiply() const; // 乘法
double divide() const; // 除法
private:
int num1; // 第一个整数
int num2; // 第二个整数
};
}
#endif // CALCULATOR_H
Files with the .cpp extension contain the specific code that makes the functions, class member functions and variables work. The .cpp file is what actually runs the program, and it defines specific operations and logic based on what is declared in the header file.
calculator.cpp
#include "calculator.h"
namespace jisuanqi {
Calculator::Calculator(int a, int b) : num1(a), num2(b) {}
int Calculator::add() const {
return num1 + num2;
}
int Calculator::subtract() const {
return num1 - num2;
}
int Calculator::multiply() const {
return num1 * num2;
}
double Calculator::divide() const {
if (num2 == 0) {
cout << "Error: Division by zero!" << endl;
return 0;
}
return static_cast<double>(num1) / num2;
}
}
The main file is the one that contains the main() function. This is the entry point of the program. When you run the program, the main() function is the first thing that is called. It is responsible for initialising, executing the main logic of the program, and ending the program. The main file can call functions and classes defined in other files. This helps to organise the different parts of the overall program.
main.cpp
#include "calculator.h"
#include <iostream>
using namespace std;
using namespace jisuanqi;
int main() {
int a, b;
// 用户输入
cout << "请输入两个整数:" << endl;
cin >> a >> b;
// 创建 Calculator 对象
Calculator calc(a, b);
// 输出运算结果
cout << a << " + " << b << " = " << calc.add() << endl;
cout << a << " - " << b << " = " << calc.subtract() << endl;
cout << a << " * " << b << " = " << calc.multiply() << endl;
cout << a << " / " << b << " = " << calc.divide() << endl;
return 0;
}
There is something really important to be told. How does the main program call the function object? in main.cpp, we creat an instance object, and then the object calls the member methods of the class to perform the arithmetic.
first, create an object in calculator class.
Calculator calc(a, b);
And then the object calls the member method of a class.
calc.add(); // 调用加法方法
calc.subtract(); // 调用减法方法
calc.multiply(); // 调用乘法方法
calc.divide(); // 调用除法方法
sometimes people write cource file in the head file:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <iostream>
using namespace std;
namespace jisuanqi {
class Calculator {
public:
// 构造函数
Calculator(int a, int b) : num1(a), num2(b) {}
// 加法
int add() const {
return num1 + num2;
}
// 减法
int subtract() const {
return num1 - num2;
}
// 乘法
int multiply() const {
return num1 * num2;
}
// 除法
double divide() const {
if (num2 == 0) {
cout << "Error: Division by zero!" << endl;
return 0;
}
return static_cast<double>(num1) / num2;
}
private:
int num1; // 第一个整数
int num2; // 第二个整数
};
} // namespace jisuanqi
#endif // CALCULATOR_H
If another program want to use the calculator algorithm >> using spacename is fine.
main1.cpp
#include "calculator.h" // 包含计算器头文件
#include <iostream>
using namespace std;
using namespace jisuanqi; // 使用命名空间 jisuanqi
int main() {
int a, b;
// 提示用户输入
cout << "请输入两个整数:" << endl;
cin >> a >> b;
// 创建 Calculator 对象
Calculator calc(a, b);
// 输出运算结果
cout << a << " + " << b << " = " << calc.add() << endl;
cout << a << " - " << b << " = " << calc.subtract() << endl;
cout << a << " * " << b << " = " << calc.multiply() << endl;
cout << a << " / " << b << " = " << calc.divide() << endl;
return 0;
}