<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Salman Hussain Memon</title>
    <description>The latest articles on DEV Community by Salman Hussain Memon (@salman_hussainmemon_d5dc).</description>
    <link>https://dev.to/salman_hussainmemon_d5dc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2658938%2Fa608af50-7d2c-4452-a4df-ee398b45a0f9.png</url>
      <title>DEV Community: Salman Hussain Memon</title>
      <link>https://dev.to/salman_hussainmemon_d5dc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salman_hussainmemon_d5dc"/>
    <language>en</language>
    <item>
      <title>A Comprehensive Introduction to C++: Master the Basics, syntax and key concepts</title>
      <dc:creator>Salman Hussain Memon</dc:creator>
      <pubDate>Sun, 05 Jan 2025 10:09:03 +0000</pubDate>
      <link>https://dev.to/salman_hussainmemon_d5dc/a-comprehensive-introduction-to-c-master-the-basics-syntax-and-key-concepts-3bdb</link>
      <guid>https://dev.to/salman_hussainmemon_d5dc/a-comprehensive-introduction-to-c-master-the-basics-syntax-and-key-concepts-3bdb</guid>
      <description>&lt;p&gt;C++ is one of the most powerful and versatile languages. it's widely used in area like game development, system programming, AI, embedded systems, and more. This guide will introduce you to the essential elements of C++ from basic syntax and control structures to advanced concepts like object oriented programming (OOP) and memory management.&lt;br&gt;
&lt;strong&gt;Why Learn C++?&lt;/strong&gt;&lt;br&gt;
  C++ combines the efficiency of low-level programming with the flexibility of higher-level programming. It allows developers to write highly optimized code with fine-grained control over hardware resources. While it has a steeper learning curve, mastering C++ gives you deep insights into how computers work at a low level.&lt;br&gt;
&lt;strong&gt;1.Basic Syntax and Structure&lt;/strong&gt;&lt;br&gt;
C++ programs are built around functions and the main() function is the entry point. Here's the simplest C++ program - Hello World:&lt;br&gt;
 #include&lt;br&gt;
 using namespace std;&lt;br&gt;
 int main(){&lt;br&gt;
   cout&amp;lt;&amp;lt;"Hello WORLD!!:&amp;lt;&amp;lt;endl;&lt;br&gt;
   return 0;&lt;br&gt;
}&lt;br&gt;
&lt;strong&gt;Key concepts:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;#include :It includes the input-output stream library for printing to the console.&lt;/li&gt;
&lt;li&gt;Using namespace std; It makes the standard C++ library accessible without prefixing everything with std::&lt;/li&gt;
&lt;li&gt;cout: The object used to print output to the console.&lt;/li&gt;
&lt;li&gt;main():The entry point of the C++ program.
&lt;strong&gt;Data Types and variable:&lt;/strong&gt;
C++ is a statically typed language meaning you must specify the datatype of each variable.
Common Data types:&lt;/li&gt;
&lt;li&gt;int: integer(for example: int x = 5;)&lt;/li&gt;
&lt;li&gt;float, double: Floating-point numbers(for example: double pi=3.14159)&lt;/li&gt;
&lt;li&gt;char: Single character (for example: char letter = 'A';)&lt;/li&gt;
&lt;li&gt;string: Sequence of character (for Example: "Alice";)&lt;/li&gt;
&lt;li&gt;bool: Boolean value (true or false)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
int age = 25;&lt;br&gt;
float height = 5.9;&lt;br&gt;
char grade = 'A';&lt;br&gt;
bool is Active = true;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Control Structures&lt;/strong&gt;&lt;br&gt;
 Control structures in C++ allow you to control the flow of your program. The most common ones are conditionals (if, else) and loops (for, while).&lt;br&gt;
