<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hixcoder on Medium]]></title>
        <description><![CDATA[Stories by Hixcoder on Medium]]></description>
        <link>https://medium.com/@hixcoder?source=rss-ee1594bf5572------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*qsS-GzFu37n7K9iX</url>
            <title>Stories by Hixcoder on Medium</title>
            <link>https://medium.com/@hixcoder?source=rss-ee1594bf5572------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:57:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hixcoder/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Types of casts in C++]]></title>
            <link>https://medium.com/@hixcoder/types-of-casts-in-c-9a8f28ad8a4a?source=rss-ee1594bf5572------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a8f28ad8a4a</guid>
            <dc:creator><![CDATA[Hixcoder]]></dc:creator>
            <pubDate>Mon, 26 Dec 2022 11:35:07 GMT</pubDate>
            <atom:updated>2022-12-26T12:34:49.298Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*8TwdYPkqg24NZ5438bw1gg.jpeg" /></figure><p>In C++, a cast is a way to explicitly convert a value from one type to another. There are several types of casts that you can use in C++, each with its own specific purpose and behavior.</p><p><a href="https://www.youtube.com/watch?v=HlNVgmvX1EI"><strong>Static cast</strong></a>: This is the most basic type of cast. It can be used to convert a value from one type to another, as long as the types are compatible. For example, you can use a static cast to convert an int to a double, or a char* to a void*. Static casts are the most common type of cast, and are often used to explicitly convert values when the compiler cannot do it automatically.</p><pre>int x = 42;<br>double y = static_cast&lt;double&gt;(x);  // y is now 42.0<br><br>class Base {};<br>class Derived : public Base {};<br><br>Base* b = new Base();<br>Derived* d = static_cast&lt;Derived*&gt;(b);  // Convert b to a pointer to Derived</pre><p><a href="https://www.youtube.com/watch?v=wE4beL95pIo"><strong>Dynamic cast</strong></a>: This type of cast is used to perform runtime type checking on pointers and references to objects of polymorphic types (types that have virtual functions). Dynamic cast allows you to convert a pointer or reference to a base class to a pointer or reference to a derived class, and checks at runtime that the object being pointed to or referenced is actually an instance of the derived class. If the object is not an instance of the derived class, dynamic_cast will return a null pointer or null reference.</p><pre>class Base {<br> public:<br>  virtual ~Base() {}<br>};<br><br>class Derived : public Base {};<br><br>Base* b = new Derived();<br>Derived* d = dynamic_cast&lt;Derived*&gt;(b);  // Convert b to a pointer to Derived<br>if (d != nullptr) {<br>  // b points to a Derived object<br>} else {<br>  // b does not point to a Derived object<br>}</pre><p><a href="https://www.youtube.com/watch?v=2_-BS9UI0po"><strong>Const cast</strong></a>: This type of cast is used to remove or add the constness of an object. It can be used to cast away the constness of a value, allowing you to modify a const object.</p><pre>const int x = 42;<br>int* p = const_cast&lt;int*&gt;(&amp;x);  // Remove constness of x and convert it to an int*<br>*p = 43;  // Modify the value of x (not recommended)<br><br>const char* str = &quot;Hello&quot;;<br>char* non_const_str = const_cast&lt;char*&gt;(str);  // Remove constness of str and convert it to a char*<br>non_const_str[0] = &#39;H&#39;;  // Modify the first character of str (not recommended)</pre><p><a href="https://www.youtube.com/watch?v=JBvEfB_F66o"><strong>Reinterpret cast</strong></a>: This type of cast is used to interpret the bits of an object in a different way. It can be used to convert a pointer to one type of object to a pointer to a different type of object, or to convert an integer or floating-point value to a pointer or vice versa. Reinterpret cast is a low-level cast that should be used with caution, as it can lead to undefined behavior if the types being cast are not compatible.</p><pre>int x = 42;<br>void* p = reinterpret_cast&lt;void*&gt;(x);  // Convert x to a void*<br><br>class Base {};<br>class Derived : public Base {};<br><br>Derived d;<br>Base* b = reinterpret_cast&lt;Base*&gt;(&amp;d);  // Convert &amp;d to a pointer to Base<br><br>int* pi = reinterpret_cast&lt;int*&gt;(p);  // Convert p back to an int*<br>std::cout &lt;&lt; *pi &lt;&lt; std::endl;  // Outputs: 42</pre><blockquote>bonus:</blockquote><p><strong>C-style cast</strong>: This type of cast is a legacy cast that is included in C++ for compatibility with C. It is similar to a static cast, but is more permissive and can be more prone to error. It is generally recommended to use the other types of casts instead of a C-style cast, unless you are working with legacy code that uses C-style casts.</p><pre>int x = 42;<br>double y = (double)x;  // y is now 42.0<br><br>class Base {};<br>class Derived : public Base {};<br><br>Base* b = new Base();<br>Derived* d = (Derived*)b;  // Convert b to a pointer to Derived</pre><p>In summary, C++ provides several types of casts that you can use to explicitly convert values from one type to another. It is important to choose the right type of cast for the task at hand, as using the wrong type of cast can lead to errors or undefined behavior.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a8f28ad8a4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The what() Function in C++]]></title>
            <link>https://medium.com/@hixcoder/the-what-function-in-c-2b1f2bacac0c?source=rss-ee1594bf5572------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b1f2bacac0c</guid>
            <dc:creator><![CDATA[Hixcoder]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 13:59:02 GMT</pubDate>
            <atom:updated>2022-12-23T14:03:26.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*T4Gya8OdpimfOEjwLmosvQ.jpeg" /></figure><p>The what() function is a virtual function defined in the std::exception class, which is the base class for all exception classes in C++. It returns a string that describes the error or exceptional event that occurred.</p><h3>Using the what() Function</h3><p>The what() function is used to get a description of an error or exceptional event that occurred during the execution of a program. It is commonly used in exception handling to provide more information about the error or exceptional event that occurred.</p><p>Here is an example of using the what() function in C++:</p><pre>#include &lt;iostream&gt;<br>#include &lt;exception&gt;<br><br>class DivisionByZeroException : public std::exception {<br> public:<br>  virtual const char* what() const throw() {<br>    return &quot;Division by zero&quot;;<br>  }<br>};<br><br>double divide(double a, double b) {<br>  if (b == 0) {<br>    throw DivisionByZeroException();<br>  }<br>  return a / b;<br>}<br><br>int main() {<br>  try {<br>    std::cout &lt;&lt; divide(5, 0) &lt;&lt; std::endl;<br>  } catch (DivisionByZeroException&amp; e) {<br>    std::cerr &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;<br>  }<br><br>  return 0;<br>}</pre><p>In this example, the divide() function throws a DivisionByZeroException if the divisor is zero. The exception is caught in the catch block, which prints an error message to the standard error stream using the what() function of the DivisionByZeroException class.</p><h3>Using the what() function has several advantages over simply printing a string:</h3><ol><li>It allows you to centralize the error handling code in the catch block, rather than having to duplicate the error message in multiple places in your code.</li><li>It allows you to customize the error message for different exception types. For example, you might have different exception classes for different types of errors, and each class could have its own implementation of the what() function that returns a different error message.</li><li>It allows you to use the std::exception class or one of its derived classes as a catch type, rather than having to catch each exception class individually. This can make your code more flexible and easier to maintain.</li></ol><p>Here is an example of how you could use the what() function to handle multiple exception types:</p><pre>#include &lt;iostream&gt;<br>#include &lt;exception&gt;<br><br>class DivisionByZeroException : public std::exception {<br> public:<br>  virtual const char* what() const throw() {<br>    return &quot;Division by zero&quot;;<br>  }<br>};<br><br>class InvalidInputException : public std::exception {<br> public:<br>  virtual const char* what() const throw() {<br>    return &quot;Invalid input&quot;;<br>  }<br>};<br><br>double divide(double a, double b) {<br>  if (b == 0) {<br>    throw DivisionByZeroException();<br>  }<br>  if (a &lt; 0 || b &lt; 0) {<br>    throw InvalidInputException();<br>  }<br>  return a / b;<br>}<br><br>int main() {<br>  try {<br>    std::cout &lt;&lt; divide(5, 0) &lt;&lt; std::endl;<br>  } catch (std::exception&amp; e) {<br>    std::cerr &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;<br>  }<br><br>  return 0;<br>}</pre><p>In this example, the divide() function throws either a DivisionByZeroException or a InvalidInputException, depending on the input. The exception is caught in the catch block, which prints an error message to the standard error stream using the what() function. This allows you to handle both exception types in the same way, without having to catch each exception type individually.</p><h4>check :</h4><blockquote><a href="https://medium.com/@hixcoder/exception-classes-in-c-b57ecc8a7f2d">Exception Classes in C++</a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b1f2bacac0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exception Classes in C++]]></title>
            <link>https://medium.com/@hixcoder/exception-classes-in-c-b57ecc8a7f2d?source=rss-ee1594bf5572------2</link>
            <guid isPermaLink="false">https://medium.com/p/b57ecc8a7f2d</guid>
            <dc:creator><![CDATA[Hixcoder]]></dc:creator>
            <pubDate>Fri, 23 Dec 2022 10:43:38 GMT</pubDate>
            <atom:updated>2022-12-23T10:43:38.938Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Beugj6l6ifqG4iFcXSBGUw.png" /></figure><p>In C++, an exception class is a class that is used to represent an exception, which is a special kind of object that is thrown when an error or exceptional condition occurs in a program. Exception classes allow you to define custom types of exceptions that can be thrown and caught in your code.</p><p>To use an exception class, you first need to define the class by deriving it from the standard exception class std::exception. Your exception class should have at least one constructor that takes one or more arguments that describe the details of the exception, and may also have other methods that allow the exception to be examined or manipulated.</p><p>Here is an example of a simple exception class:</p><pre>#include &lt;exception&gt;<br>#include &lt;string&gt;<br><br>class MyException : public std::exception {<br> public:<br>  MyException(const std::string&amp; message) : message_(message) {}<br>  const char* what() const throw() { return message_.c_str(); }<br><br> private:<br>  std::string message_;<br>};</pre><p>This exception class has a single constructor that takes a string argument containing a message, and a what() method that returns the message as a C-style string.</p><p>To throw an exception, you can use the throw keyword followed by an instance of your exception class. For example:</p><pre>void foo() {<br>  throw MyException(&quot;An error occurred in foo&quot;);<br>}</pre><p>To catch an exception, you can use a try-catch block. The try block encloses the code that may throw an exception, and the catch block handles the exception if it is thrown. The catch block can access the exception object through its parameter.</p><p>Here is an example of a try-catch block that catches an exception of type MyException:</p><pre>try {<br>  foo();<br>} catch (const MyException&amp; e) {<br>  std::cerr &lt;&lt; &quot;Error: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;<br>}</pre><p>In this example, if the foo function throws an exception, it will be caught by the catch block, which prints the error message to the standard error stream.</p><p>You can also define multiple catch blocks to handle different types of exceptions. For example:</p><pre>try {<br>  foo();<br>} catch (const MyException&amp; e) {<br>  std::cerr &lt;&lt; &quot;MyException: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;<br>} catch (const std::exception&amp; e) {<br>  std::cerr &lt;&lt; &quot;std::exception: &quot; &lt;&lt; e.what() &lt;&lt; std::endl;<br>}</pre><p>In this example, the first catch block handles exceptions of type MyException, and the second catch block handles any other exceptions of type std::exception or a derived type.</p><p>It is important to note that exception classes should be used sparingly, and only for exceptional conditions that cannot be handled in any other way. Exceptions should not be used for normal control flow or for returning simple error codes.</p><p>I hope this article has helped you understand exception classes in C++. Let me know if you have any questions or need further clarification.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b57ecc8a7f2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Difference between deep and shallow copy in cpp]]></title>
            <link>https://medium.com/@hixcoder/difference-between-deep-and-shallow-copy-in-cpp-1ff9cca2e3bc?source=rss-ee1594bf5572------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ff9cca2e3bc</guid>
            <dc:creator><![CDATA[Hixcoder]]></dc:creator>
            <pubDate>Tue, 20 Dec 2022 13:54:22 GMT</pubDate>
            <atom:updated>2022-12-20T13:56:14.639Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p31gZgetJScAthnjaI4KGg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bMDYVps4YVLTCMk4ht_krA.jpeg" /></figure><p>In C++, a deep copy is a copy of an object that creates a new object with a new memory address, and copies the values of all of the object’s data members to the new object. A shallow copy is a copy of an object that creates a new object with a new memory address, but does not copy the values of the object’s data members to the new object. Instead, the new object points to the same memory address as the original object.</p><p>Here’s an example of deep and shallow copy in C++:</p><pre>#include &lt;iostream&gt;<br><br>class A {<br> public:<br>  int* data;<br>  A(int x) {<br>    data = new int(x);<br>  }<br>  A(const A&amp; other) {  // Deep copy constructor<br>    data = new int(*other.data);<br>  }<br>  ~A() {<br>    delete data;<br>  }<br>};<br><br>class B {<br> public:<br>  int* data;<br>  B(int x) {<br>    data = new int(x);<br>  }<br>  B(const B&amp; other) {  // Shallow copy constructor<br>    data = other.data;<br>  }<br>  ~B() {<br>    delete data;<br>  }<br>};<br><br>int main() {<br>  A a1(10);<br>  A a2 = a1;  // Deep copy<br>  std::cout &lt;&lt; a1.data &lt;&lt; &quot; &quot; &lt;&lt; a2.data &lt;&lt; std::endl;  // Outputs different memory addresses<br><br>  B b1(20);<br>  B b2 = b1;  // Shallow copy<br>  std::cout &lt;&lt; b1.data &lt;&lt; &quot; &quot; &lt;&lt; b2.data &lt;&lt; std::endl;  // Outputs the same memory address<br><br>  return 0;<br>}</pre><p>In this example, we have two classes: A and B. The A class has a deep copy constructor that creates a new object with a new memory address and copies the value of the data member to the new object. The B class has a shallow copy constructor that creates a new object with a new memory address, but does not copy the value of the data member to the new object. Instead, the new object points to the same memory address as the original object.</p><p>When we create a deep copy of an object of type A, a new object is created with a new memory address and the value of the data member is copied to the new object. When we create a shallow copy of an object of type B, a new object is created with a new memory address, but the value of the data member is not copied to the new object. Instead, the new object points to the same memory address as the original object.</p><p>Deep and shallow copy have different trade-offs in terms of performance and memory usage. Deep copy requires more time and memory to create a new object, but it is generally safer and more flexible because it allows the original object and the copy to be modified independently. Shallow copy is faster and requires less memory, but it can lead to problems if the original object is modified, because the copy will also be modified.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ff9cca2e3bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The diamond problem with inheritance in cpp]]></title>
            <link>https://medium.com/@hixcoder/the-diamond-problem-with-inheritance-in-cpp-2bd99f1f9380?source=rss-ee1594bf5572------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bd99f1f9380</guid>
            <dc:creator><![CDATA[Hixcoder]]></dc:creator>
            <pubDate>Sun, 18 Dec 2022 14:48:38 GMT</pubDate>
            <atom:updated>2022-12-18T14:49:13.987Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aV2NMJIt2N9GWFTtNtdDhw.jpeg" /></figure><p>The diamond problem occurs in a class inheritance hierarchy when two or more classes inherit from a common base class, and this base class has a member with the same name as one of the members in the derived classes. In this case, the compiler is unable to determine which member to use, causing a conflict.</p><p>Here is an example of a class inheritance hierarchy that demonstrates the diamond problem:</p><pre>class A {<br> public:<br>  int x;<br>  void foo() { cout &lt;&lt; &quot;A::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class B: public A {<br> public:<br>  void foo() { cout &lt;&lt; &quot;B::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class C: public A {<br> public:<br>  void foo() { cout &lt;&lt; &quot;C::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class D: public B, public C {<br> public:<br>  void foo() { cout &lt;&lt; &quot;D::foo()&quot; &lt;&lt; endl; }<br>};</pre><p>In this example, class D has two copies of the foo() function, one inherited from B and one inherited from C. If we try to call foo() on an object of type D, the compiler will not know which version of foo() to use, causing a conflict.</p><p>One way to avoid the diamond problem is to use virtual inheritance. With virtual inheritance, a class can inherit from a base class in such a way that only one copy of the base class is used by all of the derived classes.</p><p>Here is the same example as above, but using virtual inheritance to avoid the diamond problem:</p><pre>class A {<br> public:<br>  int x;<br>  void foo() { cout &lt;&lt; &quot;A::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class B: virtual public A {<br> public:<br>  void foo() { cout &lt;&lt; &quot;B::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class C: virtual public A {<br> public:<br>  void foo() { cout &lt;&lt; &quot;C::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class D: public B, public C {<br> public:<br>  void foo() { cout &lt;&lt; &quot;D::foo()&quot; &lt;&lt; endl; }<br>};</pre><p>With virtual inheritance, class D will only have one copy of the foo() function, which it can inherit from either B or C. This avoids the conflict caused by the diamond problem.</p><p>Another way to avoid the diamond problem is to use interfaces. An interface is a class that contains only pure virtual functions, which are functions that have no implementation and are meant to be overridden by derived classes. By using interfaces, a class can inherit from multiple base classes without creating a conflict.</p><p>Here is an example of how to use interfaces to avoid the diamond problem:</p><pre>class IA {<br> public:<br>  virtual void foo() = 0;<br>};<br><br>class IB: public IA {<br> public:<br>  void foo() { cout &lt;&lt; &quot;IB::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class IC: public IA {<br> public:<br>  void foo() { cout &lt;&lt; &quot;IC::foo()&quot; &lt;&lt; endl; }<br>};<br><br>class D: public IB, public IC {<br> public:<br>  void foo() { cout &lt;&lt; &quot;D::foo()&quot; &lt;&lt; endl; }<br>};</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bd99f1f9380" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>