📜  C 结构和 C++ 结构的区别

📅  最后修改于: 2021-09-10 03:10:55             🧑  作者: Mango

在 C++ 中,struct 和 class 是完全相同的东西,除了结构默认为公共可见性而类默认为私有可见性。
C 和 C++ 结构之间的一些重要区别:

  • 结构内部的成员函数:C 中的结构不能在结构内部具有成员函数,但 C++ 中的结构可以具有成员函数和数据成员。
C
// C Program to Implement Member functions inside structure
 
#include 
struct marks {
    int num;
    void
    Set(int temp)   // Member function inside Structure to
                    // take input and store it in "num"
    {
        num = temp;
    }
    void display()  //  function used to display the values
    {
        printf("%d", num);
    }
};
// Driver Program
int main()
{
    struct marks m1;
    m1.Set(9); // calling function inside Struct to
               // initialize value to num
    m1.display(); // calling function inside struct to
                  // display value of Num
}
 
/* Error Occured
prog.c:18:4: error: ‘struct marks’ has no member named ‘Set’
  m1.Set(9);  // calling function inside Struct to
initialize value to num
    ^
prog.c:19:4: error: ‘struct marks’ has no member named
‘display’ m1.display(); // calling function inside struct to
display value of Num
 */


C++
// C++ Program to Implement Member functions inside
// structure
 
#include 
using namespace std;
struct marks {
    int num;
    void
    Set(int temp) // Member function inside Structure to
                  // take input and store it in "num"
    {
        num = temp;
    }
    void display() //  function used to display the values
    {
        cout << "num=" << num;
    }
};
// Driver Program
int main()
{
    marks m1;
    m1.Set(9); // calling function inside Struct to
               // initialize value to num
    m1.display(); // calling function inside struct to
                  // display value of Num
}
 
// This Code is Contributed by Samyak Jain


C
// C program to demonstrate that direct
// member initialization is not possible in C
#include 
 
struct Record {
    int x = 7;
};
 
// Driver Program
int main()
{
    struct Record s;
    printf("%d", s.x);
    return 0;
}
/* Output :  Compiler Error
   6:8: error: expected ':', ', ', ';', '}' or
  '__attribute__' before '=' token
  int x = 7;
        ^
  In function 'main': */


C++
// CPP program to initialize data member in c++
#include 
using namespace std;
 
struct Record {
    int x = 7;
};
 
// Driver Program
int main()
{
    Record s;
    cout << s.x << endl;
    return 0;
}
// Output
// 7


C
// C program with structure static member
struct Record {
    static int x;
};
 
// Driver program
int main()
{
    return 0;
}
/* 6:5: error: expected specifier-qualifier-list
   before 'static'
     static int x;
     ^*/


C++
// C++ program with structure static member
 
struct Record {
    static int x;
};
 
// Driver program
int main()
{
    return 0;
}


C
// C program to demonstrate that Constructor is not allowed
#include 
 
struct Student {
    int roll;
    Student(int x)
    {
        roll = x;
    }
};
 
// Driver Program
int main()
{
    struct Student s(2);
    printf("%d", s.x);
    return 0;
}
/* Output :  Compiler Error
   [Error] expected specifier-qualifier-list
    before 'Student'
   [Error] expected declaration specifiers or
   '...' before numeric constant
   [Error] 's' undeclared (first use
   5555555555in this function)
   In function 'main': */


C++
// CPP program to initialize data member in c++
#include 
using namespace std;
 
struct Student {
    int roll;
    Student(int x)
    {
        roll = x;
    }
};
 
// Driver Program
int main()
{
    struct Student s(2);
    cout << s.roll;
    return 0;
}
// Output
// 2


C
// C program to illustrate empty structure
#include 
 
// empty structure
struct Record {
};
 
// Driver program
int main()
{
    struct Record s;
    printf("%lu\n", sizeof(s));
    return 0;
}


C++
// C++ program to illustrate empty structure
#include 
using namespace std;
 
// empty structure
struct Record{   
};
 
