C ++ programming POJ "" WEEK5 Inheritance and derived "" complex relationship and inheritance "" member of a base class / derived class with the same name and the range of protected access specifier "" constructor "derived class

Inheritance and derived

Inheritance: When defining a new class B, if the class is similar to an existing class A (referring to B of A has all the features),

Then A can be used as a base class, the base class B as a derived class (also referred to as subclass).

By the derived class is the base class to modify and expand obtained. In a derived class, you can expand a new member variables and member functions.

Once defined the derived classes, can be used independently, not dependent on the base class.

It has all the derived class member functions and member variables of the base class, whether it is private, protected, public.

 in all member functions of the derived class, private members can not access the base class.

Writing the derived class
class name derived class: public class name {base};


The derived class object memory space

 

Derived object volume equal to the volume of the base class, plus the volume of its member variables derived object .

In a derived class object, the object comprising a base class, and a storage position located in front of the base class new derived object member variables .

/*001-继承和派生*/
#include<iostream>
#include<string> // if did not include this file
using namespace std;

class CStudent
{
private:
    string name;
    string id;
    char gender;
    int age;
public:
    void PrintInfo();
    void SetInfo(const string & name_, const string & id_, int age_, char gender_);
    string GetName()
    {
        return name;
    }
};
class CUndergraduateStudent :public CStudent
{
private:
    string department;
public:
    void QualifiedForBaoyan()
    {
        cout << "qualified for baoyan" << endl;
    }
    void PrintInfo()
    {
        :: PrintInfo CStudent (); // call PrintInfo base class 
        cout << " Department: " << Department << endl;
    }
    void SetInfo(const string & name_, const string & id_,
        int age_, char gender_, const string & department_)
    {
        :: the SetInfo CStudent (name_, ID_, age_, gender_); // call the base class of the SetInfo 
        Department = department_;
    }

}; // wording derived classes are: Class name: public class name yl

void CStudent::PrintInfo()
{
    cout << "name:" << name << endl;
    cout << "id:" << id << endl;
    cout << "age:" << age << endl;
    cout << "gender:" << gender << endl;
}
void CStudent::SetInfo(const string & name_, const string & id_, int age_, char gender_)
{
    name = name_;
    id = id_;
    age = age_;
    gender = gender_;
}

int main ()
{
    CUndergraduateStudent s2;
    s2.SetInfo ( " Harry Potter " , " 1188 " , . 19 , " m " , " CS " ); // "" corresponding to the character string '' corresponding to the character 
    COUT << s2.GetName () << "  " ;
    s2.QualifiedForBaoyan ();
    s2.PrintInfo();

    while (1);
    return 0;

}

Inheritance and complex relationship

Use complex relationship

 

Base class / derived class members with the same name and Protected keywords

Note: In general, base and derived class does not define a member variable of the same name

 

Access range specifier


Private members of the base class: The following functions can be accessed
member functions of the base class •
Friends • base class member function

Public members of the base class: The following functions can be accessed
member functions of the base class •
Friends • base class member functions
member functions of the derived class •
Friends of the derived class member function •
• Other functions

 

Access range specifiers: protected
protected members of the base class: The following functions can be accessed
member functions of the base class •
Friends • base class member functions
member functions • derived class can access the current object protected members of the base class

#include<iostream>
using namespace std;

class base
{
    int j;
public:
    int i;
    void func();
};

class derived :public base
{
public:
    int i;
    void access();
    void func();
};

void derived::access()
{
    J = 5 ; // can not access the private variables of the base class it? 
    = I . 5 ; // refer to the derived class I 
    Base :: = I . 5 ; // reference the base class I 
    FUNC (); // derived class 
    Base :: FUNC (); // base class


}
int main ()
{
    derived obj;
    obj.i = 1;
    obj.base::i;
}


/ * Base class member / derived class with the same name as a keyword Protected * / 
#include <the iostream>
 the using  namespace STD;

class Father
{
Private :
     int nPrivate; // private members of the 
public :
     int nPublic; // public members 
protected :
     int nProtected; // protected members 
};

class Son:public Father
{
    void AccessFather()
    {
        nPublic = . 1 ; // OK 
        nPrivate = . 1 ; // Wrong 
        nProtected = . 1 ; // the OK, inherited from a base class to access protected members 
        Son f;
        f.nProtected = . 1 ; // Wrong, not the current object F 
    }
};

int main ()
{
    Father f;
    Son s;
    f.nPublic = 1; // Ok
    s.nPublic = 1; // Ok
    f.nProtected = 1; // error
    f.nPrivate = 1; // error
    s.nProtected = 1; // error
    s.nPrivate = 1; // error
    return 0;
}

 

Constructor of a derived class

Constructor of a derived class

Derived object comprising a base class object

Before performing the derived class constructor first base class constructor is executed

