例6.1

使用默认内联函数实现单一继承。

 #include<iostream>

 using namespace std;

 class Point
{
private:
int x, y;
public:
Point(int a, int b)
{
x = a;
y = b;
cout << "Point..." << endl;
}
void Showxy()
{
cout << "x=" << x << "y=" << y << endl;
}
~Point()
{
cout << "Delete Point" << endl;
}
}; class Rectangle :public Point
{
private:
int H, W;
public:
Rectangle(int a, int b, int h, int w) :Point(a, b)//构造函数初始化列表
{
H = h;
W = w;
cout << "Rectangle..." << endl;
}
void Show()
{
cout << "H=" << H << ",W" << W << endl;
}
~Rectangle()
{
cout << "Delete Rectangle" << endl;
}
}; void main()
{
Rectangle r1(, , , );
r1.Showxy();
r1.Show(); system("pause");
}

例6.2

演示使用protected成员。

 #include <iostream>

 using namespace std;

 class Point
{
protected:
int x, y;//声明保护数据成员
public:
Point(int a, int b)
{
x = a;
y = b;
}
void Show()
{
cout << "x=" << x << ",y=" << y << endl;//基类的Show()函数
}
}; class Rectangle :public Point
{
private:
int H, W;
public:
Rectangle(int, int, int, int);//构造函数原型
void Show()
{
cout << "x=" << x << ",y=" << y << ",H=" << H << ",W=" << W << endl;
}
}; Rectangle::Rectangle(int a, int b, int h, int w) :Point(a, b)//定义构造函数
{
H = h;
W = w;
} void main()
{
Point a(, );
Rectangle r1(, , , );
a.Show();//基类对象调用基类Show()函数
r1.Show();//派生类对象调用派生类Show()函数 system("pause");
}

例6.3

使用Point和Rectangle类演示赋值兼容规则的例子。

 #include <iostream>

 using namespace std;

 class Point
{
protected:
int x, y;//声明保护数据成员
public:
Point(int a, int b)
{
x = a;
y = b;
}
void Show()
{
cout << "x=" << x << ",y=" << y << endl;//基类的Show()函数
}
}; class Rectangle :public Point
{
private:
int H, W;
public:
Rectangle(int, int, int, int);//构造函数原型
void Show()
{
cout << "x=" << x << ",y=" << y << ",H=" << H << ",W=" << W << endl;
}
}; Rectangle::Rectangle(int a, int b, int h, int w) :Point(a, b)//定义构造函数
{
H = h;
W = w;
} void main()//演示公有继承的赋值兼容性规则
{
Point a(, );//基类对象a
Rectangle b(, , , );//派生类对象b
a.Show();
b.Show(); Point& ra = b;//派生类对象初始化基类的引用
ra.Show();//实际调用的是基类的Show函数 Point *p = &b;//派生类对象的地址赋给指向基类的指针
p->Show();//实际调用的是基类的Show函数 Rectangle *pb = &b;//派生类指针pb
pb->Show();//调用派生类的Show函数 a = b;//派生类对象的属性值更新基类对象的属性值
a.Show(); system("pause");
}

例6.5

演示多重继承的例子。

 #include <iostream>

 using namespace std;

 class A
{
private:
int a;
public:
void setA(int x)
{
a = x;
}
void showA()
{
cout << "a=" << a << endl;
}
}; class B
{
private:
int b;
public:
void setB(int x)
{
b = x;
}
void showB()
{
cout << "b=" << b << endl;
}
}; class C :public A, public B
{
private:
int c;
public:
void setC(int x, int y)
{
c = x;
setB(y);
}
void showC()
{
showB();
cout << "c=" << c << endl;
}
}; void main()
{
C obj; obj.setA();
obj.showA();//输出a=53
obj.setC(, );
obj.showC();//输出b=58 c=55 system("pause");
}

例6.6

