zoukankan      html  css  js  c++  java
  • 自考新教材-p193

    源程序:

    #include<iostream>
    using namespace std;

    class BaseClass //基类
    {
    protected:
    int v1, v2;
    public:
    void SetValue(int m, int n)
    {
    v1 = m;
    v2 = n;
    }
    void PrintValue();
    };

    void BaseClass::PrintValue()
    {
    cout << "v1=" << v1 << " v2=" << v2; //直接访问
    }

    class DerivedClass :public BaseClass //派生类
    {
    int v3; //私有成员变量
    public:
    void SetValue(int m, int n, int k)
    {
    v1 = m; //派生类中可以访问基类中的保护成员变量
    v2 = n;
    v3 = k; //本类成员变量
    }
    void SetValue(int m, int n)
    {
    v1 = m; //派生类中可以访问基类中的保护成员变量
    v2 = n;
    }
    void PrintValue();
    };

    void DerivedClass::PrintValue()
    {
    cout << endl;
    BaseClass::PrintValue(); //访问基类的公有成员函数
    cout << " v3=" << v3 << endl;
    }

    int main()
    {
    BaseClass baseCla;
    DerivedClass derivedCla;
    cout << "初始时的随机值:" << endl;
    baseCla.PrintValue();
    derivedCla.PrintValue();
    cout << "从派生类修改从基类继承的值及本类的值:" << endl;
    derivedCla.SetValue(10, 20);
    baseCla.PrintValue();
    derivedCla.PrintValue();
    cout << "从派生类修改从基类继承的值及本类的值:" << endl;
    derivedCla.SetValue(100, 200, 300);
    baseCla.PrintValue();
    derivedCla.PrintValue();
    system("pause");
    return 0;
    }

    运行结果:

  • 相关阅读:
    Eclipse workspace被锁定
    OpenWrt增加软件包
    多核cpu关闭、开启核心
    python基础-元组(tuple)及内置方法
    JS变量+作用域
    JS宣传页项目-综合实战
    JS实现轮播图特效(带二级导航)
    JS DOM属性+JS事件
    JS DOM操作(创建、遍历、获取、操作、删除节点)
    JS中BOM操作知识点
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12259852.html
Copyright © 2011-2022 走看看