Learning C++ from a Java programmer’s perspective and C++ Notes

This would be a post that I record my learning process on picking up some features of C++. The last time I programmed large programs in OO language was Java. But times have changed, my research forces me to work with C++ for performance reasons. As a result, I am picking up some experience in C++ along the way

This post covers the following topics

  • Virtual Functions
  • Abstract Classes
  • Header  Files
  • Input / Output
  • Friend Functions
  • Stack allocated arrays / objects
  • Assignment Operator
  • Reinterpret_cast vs static_cast (???, this one I am not certain)
  • Atomic Instructions for doubles and floats

 

 

 

  • Virtual Functions
    • Similarities

 

  • Abstract Class
    • Similarities:
      • Abstract classes can not be instantiated
      • Abstract classes can be subclassed
      • Abstract classes can have abstract (purely virtual) or concrete methods (with implementation)
      • Constructors and Destructors can NOT be purely virtual
    • Differences
      • Overview
        • In C++, there is not a clear separation between “abstract class” and “interface”. As a result, abstract classes in C++ are acting essentially as both “abstract class” and “interface” in Java.
      •  Java
        • References
        • Definition
          • An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
        •  Declaration
          • abstract classes are declared with the “abstract” keyword
            • public abstract class GraphicObject { abstract void draw(); }
      • C++
  • Header Files
    • This is something somewhat unique to C++. There are many things I had to deal with that arises from the use of header files. As a OO programmer from Java side, it took me a while
      • Guards on Header Files
        • Often times, I encounter error messages such as “redefinition of class xxx”. This is often due to repeated header files by including header A, and header B that also included header A. To deal with this, there are a bunch of ifdefs in the header files to make sure each header file is used only ONCE.
          • #ifndef HEADERA_H
            #define HEADERA_H
            
            
            
            
            #endif //HEADERA_H
      • Circular Dependences
        • Sometimes two classes are inter dependent. If we try to include both’s header files, we would get a “circular dependency”. To deal with this, once of the class file would redefine certain classes from the other file (forward declaration). The compiler would resolve the dependencies later.
      • Include (include statements)
        • Basically copies the content of the header files during a preprocessing step
          • “Both syntax forms cause that directive to be replaced by the entire contents of the specified include file.  “
        • Type 1: include “header_file” (search order is the following)
          • 1) In the same directory as the file that contains the #include statement.2) In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files.3) Along the path that’s specified by each /I compiler option.
          • 4) Along the paths that are specified by the INCLUDE environment variable.
        • Type 2: include <header_file> (search order is the following)
          • 1) Along the path that’s specified by each /I compiler option.2) When compiling occurs on the command line, along the paths that are specified by the INCLUDE environment variable.
        • References:
  • Input / Output
    • Similarities
      • StringBuffer (Java) and StreamBuffer (C++)
    • Differences / Notes
  • Friend functions
    • This is feature unique to C++
      • A friend function of a class is defined outside that class’ scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
    • Example
      • friend std::ostream &operator<<(std::ostream &, ClassA &);
      • Then in a different file outside of the class scope, we can override this << stream operator.
  • Stack allocated arrays and objects
    • In Java, arrays, objects has to be allocated using “new” keyword
    • In C++, you can allocate arrays and objects statically without the new keyword. A non-parameterized constructor would be called.
      • When the execution goes out of the function scope, it would call the destructor of the object. If there are heap allocated components within the scope of the function, the destructor would need to free them explicitly.
  • Assignment operator
  • Atomic instructions for doubles and floats
    • This needs to use reinterpret cast to int32 or int64
      • template<>
        bool compare_and_swap(float &x, const float &old_val, const float &new_val) {
          return __sync_bool_compare_and_swap(reinterpret_cast<uint32_t*>(&x),
                                              reinterpret_cast<const uint32_t&>(old_val),
                                              reinterpret_cast<const uint32_t&>(new_val));
        }
  • Next point

 

Useful Links

https://people.eecs.ku.edu/~jrmiller/Courses/JavaToC++/JavaToC++.html

 

 

 

 

Advertisements
This entry was posted in C++, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s