Multiple-Inheritance

10.1. Separate compilation

In many cases the programmers want to secretly hide their codes from others. There are many ways to do this. One of the hidding techniques is separate compilation. For example, a programmer want to hide all codes in the class Obj, he can follow the example below.

First, he create two files. The header file

        //Obj.h
        class Obj{
            public:
            int x;
            Obj(int);
            void inc(int);
        };

The cpp file

        //Obj.cpp
        #include "Obj.h"
        Obj::Obj(int _x=0){
            x=_x;
        }
        void Obj::inc(int y){
            x+=y;
        }

The main file

        //main.cpp
        #include "Obj.h"
        #include<iostream>
        using namespace std;
        int main(){
            Obj a(3);
            a.inc(2);
            cout<<a.x<<endl;
            return 0;
        }

Compilation

        g++ -c Obj.cpp
        g++ -c main.cpp
        g++ -o final main.o Obj.o
        ./final
        g++ -c Obj.cpp
        g++ -o final main.cpp Obj.o
        ./final

g++ options

          --help                   Display this information
          --version                Display compiler version information
          -time                    Time the execution of each subprocess
          -std=<standard>          Assume that the input sources are for <standard>
          --sysroot=<directory>    Use <directory> as the root directory for headers
                                   and libraries
          -B <directory>           Add <directory> to the compiler's search paths
          -v                       Display the programs invoked by the compiler
          -E                       Preprocess only; do not compile, assemble or link
          -S                       Compile only; do not assemble or link
          -c                       Compile and assemble, but do not link
          -o <file>                Place the output into <file>
          -Wa                      All option tells the compiler to show warnings 

        Options starting with -g, -f, -m, -O, -W, or --param are automatically
         passed on to the various sub-processes invoked by g++.  In order to pass
         other options on to these processes the -W<letter> options must be used.

        For bug reporting instructions, please see:
        <http://gcc.gnu.org/bugs.html>.

        C:\Users\Wasit>

10.2. multiple inheritance

        #include<iostream>
        using namespace std;
        class Alpha{
        public:
          Alpha(){ cout<<"construct Alpha\n";}
          ~Alpha(){ cout<<"destroy Alpha\n";}
        };
        class Beta{
        public:
          Beta(){ cout<<"construct Beta\n";}
          ~Beta(){ cout<<"destroy Beta\n";}
        };
        class Chi: public Beta, public Alpha{ //private by default
        public:
          Chi(){ cout<<"construct Chi\n";}
          ~Chi(){ cout<<"destroy Chi\n";}
        };
        int main(){
            Chi c;
            return 0;
        }

output

        Running /home/ubuntu/workspace/code10_20_multi_inher.cpp
        construct Beta
        construct Alpha
        construct Chi
        destroy Chi
        destroy Alpha
        destroy Beta

10.3. multi-level inheritance

ย้อนกลับไปดู section 9

results matching ""

    No results matching ""