04737_C++程序设计_第6章_继承和派生
例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章_继承和派生的更多相关文章
- ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...
- ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...
- ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...
- 04737_C++程序设计_第10章_面向对象设计实例
10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...
- 04737_C++程序设计_第7章_类模板与向量
例7.1 使用类模板的实例. 例7.2 求4个数中最大值的类模板程序. #include <iostream> using namespace std; template <clas ...
- 04737_C++程序设计_第4章_类和对象
例4.1 描述点的Point类. 例4.2 根据上面对Point类的定义,演示使用Point类的对象. #define _SCL_SECURE_NO_WARNINGS #include <ios ...
- 04737_C++程序设计_第3章_函数和函数模板
例3.1 传对象不会改变原来对象数据成员值的例子. #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <str ...
- 04737_C++程序设计_第2章_从结构到类的演变
例2.1 使用成员函数的实例. #define _SCL_SECURE_NO_WARNINGS #include <iostream> using namespace std; struc ...
- 04737_C++程序设计_第1章_认识C++的对象
例1.1 演示使用结构对象的示例程序. //功能:将结构对象的两个域值相加,乘以2再加50 #include <iostream>//包含头文件 using namespace std;/ ...
随机推荐
- ApiGen4.1 windows安装教程
一. ApiGen4.1版本介绍 1.ApiGen介绍 ApiGen是自动生成PHP项目的阅读文档工具. 用于从PHP源代码创建专业的API文档,类似于phpDocumentor/phpDoc. Ap ...
- jupyter巨好玩-简介与安装
ipython notebook改名jupyter了而且更好玩更好用 jupyter简介 jupyter是啥啊? 这个要从ipython说起,ipython是个交互式的python的解释器,自带颜色, ...
- NetworkManager——Linux强大的网络管理工具
NetworkManager服务:NetworkManager - Linux Networking made Easy NetworkManager简介:NetworkManager由一个管理系统网 ...
- DenyHosts 安装及配置详解
DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP ...
- jquery上传图片
http://www.cnblogs.com/wutao/archive/2010/01/28/1658496.html http://www.cnblogs.com/cloudgamer/archi ...
- 基本的Logstash 例子
基本的Logstash 例子: 为了测试你的Logstash 安装,运行最基本的Logstash 管道: cd logstash-2.3.0 bin/logstash -e 'input { stdi ...
- 关于“找不到附属汇编 Microsoft.VC90.CRT,上一个错误是 参照的汇编没有安装在系统上。”的解决
关于“找不到附属汇编 Microsoft.VC90.CRT,上一个错误是 参照的汇编没有安装在系统上.”的解决 一个项目需要在Win系统用计划任务执行PHP,写了个批处理bat利用php-cgi.ex ...
- poj 1007 纯水题 排序
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> ...
- Android中Gallery显示手机中的图片
在网上找了好久似乎都没有关于这方面的(可能是自己的信息量太小吧),于是自己来填补这个漏洞吧. 常见的方法莫过于自己定义一个数组,用以存储图片的引用,如: 1 private Integer[] pic ...
- UVA 116 Unidirectional TSP 经典dp题
题意:找最短路,知道三种行走方式,给出图,求出一条从左边到右边的最短路,且字典序最小. 用dp记忆化搜索的思想来考虑是思路很清晰的,但是困难在如何求出字典序最小的路. 因为左边到右边的字典序最小就必须 ...