// Driver program
int main() {
    struct Record s;
    cout<


这将在 C 中产生错误,但在 C++ 中不会产生错误。

C++

// C++ Program to Implement Member functions inside
// structure
 
#include 
using namespace std;
struct marks {
    int num;
    void
    Set(int temp) // Member function inside Structure to
                  // take input and store it in "num"
    {
        num = temp;
    }
    void display() //  function used to display the values
    {
        cout << "num=" << num;
    }
};
// Driver Program
int main()
{
    marks m1;
    m1.Set(9); // calling function inside Struct to
               // initialize value to num
    m1.display(); // calling function inside struct to
                  // display value of Num
}
 
// This Code is Contributed by Samyak Jain
输出
num=9
  • 直接初始化:我们不能在 C 中直接初始化结构数据成员,但我们可以在 C++ 中进行。

C

// C program to demonstrate that direct
// member initialization is not possible in C
#include 
 
struct Record {
    int x = 7;
};
 
// Driver Program
int main()
{
    struct Record s;
    printf("%d", s.x);
    return 0;
}
/* Output :  Compiler Error
   6:8: error: expected ':', ', ', ';', '}' or
  '__attribute__' before '=' token
  int x = 7;
        ^
  In function 'main': */

C++

// CPP program to initialize data member in c++
#include 
using namespace std;
 
struct Record {
    int x = 7;
};
 
// Driver Program
int main()
{
    Record s;
    cout << s.x << endl;
    return 0;
}
// Output
// 7

输出:

7
  • 使用struct关键字:在C中,我们需要使用struct来声明一个struct变量。在 C++ 中,结构不是必需的。例如,让 Record 有一个结构。在 C 中,我们必须对 Record 变量使用“struct Record”。在 C++ 中,我们不需要使用 struct 并且只使用“Record”就可以了。
  • 静态成员: C 结构不能有静态成员,但在 C++ 中是允许的。

C

// C program with structure static member
struct Record {
    static int x;
};
 
// Driver program
int main()
{
    return 0;
}
/* 6:5: error: expected specifier-qualifier-list
   before 'static'
     static int x;
     ^*/

C++

// C++ program with structure static member
 
struct Record {
    static int x;
};
 
// Driver program
int main()
{
    return 0;
}

这将在 C 中产生错误,但在 C++ 中不会产生错误。

  • 结构中的构造函数创建:C中的结构不能在结构内部具有构造函数,但 C++ 中的结构可以具有构造函数创建。

C

// C program to demonstrate that Constructor is not allowed
#include 
 
struct Student {
    int roll;
    Student(int x)
    {
        roll = x;
    }
};
 
// Driver Program
int main()
{
    struct Student s(2);
    printf("%d", s.x);
    return 0;
}
/* Output :  Compiler Error
   [Error] expected specifier-qualifier-list
    before 'Student'
   [Error] expected declaration specifiers or
   '...' before numeric constant
   [Error] 's' undeclared (first use
   5555555555in this function)
   In function 'main': */

C++

// CPP program to initialize data member in c++
#include 
using namespace std;
 
struct Student {
    int roll;
    Student(int x)
    {
        roll = x;
    }
};
 
// Driver Program
int main()
{
    struct Student s(2);
    cout << s.roll;
    return 0;
}
// Output
// 2

输出:

2
  • sizeof运算符:此运算符将为 C 中的空结构生成0 ,而为 C++ 中的空结构生成1

C

// C program to illustrate empty structure
#include 
 
// empty structure
struct Record {
};
 
// Driver program
int main()
{
    struct Record s;
    printf("%lu\n", sizeof(s));
    return 0;
}

C++

// C++ program to illustrate empty structure
#include 
using namespace std;
 
// empty structure
struct Record{   
};
 
// Driver program
int main() {
    struct Record s;
    cout<

C 中的输出:

0

C++ 输出:

1
  • 数据隐藏: C 结构不允许数据隐藏的概念,但在 C++ 中是允许的,因为 C++ 是一种面向对象的语言,而 C 则不是。
  • 访问修饰符: C 结构没有访问修饰符,因为该语言不支持这些修饰符。 C++ 结构可以有这个概念,因为它内置在语言中。
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程