学完C再学C++(6)继承类


/*
第12章 类的继承
1.派生一个类
    class NewClassName: public FatherClassName{
    //自己的构造函数,数据成员和成员函数
    }
    派生类对象存储了基类的数据成员,派生类对象存储了基类的方法(相当于结构体套娃)
    派生类的构造函数必须给自己和基类成员提供数据.

    派生类的构造函数:
    NewClassName:NewClassName(...):FatherClassName(...)使用基类的特定某个构造函数
    NewClassName:NewClassName(...)等价于下面的
    NewClassName:NewClassName(...):FatherClassName()使用基类的默认构造函数
    派生类会先创建基类再创建自己,派生类不能访问基类的私有成员
2.多态公有继承
    虚函数:

    当派生类中有与基类一样的方法时,在基类声明时使用virtual 关键字代表它是虚函数,当基类用指针或者引用强制指向派生类,然后再强转这是会有不一样.
    NewClassName a;
    FatherClassName *b =&a;
    (*b).Virtualfunc()
    //    若有创建虚方法,则是NewClassName::Virtualfunc(),没有声明则是FatherClassName的方法.
    //意思是不管你指针指类型换成谁,原来的指针是虚函数我就要调用原来那个的.
    使用虚的析构函数可以保证不管   你咋折腾你的对象  她还是最后会先调用派生类的析构函数,然后再调用基类的析构函数

    记住,派生类要在某个函数定义中调用自己与基类有同名字的函数时,要加上作用域.
    不然结果变成你想调用基类的,最后调用自己的了.
    BothFunc()  //NewClassName的
    FatherClassName:BothFunc() //FatherClassName的

3.静态联合编译和动态联合编译
    编译过程中就能知道调用哪个函数叫静态联编,
    但是由于虚函数的出现就导致不能编译时就确定,需要再程序运行时确定使用哪个同名函数.

    指针和引用的兼容性
    一般情况不允许一个指针指向其他的类型,比如int的指针指向double的地址,
    但是基类和派生类例外.
    基类的指针可以指向派生类的引用(地址).
    也可以派生类指针指向基类,不过要做显式的强制转换
    
    静态联编效率更高但是不能编虚函数,若设计了虚函数,则需要动态联编.
4.访问控制:protected
    protected 派生类和自己可以访问其他不能访问

5.抽象基类
    ABC简称abstract base class 意思是两个类有些共同点,然后就写一个基类派生出这两个类

    纯虚函数:
        ABC相较于被抽象的不能实现的函数 
        形式为:虚函数后面加一个const=0;
    
    ABC中必须有至少一个纯虚函数,不然她就不叫抽象了.
6.继承中的内存分配
    大前提:基类使用了new分配空间
    派生类使用new和不使用new是两种情况
    不使用
        就不需要编写额外的赋值重载函数和构造函数,这在stringbad里面是一样的
    使用new:
        析构函数只需要释放自己分配的空间,C++会自动调用基类的析构函数,不需要释放基类的空间.

        派生类复制类的构造函数
        入想把一个派生类赋值给另一个派生类,编写函数时,先用基类的复制构造函数把基类部分复制过去,再复制自己的.
        这么写: SonClass::SonClass(const SonClass &sc):FatherClass(sc){new xx ;strcpy xx;}

        派生类等号的重载
        等号重载同样要多一步,那就是将其所包含的基类也赋值了

        SonClass &SonClass::operator=(const SonClass &sc)
        {
            if(this==sc)return *this;
            FatherClass::operator=(cs);//让自己的基类也被赋值
            delete [] x;
            x= new(...);
            strcpy(...);//赋值自己的内存空间
            ///.....
        }
        若使用友元函数重载符号,那么友元函数不属于这个类,所以不能用作用域区分谁的,怎么让派生类也使用基类的重载后的运算符呢?
        答案是使用派生类的友元重载,然后在派生类中使用强制类型转换成基类,这样就能兼容了

这一章又臭又长,寄
*/

#include <iostream>
#include <fstream>
#include<string.h>
#include "study.h"//里面写的#define DAY1....


