cpp:basic_data_types
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cpp:basic_data_types [2017/03/15 16:45] – [Δήλωση και αρχικοποίηση μεταβλητών] gthanos | cpp:basic_data_types [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 39: | Line 39: | ||
int x(100); | int x(100); | ||
int x{100}; | int x{100}; | ||
- | </code | + | </code> |
<code c++ main.cpp> | <code c++ main.cpp> | ||
Line 50: | Line 50: | ||
int b(3); // initial value: 3 | int b(3); // initial value: 3 | ||
int c{2}; // initial value: 2 | int c{2}; // initial value: 2 | ||
- | int result; | + | |
a = a + b; | a = a + b; | ||
- | | + | |
- | cout << | + | b = a - c; |
+ | cout << | ||
return 0; | return 0; | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Αυτόματη εξαγωγή τύπου δεδομένων ===== | ||
+ | |||
+ | Όταν μία μεταβλητή δηλώνεται ο μεταγλωττιστής μπορεί να προσδιορίσει τον τύπο της από τον τύπο της τιμής που ανατίθεται σε αυτή. Σε αυτή τη περίπτωση αρκεί ο τύπος της μεταβλητής να δηλωθεί ως **auto**, όπως στο παρακάτω παράδειγμα. | ||
+ | |||
+ | <code cpp using-auto.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main () | ||
+ | { | ||
+ | int a{5}; // initial value: 5, sizeof int | ||
+ | long b(3); // initial value: 3 sizeof long | ||
+ | |||
+ | auto result = a + b; // compiler automatically extracts the variable type. | ||
+ | | ||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Επίσης, | ||
+ | |||
+ | <code cpp using-decltype.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main () | ||
+ | { | ||
+ | int a{5}; // initial value: 5 | ||
+ | long b(3); // initial value: 3 | ||
+ | decltype(b) result; | ||
+ | |||
+ | cout << " | ||
+ | cout << " | ||
+ | cout << " | ||
+ | | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
cpp/basic_data_types.1489596352.txt.gz · Last modified: 2017/03/15 16:45 by gthanos