c++课件第十一章继承与派生

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

c++课件第⼗⼀章继承与派⽣第11章继承和派⽣
11.1继承与派⽣的概念
图11.1 图11.2
本书中的约定,箭头表⽰继承的⽅向,从派⽣类指向基类。

图11.3
图11.4
图11.5
11.2派⽣类的声明⽅式
class Student
{public:
void get_value(){cin>>num>>name>>sex;}
void display( )
{ cout<<"num: "<
cout<<"name: "<
cout<<"sex: "<
}
private :
int num;
string name;
char sex;
};
class Student1: public Student
{public:
void get_value_1(){cin>>age>>addr;}
void display_1()
{ cout<<"age:"<
cout<<"address: "<
int age;
string addr;
};
继承⽅式有:public(公有),private(私有),protected(保护) 11.3派⽣类的构成
图11.6
11.4派⽣类成员的访问属性
11.4.1公⽤继承
例11.1公有继承实例。

#include
#include
using namespace std;
class Student
{public:
void get_value(){cin>>num>>name>>sex;}
void display( )
{ cout<<"num: "<
cout<<"name: "<
cout<<"sex: "<
}
protected :
int num;
string name;
char sex;
};
class Student1: public Student
{public:
void get_value_1(){cin>>age>>addr;}
void display_1()
{ //cout<<"num: "<
//企图引⽤基类的私有成员,错误 //cout<<"name: "<
//企图引⽤基类的私有成员,错误 //cout<<"sex: "<
//引⽤派⽣类的私有成员,正确
cout<<"address: "<
//引⽤派⽣类的私有成员,正确private: int age;
string addr;
};
int main()
{
Student1 stud1;
stud1.get_value();
stud1.get_value_1();
stud1.display();
stud1.display_1();
return 0;
}
1001 huang F 18 Beijing
num: 1001
name: huang
sex: F
age: 18
address: Beijing
#include
class Location
{
private:
int X,Y;
public:
int GetX(){return X;}
int GetY(){return Y;}
int Z;
protected:
int P;
};
class Rectangle:public Location
int Height,Width;
public:
// int GetHH(){X=5;return Height;}
//在公有派⽣中基类的私有成员在派⽣类中是不可访问的int GetH(){cout<
//在公有派⽣中基类的保护成员在派⽣类中还是保护成员}; class test:public Rectangle
{
void fun2(){P=89;}//P在test类中是可访问的
void fun1(){Z=66;}//Z在test类中是可访问的
};
void fun()
{
Rectangle r; //定义对象r
int Xvalue=r.GetX(); //使⽤基类返回r的X值
int h=r.GetH(); //通过派⽣类返回r的H值
r.Z=99; //在公有派⽣中基类的公有成员在
//派⽣类中还是公有成员
r.P=109;//在公有派⽣中基类的保护成员
//在派⽣类中还是保护成员
}
void main()
{
fun();
}
11.4.2私有继承
图11.7
例11.2 私有继承实例
#include
#include
using namespace std;
using namespace std;
class Student
{public:
void display( )
{ cout<<"num: "<
cout<<"name: "<
cout<<"sex: "<
}
private :
int num;
string name;
char sex;
};
class Student1: private Student
{
public:
void display_1()
{ display();//调⽤基类公有成员函数,//在派⽣类中成为私有成员
cout<<"age: "<
//引⽤派⽣类的私有成员,正确cout<<"address: "<
//引⽤派⽣类的私有成员,正确private:
int age;
string addr;
};
int main()
{ Student1 stud1;
//stud1.display();//错误
stud1.display_1();
return 0;
}
class Location
{
private:
int X,Y;
public:
i nt GetX(){return X;}
i nt GetY(){return Y;}
i nt Z;
protected:
i nt P;
};
class Rectangle:private Location
{
private:
int Height,Width;
public:
// int GetHH(){X=5;return Height;}
//在私有派⽣中基类的私有成员在派⽣类中是不可访问的
i nt GetH(){cout<
//在私有派⽣中基类的公有成员在派⽣类中是私有成员i nt GetW(){P=55;return Width;}
//在私有派⽣中基类的保护成员在派⽣类中是私有成员};
class test:public Rectangle
{
// void fun2(){P=89;}//P在test类中是不可访问的
// void fun1(){Z=66;}//Z在test类中是不可访问的
};
void fun()
{
Rectangle r; //定义对象r
//int Xvalue=r.GetX(); //使⽤基类返回r的X值
int h=r.GetH(); //通过派⽣类返回r的H值
//r.P=109;
//在私有派⽣中基类的保护成员在派⽣类中还是私有成员}
void main()
{
f un();
}
11.4.3保护成员和保护继承
例11.3在派⽣类中引⽤保护成员。

#include
#include
using namespace std;
class Student //声明基类
{public: //基类公⽤成员
void display( );
protected : //基类保护成员
int num;
string name;
char sex;
};
void Student::display( )
{ cout<<"num: "<
cout<<"name: "<
cout<<"sex: "<
}
class Student1: protected Student
//⽤protected继承⽅式声明⼀个派⽣类
{public:
void display1( );
private:
int age;
string addr;
void Student1::display1( )
{ cout<<"num: "<
//引⽤基类的保护成员,合法
cout<<"name: "<
//引⽤基类的保护成员,合法
cout<<"sex: "<
//引⽤基类的保护成员,合法
cout<<"age: "<
//引⽤派⽣类的私有成员,合法cout<<"address: "<
//引⽤派⽣类的私有成员,合法
}
int main( )
{ Student1 stud1;
//stud1是派⽣类student2的对象stud1.display1( );
//display是派⽣类中的保护成员函数return 0;
}
#include
class Location
{
private:
int X,Y;
public:
i nt GetX(){return X;}
i nt GetY(){return Y;}
i nt Z;
protected:
i nt P;
};
class Rectangle:protected Location {
private:
public:
// int GetHH(){X=5;return Height;}
//在保护派⽣中基类的私有成员在派⽣类中是不可访问的
i nt GetH(){cout<
//在保护派⽣中基类的公有成员在派⽣类中是保护的
i nt GetW(){P=55;return Width;}
//在保护派⽣中基类的保护成员在派⽣类中是保护的
};
class test:public Rectangle
{
v oid fun2(){P=89;}
v oid fun1(){Z=66;}
};
class test_test:public test
{
v oid fun3(){P=89;}//证明P是保护的
v oid fun4(){Z=66;}//证明Z是保护的
};
void fun()
{
Rectangle r; //定义对象r
int h=r.GetH(); //通过派⽣类返回r的H值
//r.Z=99;//在保护派⽣中基类的公有成员在派⽣类中是保护的 //r.P=109;//在保护派⽣中基类的公有成员在派⽣类中是保护的}
void main()
{
f un();
t est t1;
// t1.P=90;//错误证明P不是公有的
// t1.Z=79;//错误证明Z不是公有的
}
11.4.4多级派⽣时的访问属性
例11.4 多级派⽣时的访问属性
class A //基类
protected: void f2(); int j;
private: int k;
};
class B: public A //public⽅式
{public: void f3( );
protected: void f4();
private: int m;
};
class C: protected B //protected⽅式
{public: void f5();
private: int n;
}; i f2 j k
基类A
派⽣类B
派⽣类C
11.5派⽣类的构造函数和析构函数
11.5.1简单的派⽣类的构造函数
例11.5 简单的派⽣类的构造函数
#include
#include
using namespace std;
class Student //声明基类
{public: //公⽤部分
Student(int n,string nam,char s )//基类构造函数
{num=n;
name=nam;
sex=s; }
~Student( ) { }//基类析构函数
protected: //保护部分
int num;
string name;
char sex ;
};
class Student1: public Student//声明公⽤派⽣类student {public:
{age=a;//在函数体中只对派⽣类新增的数据成员初始化addr=ad;
}
void show( )
{cout<<"num: "<
cout<<"name: "<
cout<<"sex: "<
cout<<"age: "<
cout<<"address: "<
}
~Student1( ) { } //派⽣类析构函数
private: //派⽣类的私有部分
int age;
string addr;
};
int main( )
{ Student1 stud1(10010,"Wang-li",'f',19,
"115 Beijing Road,Shanghai");
Student1 stud2(10011,"Zhang-fun",'m',21,
"213 Shanghai Road,Beijing");
stud1.show( ); //输出第⼀个学⽣的数据
stud2.show( ); //输出第⼆个学⽣的数据
return 0;
}
num: 10010
name: Wang-li
sex: f
age: 19
address: 115 Beijing Road,Shanghai
num: 10011
name: Zhang-fun
sex: m
age: 21
address: 213 Shanghai Road,Beijing
图11.10
图11.11
11.5.2有⼦对象的派⽣类的构造函数
例11.6包含⼦对象的派⽣类的构造函数。

#include
#include
using namespace std;
class Student //声明基类
{public: //公⽤部分
Student(int n,string nam)//基类构造函数
{num=n;name=nam;}
void display() //输出基类数据成员
{cout<<"num:"<
int num;
string name;
};
class Student1: public Student
//⽤public继承⽅式声明派⽣类student
{public:
Student1(int n,string nam,int n1,string nam1,int a, string ad):Student(n,nam),monitor(n1,nam1) //派⽣类构造函数
{age=a; //在此处只对派⽣类新增的数据成员初始化
addr=ad;
}
void show( )
{ cout<<"This student is:"<
display(); //输出num和name
cout<<"age: "<
cout<<"address: "<
}
void show_monitor()//输出⼦对象的数据成员
{ cout<
monitor.display();//调⽤基类成员函数
}
private: //派⽣类的私有数据
Student monitor; //定义⼦对象(班长)
int age;
string addr;
};
int main( )
{ Student1 stud1(10010,"Wang-li",10001,"Li-sun",19, "115 Beijing Road,Shanghai"); stud1.show( );//输出第⼀个学⽣的数据
stud1.show_monitor(); //输出⼦对象的数据
return 0;
}
This student is:
num:10010
name:Wang-li
age: 19
address: 115 Beijing Road,Shanghai
Class monitor is:
num:10001
name:Li-sun
图11.12
执⾏派⽣类构造函数的顺序是:
(1)调⽤基类构造函数,对基类数据成员初始化。

(2)调⽤⼦对象的构造函数,对⼦对象的数据成员初始化。

(3)执⾏派⽣类构造函数,对派⽣类数据成员初始化。

11.5.3多层派⽣时的构造函数
例11.7
#include
#include
using namespace std;
class Student //声明基类
{public: //公⽤部分
Student(int n, string nam)//基类构造函数
{num=n;name=nam;}
void display() //输出基类数据成员
{ cout<<"num:"<
cout<<"name:"<
}
protected: //保护部分
int num; //基类有两个数据成员
string name;
};
class Student1: public Student//声明公⽤派⽣类Student1 {public:
Student1(int n,string nam,int a):Student(n,nam)
//派⽣类构造函数
{age=a; } //在此处只对派⽣类新增的数据成员初始化 void show( ) //输出num,name和age { display(); //输出num和name
cout<<"age: "<
}
private: //派⽣类的私有数据
int age; //增加⼀个数据成员
};
class Student2:public Student1 //声明间接公⽤派⽣类student2
{public:
//下⾯是间接派⽣类构造函数
Student2(int n,string nam,int a,int s):Student1(n,nam,a)
{score=s;}
void show_all() //输出全部数据成员
{ show(); //输出num和name
cout<<"score:"<
}
private:
int score;//增加⼀个数据成员
};
int main( )
{ Student2 stud(10010,"Li",17,89);
stud.show_all( ); //输出学⽣的全部数据
return 0;
}
num:10010
name:Li age: 17 score:89。

相关文档
最新文档