Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ inheritance and derived (induction principle)     - Linux C source code (sockettype: socket descriptor determination function) (Programming)

- CentOS 6.5 install Firefox (Linux)

- Initialization and starting process of Linux (Linux)

- Custom Android UI template Comments (Programming)

- Linux file system data file deletion problem space is not freed (Database)

- OpenJDK 7 compiled under Ubuntu 14.04.3 64-bit (Linux)

- Grub2 Boots the openSUSE installation image (Linux)

- Quagga use authentication on strengthening BGP session security (Linux)

- iTerm - let your command line can also be colorful (Linux)

- Linux daemon (Linux)

- Ubuntu 14.04 installed VirtualBox 4.3 appears vboxdrv: Unknown symbol mcount (Linux)

- Learn about EditText little depth (Programming)

- Denyhosts prevent hackers using SSH scanning (Linux)

- See how --nand flash timing diagram of a read operation Comments (Programming)

- There are more than one server, there is reason to try cloud operating system (Server)

- APT-mirror using a four-step configuration Ubuntu local depot (Linux)

- 12 kinds of detection of CPU information on a Linux system tools (Linux)

- Linux Network Programming - raw socket Example: sending a UDP packet (Programming)

- Linux operating system security management skills notes (Linux)

- Delete specific files using bash directory under Linux (Linux)

 
         
  C ++ inheritance and derived (induction principle)
     
  Add Date : 2017-04-19      
         
         
         
  1. C ++ inheritance and different java, java follow single inheritance, but the interface for its lack of java to do a good make up. C ++ is more flexible for multiple inheritance. That is, a C ++ class can inherit the properties of the N class.

2. For inheritance:

There are three: public, private, protect, for public inherited class, its public members are still members of the public, private members still private members.

For protect, private, there are limitations, like a water pipe, water pipe is the largest number of public, there is no restriction on the flow of water. Protection of water, a medium number, for the large flow of water flow so that it becomes a medium for the medium does not limit. Private water pipes, it is the smallest number, for more than a private water restrictions to all of the private standard pipe. Where private inheritance, is actually a measure of sterilization. It is that in future inheritance would not make much sense.

3. For inheritance on constructors and destructors in the original order of induction:

Look at the code:

#include < iostream>
using namespace std;

class Base1 {

public:
    Base1 () {
        cout < < "Default Base1" < < endl;
    }
    Base1 (int i) {
        cout < < "Base1" < < i < < endl;
    }
    ~ Base1 () {
        cout < < "Base1 destructor" < < endl;
    }
};

class Base2 {

public:
    Base2 () {
        cout < < "Default Base2" < < endl;
    }
    ~ Base2 () {
        cout < < "Base2 destructor" < < endl;
    }
    Base2 (int i) {
        cout < < "Base2" < < i < < endl;
    }
};
class Base3 {

public:
    Base3 () {
        cout < < "Default Base3" < < endl;
    }
    ~ Base3 () {
        cout < < "Base3 destructor" < < endl;
    }
    Base3 (int i) {
        cout < < "Base3" < < i < < endl;
    }

};


class Derived: public Base1, public Base2, public Base3 // (1) began construction here first from left to right
    // Destructor is from right to left
{

public:
    Derived () {
        cout < < "Default Derived" < < endl;
    }
    ~ Derived () {
        cout < < "Derived destructor" < < endl;
    }
    Derived (int a, int b, int c, int d)
        : Men2 (b), Base1 (a), Base3 (c), Base2 (d), men1 (b) {
        cout < < "Derived" < < endl;
    };

private:
    // Constructor from left to right
    Base3 men3;
    Base2 men2;
    Base1 men1;

    // Destructor destructor is up from the bottom, the first Base 1,2,3
};

int main (void) {

    Derived obj (1,2,3,4);
    return 0;
    
}

 result:

Base11
Base24
Base33
Default Base3
Base22
Base12
Derived
Derived destructor
Base1 destructor
Base2 destructor
Base3 destructor
Base3 destructor
Base2 destructor
Base1 destructor
Press any key to continue...

4. The above description is for single inheritance, multiple inheritance if it is, then the routine, we can easily understand the order of execution is, but if it is virtual inheritance, how will its execution order?

#include < iostream>
using namespace std;

class Boss {

public:
    Boss () {
        cout < < "this is Boss's constructor!" < < endl;
    };
    Boss (int i) {
        cout < < "this is Boss's constructor!" \
            < < "Moneny =" < < i < < endl;
    }

    void show () {
      cout < < "sharpen the sword, cut the demon soul, frosted time, the magic blade" < < endl;
    }
    virtual ~ Boss () {
        cout < < "this is Boss's xigou function!" < < endl;
    }; // Virtual destructor
};

// Waiter
class xiao_er: virtual public Boss
{
 public:
    xiao_er () {
        cout < < "this is xiao_er's constructor!" < < endl;
    }
    xiao_er (int i): Boss (i) {
        cout < < "this is xiao_er's constructor!" \
            < < "Moneny =" < < i < < endl;
    }
    virtual ~ xiao_er () {
        cout < < "this is xiao_er's xigou function!" < < endl;
    }
    void show () {
        cout < < "I'm a waiter, Keguan!" < < endl;
    }
};

