User Tools

Site Tools


cpp:basic_data_types

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:basic_data_types [2017/03/15 16:46] – [Δήλωση και αρχικοποίηση μεταβλητών] gthanoscpp:basic_data_types [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
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;            // initial value undetermined+
  
   a = a + b;   a = a + b;
-  result = a - c; +  cout << "a: " << a << endl;  // endl stands for newline 
-  cout << result;+  b = a - c;               
 +  cout << "b: " << b << endl;
  
   return 0;   return 0;
 } }
 </code> </code>
 +
 +===== Αυτόματη εξαγωγή τύπου δεδομένων =====
 +
 +Όταν μία μεταβλητή δηλώνεται ο μεταγλωττιστής μπορεί να προσδιορίσει τον τύπο της από τον τύπο της τιμής που ανατίθεται σε αυτή. Σε αυτή τη περίπτωση αρκεί ο τύπος της μεταβλητής να δηλωθεί ως **auto**, όπως στο παρακάτω παράδειγμα.
 +
 +<code cpp using-auto.cpp>
 +#include <iostream>
 +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 << "sizeof(result): " << sizeof(result) << endl;
 +  cout << "sizeof(int): " << sizeof(int) << endl;
 +  cout << "sizeof(long): " << sizeof(long) << endl;
 +
 +  return 0;
 +}
 +</code>
 +
 +Επίσης, εάν μία μεταβλητή δεν αρχικοποιείται μπορείτε να δηλώσετε ότι είναι ιδίου τύπου με μία άλλη μεταβλητή ως εξής;
 +
 +<code cpp using-decltype.cpp>
 +#include <iostream>
 +using namespace std;
 +
 +int main ()
 +{
 +  int a{5};               // initial value: 5
 +  long b(3);              // initial value: 3
 +  decltype(b) result;
 +
 +  cout << "sizeof(result): " << sizeof(result) << endl;
 +  cout << "sizeof(int): " << sizeof(int) << endl;
 +  cout << "sizeof(long): " << sizeof(long) << endl;
 +  
 +  return 0;
 +}
 +</code>
 +
  
cpp/basic_data_types.1489596404.txt.gz · Last modified: 2017/03/15 16:46 by gthanos