Derived class account base class initialization, specifically form:
constructor function name (parameter list): the name of the base class (base class constructor argument list)
{
}

The derived class constructor

class Bug {
Private:
int nLegs; int nColor;
public:
int nType;
Bug (legs' int, int Color);
void PrintBug () {};
};
class FlyBug: public Bug { // FlyBug is Bug the derived class
int nWings;
public:
FlyBug (int legs', Color int, int WINGS);
};

FlyBug fb (2,3,4);

 

When you create an object of a derived class,
• need to call the base class constructor:
initialize a derived class object inherits from the base class members


• Before performing the constructor of a derived class,
always the first implementation of the base class constructor

 

Two ways to call the base class constructor
• explicitly:
derived class constructor of a base class constructor  provide parameters
derived :: derived (arg_derived-List): Base (arg_base-List)
• Implicit mode:
Derived constructor class, omit base class constructor
constructor of a derived class, automatically call the default constructor for the base class destructor derived class, when executed , the destructor executing the derived class automatically calls the base class destructor

Calling sequence constructor and destructor are reversed

When you create an object of a derived class before the constructor executes derived class:
• call the base class constructor
 initialize a derived class object from the base class inherits members
• call members of the object class constructor
 initialize a derived class object member objects

After performing destructor derived class after:
• Call member object class destructor
• Call base class destructor

Call sequence calling sequence constructor opposite destructor

/*
Constructor of a derived class
*/
#include<iostream>
using namespace std;
#if 0
class Bug
{
private:
    int nLegs;
    int nColor;
public:
    int nType;
    Bug(int legs, int color);
    void PrintBug() {}

};
class FlyBug :public Bug
{
    int nWings;
public:
    FlyBug(int legs, int color, int wings);
};
Bug::Bug(int legs, int color)
{
    nLegs = legs;
    nColor = color;
}
// expression may appear: Parameter FlyBug constructor 
FlyBug :: FlyBug ( int legs ', int Color, int WINGS): Bug (legs', Color)
{
    nWings = wings;
}
int main ()
{
    FlyBug fb(2, 3, 4);
    fb.PrintBug();
    fb.nType = 1;
    //fb.nLegs = 2
    while (1);
    return 0;
}
#endif

/*
Two ways to call the base class constructor
*/
class Base
{
public:
    int n;
    Base(int i) :n(i)
    {
        cout << "base " << n << " constructed" << endl;

    }
    ~Base()
    {
        cout << "base " << n << " desstructed" << endl;
    }
};

class Derived :public Base
{
public:
    Derived(int i) :Base(i)
    {
        cout << "Derived constructed" << endl;
    }
    ~Derived()
    {
        cout << "derived destructed" << endl;
    }
};
int main ()
{
    Derived obj(3);
    while (1);
    return 0;
}

/*
Constructor of a derived class that contains member objects
*/
class Skill
{
public:
    Skill(int n)
    {
    }
};

class FlyBug :public Bug
{
    int nWings;
    Skill sk1, sk2;
public :
     // expression can occur: FlyBug constructor parameters, constants 
    FlyBug ( int legs ', int Color, int WINGS): the Bug (legs', Color), SK1 ( . 5 ), SK2 (Color)
    {
        nWings = wings;
    }
    
};

 

assignment-compatible with the rules of public inheritance

class base { };

class derived : public base { };

base b;

derived d;

1) the object of the derived class can be assigned to the base object
B = D;
2) derived class objects can initialize the base class reference
base & br = d;
address 3) derived class objects can be assigned to the base pointer
base * pb = & D;

Direct and indirect base class of the base class

A derived class Class B, Class C B derived class, the derived class D Class C, ......
- A class is a base class B is a direct
- direct base class is Class B Class C and Class A is an indirect base class of C
- C is a direct class D base class, the class a, B is an indirect base class of D

 

When you declare a derived class, only you need to list its direct base class


- automatically derived class inherits its class upward along indirect base class hierarchy


- members of the derived class, including
members of their own definition of derived classes •
• All members of the immediate base class of
all members of the base class of all indirect •

#include<iostream>
using namespace std;

class Base
{
public:
    int n;
    Base(int i) :n(i)
    {
        cout << "Base " << n << "constructed"<<endl;
    }
    ~Base()
    {
        cout << "Base " << n << "destructed" << endl;
    }
};

class Derived :public Base
{
public:
    Derived(int i) :Base(i)
    {
        cout << "derived constructed" << endl;
    }
    ~Derived()
    {
        cout << "derived destructed" << endl;
    }
};
class MoreDerived :public Derived
{
public:
    MoreDerived() :Derived(4)
    {
        cout << "more derived constructed" << endl;
    }
    ~ MoreDerived ()
    {
        cout << "more derived destructed" << endl;
    }
};
int main ()
{
    MoreDerived obj;
    while (1);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/focus-z/p/11032310.html