飞道的博客

C++程序设计【五】之 类的继承与派生

348人阅读  评论(0)

感谢内容提供者:金牛区吴迪软件开发工作室
接上一篇:C++程序设计【四】之运算符重载的概念

第五章:类的继承与类的派生

一、类的继承与类的派生

#include <iostream>
using namespace std;

class another;  // 前向引用声明
class Base {
       // 基类
    private:
        float x;
    public:
        void print(const another &K);
};
class Derived:public Base {
    // 派生类
    private:
        float y;
};
class another {
   
    private:
        int aaa;
    public:
        another() {
   
            aaa = 100;
        }
        friend void Base::print(const another &K);  // 基类的成员函数声明为本类的友元
};
void Base::print(const another &K) {
   
    cout << "Base:" << K.aaa << endl;   // 可以访问私有成员变量
}
int main() {
   
    Base a;
    Derived d;
    another ano;    // aaa初始化为100
    a.print(ano);   // 输出为: Base: 100
    d.print(ano);   // 输出为: Base: 100
    return 0;
}

1.继承的概念


2.派生类的定义与大小

a.派生类的定义
class BaseClass {
   	// 基类
	int v1, v2;
}

class DeviredClass: public BaseClass {
   	// 派生类
	int v3;
}

class emptyClass {
   };  // 空基类
class subemptyClass: public emptyClass {
   };	// 派生类
// 派生类可以改变基类中成员的访问权限
b.类的大小

#include<iostream>
using namespace std;
class BaseClass {
   	// 基类
	int v1, v2;
	char v4;
	public:
		int templ() {
   }
};

class DerivedClass: public BaseClass {
   	// 派生类
	int v3;
	int*p;
	public:
		int temp() {
   }
};

int main() {
   
	cout << "Base=" << sizeof(BaseClass) << endl;	// 输出Base=12
	cout << "Derived=" << sizeof(DerivedClass) << endl;	// 输出Derived=24
	return 0;
}
c.继承关系的特殊性

3.继承关系的特殊性

#include<iostream>
using namespace std;
class Base {
       // 基类
    private:
        float x;
    public:
        static int staV;
        Base() {
   
            staV ++;
        }
};
int Base::staV = 0;
class Derived: public Base {
       // 派生类
    private:
        float y;
    public:
        Derived() {
   
            staV++;
        }
};


int main() {
   
    Base a;
    cout << a.staV << endl; // 输出1
    Derived d;
    cout << d.staV << endl; // 输出3
    return 0;
}

4.有继承关系的类之间的访问

#include<iostream>
using namespace std;
class CB {
   
public:
    int a;
    CB(int x) {
   
        a = x;
    }
    void showa() {
   
        cout << "Class CB--a = " << a << endl;
    }
};

class CD:public CB {
   
public:
    int a;  // 与基类a同名
    CD(int x, int y):CB(x) {
       // x用来初始化基类的成员变量a
        a = y;
    }
    void showa() {
   
        cout << "Class CD--a = " << a << endl;
    }
    void print2a() {
   
        cout << "a = " << a << endl;    // 访问派生类a
        cout << "CB::a = " << CB::a << endl;    // 访问基类a
    }
};

int main() {
   
    CB CBobj(12);
    CBobj.showa();
    CD CDobj(48, 999);
    CDobj.showa();  // 访问派生类的showa()
    CDobj.CB::showa();  // 访问基类的showa()
    cout << "CDobj.a = " << CDobj.a << endl;
    cout << "CDobj.CB::a = " << CDobj.CB::a << endl;
    return 0;
}

5.protected访问范围说明符

6.多重继承


#include<iostream>
using namespace std;
class CB1 {
   
public:
    int a;  // 重名
    CB1(int x) {
   
        a = x;
    }
    void showa() {
     // 重名
        cout << "Class CB1 ==> a = " << a << endl;
    }
};

class CB2 {
   
public:
    int a;  // 重名
    CB2(int x) {
   
        a = x;
    }
    void showa() {
     // 重名
        cout << "Class CB2 ==> a = " << a << endl;
    }
};

class CD:public CB1, public CB2 {
      // 多重继承,俩个基类
public:
    int a;  // 与俩个基类成员变量a同名
    CD(int x, int y, int z):CB1(x), CB2(y) {
   
        a = z;
    }
    void showa() {
     // 与俩个基类成员函数showa()同名
        cout << "Class CD ==> a = " << a << endl;
    }
    void print3a() {
   
        cout << "a = " << a << endl;    // 访问派生类a
        cout << "CB1::a = " << CB1::a << endl;    // 访问基类a
        cout << "CB2::a = " << CB2::a << endl;    // 访问基类a
    }
};

int main() {
   
    CB1 CB1obj(11);
    CB1obj.showa(); // 11
    CD CDobj(101, 202, 909);
    CDobj.showa();  // 访问派生类的showa() 909
    CDobj.CB1::showa();  // 访问基类的showa() 101
    cout << "CDobj.a = " << CDobj.a << endl;    // 909
    cout << "CDobj.CB2::a = " << CDobj.CB2::a << endl;  // 202
    return 0;
}

二、访问控制

1.公有继承

2.类型兼容规则


#include<iostream>

using namespace std;

class A {
   
    int an;
public:
    A() {
   }
    A(int n) {
   
        an = n;
    }
    void printa() {
   
        cout << "A:" << an << endl;
    }
};

