结构体定义
struct StructName {
// 成员变量
DataType1 member1;
DataType2 member2;
// ...
DataTypeN memberN;
// 成员函数(可选)
ReturnType functionName(Parameters) {
// 函数体
}
};
struct:关键字用于定义结构体。
StructName:结构体的名称。结构体名称遵循标识符命名规则。
成员变量:结构体中包含的数据成员。每个成员变量都有其类型和名称。
成员函数(可选):结构体中可以包含成员函数,用于操作结构体的数据成员。成员函数的定义在结构体内部,通常,结构体内部只应包含成员变量和成员函数的声明,**而实际的实现应该在类外部。**
example:
```cpp
// 定义结构体
struct Person {
// 成员变量
std::string name;
int age;
// 成员函数
void display() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
结构体的内容
在C++中,结构体可以包含各种不同类型的成员,包括但不限于:
1、基本数据类型: 整数、浮点数、字符等。
struct Person {
int age;
float height;
char gender;
};
2、其他结构体: 结构体可以包含其他结构体作为其成员。
struct Date {
int day;
int month;
int year;
};
struct Student {
std::string name;
Date birthdate;
int rollNumber;
};
3、数组: 结构体内可以包含数组,这样就可以表示一组相关的数据。
struct Rectangle {
float width;
float height;
float corners[4]; // Array representing coordinates of corners
};
4、指针: 可以包含指向其他数据类型的指针。
struct Node {
int data;
Node* next; // Pointer to the next node in a linked list
};
5、函数指针: 结构体内可以包含指向函数的指针。
struct MathOperations {
int (*add)(int, int);
int (*subtract)(int, int);
};
6、枚举类型: 可以包含枚举类型作为成员。
enum Color { RED, GREEN, BLUE };
struct ColoredObject {
Color color;
int size;
};
7、成员函数: C++中的结构体可以包含成员函数。
struct Circle {
float radius;
float getArea() {
return 3.14 * radius * radius;
}
};
类的定义
class MyClass {
public:
// 公有成员(可以在类外访问)
// ...
private:
// 私有成员(只能在类内访问)
// ...
protected:
// 保护成员(可以在类及其派生类中访问)
// ...
public:
// 构造函数
MyClass();
// 公有成员函数的原型声明
void publicFunction1();
void publicFunction2(int parameter);
// 析构函数
~MyClass();
};
MyClass::MyClass() {
std::cout << "Constructor called" << std::endl;
}
// 公有成员函数1的实现
void MyClass::publicFunction1() {
std::cout << "Public Function 1 called" << std::endl;
}
// 公有成员函数2的实现
void MyClass::publicFunction2(int parameter) {
std::cout << "Public Function 2 called with parameter: " << parameter << std::endl;
}
// 析构函数的实现
MyClass::~MyClass() {
std::cout << "Destructor called" << std::endl;
}
类里面定义的函数,可以类外写它的实现方式,注意函数格式
类名::函数名
命名空间
我也是第一次知道命名空间这种东西,目前为止还没有做过c++方面的开发,很多东西都是存在于一个理论层面,现在就一边读代码,一边学习,一边记录我学到的理论知识
命名空间的定义
命名空间(Namespace)在C++中用于解决命名冲突的问题,它可以包含以下内容:
1、变量: 命名空间可以包含变量的声明和定义。
2、函数: 命名空间可以包含函数的声明和定义。
3、类和结构体: 命名空间可以包含类和结构体的声明和定义。
4、其他命名空间: 命名空间可以嵌套,一个命名空间可以包含其他命名空间。
5、枚举: 命名空间可以包含枚举的声明。
6、类型定义(typedef): 命名空间可以包含类型定义。
一个命名空间的基本结构如下:
namespace MyNamespace {
// 变量声明/定义
extern int myVariable;
// 函数声明/定义
void myFunction();
// 类声明/定义
class MyClass {
// ...
};
// 其他命名空间
namespace NestedNamespace {
// ...
}
// 枚举声明
enum MyEnum {
// ...
};
// 类型定义
typedef int MyType;
}
命名空间的主要作用有两个:
避免命名冲突: 命名空间允许将相同名称的实体(如变量、函数、类等)组织在一个逻辑单元中,避免了命名冲突。
代码组织和模块化: 命名空间可以帮助组织代码,将相关的实体放在一个逻辑单元中,提高代码的可维护性和可读性。
命名空间使用演示:
使用命名空间限定符来调用相应的函数或变量等
命名空间的调用方式
命名空间::变量/函数
#include <iostream>
// 定义命名空间
namespace Math {
const double PI = 3.141592653589793;
double add(double a, double b) {
return a + b;
}
}
int main() {
// 使用命名空间中的常量和函数
std::cout << "PI: " << Math::PI << std::endl;
std::cout << "Sum: " << Math::add(2.0, 3.0) << std::endl;
return 0;
}
同名函数在不同的命名空间中的调用
#include <iostream>
// 第一个命名空间
namespace Namespace1 {
void commonFunction() {
std::cout << "Function in Namespace1" << std::endl;
}
}
// 第二个命名空间
namespace Namespace2 {
void commonFunction() {
std::cout << "Function in Namespace2" << std::endl;
}
}
int main() {
// 调用第一个命名空间的函数
Namespace1::commonFunction();
// 调用第二个命名空间的函数
Namespace2::commonFunction();
return 0;
}
命名空间与类的区别和联系
命名空间中的函数可以和命名空间中的类的内部函数同名
命名空间中的类的内部函数可以和命名空间外部的类中的函数同名
#include <iostream>
// 在命名空间中定义同名函数
namespace MyNamespace {
void commonFunction() {
std::cout << "Namespace Function" << std::endl;
}
//函数声明
void A();
// 在命名空间中定义同名类
class MyClass {
public:
// 构造函数声明
MyClass();
void commonFunction() {
std::cout << "Namespace Class Member Function" << std::endl;
}
//成员函数声明
void A();
void B();
// 析构函数声明
~MyClass();
};
// 在命名空间内部实现函数 B
void MyClass::B() {
std::cout << "Function B called" << std::endl;
}
}
// 函数的实现
void MyNamespace::A() {
// 实现函数的具体逻辑
}
//命名空间内部类的构造函数在命名空间外部的实现
MyNamespace::MyClass::MyClass() {
std::cout << "Constructor called" << std::endl;
}
// 成员函数的实现
void MyNamespace::MyClass::A() {
std::cout << "Member Function called" << std::endl;
}
// 析构函数的实现
MyNamespace::MyClass::~MyClass() {
std::cout << "Destructor called" << std::endl;
}
// 在命名空间外部定义同名类
class MyClass {
public:
//构造函数
MyClass();
// 成员函数
void commonFunction() {
std::cout << "Global Class Member Function" << std::endl;
}
//成员函数的声明
void A();
//析构函数
~MyClass();
};
//在类外实现类内定义的函数
// 构造函数的实现
MyClass::MyClass() {
std::cout << "Constructor called" << std::endl;
}
void MyClass::A() {
std::cout << "Member Function called" << std::endl;
}
// 析构函数的实现
MyClass::~MyClass() {
std::cout << "Destructor called" << std::endl;
}
// 实现同名函数
void commonFunction() {
std::cout << "Global Function" << std::endl;
}
int main() {
// 调用命名空间中的函数
MyNamespace::commonFunction();
// 创建命名空间中的类的对象并调用类的函数
MyNamespace::MyClass namespaceObj;
namespaceObj.commonFunction();
// 创建命名空间外部的类的对象并调用类的函数
::MyClass globalObj;
globalObj.commonFunction();
// 调用命名空间外部的函数
::commonFunction();
return 0;
}
总结一下就是:
1、命名空间: 在命名空间内部定义的函数,可以在命名空间外部实现,eg:void MyNamespace::A() {},或者命名空间内部实现,eg:void commonFunction() {}
2、命名空间内的类:函数可以在命名空间外部实现,eg:MyNamespace::MyClass::MyClass() {} ,
可在类内实现,eg:void commonFunction() {}
可在类外,命名空间内部实现,eg: void MyClass::B() {},
3、命名空间外的类,可以在类外实现,或者类内实现:void MyClass::A() {},void commonFunction() {},
当存在同名函数,并且这些函数分别位于命名空间内、命名空间的类内以及命名空间外部的类中,同时这些同名函数在类外实现时,可以通过以下方式进行调用:
- 命名空间内的同名函数:
MyNamespace::commonFunction();
- 命名空间中的同名类的同名函数:
MyNamespace::MyClass namespaceObj;
namespaceObj.commonFunction();
- 命名空间外部的同名函数:
::commonFunction();
- 命名空间外部的同名类的同名函数:
MyClass globalObj;
globalObj.commonFunction();