博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基类成员访问方式
阅读量:4695 次
发布时间:2019-06-09

本文共 1909 字,大约阅读时间需要 6 分钟。

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

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

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

1 #include
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 << "\nLength = " << length << "\nWidth = " << width;33 }34 protected:35 double length;36 double width;37 };38 39 class Cube:public Rectangle40 {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 << "\nhigh = " << 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

转载于:https://www.cnblogs.com/mch5201314/p/11607547.html

你可能感兴趣的文章
PHP 自动加载类 __autoload() 方法
查看>>
JDK中的Timer和TimerTask详解(zhuan)
查看>>
【python练习】ATM&购物商城程序
查看>>
nginx 日志问题(\x22)
查看>>
装饰器、迭代器、生成器
查看>>
类对象作为类成员
查看>>
面向对象和面向过程的区别及优劣对比详解
查看>>
const与指针
查看>>
thsi指针的一些用法及作用
查看>>
c++友元
查看>>
c++运算符重载
查看>>
一元运算符重载
查看>>
Windows 远程栈溢出挖掘
查看>>
(网页)the server responded with a status of 403 (Forbidden)
查看>>
葡萄城报表介绍:Java 报表
查看>>
android 通知消息一
查看>>
UNET学习笔记2 - 高级API(HLAPI)
查看>>
腾讯编程马拉松2012第一题
查看>>
Day18
查看>>
Web Service数据源
查看>>