Table of Contents
oop with cpp
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=enUS LCALL=en_US
- C/C++ General
- Paths and Symbols:
- /usr/include /usr/include/linux /usr/include/c++/x.x.x usr/local/include
Include
- <iostream.h>. Only <string.h> (should be enough) and <strings.h> (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
C++ Inheritance
The example:
1 #include <iostream> 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"<<endl; 12 } 13 }; 14 15 class GetTimeCommandPath : public CommandPath { 16 public: 17 GetTimeCommandPath(int stage_) : CommandPath(stage_) {} 18 int execute(){ 19 cout << "this is it "<< stage<<endl; 20 }; 21 }; 22 23 class GetSecCommanPath : public GetTimeCommandPath { 24 public: 25 GetSecCommanPath(int stage_) : GetTimeCommandPath(stage_) {} 26 GetSecCommanPath(int stage_, int stage2_) {} 27 28 }; 29 30 31 int main() { 32 GetTimeCommandPath gtcp(9999); 33 gtcp.execute(); 34 35 36 GetSecCommanPath gscp(8888); 37 gscp.execute(); 38 }
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
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.