《C++语言基础》程序阅读——继承和派生

返回:贺老师课程教学链接


(1)

#include 
using namespace std;
class Data
{
public:
    Data(int i):x(i)
    {
        cout<<"A";
    }
    ~Data()
    {
        cout<<"B";
    }
private:
    int x;
};
class Base
{
public:
    Base(int i):b1(i)
    {
        cout<<"C";
    }
    ~Base()
    {
        cout<<"D";
    }
private:
    int b1;
};
class Derived:public Base
{
public:
    Derived (int i,int j):Base(i),d1(j)
    {
        cout<<"E";
    }
    ~Derived()
    {
        cout<<"F";
    }
private:
    Data d1;
};
int main()
{
    Derived obj(1,2);
    return 0;
}

(2)
#include
using namespace std;
class G
{
public:
    static int m;
    G( )  //构造函数
    {
        m++;
        cout<<"G begins\n";
    }
    ~G( )
    {
        cout<<"G ends\n";
        m--;
    }
};
int G::m=0;
class D:public G
{
public:
    D( )  //构造函数
    {
        m++;
        cout<<"D begins\n";
    }
    ~D( )
    {
        cout<<"D ends\n" ;
        m-- ;
    }
};
int main( )
{
    D objg;
    cout<

(3)
#include
using namespace std;
class  A
{
private:
    int  x;
protected:
    int y;
public:
    int z;
    A(int a,int b,int c)
    {
        x=a;
        y=b;
        z=c;
    }
    int  Getx()
    {
        return x;
    }
    int  Gety()
    {
        return y;
    }
    void ShowA()
    {
        cout<< "x="<using namespace std;
class Part  //部件类
{
public:
    Part();
    Part(int i);
    ~Part();
private:
    int val;
};
Part::Part()
{
    val=0;
    cout<<"调用Part的默认构造函数:"<
先分析程序的执行结果,在上机时运行程序进行对照,再通过单步执行跟踪程序的运行,达到理解基类、派生类中构造函数、析构函数执行过程的目的。
将Whole类的构造函数(见注释//问题2)改为下面形式,请解释出现的警告信息。
Whole::Whole(int p, int i,int j,int k):  Part(p),two(i),one(j),data(k)  //问题2

自选阅读:若还想通过运行程序的方式掌握继承机制的运行过程,阅读并运行下面的程序
(1)
#include 
using namespace std;
class A
{
public:
    A()
    {
        cout<<"A";
    }
    ~A()
    {
        cout<<"~A";
    }
};
class B :public A
{
    A *p;
public:
    B()
    {
        cout<<"B";
        p=new A();
    }
    ~B()
    {
        cout<<"~B";
        delete p;
    }
};
int main()
{
    B obj;
    return 0;
}

(2)
#include 
using namespace std;
class A
{
protected:
    int x;
public:
    A(int x)
    {
        A::x=x;
        cout<<"class A"<

(3)
#include
using namespace std;
class my_base
{
    int a,b;
public:
    my_base(int x,int y)
    {
        a=x;
        b=y;
    }
    virtual void show()
    {
        cout<<"base";
        cout<#include 
using namespace std;
class Animal    //动物类
{
public:
    Animal() {}
    void eat(){
        cout << "eat\n";
    }
protected:
    void play()
    {
        cout << "play\n";
    }
private:
    void drink()
    {
        cout << "drink\n";
    }
};
class Giraffe: public Animal   //长颈鹿类
{
public:
    Giraffe() {}
    void StrechNeck()
    {
        cout << "Strech neck \n";
    }
private:
    void take()
    {
        eat();        // 正确,公有继承下,基类的公有成员对派生类可见
        drink();      // _______________
        play();       // _______________
    }
};
int main()
{
    Giraffe gir;      //定义派生类的对象
    gir.eat();        // 正确,公有继承下,基类的公有成员对派生类对象可见
    gir.play();       // _______________
    gir.drink();      // _______________
    gir.take();       // _______________
    gir.StrechNeck(); // _______________
    Animal ani;
    ani.eat();        // _______________
    ani.play();       // _______________
    ani.drink();      // _______________
    ani.take();       //错误,派生类的成员对基类对象(不论访问属性)不可见
    ani.StrechNeck(); // _______________
    return 0;
} 

(2)private继承方式下


#include 
using namespace std;
class Animal
{
public:
    Animal() {}
    void eat()
    {
        cout << "eat\n";
    }
protected:
    void play()
    {
        cout << "play\n";
    }
private:
    void drink()
    {
        cout << "drink\n";
    }
};
class Giraffe: private Animal
{
public:
    Giraffe() {}
    void StrechNeck()
    {
        cout << "Strech neck \n";
    }
    void take()
    {
        eat();     // _______________
        drink();   // _______________
        play();    // _______________
    }
};
int main()
{
    Giraffe gir;
    gir.eat();    // _______________
    gir.play();   // _______________
    gir.drink();  // _______________
    return 0;
}

(3)protected继承方式下
#include 
using namespace std;
class Animal
{
public:
    Animal() {}
    void eat()
    {
        cout << "eat\n";
    }
protected:
    void play()
    {
        cout << "play\n";
    }
private:
    void drink()
    {
        cout << "drink\n";
    }
};
class Giraffe: protected Animal
{
public:
    Giraffe() {}
    void StrechNeck()
    {
        cout << "Strech neck \n";
    }
    void take()
    {
        eat();    // _______________
        drink();  // _______________
        play();   // _______________
    }
};
int main()
{
    Giraffe gir;
    gir.eat();   // _______________
    gir.play();  // _______________
    gir.drink(); // _______________
    return 0;
}





你可能感兴趣的:(《C++语言基础》程序阅读——继承和派生)