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;/ ...
随机推荐
- lua学习笔记(1)-基本语法
==============变量类型nilnumber(实数) 1 2 3.14 7.65e8string "hello world" "\n ...
- Oracle EBS-SQL (PO-11):检查采购订单退货数.sql
select msi.segment1 物料编码, -- msi.inventory_item_id return_it ...
- [Django 1.5] Django 开发学习资源链接
jQuery : jQuery API introduction:http://api.jquery.com/ jQuery plugins: http://benalman.com/projects ...
- C/C++ 用libcurl库进行http通讯网络编程
C/C++ 用libcurl库进行http通讯网络编程 目录索引: 一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_p ...
- Javascript基本算法演练 Seek and Destroy
转载自:http://aeroj-blog.logdown.com/posts/435808 Seek and Destroy You will be provided with an initial ...
- Android UI ActionBar功能-ActionBarSherlock 的使用
ActionBarSherlock实现了在ActionBar上添加一个下拉菜单的功能,也是App常用的功能之一: ActionBarSherlock是第三方提供的一个开源类库,下载地址:http:// ...
- 全国计算机等级考试二级教程-C语言程序设计_第16章_文件
写入一段文本到文件 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> main() { ...
- hdu 4602 Partition(矩阵快速幂乘法)
Problem Description Define f(n) , we have =+++ =++ =++ =++ =+ =+ =+ = totally ways. Actually, we wil ...
- UIImage 图片处理:截图,缩放,设定大小,存储
图片的处理大概就分 截图(capture), 缩放(scale),设定大小(resize), 存储(save)这几样比较好处理, 另外还有滤镜,擦试等, 以后再说在这个Demo code裡, 我写了几 ...
- sql 查询结果中加入空值列
select a,b,c,''d from X; 其中d为新加列,''表示默认值为空值 原文:http://hi.baidu.com/ddduggguo/item/747d5ba5ca18fd2689 ...