我们知道 C++ 中的数据类型主要分为三种:预定义数据类型、派生数据类型和用户定义数据类型。预定义数据类型已在之前的教程中讨论过。
在本教程中,我们主要关注派生数据类型以及用户定义数据类型。
派生数据类型是从原始或内置数据类型派生出来的数据类型。
它们分为四种类型
让我们快速解释上面列出的每种派生数据类型
函数是一段代码或程序段,其定义旨在完成特定的、定义明确的任务。通常,函数的目的是防止用户为相同的输入重复输入相同的代码行。所有代码行都组合成一个函数,可以从任何地方调用。main() 是每个 C++ 程序中定义的默认函数。
语法
FunctionType FunctionName(parameters)
#include <iostream>
using namespace std;
// max here is a function derived type
int max(int y, int z)
{
if (y > z)
return y;
else
return z;
}
// main is the default function derived type
int main()
{
int a = 30, b = 60;
// Calling above function to
// find max of 'a' and 'b'
int m = max(a, b);
cout << "m is " << m;
return 0;
}
输出
m is 60
数组是存储在连续内存区域中的项的集合。数组的目的是在单个变量中存储多个实例。
DataType ArrayName[size_of_array];
#include <iostream>
using namespace std;
int main()
{
// Array Derived Type
int arr[5];
arr[0] = 5;
arr[2] = -10;
// this is same as arr[1] = 2
arr[3 / 2] = 2;
arr[3] = arr[0];
cout<
5 2 -10 5
地址的符号表示称为指针。它们允许程序模拟按引用调用操作以及生成和管理动态数据结构。它在 C/C++ 中的一般声明格式是
语法
datatype *var_name;
示例
int *ptr;
ptr 指向一个将保存 int 数据类型的地址
#include <bits/stdc++.h>
using namespace std;
void learn()
{
int var = 20;
// Pointers Derived Type
// declare pointer variable
int* ptr;
// note that data type of ptr
// and var must be same
ptr = &var;
// assign the address of a variable
// to a pointer
cout << "Value at ptr = "
<< ptr << "\n";
cout << "Value at var = "
<< var << "\n";
cout << "Value at *ptr = "
<< *ptr << "\n";
}
// Driver program
int main()
{
learn();
}
}
输出
Value at ptr = 0x7ffc10d7fd5c Value at var = 20 Value at *ptr = 20
当一个变量声明为引用时,它就成为另一个变量的同义词。通过在声明中包含 '&',可以将变量声明为引用。
// C++ program to illustrate // Reference Derived Type #include <iostream> using namespace std; int main() { int x = 10; // Reference Derived Type // ref is a reference to x. int& ref = x; // Value of x is now changed to 20 ref = 20; cout << "x = " << x << endl; // Value of x is now changed to 30 x = 30; cout << "ref = " << ref << endl; return 0; }
输出
x = 20 ref = 30
用户定义的数据类型被称为派生数据类型或用户定义的派生数据类型。
这些类型包括
以下类型在下面详细描述。
类是 C++ 的构建块,它引出了面向对象编程。它是一种用户定义的数据类型,拥有自己的数据成员和成员函数集,可以通过创建该类的实例来访问和使用。类类似于对象的蓝图。
// C++ program to demonstrate
// Class
#include <bits/stdc++.h>
using namespace std;
class Learns {
// Access specifier
public:
// Data Members
string learnname;
// Member Functions()
void printname()
{
cout << "Learnname is: " << learnname;
}
};
int main()
{
// Declare an object of class geeks
Learns obj1;
// accessing data member
obj1.learnname = "Learn eTutorials";
// accessing member function
obj1.printname();
return 0;
}
输出
Learnname is: Learn eTutorials
在 C/C++ 中,结构体是一种用户定义的数据类型。结构体定义了一种数据类型,可用于将可能不同类型的项组合成单一类型。
语法
struct address {
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
// C++ program to demonstrate
// Structures in C++
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int main()
{
// Create an array of structures
struct Point arr[10];
// Access array members
arr[0].x = 10;
arr[0].y = 20;
cout << arr[0].x << ", " << arr[0].y;
return 0;
}
10, 20
联合体也是一种用户定义的数据类型,类似于结构体。联合体的所有成员共享相同的内存位置。例如,在以下程序中,x 和 y 共享相同的内存位置。当我们更改 x 时,我们可以看到这些更改是如何反映在 y 中的。
#include <iostream>
using namespace std;
// Declaration of union is same as the structures
union test {
int x, y;
};
int main()
{
// A union variable t
union test t;
// t.y also gets value 2
t.x = 2;
cout << "After making x = 2:"
<< endl
<< "x = " << t.x
<< ", y = " << t.y
<< endl;
// t.x is also updated to 10
t.y = 10;
cout << "After making Y = 10:"
<< endl
<< "x = " << t.x
<< ", y = " << t.y
<< endl;
return 0;
}
After making x = 2: x = 2, y = 2 After making Y = 10: x = 10, y = 10
枚举(或 enum)主要是一种用户定义的数据类型。它主要用于为整数常量命名,这使得程序更易于阅读和维护。
语法
enum State {Working = 1, Failed = 0};
#include <iostream> using namespace std; enum week { Mon, Tue, Wed, Thur, Fri, Sat, Sun }; int main() { enum week day; day = Wed; cout << day; return 0; }
2
C++ 中的关键字 typedef 允许您显式定义新的数据类型名称。使用 typedef 并不会创建新的数据类,而是为现有类型定义一个名称。这可以提高程序的移植性(程序在不同类型的机器上使用(例如,小型机、大型机、微型机等)而无需对代码进行重大更改的能力),因为只需要更改 typedef 语句。通过允许为标准数据类型使用描述性名称,typedef 还可以帮助实现自文档化代码。
typedef type name;
#include <iostream>
using namespace std;
// After this line BYTE can be used
// in place of unsigned char
typedef unsigned char BYTE;
int main()
{
BYTE b1, b2;
b1 = 'c';
cout << " " << b1;
return 0;
}
c