Skip to content

Instantly share code, notes, and snippets.

@Theartbug
Last active January 18, 2020 00:04
Show Gist options
  • Save Theartbug/48617c1bf4b6a9a208985d98afb63904 to your computer and use it in GitHub Desktop.
Save Theartbug/48617c1bf4b6a9a208985d98afb63904 to your computer and use it in GitHub Desktop.
C++ classes

Classes

data abstraction

  • allow you to preform data abstraction and create new dataTypes in an Object Oriented fashion
  • a place to hold data and manage operations on that data
    • holds functions that a consumer can call
  • class and struct both have the same capabilities in c++
    • have members, functions, constructors
    • different defaults:
      • class all members are private
      • struct all members are public
  • class is a dataType
    • have data members and member functions
    • should be placed in a .h file
  • object is an instance of a class, a variable
  • format:
      class nameType
      {
      	public:
      		// place member functions (prototype!)
      	private:
      		// place private data members
      }; // need a semicolon
    

modular abstraction

  • extra files to better organize code
  • can come in .cpp, .o, .h
  • .cpp where functions go
    • implementation of code
    • #include "header.cpp"
    • a single main() function for all .cpp files
  • .h is a header file that contains a class definition
    • no need to compile with g++
      • if you do, you will created a hidden .gch file and changes to .h file will not be made
      • can find these with ls -a
    • #include
    • constants
    • structs
    • prototypes
    • class interfaces
    • NOT function body
    • NOT #include .cpp files
  • .o is a file that contains a pre-compiled object
    • this allows the object to be used by other files
    • hides the code secrets of the object
    • not everything is open-source in industry
  • <> with #includes means system directory
  • g++ *.cpp compile with multiple files
    • only works if all files are in the same directory
    • compiler does not need a file to be named anything in particular if it contains main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment