C++课程学习笔记第五周:继承

时间:2020-03-03 01:55:28   收藏:0   阅读:79

注:该课程大纲详见 https://www.cnblogs.com/inchbyinch/p/12398921.html

1 继承和派生的概念

2 类之间的关系

两种类关系:

复合关系的案例:

备注:也有资料说类之间的关系有三种,即is A, has A, uses A。

3 派生类的访问控制

派生类继承了基类的全部成员变量和成员方法(除了构造和析构之外的成员方法),但是这些成员的访问属性,在派生过程中是可以调整的。

(摘自C++ Primer)类的用户有三种:普通用户(通过对象访问)、类的实现者(通过类成员函数访问)、派生类。

3.1 单个类的访问控制

类成员访问级别(public、private、protected)

class Father {
    private: int nPrivate; // 私有成员
    public: int nPublic; // 公有成员
    protected: int nProtected; // 保护成员
};
class Son :public Father{
    void AccessFather () {
        nPublic = 1; // ok;
        nPrivate = 1; // wrong
        nProtected = 1; // OK ,访问从基类继承的protected 成员
        Son f;
        f.nProtected = 1; //wrong,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;
}

3.2 不同的派生方式会改变派生类成员的访问属性

父类访问控制符(第一行)/派生方式(第一列) public protected private
public继承 public protected 不可见
protected继承 protected protected 不可见
private继承 private private 不可见

4 派生类的构造与析构

继承中的构造析构调用原则:

继承与成员对象混搭情况下,构造和析构调用原则:

5 public继承的赋值兼容规则

类型兼容规则是指在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。通过公有继承,派生类得到了基类中除构造函数、析构函数之外的所有成员。这样,公有派生类实际就具备了基类的所有功能,凡是基类能解决的问题,公有派生类都可以解决。类型兼容规则中所指的替代包括以下情况:

在替代之后,派生类对象就可以作为基类的对象使用,但是只能使用从基类继承的成员。

类型兼容规则是多态性的重要基础之一。

//示例1:常用的类型兼容 
class base { };
class derived : public base { };
base b;
derived d;
//派生类的对象可以赋值给基类对象: b = d;
//派生类对象可以初始化基类引用: base & br = d;
//派生类对象的地址可以赋值给基类指针: base * pb = & d;

//示例2:可以通过强制指针类型转换,把ptrBase转换成Derived类的指针
Base * ptrBase = &objDerived; 
Derived * ptrDerived = (Derived * ) ptrBase; 

注意:

6 练习

//001:全面的MyString  http://cxsjsxmooc.openjudge.cn/2019t3fall5/001/
//要求:编写MyString类
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;

/*******************your code start here*****************/
class MyString{
    char* p;
public:
    MyString():p(new char[1]){ p[0]=0; } //构造函数
    MyString(const char* p1);  //构造函数
    MyString(const MyString& s); //复制构造函数
    ~MyString(){ if(p) delete []p; } //析构函数

    //定义功能函数
    const char* c_str(){ return p; }

