zoukankan      html  css  js  c++  java
  • 基类成员访问方式

    1.通过派生类的对象直接访问。前提是public继承

    2.通过基类成员函数直接访问基类成员。无论哪种继承方式。基类的public和private都不可以以这种方式访问

    3.通过基类名称访问被派生类重定义所隐藏的成员

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 class Rectangle
     5 {
     6     public:
     7         void setLength(double h) {
     8             length = h;
     9         }
    10         void setWidth(double w)
    11         {
    12             width = w;
    13         }
    14         double getLength()
    15         {
    16             return length;
    17         }
    18         double getWidth()
    19         {
    20             return width;
    21         }
    22         double area()
    23         {
    24             return width*length;
    25         }
    26         double volume()
    27         {
    28             return 0;
    29         }
    30         void print()
    31         {
    32             cout << "
    Length = " << length << "
    Width = " << width;
    33         }
    34     protected:
    35         double length;
    36         double width;
    37 };
    38 
    39 class Cube:public Rectangle
    40 {
    41     public:
    42         void setHigh(double h)
    43         {
    44             high = h;
    45         }
    46         double getHigh()
    47         {
    48             return high;
    49         }
    50         double area()
    51         {
    52             //重定义area函数 
    53             return 2*(width*length + width*high + high*length);
    54         }
    55         double volume()
    56         {
    57             return width*length*high;
    58         }
    59         
    60         void print()
    61         {
    62             Rectangle::print();
    63             cout << "
    high = " << high << endl;
    64         }
    65     private:
    66         double high;
    67 };
    68 
    69 int main()
    70 {
    71     Cube c1;
    72     c1.setLength(4);
    73     c1.setHigh(3);
    74     c1.setWidth(5);
    75     
    76     c1.Rectangle::print();
    77     c1.print();
    78     cout << "Cube area = " << c1.area() << endl;
    79     cout << "Cube bottom area = " << c1.Rectangle::area() << endl;
    80     cout << "Cube volume = " << c1.volume() << endl;
    81     return 0;
    82 }
    View Code

  • 相关阅读:
    VS2005编译mysql5.1.68
    用boost库实现traceroute小工具
    linux内核选项部分翻译
    linux 内核中的锁机制RCU
    先装windows 还是linux ?
    U盘装ubuntu
    编译linux内核3.0
    root密码丢失了怎么办?
    linux配置文件
    新一代linux文件系统btrfs
  • 原文地址:https://www.cnblogs.com/mch5201314/p/11607547.html
Copyright © 2011-2022 走看看