#ifdef DAY7
//练习课后题4.  课后题3给人感觉完全没卵用,强行为了抽象而抽象.
#ifndef Hfile
//头文件部分
using namespace std;
//基类
class port
{
private:
    char* brand;    //应该是型号
    char style[20];//红酒品类吧
    int bottles;    //酒的数量

public:
    port(const char* br = "none", const char* st = "none", int bo = 0);
    port(const port& p);//防止构造时生成默认的,默认的会直接复制对象,里面指针还是没变
    virtual    ~port() { delete[] brand; }
    port& operator=(const port& p);
    port& operator+=(int b);
    port& operator-=(int b);
    virtual void show()const;
    int bottle_count()const { return bottles; }
    friend ostream& operator<<(ostream& os, const port& p);

};
//老程序员留的烂摊子,派生类
//问题b:为什么有些方法重新定义了,有些没有?
//答:因为show多了nickname和year,等号也需要给新的这些成员赋值
//问题c:为什么这个没有将operator=和operator<<声明成虚的?
//答:operator<<声明成了友元函数,没理由变成虚的,等号是因为若是等号也虚了,那么指针就无法相互转换了 
// 比如vp *a= b(p) 就会变成 vp::operator=(b);那最后就变成直接重新分配了一个空间给*a,虚函数作用就体现不出来了.
//声明成虚的那两个,析构函数必须保证能释放完全,show则是也要想显示完全
class vin_port :public port
{
private:
    char* nickname;
    int year;
public:
    //vin_port();
    vin_port(const char *br="none", int bo=0, const char* nn="none", int y=0);
    vin_port(const vin_port& p);
    ~vin_port() { delete[] nickname; }
    vin_port& operator=(const vin_port& vp);
    void show()const;
    friend ostream& operator<<(ostream& os, const vin_port& vp);

};


#endif

//基类部分
port::port(const char* br , const char* st , int bo)
{
    int len = strlen(br)+1 ;
    brand = new char[len];
    memcpy(brand,br,len);
    len= strlen(st);
    memcpy(style, st,len);
    bottles = bo;
}
port::port(const port& p)
{
    int len = strlen(p.brand)+1;
    brand = new char[len];
    memcpy(brand, p.brand, len);
    len = strlen(p.style);
    memcpy(style, p.style, len);
    bottles = p.bottles;
}
//为了防止赋值时把指针也复制过去
port& port::operator=(const port& p)
{
    if (this == &p)return *this;
    free(brand);//先释放原来的
    int len = strlen(p.brand)+1;
    brand = new char[len];
    memcpy(brand, p.brand, len);
    len = strlen(p.style);
    memcpy(style, p.style, len);
    bottles = p.bottles;
    return *this;
}
port& port:: operator+=(int b)
{
    bottles += b;
    return *this;
}
port& port:: operator-=(int b)
{
    bottles -= b;
    return *this;
}
void port::show()const
{
    cout << "Brand:" << brand << endl;
    cout << "Kind:" << style << endl;
    cout << "Bottles:" << bottles << endl;
}
//友元函数,返回os是为了os<<a<<b也能用
ostream& operator<<(ostream& os, const port& p)
{
    os << p.brand << ", " << p.style << ", " << p.bottles << endl;
    return os;
}

//派生类部分
vin_port::vin_port(const char* br, int bo, const char* nn, int y):port(br,"none", bo)
{
    int len = strlen(nn) + 1;
    nickname = new char[len];
    memcpy(nickname, nn, len);
    year = y;
}
vin_port::vin_port(const vin_port& vp):port(vp)
{
    int len = strlen(vp.nickname) + 1;
    nickname = new char[len];
    memcpy(nickname, vp.nickname, len);
    year = vp.year;
}
vin_port& vin_port::operator=(const vin_port& vp)
{
    if (this == &vp)return *this;
    port::operator=(vp);
    
    free(nickname);//先释放原来的
    int len = strlen(vp.nickname) + 1;
    nickname = new char[len];
    memcpy(nickname, vp.nickname, len);
    year = vp.year;
    return *this;
}
void vin_port::show()const
{
    port::show();
    cout << "Name:" << nickname << endl;
    cout << "Year:" << year << endl;
}
ostream& operator<<(ostream& os, const vin_port& vp)
{
    os << ", " << vp.nickname<<", "<<vp.year;
    return os;
}
int main()
{
    port* point;
    port p1 ("老八","小汉堡儿",114514);
    p1.show();
    p1 = p1;
    cout << p1;

    vin_port p2("老八",14,"豆腐乳",514);
    point = &p2;
    point->show();
    cout << *point;//会调用port的<<
    //vin_port();= (vin_port)p1
    
    return 0;
}

#endif`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值