Skip to content

azimiHamid/C_plus_plus

Repository files navigation

C++ Notes

C++ 'Hello world' Code Expalin

Different Ways to Write "Hello, World!" in C++

1. Using namespace and using Keywords

  • Method 1: Full use of namespace:

    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Hello, World!" << endl;
        return 0;
    }
  • Method 2: Selective use of using for specific elements:

    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main() {
        cout << "Hello, World!" << endl;
        return 0;
    }

2. Without namespace or using Keywords

This approach avoids bringing anything from the standard library into the global namespace, making it clearer where each element comes from.

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

#include < iostream>

  • This line includes the iostream library, which provides essential input and output functionalities, such as cin (for input) and cout (for output).

using namespace std;

  • The namespace keyword allows us to use names for objects and variables from the C++ standard library.
  • std is the standard namespace that includes common functionalities like cin and cout.
  • We can define our own namespaces for custom code organization.

int main() {...};

  • This function tells the C++ compiler where to start executing the program. It serves as the entry point for any C++ application.
  • Remember to always specify the return type (int in this case), which indicates that the main function will return an integer value to the operating system, typically indicating the success or failure of the program.

cout << "Hello World!" << endl;

  • cout & endln : These are part of the std(standard) namespace. cout is used for outputting text to the console, and endl is used to insert a newline character and flush the output buffer.
  • << operator : This is the stream insertion operator. It directs the text 'Hello World!' to the cout object, which then displays it on the console. The << operator is also used to chain multiple outputs together.

return 0; or return 1;

  • return 0;__ indicates that the program has executed successfully.
  • return 1; (or any non-zero value)__ indicates that the program encountered an error or failure. Different non-zero values can be used to represent specific types of errors.

  • Since we declared int as the return type for the main function, it is mandatory to return an integer value. This value is communicated to the operating system, indicating the program’s outcome.

Custom namespace/region

  • Defining a custom namespace similar to the standard 'std' namespace

      #include < iostream>
    
      using namespace std;
    
      namespace myRegion {
          void display() {
              // This function currently does nothing
          }
      }
    
      namespace anotherRegion {
          void display() {
              // This function currently does nothing
          }
      }
    
      int main() {
          myRegion::display();
          anotherRegion::display();
          cout << "Hello, World!" << endl;
          return 0;
      }
    
  • We can now use myRegion::display() to call the display function.

  • Here, myRegion::display() and anotherRegion::display() are distinguished by their namespaces, so there’s no confusion about which display function is being used.

#include & using in C++ Vs. import in JavaScript

  • #include: In C++, it’s a preprocessing directive used to include code from another file. This is like a direct code copy-and-paste, as the contents of the included file are inserted into the source file before compilation begins.
  • using: It helps to avoid prefixing with the namespace name every time you access a function or variable from that namespace. It does not affect the inclusion of code but rather the visibility of names.
  • import: In JavaScript, it handles module imports, allowing you to use code from other files or libraries with a runtime mechanism. It doesn’t involve copying code but linking/referencing modules.

What does mean runtime mechanism in 'JavaScript'

  • Module Loading: When you use import, JavaScript loads the module or file at runtime (when the code is executed). This is different from compile-time inclusion, like #include in C++, where the code is directly copied into the source file before compilation.

  • Dynamic Importing: JavaScript modules can be dynamically imported using the import() function, which allows for modules to be loaded on demand. This can improve performance by loading code only when it is actually needed.

  • Caching: Once a module is imported, JavaScript engines cache the module's exports. This means that if the same module is imported again, the engine reuses the cached version instead of loading it again, which improves efficiency.

🔍 C++ vs. JavaScript: A Performance Perspective

As a developer, understanding the differences between languages can significantly impact your choice of technology. Here’s a quick comparison between C++ and JavaScript focusing on performance aspects:

  • Compilation vs. Interpretation:

C++: A compiled language where code is translated into machine code before execution. This direct execution by hardware often results in faster performance.
JavaScript: Primarily an interpreted language, though modern engines use Just-In-Time (JIT) compilation. Code is executed at runtime, which can introduce additional overhead.

  • Static vs. Dynamic Typing:

C++: Statically typed, meaning types are checked at compile-time. This allows for optimizations during compilation.
JavaScript: Dynamically typed, with type checking occurring at runtime, leading to some performance overhead.

  • Memory Management:

C++: Offers fine-grained control over memory, leading to potentially more optimized and efficient code.
JavaScript: Uses automatic garbage collection, which introduces some overhead due to periodic memory management.

  • Low-Level Operations:

C++: Provides low-level access to system resources and hardware, enabling highly optimized execution for performance-critical tasks.
JavaScript: Operates at a higher level with additional abstractions, designed for flexibility and ease of use in web development.

  • Summary :   C++ excels in performance-critical applications due to its compiled nature, static typing, and low-level control. JavaScript, while potentially slower, offers flexibility and ease of development for web environments. Understanding these differences helps in choosing the right tool for your specific needs.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors