Prabu Subroto wrote:
I am still confused. Sorry I am a really beginner in GNU C++. 1. May I make 2 classes in one file?
Yes.
2. header is somewhat like this : "#include <iostream.h>.
Typically you would use #include<iostream> rather than #include<iostream.h> Both work, but gcc generates a warning with the second. The difference is subtle. If you use the new versions, <iostream>, <cstdlib> rather than the old ones <iostream.h>, <stdlib.h>, you get standard library functions in a namespace called std. The advantage is that you can functions and classes with the same names as the standard library functions. In practice there are two ways to use the namespace: 1. (java-style) Always qualify standard library functions with std:: as in std::cout << "Hello." << std::endl I prefer this style. 2. (C-style) Use the using directive to make the standard library available whenever you want it. The typical way to do this is to start a file #include<iostream> using namespace std; Then any standard library function becomes available globally without the std:: qualifier. You can also make the standard library available locally, e.g. double function( double x ){ // OK using namespace std; return x + sqrt( x ); } double another_function( double x ){ // error - no function sqrt() in current scope. return x - sqrt( x ); }
I am confused how should the header file of my "menu" class for registering "samba" class in the "menu" class?
Could you give me an example of the header file of my "menu" class please....?
Typical structure: samba.h: #include<iostream> #ifndef SAMBAH #define SAMBAH class samba { public: samba(); // more class declarations }; #endif samba.cc #include"samba.h" samba::samba(){ // initialisation } // other class definitions. menu.h: #include<iostream> #include"samba.h" #ifndef MENUH #define MENUH class menu { public: menu(); // more class declarations }; #endif etc. ++ How does this work? The samba.h (and menu.h) classes _declare_ the class, but don't _define_ its functions (except maybe some inline ones): that is the .h files don't contain anything like samba::samba(){ ... } This is important because you're only allowed to define a function once. But you can declare it as many times as you like. The #ifndef SAMBAH #define SAMBAH ... #endif part isn't strictly necessary but stops the compiler from reading a header file more than once. It's actually OK to read it more than once because it constains only declarations and not definitions, but compilation will be faster if the compiler doesn't make repeated checks for consistency. menu.h #includes samba.h and so gets to know about the samba class (without introducing any definitions) Typically you compile this bit by bit: g++ samba.cc -c compiles the samba class to samba.o g++ menu.cc -c compiles the menu class to menu.o This becomes advantageous when the whole program is big because you don't have to recompile all the code in the individual .o files when you make a small change. Put it all together with something like g++ -o program main.cc samba.o menu.o where main.cc is the file containing main(). -- JDL