继承

一.基类和派生类概念
1.继承的概念:在已有类的基础上创建新类的过程
一个 B 类继承A类,或称从类 A 派生类 B。类 A 称为基类(父类),类 B 称为派生类(子类)

2.类继承关系的语法形式
class 派生类名 : 基类名表
{
数据成员和成员函数声明
};

  1. 基类名表 构成
    访问控制 基类名1, 访问控制 基类名2 ,… , 访问控制 基类名n

4.访问控制 表示派生类对基类的继承方式,使用关键字:
public 公有继承
private 私有继承
protected 保护继承
注意:不论种方式继承基类,派生类都不能直接使用基类的私有成员 。

5.派生类的生成过程
●吸收基类成员(全部吸收(构造、析构除外),但不一定可见)
●改造基类成员
●添加派生类新成员

6.派生类对象结构

#include <iostream>
using namespace std;
class A{
public:
	int a;
	int b;
private:
	int c;
protected:
	int d;
};
class B: public A
{	      int c;      };
main( ){
	cout << ” size of A is” << sizeof(A);
	cout << ” size of B is” << sizeof(B); }

二.关于重名成员
派生类定义了与基类同名的成员,在派生类中访问同名成员时屏蔽(hide)了基类的同名成员。在派生类中使用基类的同名成员,显式地使用类名限定符:
类名 :: 成员

#include<iostream>
using namespace std ;
class A
{ public:	  
       int a1, a2 ;
      A( int i1=0, int i2=0 ) { a1 = i1; a2 = i2; }
      void print() 
         { cout << "a1=" << a1 << '\t' << "a2=" << a2 << endl ; }
};
class B : public A
{ public:	
       int b1, b2 ;
       B( int j1=1, int j2=1 ) { b1 = j1; b2 = j2; }
       void print()		//定义同名函数
         { cout << "b1=" << b1 << '\t' << "b2=" << b2 << endl ; }
void printAB()
        { A::print() ;		//派生类对象调用基类版本同名成员函数
           print() ;		//派生类对象调用自身的成员函数
       }
};
int main()
{ B  b ;        b.A::print();	b.printAB();  }

三.派生类中访问静态成员
基类定义的静态成员,将被所有派生类共享(基类和派生类共享基类中的静态成员)。根据静态成员自身的访问特性和派生类的继承方式,在类层次体系中具有不同的访问性质
派生类中访问静态成员,用以下形式显式说明:
类名 :: 成员
或通过对象访问 对象名 . 成员

#include<iostream>
using namespace std ;
class B
{ public:
    static void Add() { i++ ; }
    static int i;
    void out() { cout<<"static i="<<i<<endl; }
};
int B::i=0;
class D : private B
{ public:    
      void f() 
       { i=5;
         Add();
         B::i++;
         B::Add();
       }
};
int main()
{ B x;  D y;
  x.Add();
  x.out();
  y.f();
  cout<<"static i="<<B::i<<endl;
  cout<<"static i="<<x.i<<endl;
  //cout<<"static i="<<y.i<<endl;
}

四.基类的初始化
在创建派生类对象时用指定参数调用基类的构造函数来初始化派生类继承基类的数据。派生类构造函数声明为
派生类构造函数 ( 变元表 ) : 基类 ( 变元表 ) , 对象成员1( 变元表 ) … 对象成员n ( 变元表 ) ;
构造函数执行顺序:基类  对象成员 派生类

#include<iostream>
using namespace std ;
class  parent_class
{     int  data1 , data2 ;
   public :
       parent_class ( int  p1 , int  p2 ) { data1 = p1; data2 = p2; }
       int  inc1 () { return  ++ data1; }
       int  inc2 () { return  ++ data2 ; }
       void  display  ()  {cout << "data1=" << data1 << " , data2=" << data2 << endl ; }
};
class  derived_class : private  parent_class
{     int  data3 ;
       parent_class  data4 ;
   public:
       derived_class ( int  p1 , int  p2 , int  p3 , int  p4 , int  p5 ): parent_class ( p1 , p2 ) , data4 ( p3 , p4 )
           { data3 = p5 ; }
int  inc1 ( ) { return  parent_class :: inc1 ( ) ; }
       int  inc3 ( ) { return  ++ data3 ; }
       void  display ( )
          { parent_class :: display ( ) ;   data4.display ( ) ;
             cout << "data3=" << data3 << endl ;
          }
} ;
int main ( )
{ derived_class  d1 ( 17 , 18 , 1 , 2 , -5 ) ;   d1 . inc1 ( ) ;     d1 . display ( ) ;  }

