📜  C ++中的用户定义数据类型

📅  最后修改于: 2021-05-31 23:23:21             🧑  作者: Mango

数据类型是识别数据类型以及处理数据的相关操作的手段。数据类型有三种:

  1. 预定义的数据类型
  2. 派生数据类型
  3. 用户定义的数据类型

在本文中,说明了用户定义的数据类型:

用户定义的数据类型:

用户定义的数据类型称为派生数据类型或用户定义的派生数据类型。
这些类型包括:

  • 班级
  • 结构
  • 联盟
  • 枚举
  • Typedef定义的DataType

以下是以下类型的详细说明:

  1. 类:导致面向对象编程的C++构建块是Class 。它是用户定义的数据类型,它拥有自己的数据成员和成员函数,可以通过创建该类的实例来访问和使用它们。类就像对象的蓝图。

    语法
    C类和对象

    例子:

    // C++ program to demonstrate
    // Class
      
    #include 
    using namespace std;
      
    class Geeks {
        // Access specifier
    public:
        // Data Members
        string geekname;
      
        // Member Functions()
        void printname()
        {
            cout << "Geekname is: " << geekname;
        }
    };
      
    int main()
    {
      
        // Declare an object of class geeks
        Geeks obj1;
      
        // accessing data member
        obj1.geekname = "GeeksForGeeks";
      
        // accessing member function
        obj1.printname();
      
        return 0;
    }
    
    输出:
    Geekname is: GeeksForGeeks
    
  2. 结构:结构是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 
    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
    
  3. 联合:与结构类似,联合是用户定义的数据类型。在并集中,所有成员共享相同的内存位置。例如,在下面的C程序中,x和y共享相同的位置。如果更改x,我们可以看到更改反映在y中。
    #include 
    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
    
  4. 枚举:枚举(或枚举)是C中用户定义的数据类型。它主要用于将名称分配给整数常量,这些名称使程序易于阅读和维护。

    句法:

    enum State {Working = 1, Failed = 0}; 
    // Program to demonstrate working
    // of enum in C++
      
    #include 
    using namespace std;
      
    enum week { Mon,
                Tue,
                Wed,
                Thur,
                Fri,
                Sat,
                Sun };
      
    int main()
    {
        enum week day;
      
        day = Wed;
      
        cout << day;
      
        return 0;
    }
    
    输出:
    2
    
  5. Typedef :C++允许您使用关键字typedef显式定义新的数据类型名称。使用typedef实际上并不会创建新的数据类,而是会为现有类型定义一个名称。这可以提高程序的可移植性(程序可以在不同类型的机器上使用的能力;即小型,大型机,微型等;无需对代码进行太多更改),因为只需更改typedef语句即可。使用typedef,还可以通过允许标准数据类型使用描述性名称来帮助自我记录代码。

    句法:

    typedef type name;

    其中type是任何C++数据类型,而name是该数据类型的新名称。
    这为C++的标准类型定义了另一个名称。

    例子:

    // C++ program to demonstrate typedef
    #include 
    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
    
    想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”