    //重载运算符 =, +, +=, <, >, ==, <<, []
    MyString& operator=(const char* p1);
    MyString& operator=(const MyString& s);
    friend MyString operator+(const char* p1, const MyString& s);
    friend MyString operator+(const MyString& s1, const MyString& s2);
    MyString& operator+=(const char* p1);
    MyString operator()(int start, int len); //取子串
    bool operator<(const MyString& s);
    bool operator>(const MyString& s);
    bool operator==(const MyString& s);
    friend ostream& operator<<(ostream& o, const MyString& s);
    char& operator[](int i);
};

MyString::MyString(const char* p1){
    p = new char[strlen(p1)+1];
    strcpy(p, p1);
}

MyString::MyString(const MyString& s){
    p = new char[strlen(s.p)+1];
    strcpy(p, s.p);
}

//重载运算符 =, +, <, >, ==, <<, []
MyString& MyString::operator=(const char* p1){ //MF:类外实现时忘加 类::
    if(p) delete []p;
    p = new char[strlen(p1)+1];
    strcpy(p, p1);
    return *this;
}

MyString& MyString::operator=(const MyString& s){
    if(this==&s) return *this;
    if(p) delete []p;
    p = new char[strlen(s.p)+1];
    strcpy(p, s.p);
    return *this;
}

MyString operator+(const char* p1, const MyString& s){
    MyString new_s;
    new_s.p = new char[strlen(p1)+strlen(s.p)+1];
    strcpy(new_s.p, p1);
    strcpy(new_s.p+strlen(p1), s.p);
    return new_s;
}

MyString operator+(const MyString& s1, const MyString& s2){
    MyString new_s;
    new_s.p = new char[strlen(s1.p)+strlen(s2.p)+1];
    strcpy(new_s.p, s1.p);
    strcpy(new_s.p+strlen(s1.p), s2.p);
    return new_s;
}

MyString& MyString::operator+=(const char* p1){
    char* temp = new char[strlen(p)+strlen(p1)+1];
    strcpy(temp, p);
    strcpy(temp+strlen(p), p1);
    if(p) delete []p;
    p = temp;
    return *this;
}

MyString MyString::operator()(int start, int len){
    //暂不做有效性检查
    char* temp = new char[len+1];
    for(int i=0; i<len; i++)
        temp[i] = p[start++];
    temp[len] = 0;
    MyString new_s(temp);
    delete []temp;
    return new_s;
}

bool MyString::operator<(const MyString& s){
    if(strcmp(p, s.p) < 0)
        return true;
    else
        return false;
}

bool MyString::operator>(const MyString& s){
    if(strcmp(p, s.p) > 0)
        return true;
    else
        return false;
}

bool MyString::operator==(const MyString& s){
    if(strcmp(p, s.p) == 0)
        return true;
    else
        return false;
}

ostream& operator<<(ostream& o, const MyString& s){ //MF:ostream设成const,出错
    o << s.p;
    return o;
}

char& MyString::operator[](int i){
    if(i<0 || i>=strlen(p)){
        cout << "out of range." << endl;
        exit(-1);
    }
    return p[i];
}
/********************code end here***********************/


int CompareString( const void * e1, const void * e2)
{
    MyString *s1 = (MyString * ) e1;
    MyString *s2 = (MyString * ) e2;
    if( *s1 < *s2 )
        return -1;
    else if( *s1 == *s2)
        return 0;
    else if( *s1 > *s2 )
        return 1;
}

int main(){
    MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
    MyString SArray[4] = {"big","me","about","take"};
    cout << "1. " << s1 << s2 << s3<< s4<< endl;
    s4 = s3;
    s3 = s1 + s3;
    cout << "2. " << s1 << endl;
    cout << "3. " << s2 << endl;
    cout << "4. " << s3 << endl;
    cout << "5. " << s4 << endl;
    cout << "6. " << s1[2] << endl;
    s2 = s1;
    s1 = "ijkl-";
    s1[2] = 'A' ;
    cout << "7. " << s2 << endl;
    cout << "8. " << s1 << endl;
    s1 += "mnop";
    cout << "9. " << s1 << endl;
    s4 = "qrst-" + s2;
    cout << "10. " << s4 << endl;
    s1 = s2 + s4 + " uvw " + "xyz";
    cout << "11. " << s1 << endl;
    qsort(SArray,4,sizeof(MyString),CompareString);
    for( int i = 0;i < 4;i ++ )
        cout << SArray[i] << endl;
    //s1的从下标0开始长度为4的子串
    cout << s1(0,4) << endl;
    //s1的从下标5开始长度为10的子串
    cout << s1(5,10) << endl;
    return 0;
}

原文:https://www.cnblogs.com/inchbyinch/p/12398355.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!