五.派生类构造函数和析构函数的定义规则
1.基类的构造函数和析构函数不能被继承
2.如果基类没有定义构造函数或有无参的构造函数, 派生类也可以不用定义构造函数
3.如果基类无无参的构造函数,派生类必须定义构造函数
4.如果派生类的基类也是派生类,则每个派生类只负责直接基类的构造
5.派生类是否定义析构函数与所属的基类无关

派生类构造函数的定义
在C++中,派生类构造函数的一般格式为:
派生类::派生类名(参数总表):基类名(参数表)
{
// 派生类新增成员的初始化语句
}
注意:这是基类有构造函数且含有参数时使用.

派生类析构函数
(1)当派生类中不含对象成员时
●在创建派生类对象时,构造函数的执行顺序是:基类的构造函数→派生类的构造函数;
●在撤消派生类对象时,析构函数的执行顺序是:派生类的析构函数→基类的析构函数。
(2)当派生类中含有对象成员时
●在定义派生类对象时,构造函数的执行顺序:基类的构造函数→对象成员的构造函数→派生类的构造函数;
●在撤消派生类对象时,析构函数的执行顺序:派生类的析构函数→对象成员的析构函数→基类的析构函数。

class B
{
public:
    B() { cout<<"B()"<<endl; }
    ~B() { cout<<"~B()"<<endl; }
};
class D : public B
{
public:
    D(){ cout<<"D()"<<endl; }
    ~D() { cout<<"~D()"<<endl; }
};
int main()
{
    D d;
    return 0;
}

六.多继承
1.一个类有多个直接基类的继承关系称为多继承
多继承声明语法
class 派生类名 : 访问控制 基类名1 , 访问控制 基类名2 , … , 访问控制 基类名n
{
数据成员和成员函数声明
};

多继承的简单应用

class Base1
{ public:
      Base1(int x) { value = x ; }
      int getData() const { return value ; }
   protected:
      int value;
};
class Base2
{ public:
      Base2(char c) { letter=c; }
      char getData() const { return letter;}
   protected:
      char letter;
};
class Derived : public Base1, public Base2
{   friend ostream &operator<< ( ostream &, const Derived & ) ;
   public :
      Derived ( int, char, double ) ;
      double getReal() const ;
   private :
      double real ;
};
int main()
{ Base1 b1 ( 10 ) ;
   Base2 b2 ( 'k' ) ;
   Derived d ( 5, 'A', 2.5 ) ;
      :
   return ;
}

2.多继承的构造函数
派生类名(参数总表):基类名1(参数表1),基类名2(参数表2),…,基类名n(参数表n)
{
// 派生类新增成员的初始化语句
}

七.虚基类
1.概念:
如果一个派生类从多个基类派生,而这些基类又有一个共同的基类,则在对该基类中声明的名字进行访问时,可能产生二义性。
如果在多条继承路径上有一个公共的基类,那么在继承路径的某处汇合点,这个公共基类就会在派生类的对象中产生多个基类子对象。
要使这个公共基类在派生类中只产生一个子对象,必须对这个基类声明为虚继承,使这个基类成为虚基类。
虚继承声明使用关键字 virtual

#include<iostream>
using namespace std ;
class  A
{ public :
       A ( ) { cout << "class A" << endl ; } 
} ;
class  B :  public  A
{ public :  
       B ( ) {cout << "class B" << endl ;  } 
} ;
class  C :  public  A
{ public :
       C ( ) {cout << "class C" << endl ;  }
} ;
class  D :  public  B ,  public  C
{ public :  
       D ( ) {cout << "class D" << endl ; } 
} ;
int main ( )
{ D  dd  ;  }

八.例题
1.定义一个基类person(不定义构造函数):姓名、性别、年龄(访问权限设置为私有);定义公有的成员函数set_p();定义公有的成员函数display_p(),显示person的信息。
再由基类派生出学生类(不定义构造函数,采用公有继承的方式):增加学号、班级、专业和入学成绩 ;定义公有成员函数set_t();定义成员函定义公有的成员函数display_s(),显示所有的信息。

#include<iostream>
#include <string>
using namespace std;
class Person
{

	string name;
	int age;
	string sex;
public:
	void set_p()	{
		cout<<"name\tage\tsex"<<endl;
		cin>>name>>age>>sex;
	}
	void show_p()	{
 		cout<<name<<"  "<<age<<"  "<<sex<<endl;
	}
};
class student :public Person
{
	string no;
	string zhuanye;
	string t_class;
	float score;
public:
	void set_t(){
  	    set_p(); //调用继承于基类的成员函数访问继承于基类的私有数据成员
	    cout<<"zhuanye\tt_class\tscore"<<endl;
	    cin>>zhuanye>>t_class>>score;
	}
	void show_t()	{
		show_p();
		cout<<zhuanye<<"  "<<t_class<<"  "<<score<<endl;
	}
};