&lt;strong&gt;Example: Conditional Statements:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;int x = 10;&lt;br&gt;
if (x &amp;gt; 5) {&lt;br&gt;
    cout &amp;lt;&amp;lt; "x is greater than 5" &amp;lt;&amp;lt; endl;&lt;br&gt;
} else {&lt;br&gt;
    cout &amp;lt;&amp;lt; "x is less than or equal to 5" &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;br&gt;
&lt;strong&gt;Example: Loops:&lt;/strong&gt;&lt;br&gt;
for(int i = 0; i &amp;lt; 5; i++) {&lt;br&gt;
    cout &amp;lt;&amp;lt; "Iteration " &amp;lt;&amp;lt; i &amp;lt;&amp;lt; endl;&lt;br&gt;
}&lt;br&gt;
 &lt;strong&gt;Functions&lt;/strong&gt;&lt;br&gt;
  Functions in C++ allow you to break your code into smaller, reusable pieces. A function typically has a return type, a name, parameters, and a body.&lt;br&gt;
Example of Functions:&lt;br&gt;
int add(int a, int b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
    int result = add(3, 4);&lt;br&gt;
    cout &amp;lt;&amp;lt; "Sum: " &amp;lt;&amp;lt; result &amp;lt;&amp;lt; endl;&lt;br&gt;
    return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arrays and Strings&lt;/strong&gt;&lt;br&gt;
  Arrays are used to store multiple values of the same type. C++ also supports strings (a sequence of characters).&lt;br&gt;
Example of Array:&lt;br&gt;
int arr[5] = {1, 2, 3, 4, 5};&lt;br&gt;
cout &amp;lt;&amp;lt; "First element: " &amp;lt;&amp;lt; arr[0] &amp;lt;&amp;lt; endl;  // Output: 1&lt;br&gt;
&lt;strong&gt;Example Of String :&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;string name = "Ali";&lt;br&gt;
cout &amp;lt;&amp;lt; "Name: " &amp;lt;&amp;lt; name &amp;lt;&amp;lt; endl;&lt;br&gt;
** Pointers and Memory Management**&lt;br&gt;
C++ gives you direct over memory via pointers. Pointers are variables that store memory addresses of other variables.&lt;br&gt;
Example Of Pointers :&lt;br&gt;
int num = 10;&lt;br&gt;
int* ptr = &amp;amp;num;&lt;br&gt;&lt;br&gt;
cout &amp;lt;&amp;lt; "Value: " &amp;lt;&amp;lt; &lt;em&gt;ptr &amp;lt;&amp;lt; endl;&lt;br&gt;&lt;br&gt;
Dynamic Memory Allocation:&lt;br&gt;
In C++ memory can be dynamically allocated using "new" and "delete".&lt;br&gt;
int&lt;/em&gt; ptr = new int;&lt;br&gt;&lt;br&gt;
&lt;em&gt;ptr = 20;&lt;br&gt;
cout &amp;lt;&amp;lt; "Value: " &amp;lt;&amp;lt; *ptr &amp;lt;&amp;lt; endl;&lt;br&gt;&lt;br&gt;
delete ptr;&lt;br&gt;&lt;br&gt;
**Object-Oriented Programming (OOP) in C++ *&lt;/em&gt;&lt;br&gt;
C++ is an object- oriented language and its supports concepts like classes, inheritance, polymorphism and&lt;br&gt;
encapsulation.&lt;br&gt;
&lt;strong&gt;Example of Class and Object:&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;using namespace std;&lt;/p&gt;

&lt;p&gt;class Car {&lt;br&gt;
public:&lt;br&gt;
    string model;&lt;br&gt;
    int year;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void displayInfo() {
    cout &amp;lt;&amp;lt; "Model: " &amp;lt;&amp;lt; model &amp;lt;&amp;lt; ", Year: " &amp;lt;&amp;lt; year &amp;lt;&amp;lt; endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
    Car myCar;&lt;br&gt;
    myCar.model = "Tesla Model 3";&lt;br&gt;
    myCar.year = 2021;&lt;br&gt;
    myCar.displayInfo();&lt;br&gt;
    return 0;&lt;br&gt;
}&lt;br&gt;
&lt;strong&gt;Key OOP concept:&lt;/strong&gt;&lt;br&gt;
Classes: Blueprint for creating objects.&lt;br&gt;
Objects: Instances of classes that hold data and methods.&lt;br&gt;
Encapsulation: Hiding the internal details and only exposing the necessary functionality.&lt;br&gt;
&lt;strong&gt;Inheritance&lt;/strong&gt; &lt;br&gt;
inheritance allow one class to inherit properties and methods from another. &lt;br&gt;
&lt;strong&gt;Example of Inheritance:&lt;/strong&gt;&lt;br&gt;
class Vehicle {&lt;br&gt;
public:&lt;br&gt;
    string brand;&lt;br&gt;
    int speed;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void displayInfo() {
    cout &amp;lt;&amp;lt; "Brand: " &amp;lt;&amp;lt; brand &amp;lt;&amp;lt; ", Speed: " &amp;lt;&amp;lt; speed &amp;lt;&amp;lt; endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;class Car : public Vehicle {&lt;br&gt;
public:&lt;br&gt;
    int doors;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void displayCarInfo() {
    displayInfo();
    cout &amp;lt;&amp;lt; "Doors: " &amp;lt;&amp;lt; doors &amp;lt;&amp;lt; endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
    Car myCar;&lt;br&gt;
    myCar.brand = "Toyota";&lt;br&gt;
    myCar.speed = 180;&lt;br&gt;
    myCar.doors = 4;&lt;br&gt;
    myCar.displayCarInfo();&lt;br&gt;
    return 0;&lt;br&gt;
}&lt;br&gt;
&lt;strong&gt;Templates:&lt;/strong&gt;&lt;br&gt;
Templates allow you to write generic functions or classes that can work with any data type.&lt;br&gt;
Example: Template Function&lt;/p&gt;

&lt;p&gt;template &lt;br&gt;
T add(T a, T b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;int main() {&lt;br&gt;
    cout &amp;lt;&amp;lt; add(3, 4) &amp;lt;&amp;lt; endl;&lt;br&gt;&lt;br&gt;
    cout &amp;lt;&amp;lt; add(3.5, 4.5) &amp;lt;&amp;lt; endl;&lt;br&gt;&lt;br&gt;
    return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Exception Handling:&lt;/strong&gt;&lt;br&gt;
C++ provides exception handling using "try", "catch" and "throw" to deal with errors during runtime&lt;br&gt;
Example Of Exception Handling:&lt;/p&gt;

&lt;p&gt;try {&lt;br&gt;
    int x = 10, y = 0;&lt;br&gt;
    if (y == 0) {&lt;br&gt;
        throw "Division by zero error";&lt;br&gt;
    }&lt;br&gt;
    cout &amp;lt;&amp;lt; x / y &amp;lt;&amp;lt; endl;&lt;br&gt;
} catch (const char* msg) {&lt;br&gt;
    cout &amp;lt;&amp;lt; msg &amp;lt;&amp;lt; endl;  // Output: Division by zero error&lt;br&gt;
}&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
