在 C++ 中,結構的工作方式與類相同,除了兩個小差異之外。其中最重要的是隱藏實現細節。默認情況下,結構不會向在代碼中使用它的人隱藏其實現細節,而類默認情況下隱藏其所有實現細節,因此默認情況下會阻止程序員訪問它們。下表總結了所有根本差異。
Class |
Structure |
---|---|
1. 類的成員默認是私有的。 | 1. 結構體的成員默認是公共的。 |
2. 類的實例稱為‘object’。 | 2. 結構的實例稱為“結構變量”。 |
3.默認情況下,類的成員類/結構是私有的,但並非所有編程語言都有這種默認行為,例如Java等。 | 3. 結構體的成員類/結構體默認是公共的。 |
4. 聲明使用類關鍵詞。 | 4. 聲明使用結構體關鍵詞。 |
5. 通常用於數據抽象和進一步繼承。 | 5. 一般用於數據分組 |
6. 類中可以有 NULL 值。 | 6. NULL 值是不可能的。 |
7. 用法: 類class_name{ data_member; member_function; }; |
7. 用法: 結構structure_name{ 輸入structure_member1; 輸入structure_member2; }; |
詳細說明這些差異的一些示例:
1) 類的成員默認是私有的,結構的成員默認是公共的。
例如,程序1編譯失敗,但程序2正常,
程序1:
C++
// C++ Program to demonstrate that
// Members of a class are private
// by default
#include <iostream>
using namespace std;
class Test {
// x is private
int x;
};
int main()
{
Test t;
// compiler error because x
// is private
t.x = 20;
return t.x;
}
時間複雜度:O(1)
輔助空間:O(1)
輸出:
./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp: In function 'int main()': ./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:10:9: error: 'int Test::x' is private int x; ^ ./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:18:7: error: within this context t.x = 20; ^ ./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:10:9: error: 'int Test::x' is private int x; ^ ./cf03c8d1-d4a3-43ea-a058-fe5b5303167b.cpp:20:14: error: within this context return t.x; ^
程序2:
C++
// C++ Program to demonstrate that
// members of a structure are public
// by default
#include <iostream>
using namespace std;
struct Test {
// x is public
int x;
};
int main()
{
Test t;
t.x = 20;
// works fine because x is public
cout << t.x;
}
輸出
20
時間複雜度:O(1)
輔助空間:O(1)
2) 使用class關鍵字聲明類,使用struct關鍵字聲明結構。
用法:
class ClassName { private: member1; member2; public: member3; . . memberN; };
用法:
struct StructureName { member1; member2; . . . memberN; };
3) 可以通過類和結構進行繼承
例如,程序 3 和 4 運行良好。
程序3:
C++
// C++ Program to demonstrate
// Inheritance with classes.
#include <iostream>
using namespace std;
// Base class
class Parent {
public:
int x;
};
// Subclass inheriting from
// base class (Parent).
class Child : public Parent {
public:
int y;
};
int main()
{
Child obj1;
// An object of class Child has
// all data members and member
// functions of class Parent.
obj1.y = 7;
obj1.x = 91;
cout << obj1.y << endl;
cout << obj1.x << endl;
return 0;
}
輸出
7 91
時間複雜度:O(1)
輔助空間:O(1)
程序4:
C++
// C++ Program to demonstrate
// Inheritance with structures.
#include <iostream>
using namespace std;
struct Base {
public:
int x;
};
// is equivalent to
// struct Derived : public Base {}
struct Derived : Base {
public:
int y;
};
int main()
{
Derived d;
// Works fine because inheritance
// is public.
d.x = 20;
cout << d.x;
cin.get();
return 0;
}
輸出
20
時間複雜度:O(1)
輔助空間:O(1)
要了解C結構體和C++結構體之間的區別,請參閱 this article.
相關用法
- C++ String Assign()用法及代碼示例
- C++ String Data()用法及代碼示例
- C++ String Find()用法及代碼示例
- C++ String append()用法及代碼示例
- C++ String at()用法及代碼示例
- C++ String back()用法及代碼示例
- C++ String begin()用法及代碼示例
- C++ String c_str()用法及代碼示例
- C++ String capacity()用法及代碼示例
- C++ String cbegin()用法及代碼示例
- C++ String cend()用法及代碼示例
- C++ String clear()用法及代碼示例
- C++ String compare()用法及代碼示例
- C++ String copy()用法及代碼示例
- C++ String crbegin()用法及代碼示例
- C++ String crend()用法及代碼示例
- C++ String empty()用法及代碼示例
- C++ String end()用法及代碼示例
- C++ String erase()用法及代碼示例
- C++ String find_first_not_of()用法及代碼示例
- C++ String find_first_of()用法及代碼示例
- C++ String find_last_not_of()用法及代碼示例
- C++ String find_last_of()用法及代碼示例
- C++ String front()用法及代碼示例
- C++ String insert()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Difference Between Structure and Class in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。