例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. Flask中endpoint的理解

    在flask框架中,我们经常会遇到endpoint这个东西,最开始也没法理解这个到底是做什么的.最近正好在研究Flask的源码,也就顺带了解了一下这个endpoint 首先,我们看一个例子: @app ...

  2. eclipse 改包名

    转载自: http://www.2cto.com/kf/201304/206747.html 1.在项目上右键,选择android tools->rename application packa ...

  3. Oracle EBS-SQL (INV-4):检查负库存记录数.sql

    DEFINE DATE1="01/15/20** 23:59:59"      /*输入指定日期*/DEFINE CODE="%"                ...

  4. .NET日志工具介绍

    最近项目需要一个日志工具来跟踪程序便于调试和测试,为此研究了一下.NET日志工具,本文介绍了一些主流的日志框架并进行了对比.发表出来与大家分享. 综述 所谓日志(这里指程序日志)就是用于记录程序执行过 ...

  5. Android stagefright与opencore对比

      [转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...

  6. nodejs递归创建目录,同步和异步方法

    nodejs递归创建目录,同步和异步方法.在官方API中只提供了最基本的方法,只能创建单级目录,如果要创建一个多级的目录(./aaa/bbb/ccc)就只能一级一级的创建,感觉不是很方便,因此简单写了 ...

  7. user Collaborative Filtering

    ---恢复内容开始--- 算法步骤: 1.计算用户相似度 2.对于特定用户,选出k个最相似的用户,将这些用户评价过的前k好的物品推荐给该用户   用户相似度 度量: 其中|N(u)|表示用户u评价过的 ...

  8. python 如何判断对象是否为类(class)

    if type(att).__name__ == 'classobj':     pass else: pass

  9. php-GD库的函数(一)

    <?php //getimagesize - 取得图片的大小[即长与宽] //print_r(getimagesize("./logo_i.gif")); //Array ( ...

  10. Hibernate 多表关联映射- 一对多关系映射(one-to-many)

    Hibernage.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...