访问具有二义性的例子。

 #include <iostream>

 using namespace std;

 class A
{
public:
void func()
{
cout << "a.func" << endl;
}
}; class B
{
public:
void func()
{
cout << "b.func" << endl;
}
void gunc()
{
cout << "b.gunc" << endl;
}
}; class C :public A, public B
{
public:
void gunc()
{
cout << "c.gunc" << endl;
}
void hun1()
{
A::func();
}
void hun2()
{
B::func();
}
}; void main()
{
C obj; obj.A::func();//输出a.func
obj.B::func();//输出b.func
obj.B::gunc();//输出b.func
obj.C::gunc();//输出c.gunc obj.gunc();//输出c.gunc
obj.hun1();//输出a.func
obj.hun2();//输出b.func system("pause");
}

123

04737_C++程序设计_第6章_继承和派生的更多相关文章

  1. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  2. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  3. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  4. 04737_C++程序设计_第10章_面向对象设计实例

    10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...

  5. 04737_C++程序设计_第7章_类模板与向量

    例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. #include <iostream> using namespace std; template <clas ...

  6. 04737_C++程序设计_第4章_类和对象

    例4.1 描述点的Point类. 例4.2 根据上面对Point类的定义,演示使用Point类的对象. #define _SCL_SECURE_NO_WARNINGS #include <ios ...

  7. 04737_C++程序设计_第3章_函数和函数模板

    例3.1 传对象不会改变原来对象数据成员值的例子. #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <str ...

  8. 04737_C++程序设计_第2章_从结构到类的演变

    例2.1 使用成员函数的实例. #define _SCL_SECURE_NO_WARNINGS #include <iostream> using namespace std; struc ...

  9. 04737_C++程序设计_第1章_认识C++的对象

    例1.1 演示使用结构对象的示例程序. //功能:将结构对象的两个域值相加,乘以2再加50 #include <iostream>//包含头文件 using namespace std;/ ...

随机推荐

  1. PHP学习日记(一)——类、函数的使用

    一.自定义函数 function add($a,$b){ $c=$a+$b; echo 'add test:'; echo $c; return $c; } add(1,2); 输出结果: add t ...

  2. conda配置python混合开发环境一站式入门【全平台】

    下载安装 清华的镜像 [https://mirror.tuna.tsinghua.edu.cn/help/anaconda/] 官方说明 [http://conda.pydata.org/docs/u ...

  3. DLL中传递STL参数,vector对象作为dll参数传递等问题(转)

    STL跨平台调用会出现很多异常,你可以试试. STL使用模板生成,当我们使用模板的时候,每一个EXE,和DLL都在编译器产生了自己的代码,导致模板所使用的静态成员不同步,所以出现数据传递的各种问题,下 ...

  4. EF 6 调用存储过程时返回多结果集和OUTPUT参数问题

    原文地址:http://q.cnblogs.com/q/56836/ 各位大侠,提问一个关于EF6调用存储过程时返回多结果集和OUTPUT参数问题 目前已经可以调用存储过程并且可以返回多个结果集. 但 ...

  5. Guava缓存器源码分析——缓存统计器

    Guava缓存器统计器实现: 全局统计器——         1.CacheBuilder的静态成员变量Supplier<StatsCounter> CACHE_STATS_COUNTER ...

  6. HDOJ1166 敌兵布阵

    赤裸裸的线段树,借个模板,改写一下即可. 代码: #include<iostream> #include<cstdio> #include<stdio.h> #in ...

  7. python升级导致的坑

    问题来源 问题往往都是这样来的突然,让我措手不及. 小孩没娘说来话长啊,操作系统是centos6.5因此默认自带的python是2.6.6的,突然有一天我要写一个关于kafka topic消费情况的监 ...

  8. 任务管理器进程中多个chrome.exe的问题

    偶然发现任务管理器进程中有多个chrome.exe进程,非常奇怪自己仅仅打开了一次浏览器,为什么会有多个?! 上网一查才发现:原来使用Google浏览器Google Chrome每开一个新标签页面,都 ...

  9. ViewDragHelper练习使用

    转载博客地址:http://www.cnblogs.com/flyme2012/p/4076674.html 这个Demo是用来练习VIewDragHelper的,也是仿照网上一个大神的代码.我通过他 ...

  10. Acess错误:"文件共享锁定数溢出"

    对于ACCESS数据库,如果通过大量的SQL来操作数据库或者直接操作大量的数据时,经常会出现这种错误: "文件共享锁定数溢出" 原因如下:     Access数据库,同时操作大量 ...