IFD:Acoustic Interfaces/introduction to c++: Difference between revisions

From Medien Wiki
No edit summary
No edit summary
 
Line 1: Line 1:
Hey there, we were using this  [https://www.tutorialspoint.com/compile_cpp_online.php online compiler] to learn some basics about c++. Here are the code snippets we discussed:
Little intro to data types:
Little intro to data types:


Line 68: Line 70:
         point1.print();
         point1.print();
         point2.print();
         point2.print();
          
         // this is an array of integers
         int integerArray[5] = { 0,1,2,3,4 };
         int integerArray[5] = { 0,1,2,3,4 };
          
          
         cout << "first element: " << integerArray[0] << endl;
         cout << "first element: " << integerArray[0] << endl;
       
        // we can also declare an array points
         Point2D pointArray[3] = {point1, point2, point3 };
         Point2D pointArray[3] = {point1, point2, point3 };
         pointArray[2].print();
         pointArray[2].print();
       
         // pointer version of the code
         // pointer version of the code
         Point2D* p2d1_ptr = new Point2D(123, 234);
         Point2D* p2d1_ptr = new Point2D(123, 234);

Latest revision as of 10:39, 11 May 2020

Hey there, we were using this online compiler to learn some basics about c++. Here are the code snippets we discussed:

Little intro to data types:

   #include <iostream>
   #include <string>
   using namespace std;
   int main()
   {
       int int_number = 2147483647; // 2^32 / 2 -1 = 2^31 -1 = 2147483648 -1 // -2^31 = -2147483648
       long long_integer = 9223372036854775807; // 2^64 / 2 - 1= 2^63 -1 = 2147483648 -1 // -2^63 = -2147483648
       float low_precision_big_float_number = 9223372036854775807; // 9.22337e+18 = 9.22337 *10^18
       double high_precision_big_float_number = 9223372036854775807; // 9.22337e+18 = 9.22337 *10^18
       
       //char are 8bit = 2^8 = 256 
       char letter_exclamation = 33; // this gets converted through ASCII table to the letter '!'
       char letter_A = 65; // this gets converted through ASCII table to the letter '!'
       
       string text = "this is a string, it's not included by default";
       
       int converted_float = 3.21;
       
       cout << "Integer:  " << int_number << endl; 
       cout << "Long Integer: " << long_integer  << endl; 
       cout << "Overflow Integer:  " << int_number + 1 << endl; 
       cout << "Overflow Long Integer: " << long_integer  +1 << endl; 
       cout << "Low precision Big float: " << low_precision_big_float_number << endl;
       cout << "High precision Big float: " << high_precision_big_float_number << endl; // command line is the bottleneck of putting out more precision
       cout << "Thats the letter '!': " << letter_exclamation << endl; 
       cout << "Thats the letter 'A': " << letter_A << endl; 
       cout << text;
       
       return 0;
   }

The code about classes and pointers:

   #include <iostream>
   using namespace std;
   class Point2D {
       public:
           Point2D(float newX, float newY){
               _x = newX;
               _y = newY;
           };
       
           float getX() {
              return _x;  
           };
           
           float getY() {
              return _y;  
           };
           
           void print() {
               cout << "(" << getX() << ", " << getY()<< ")" << endl;
           }
           
       private:
           float _x;
           float _y;
   };
   
   int main()
   {
       Point2D point1(5,1);
       Point2D point2(10,100);
       Point2D point3(27,2);
       
       point1.print();
       point2.print();
       // this is an array of integers
       int integerArray[5] = { 0,1,2,3,4 };
       
       cout << "first element: " << integerArray[0] << endl;
       
       // we can also declare an array points
       Point2D pointArray[3] = {point1, point2, point3 };
       pointArray[2].print();
       
       // pointer version of the code
       Point2D* p2d1_ptr = new Point2D(123, 234);
       Point2D* p2d2_ptr = new Point2D(123, 234);
       Point2D* p2d3_ptr = new Point2D(123, 234);
       
       (*p2d1_ptr).print();
       
       p2d1_ptr->print();
       Point2D* point_ptr_Array[3] = {p2d1_ptr, p2d2_ptr, p2d3_ptr };
       cout << "pointer address: " << point_ptr_Array[0] << "x component of the point at that address: " << point_ptr_Array[0]->getX()  << endl;
       
       return 0;
   }