class B : public A {
     // 共有派生
    int bn;
public:
    B(int n) : A(2 * n) {
   
        bn = n;
    }
    void printa() {
   
        cout << "b:" << bn << endl;
    }
};

int main() {
   
    A a(10);
    a.printa(); // 10
    B b(20);
    a.printa(); // 10
    a = b;  // 派生类对象赋值给基类对象
    a.printa(); // 40
    b.printa(); // 20
    return 0;
}

3.私有继承

4.保护继承

三、派生类的构造函数和析构函数

1.构造函数与析构函数

#include<iostream>

using namespace std;

class BaseClass {
      // 基类
protected:
    int v1, v2;
public:
    BaseClass();
    BaseClass(int, int);
    ~BaseClass();
};

BaseClass::BaseClass() {
   
    cout << "BaseClass 无参构造函数" << endl;
}
BaseClass::BaseClass(int m, int n) {
   
    v1 = m;
    v2 = n;
    cout << "BaseClass 2个参数构造函数" << endl;
}
BaseClass::~BaseClass() {
   
    cout << "BaseClass 析构函数" << endl;
}
class DerivedClass:public BaseClass {
      // 公有继承的派生类
    int v3;
public:
    DerivedClass();
    DerivedClass(int);
    DerivedClass(int, int, int);
    ~DerivedClass();
};

DerivedClass::DerivedClass() {
   
    cout << "DerivedClass无参构造函数" << endl;
}
DerivedClass::DerivedClass(int k):v3(k) {
   
    cout << "DerivedClass带1个参数构造函数" << endl;
}
DerivedClass::DerivedClass(int m, int n, int k):BaseClass(m, n), v3(k) {
   
    cout << "DerivedClass带3个参数构造函数" << endl;
}
DerivedClass::~DerivedClass() {
   
    cout << "DerivedClass析构函数" << endl;
}

int main() {
   
    cout << "无参对象的创建" << endl;
    BaseClass baseCla;  // 基类对象
    DerivedClass derivedCla;    // 派生类对象
    return 0;
}

#include<iostream>

using namespace std;

class Base {
   
private:
    int Y;
public:
    Base(int y = 0) {
   
        Y = y;
        cout << "Base(" << y << ")" << endl;
    }
    ~Base() {
   
        cout << "~Base()" << endl;
    }
    void print() {
   
        cout << Y << endl;
    }
};

class Derived:public Base {
   
private:
    int Z;
public:
    Derived(int y, int z):Base(y) {
   
        Z = z;
        cout << "Derived(" << y << "," << z << ")" << endl;
    }
    ~Derived() {
   
        cout << "~Derived()" << endl;
    }
    void print() {
   
        Base::print();
        cout << Z << endl;
    }
};

int main() {
   
    Derived d(10, 20);
    d.print();
    return 0;
}

2.复制构造函数

#include<iostream>

using namespace std;

class CBase {
   
public:
    CBase() {
   }
    CBase(CBase & c) {
   
        cout << "CBase::复制构造函数" << endl;
    }
    CBase & operator=(const CBase &b) {
   
        cout << "CBase::operator=" << endl;
        return *this;
    }
};

class CDerived:public CBase {
   
public:
    CDerived() {
   
        cout << "CDerived::复制构造函数" << endl;
    }
};

int main() {
   
    CDerived d1, d2;
    CDerived d3(d1);
    // d3初始化过程中会调用类CBase的复制构造函数
    d2 = d1;    // 会调用类CBase重载的"="运算符
    return 0;
}

3.多重继承的构造函数与析构函数


四、类之间的关系

1.类与类之间的关系

2.封闭类的派生

3.互包含关系的类

class A {
   	// 类A的定义
	public:
		void f(B b);	// 以类B对象为形参的成员函数
};
class B {
   	// 类B的定义
	public:
		void g(A a);	// 以类A对象为形参的成员函数
}

五、多层次的派生

六、基类与派生类指针的互相转换

#include<iostream>

using namespace std;

class CBase {
   
protected:
    int n;
public:
    CBase(int i): n(i) {
   }
    void print() {
   
        cout << "CBase:n = " << n << endl;
    }
};
class CDerived: public CBase {
   
public:
    int v;
    CDerived(int i): CBase(i), v(2 * i) {
   }
    void Func() {
   };
    void print() {
   
        cout << "CDerived:n=" << n << endl;
        cout << "CDerived:v=" << v << endl;
    }
};


int main() {
   
    CDerived objDerived(3);
    CBase objBase(5);
    CBase * pBase = &objDerived;    // 使用基类指针指向派生类对象
    CDerived *pDerived;
    pDerived = &objDerived;
    cout << "使用派生类指针调用函数" << endl;
    pDerived -> print();    // 调用的是派生类中的函数
    pBase = pDerived;   // 基类指针 = 派生类指针,正确
    cout << "使用基类指针调用函数" << endl;
    pBase -> print();   // 调用的是基类中的函数
    //pBase -> Func()   // 错误,通过基类指针不能调用派生类函数
    //pDerived = pBase; // 错误,派生类指针=基类指针
    pDerived = (CDerived*)pBase;    // 强制类型转换,派生类指针=基类指针
    cout << "使用派生类指针调用函数" << endl;
    pDerived -> print();    // 调用的是派生类中的函数
    return 0;
}

下一篇:C++程序设计【六】之 多态与虚函数


转载:https://blog.csdn.net/weixin_43606158/article/details/114065904
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场