瀏覽代碼

Add structure

Zhilong Li 4 年之前
父節點
當前提交
99a5ae3a08
共有 2 個文件被更改,包括 37 次插入0 次删除
  1. 1 0
      CMakeLists.txt
  2. 36 0
      structures.cpp

+ 1 - 0
CMakeLists.txt

@@ -14,4 +14,5 @@ add_executable(try_pointer try_pointer.cpp)
 add_executable(pointer_array pointer_array.cpp)
 add_executable(min_array min_array.cpp)
 add_executable(dyna_mem_array dyn_mem_array.cpp)
+add_executable(structures structures.cpp)
 target_link_libraries(CppYoutube functions)

+ 36 - 0
structures.cpp

@@ -0,0 +1,36 @@
+#include <iostream>
+
+using namespace std;
+
+// structure is a user defined data type
+// this is a buleprint, it does not contain any values
+struct SmartPhone
+{
+    string name = "iPhone 12";
+    int storageSpace = 256;
+    string color = "Space Grey";
+    float price;
+};
+
+int main()
+{
+    // If we want to define a phone
+    string name = "iPhone 12";
+    int storageSpace = 256;
+    string color = "Space Grey";
+    float price = 999.99;
+
+    // We must copy all the code to define second phone
+    string name2 = "Samsung S21 Ultra";
+    int storageSpace2 = 128;
+    string color2 = "Space Grey";
+    float price2 = 899.99;
+
+    SmartPhone Apple;
+    Apple.name = "iPhone12";
+    Apple.color = "Space Grey";
+    Apple.price = 999.99;
+    Apple.storageSpace = 512;
+
+    cout << Apple.name << endl;
+}