头歌C++程序设计二(面向对象)_实训12_公有继承

第1关:父类和子类

list.h

#ifndef _LIST_H_
#define _LIST_H_

class List{
protected:
    //注意size是protected,这样子类就可以操作这个成员
    int size;

public:
    //请实现成员size的存取函数
    void setSize(int v)
    {
        size=v;
    }

    int getSize()
    {
        return size;
    }
};

#endif // _LIST_H_

ArrayList.h

#ifndef _ARRAYLIST_H_
#define _ARRAYLIST_H_
#include"List.h"
//请在下面定义ArrayList类
//ArrayList类公有继承List类,且额外拥有2个数据成员:data和capacity
class ArrayList:public List
{
    public:
    void setSize(int v)
    {
        size=v;
    }

    int getSize()
    {
        return size;
    }
};
#endif // _ARRAYLIST_H_

linkedlist.h

#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#include"List.h"
//请在下面定义LinkedList类
//LinkedList类公有继承List类,且拥有一个内部结构体Node,以及一个数据成员head
class LinkedList:public List
{
    public:
    void setSize(int v)
    {
        size=v;
    }

    int getSize()
    {
        return size;
    }
};

#endif // _LINKEDLIST_H_

第2关:子类的对象可以作为父类类型使用

//请在该文件中完成f函数
/******begin your code here******/
#include"List.h"
#include"ArrayList.h"
#include"LinkedList.h"
#include<iostream>
using namespace std;
void f(const List&list)
{
    cout<<list.getSize()<<endl;
}
/******end your code********/

第3关:利用父类的构造函数实现子类的构造函数

arrylist.cpp

/******begin your code here******/
#include"ArrayList.h"
#include"List.h"

ArrayList:: ArrayList()//初始化
{
    size=0;
}

ArrayList:: ArrayList(const ArrayList&rhs)
{
    size=rhs.getSize();
}

ArrayList::ArrayList(int const a[],int n)
{
    size=n;
}

ArrayList::ArrayList(int n,int value)
{
    size=n;
}
ArrayList::~ArrayList()
{
}

/******end your code******/

linkedlist.cpp

/******begin your code here******/
#include"LinkedList.h"
LinkedList::LinkedList()
{
    size=0;
}
LinkedList::LinkedList(const LinkedList&rhs)
{
    size=rhs.getSize();
}

LinkedList::LinkedList(int const a[],int n)
{
    size=n;
}
LinkedList::LinkedList(int n,int value)
{
    size=n;
}
LinkedList::~LinkedList()
{

}

/******end your code******/

  • 16
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
保护继承公有继承的区别在于派生类对于基类的保护成员的访问权限不同。具体来说,保护继承会将基类的保护成员作为派生类的保护成员,而公有继承会将基类的保护成员作为派生类的公有成员。 下面是一个示例代码,演示了保护继承公有继承的区别: ```c++ #include <iostream> using namespace std; class Base { public: int public_var; protected: int protected_var; private: int private_var; }; // 公有继承 class Derived1 : public Base { public: void access_base_members() { cout << public_var << endl; // 可以访问基类的公有成员 cout << protected_var << endl; // 可以访问基类的保护成员 //cout << private_var << endl; // 不能访问基类的私有成员 } }; // 保护继承 class Derived2 : protected Base { public: void access_base_members() { cout << public_var << endl; // 可以访问基类的公有成员 cout << protected_var << endl; // 可以访问基类的保护成员 //cout << private_var << endl; // 不能访问基类的私有成员 } }; int main() { Derived1 d1; d1.public_var = 1; // 可以访问基类的公有成员 //d1.protected_var = 2; // 不能访问基类的保护成员 //d1.private_var = 3; // 不能访问基类的私有成员 d1.access_base_members(); // 可以访问基类的公有成员和保护成员 Derived2 d2; //d2.public_var = 1; // 不能访问基类的公有成员 //d2.protected_var = 2; // 不能访问基类的保护成员 //d2.private_var = 3; // 不能访问基类的私有成员 d2.access_base_members(); // 可以访问基类的公有成员和保护成员 return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值