@ King for two
class er_xiao: virtual public Boss \
    , Virtual public xiao_er / * In fact, this may be omitted here, but here is to write the code and write code * /
{
 public:
    er_xiao () {
        cout < < "this is er_xiao's constructor!" < < endl;
    }
    er_xiao (int i): \
    Boss (i), xiao_er (i + 1)
    {
        cout < < "this is er_xiao's constructor!" \
            < < "Moneny =" < < i < < endl;
    }
    virtual ~ er_xiao () {
        cout < < "this is er_xiao's xigou function!" < < endl;
    }
    void show () {
        cout < < "I was king for two, for the bad guys to lead the way for two of the king!" < < endl;
    }
};

// Heavenly VIP staff
class VIP_em: virtual public Boss
{

public:
    VIP_em () {
        cout < < "this is VIP_em's constructor!" < < endl;
    }

    VIP_em (int i): \
    Boss (i)
    {
        cout < < "this is VIP_em's constructor!" \
            < < "Moneny =" < < i < < endl;
    }
    virtual ~ VIP_em () {
        cout < < "this is VIP_em's xigou function!" < < endl;
    }
    void show () {
        cout < < "I am a VIP, I have the privilege!" < < endl;
    }
};

//bad boy
class stupid_kid: virtual public VIP_em \
    , Virtual public xiao_er, \
    virtual public er_xiao
{
 public:
    stupid_kid () {
        cout < < "this is stupid_kid's constructor!" < < endl;
    }

    stupid_kid (int i): \
        VIP_em (i), xiao_er (12), er_xiao (13), xe (i)
    {
        cout < < "this is stupid_kid's constructor!" \
            < < "Moneny =" < < i < < endl;
    }
    ~ Stupid_kid () {
      cout < < "this is stupid_kid's xigou function!" < < endl;
    }

    void show () {
        cout < < "I bear a child, milo, sorghum, hug!" < < endl;
    }
private:
    VIP_em vi;
    xiao_er xe;
    er_xiao ex;
};

int main () {

    stupid_kid st (100);
    // Parent class functions are covered
    st.show ();
    // How to call a parent class is a mandatory.
    ((Boss) st) .show ();
    
    // Stupid_kid * pt = & st;
    // Stupid_kid & sb = st;
    // Pt-> show ();
    // ((Boss) sb) .show ();
    return 0;
}


Results:

this is Boss's constructor!
this is VIP_em's constructor! moneny = 100
this is xiao_er's constructor! moneny = 12
this is er_xiao's constructor! moneny = 13

------------- This section is divided into bear children inherit constructors

Here is the constructor private variables

this is Boss's constructor!
this is VIP_em's constructor!

------ Private variables Vip_em call no-argument constructor
this is Boss's constructor! moneny = 100
this is xiao_er's constructor! moneny = 100

------ Private variables xiao_er call has parameters constructor

this is Boss's constructor!
this is xiao_er's constructor!

------ Private variables xiao_er call no-argument constructor
this is er_xiao's constructor!

this is stupid_kid's constructor! moneny = 100
I bear a child, milo, sorghum, hug!
Sharpen the sword, cut the demon soul, frosted time, the magic blade
this is Boss's xigou function!
this is stupid_kid's xigou function!
this is er_xiao's xigou function!
this is xiao_er's xigou function!
this is Boss's xigou function!
this is xiao_er's xigou function!
this is Boss's xigou function!
this is VIP_em's xigou function!
this is Boss's xigou function!
this is er_xiao's xigou function!
this is xiao_er's xigou function!
this is VIP_em's xigou function!
this is Boss's xigou function!
Press any key to continue...

6, the code can be difficult to see from the above, in the virtual inheritance to avoid ambiguity, only compressed public virtual classes inherit your class.

If you want to find out virtual inheritance, you have to know the virtual table (vtbl) ---- we say virtual function table

In memory piece, will remain under a contiguous block of memory used as storage JMP vtble address, vtble is stored inside virtual function (virtual function) address,

Each time inheritance, the base class will have a vptr pointer to the derived class geology, when vptr pointer pointing at the same address, will not repeat structure. Others, constructors and destructors, based on the first code Liezi. According to their own summary of the source of understanding and testing, if wrong, please correct me.

Programming is a pleasure, the code gives me to enjoy the fun! ! !
     
         
         
         
  More:      
 
- Linux system started to learn: how to view the Linux thread of a process (Linux)
- Swift defined type conversion and type aliases (typealias) (Programming)
- A brief introduction to some important Docker commands (Server)
- How to download GOG games in Linux command line (Linux)
- Ubuntu 14.04 / 14.10 how to install FFmpeg 2.5.1 (Linux)
- Configuring Eclipse Note Templates (Linux)
- Restrict console access to Linux servers to improve security (Linux)
- MySQL InnoDB table --BTree basic data structures (Database)
- RedHat Linux 6.4 installation RTL8188CUS wireless network card driver (Linux)
- Linux group account deletion, and other related operations (Linux)
- Paint with Vim (Linux)
- MySQL query plan key_len know all (Database)
- MyCAT easy entry (Database)
- MySQL restart process can not be taken lightly (Database)
- Chkconfig command Detailed service is added and shut down the system in two ways to start service under Linux (Linux)
- Oriented C ++ test-driven development (Programming)
- Spring Data JPA @EnableJpaRepositories configuration in detail (Programming)
- C ++ class implementation date operator overloading (Programming)
- Mac OS X systems create Ubuntu USB boot disk for the Mac (Linux)
- Talk Packages (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.