====== oop with cpp ====== {{tag>cpp oop}} ===== Eclipse etc ===== Problem: std::string std::cout not resolved. ==== Conf project preferences ==== * maketools project * Preferences: * C/C++ Build * Discovery options: enalbe "automate discovery of paths and symbols" (both C and C++). * Environment: add LANG=en_US LC_ALL=en_US * C/C++ General * Paths and Symbols: * /usr/include /usr/include/linux /usr/include/c++/x.x.x usr/local/include ==== Include ==== * . Only (should be enough) and (additional string manipulations bcopy..) don't work. ==== Add a default project of same type so global settings are sorted out ==== otherwise try this: http://stackoverflow.com/questions/10803685/eclipse-cdt-symbol-cout-could-not-be-resolved ===== Basic ===== ==== enum declarations ==== source: http://www.enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/ There are two kinds of enum type declarations. One kind creates a named type, as in enum MyEnumType { ALPHA, BETA, GAMMA }; If you give an enum type a name, you can use that type for variables, function arguments and return values, and so on: enum MyEnumType x; /* legal in both C and C++ */ MyEnumType y; // legal only in C++ The other kind creates an unnamed type. This is used when you want names for constants but don't plan to use the type to declare variables, function arguments, etc. For example, you can write enum { HOMER, MARGE, BART, LISA, MAGGIE }; ==== Reuse value-names ==== C++ doesn't allow reuse names inside enums. e.g.: enum WE { HOMER, MARGE, BART, LISA, MAGGIE }; enum THEY { HOMER, MOE }; <-- already defined And also WE::HOMER notation doesn't exist before C++11. **The solution** namespace ns1 { enum WE { HOMER, MARGE, BART, LISA, MAGGIE };} namespace ns2 { enum THEY { HOMER, MOE };} Enum value can be accessed like this: ns1::WE var_x; var_x = ns1::HOMER; **Nightmare** ===== Interfaces and abstract classes ===== * http://stackoverflow.com/questions/12854778/abstract-class-vs-interface-in-c * http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/ ====== C++ Inheritance ====== The example: 1 #include 2 3 using namespace std; 4 5 class CommandPath { 6 protected: 7 int stage; 8 public: 9 int virtual execute() = 0; 10 CommandPath(int stage_) : stage(stage_) { 11 cout<<"i'm doing it"< ===== No default constructor ===== When compile the code, there is an error because for GetSecCommanPath there is no respective Constructor in Base class with 2 arguments. So when calling the GetSecCommanPath(int, int), a default Constructor of the base class is created first, and there is none. ===== Headline ===== * source: http://stackoverflow.com/questions/9907722/why-is-default-constructor-called-in-virtual-inheritance When Base is virtually inherited by F1, F2 and F3 inherits F1, F2. Creating F3 will cause constructor of Base to be called. Because of the 'virtual' keyword.