Upload
xanthe
View
148
Download
0
Embed Size (px)
DESCRIPTION
第 11 章 继承与派生. 要点: 类的继承与派生的基本概念 三种继承的方法 多继承类的使用 重点内容: C++ 继承与派生的基本概念及三种继承方法. 一、 继承与派生的基本概念. 1 .继承的概念 在 C++ 语言中,可以从一个类派生出另一个类。派生出其它类的类称为基类,又称为父类。被派生的类称为派生类,又称为子类。派生类可以具有基类的特性,共享基类的成员函数,使用基类的数据成员,还可以定义自己的数据成员和成员函数。一个派生类可以从一个基类派生,也可以从多个基类派生。从一个基类派生的继承称为单继承;从多个基类派生的继承称为多继承。. - PowerPoint PPT Presentation
Citation preview
第 11 章 继承与派生要点: 类的继承与派生的基本概念 三种继承的方法 多继承类的使用重点内容: C++ 继承与派生的基本概念及三种继承方法
一、继承与派生的基本概念
1 .继承的概念 在 C++ 语言中,可以从一个类派生出另一个类。派
生出其它类的类称为基类,又称为父类。被派生的类称为派生类,又称为子类。派生类可以具有基类的特性,共享基类的成员函数,使用基类的数据成员,还可以定义自己的数据成员和成员函数。一个派生类可以从一个基类派生,也可以从多个基类派生。从一个基类派生的继承称为单继承;从多个基类派生的继承称为多继承。
一、继承与派生的基本概念
2 .派生类的定义格式单继承的定义格式如下:class < 派生类名 > : < 继承方式 >< 基类名 >{public : members ; // 派生类新定义成员<private : > members ;<protected : > members ;} ;
一、继承与派生的基本概念
多继承的定义格式如下:class< 派生类名 > : < 继承方式 1>< 基类名 1> , < 继承
方式 2>< 基类名 2> ,…{ public : // 派生类新定义成员 members ; <private : > members ; <protected : > members ;} ;
二、继承的基本方式
继承方式主要表示派生类继承基类时采用的继承方式,主要有三种,即公有继承、私有继承和保护继承。1 .公有继承( public ) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时都保持原有的状态,而基类的私有成员仍然是私有的。
二、继承的基本方式
例 11.1 :#include<iostream.h>class A{ private: int x; public: void setx(int i){x=i;} void showx() { cout<<x<<endl; }};
class B:public A
{
private:
int y;
public:
void sety(int i){y=i;}
void showy()
{
showx();
cout<<y<<endl;
}
};
void main()
{
B b;
b.setx(10);
b.sety(20);
b.showy();
}
二、继承的基本方式
2 .私有继承( private ) 私有继承的特点是基类的公有成员和保护成员作
为派生类的私有成员,并且不能被这个派生类的子类访问。
二、继承的基本方式
例 11.2#include<iostream.h>class A{ private: int x; public: void setx(int i){x=i;} void showx(){cout<<x<<endl;}};
class B:private A{ private:
int y; public:
void sety(int m,int n){ setx(m); y=n;}
void showxy(){
showx(); cout<<y<<endl;
}};
void main(){ A a;
a.setx(10);a.showx();B b;b.sety(10,20);b.showx();
b.showxy();}
二、继承的基本方式
3. 保护继承( protected ) 保护继承的特点是基类的所有公有成员和保护成
员都成为派生类的保护成员,并且只能被它的派生类成员函数或友元访问,基类的私有成员仍然是私有的。
例 11.3#include<iostream.h>class A{
private: int x; public: void setx(int i){x=i;} void showx(){cout<<x<<endl;}};
class B:protected A{ private:
int y; public:
void sety(int m,int n){
setx(m); y=n;
}
void showxy()
{
showx();
cout<<y<<endl;
}
};
class C:private B{
public: void set(int m) {
setx(m); }
void show() { showx(); }
};
void main(){ A a; a.setx(10); a.showx(); B b; b.sety(10,20); b.showxy(); C c; c.set(50); c.show();}
练习:编一个设定长方形边长的类,利用继承机制求长方形的面积及周长。
#include<iostream.h>class leng{
protected: int x,y; public: void init(int a,int b)
{ x=a; y=b;}
};
class area:public leng{ private:
int s,t; public:
void setarea(){
s=x*y; }
void setgirth(){ t=2*(x+y);}
void show(){
cout<<"area="<<s<<endl; cout<<"girth="<<t<<endl;
}};void main(){ area m; m.init(5,8); m.setarea (); m.setgirth (); m.show ();}
三、多继承
有多个基类的派生类的继承称为多继承。多继承可以看作是单继承的扩展。
多继承的定义格式如下:class< 派生类名 > : < 继承方式 1>< 基类名 1> , < 继承方
式 2>< 基类名 2> ,…{
< 派生类新定义的成员 >
} ;
例:#include<iostream.h>class B1{ private: int b1; public: void setb1(int i) { b1=i;} void showb1() { cout<<“b1=“<<b1<<endl; }};
class B2{ private: int b2; public: void setb2(int i) { b2=i;} void showb2() { cout<<“b2=“<<b2<<endl; } };
class D:public B1,public B2
{
private:
int d;
public:
void setd(int i,int j)
{
d=i;
setb2(j);
}
void showd()
{
showb2();
cout<<“d=“<<d<<endl;
}
};
void main()
{
D objd;
objd.setb1(10);
objd.showb1();
objd.setd(15,20);
objd.showd();
}
#include<iostream.h>
class A1
{
private:
int x1,x2;
public:
void init1(int n1,int n2){x1=n1,x2=n2;}
void set1() { x1++; x2--; } void show1() {
cout<<"x1="<<x1<<endl; cout<<"x2="<<x2<<endl;
}};
class A2:public A1{ private: int x3; public: void init2(int n){x3=n;} void set2() { x3=x3*x3; }
void show2()
{
show1();
cout<<"x3="<<x3<<endl;
}
};
class A3:public A2{private: int x4;public: void init3(int n){x4=n;} void set3() { x4=x4*3;} void show3(){ show1(); show2(); cout<<"x4="<<x4<<endl;} };
void main(){ A1 m; m.init1(3,7); m.set1(); m.show1(); A3 t; t.init2(15); t.init3(5); t.set2(); t.set3(); t.show3();}
归纳与思考 继承是面向对象程序设计的重要特性之一,是实现代码重用的重要手段,本章介绍了继承与派生的基本概念。
在继承与派生中,有三种继承的方式,分别是私有继承、公有继承及保护继承,它们有什么区别?
对于具有多个基类的继承称为多继承,多继承与单继承有什么相同点,有什么不同点?