2.考察一个点、圆、圆柱体的层次结构

class Point
{   friend ostream &operator<< (ostream &, const Point &);
  public:
    Point( int = 0, int = 0 ) ;	// 带默认参数的构造函数
    void setPoint( int, int ) ;	// 对点坐标数据赋值
    int getX() const { return x ; }	    int getY() const { return y ; }
  protected:    int x, y;	// Point类的数据成员
};
class Circle : public Point
{   friend ostream &operator<< (ostream &, const Circle &);	// 友元函数
  public:
    Circle(double r=0.0, int x=0, int y=0);	// 构造函数
    void setRadius(double);  /*置半径*/          double getRadius() const;     /*返回半径*/ 
    double area() const;		// 返回面积
  protected:    double radius;	// 数据成员,半径
};
class Cylinder:public Circle
{    friend ostream & operator<<(ostream &, const Cylinder &);    // 友元函数
   public:
     Cylinder(double h=0.0, double r=0.0, int x=0, int y=0);      // 构造函数
     void setHeight(double);    /* 置高度值*/           double getHeight() const;	    /* 返回高度值*/
     double area() const;	     /* 返回面积*/            double volume() const;	    /* 返回体积*/
   protected:     double height;	// 数据成员,高度
};
// Point 类的成员函数 

// 构造函数,调用成员函数对 x,y作初始化
Point::Point ( int a, int b ) 
    { setPoint ( a , b ) ; } 
// 对数据成员置值
void Point :: setPoint ( int a, int b )  { x = a ;  y = b ; }
// 重载插入算符,输出对象数据
ostream &operator<< ( ostream &output , const Point &p )
{ output << '[' << p.x << "," << p.y << "]"  ;
     return output ;
}
// Circle 类的成员函数 
// 带初始化式构造函数,首先调用基类构造函数
Circle::Circle( double r, int a, int b ): Point( a, b )  { setRadius ( r ); }
// 对半径置值
void Circle::setRadius ( double r )  { radius = ( r >= 0 ? r : 0 ); }
// 返回半径值
double Circle::getRadius() const { return  radius; }
// 计算并返回面积值
double Circle::area() const  { return  3.14159 * radius * radius ; }
// 输出圆心坐标和半径值
ostream & operator<< ( ostream &output, const Circle &c)
{ output << "Center = " << '[' << c.x << "," << c.y << "]" << "; Radius = "
              << setiosflags(ios::fixed|ios::showpoint) << setprecision(2) << c.radius ;
   return  output ;
} 
Cylinder::Cylinder(double h, double r, int x, int y):Circle(r,x,y)  { setHeight(h); }
void Cylinder::setHeight(double h)  { height = ( h >= 0 ? h : 0 ); }
double Cylinder::getHeight() const { return height; }
double Cylinder::area()const{ return  2*Circle::area()+2*3.14159*radius*height; }
double Cylinder::volume() const  { return  Circle::area()*height; }
ostream &operator<< ( ostream &output, const Cylinder &cy )
{ output << "Center = " << '[' << cy.x << "," << cy.y << "]" << "; Radius = "
                << setiosflags(ios::fixed|ios::showpoint) << setprecision(2) << cy.radius
                << "; Height = " << cy.height << endl ;
     return output;
} 
int main(){
   Point p ( 72, 115 ) ;		//定义点对象并初始化
   cout << "The initial location of p is " << p << endl ;
   p.setPoint ( 10, 10 ) ;		//置点的新数据值
   cout << "\nThe new location of p is " << p << endl ;	//输出数据
   Circle c ( 2.5, 37, 43 ) ;	//定义圆对象并初始化
   cout<<"\nThe initial location and radius of c are\n"<<c<<"\nArea = "<<c.area()<<"\n" ;
   c.setRadius ( 4.25 ) ;    c.setPoint ( 2, 2 ) ;
   cout<<"\nThe new location and radius of c are\n"<<c<<"\nArea = "<<c.area()<< "\n" ;
   Cylinder cyl ( 5.7, 2.5, 12, 23 ) ;	//定义圆柱体对象并初始化
cout << "\nThe initial location, radius ang height of cyl are\n" << cyl
      << "Area = " << cyl.area() << "\nVolume = " << cyl.volume() << '\n';
   cyl.setHeight ( 10 ) ;   cyl.setRadius ( 4.25 ) ;    cyl.setPoint ( 2, 2 ) ;
   cout << "\nThe new location, radius ang height of cyl are\n" << cyl
        << "Area = " << cyl.area() << "\nVolume = "<<cyl.volume()<< "\n" ;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值