66
CHAPTER 10 Inheritance 继继 Introduction A key feature of an object-oriented language in in heritance. Inheritance is the ability to define ne w classes using existing classes as a basis. The n ew class inherits the attributes and behaviors of the classes on which it is based, and it can also have attributes and behaviors that are specific to it. 继继继继继继继继继继继继继继继 继继继继继继继继继继继继继继继继继 继继继继继继继继继继继继继继 一。。 继继 继继继继继继继继继继继继继继继继继继 ,。

CHAPTER 10 Inheritance 继承

  • Upload
    erica

  • View
    160

  • Download
    2

Embed Size (px)

DESCRIPTION

CHAPTER 10 Inheritance 继承. Introduction - PowerPoint PPT Presentation

Citation preview

Page 1: CHAPTER  10 Inheritance 继承

CHAPTER 10 Inheritance 继承

IntroductionA key feature of an object-oriented language in inheritance. Inheritance is t

he ability to define new classes using existing classes as a basis. The new c

lass inherits the attributes and behaviors of the classes on which it is based,

and it can also have attributes and behaviors that are specific to it.

继承是面向对象语言的重要特征之一。继承是把已有类作为基类定义新的类。这个新的类继承了基类的属性和操作,而且还可以具有自己特定的属性和操作。

Page 2: CHAPTER  10 Inheritance 继承

Three relationships are commonly used in object-oriented design :

• is-a relationships

Hierarchy relationship. It specifies that one abstraction is a specialization

of another.

• has-a relationships

Contain relationship. Some object is part of another.

• uses-a relationships

One object uses another object in some way.

10.1 OBJECT-ORIENTED DESIGN USING INHERITANCE

Page 3: CHAPTER  10 Inheritance 继承

is-a relationships

AA

B1B1 B2B2

C1C1 C2C2 C3C3

Base class of B1 and B2

Derived class of ABase class of C1, C2 and C3

Derived class of ABase class of C3

Derived class of B1 and B2Multiple inheritance

Derived class of B1

Page 4: CHAPTER  10 Inheritance 继承

has-a relationships

class A { … B DMiB DMi … FM} ;

class BB { … DM … FM} ;

Page 5: CHAPTER  10 Inheritance 继承

uses-a relationships

class A { … DM … FMj ( B &B & … )} ;

class BB { … DM … FM} ;

Page 6: CHAPTER  10 Inheritance 继承

class DerivedClass : public BaseClass {

public :

// public section

private :

// private section

} ;

10.3.1 Declaring a derived class

Page 7: CHAPTER  10 Inheritance 继承

class DerivedClass : public BaseClass {

public :

// public section

private :

// private section

} ;

10.3.1 Declaring a derived class

Derived class name

Page 8: CHAPTER  10 Inheritance 继承

class DerivedClass : public BaseClass {

public :

// public section

private :

// private section

} ;

10.3.1 Declaring a derived class

Access specifier(usually public)

Page 9: CHAPTER  10 Inheritance 继承

class DerivedClass : public BaseClass {

public :

// public section

private :

// private section

} ;

10.3.1 Declaring a derived class

Class name of base class

Page 10: CHAPTER  10 Inheritance 继承

Declare vehicle class and derived class car :class vehiclevehicle { public : void initialize (int in_wheels , float in_weight ) ; int get_wheels ( ) ; float get_weight ( ) ; float get_loading ( ) ; private : int wheels ; float weight ; float loading ;} ;class carcar : public vehiclevehicle { public : void initialize ( int in_wheels , float in_weight , int people = 4 ) ; int passengers ( ) ; private : int passenger_load ;} ;

Example

Page 11: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

}

Page 12: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

}Derived class name

Page 13: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

}Derived class constructor

parameter list

Page 14: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

}Base class name

Page 15: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

}Base class constructor

parameter list

Page 16: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

} Class data member initialization list

Page 17: CHAPTER  10 Inheritance 继承

The syntax for the constructor of derived class isThe syntax for the constructor of derived class is

10.3.2 Implementing a derived class

DClass :: DClass ( Plist ) : BClass ( Plist ) , DMbrList {

// Body of derived class constructor…

}

• First base classFirst base class

• Second class data memberSecond class data member

• Last derived classLast derived class

Page 18: CHAPTER  10 Inheritance 继承

// Example13-1#include < iostream.h >class Base{ public : Base ( ) { cout << "\nBase created.\n" ; }} ;class D_class : public Base{ public : D_class ( ) { cout << "D_class created.\n" ; }} ;void main (){ D_class d1 ; }

Output Base created.

D_class created.

Page 19: CHAPTER  10 Inheritance 继承

// Example13-2

#include <iostream.h>

class parent_class

{ int private1 , private2 ;

public :

parent_class ( int p1 , int p2int p1 , int p2 ) { private1 = p1; private2 = p2; }

int inc1 ( ) { return ++private1; }

int inc2 ( ) { return ++private2 ; }

void display ( )

{ cout << "private1=" << private1 << " , private2=" << private2 << endl ; }

};

Base class constructor parameter list

Page 20: CHAPTER  10 Inheritance 继承

class derived_class : private parent_class{ int private3 ; parent_class private4 ; // Class data member public: derived_class ( int p1 , int p2 , int p3 , int p4 , int p5 )

: parent_class ( p1 , p2 ) , private4 ( p3 , p4 ) { private3 = p5 ; } int inc1 ( ) { return parent_class :: inc1 ( ) ; } int inc3 ( ) { return ++private3 ; } void display ( ) { parent_class :: display ( ) ; private4 . display ( ) ; cout << "private3=" << private3 << endl ; }} ;void main ( ){ derived_class d1 ( 17 , 18 , 1 , 2 , -5 ) ; d1 . inc1 ( ) ; d1 . display ( ) ;}

Five parameters of derived class constructor

Page 21: CHAPTER  10 Inheritance 继承

class derived_class : private parent_class{ int private3 ; parent_class private4 ; // Class data member public: derived_class ( ( int p1 , int p2int p1 , int p2 , int p3 , int p4 , int p5 )

: parent_class ( p1 , p2 )parent_class ( p1 , p2 ) , private4 ( p3 , p4 ) { private3 = p5 ; } int inc1 ( ) { return parent_class :: inc1 ( ) ; } int inc3 ( ) { return ++private3 ; } void display ( ) { parent_class :: display ( ) ; private4 . display ( ) ; cout << "private3=" << private3 << endl ; }} ;void main ( ){ derived_class d1 ( 17 , 18 , 1 , 2 , -5 ) ; d1 . inc1 ( ) ; d1 . display ( ) ;}

For base classparent_class(p1,p2)

Initialize private1 and private2

Page 22: CHAPTER  10 Inheritance 继承

class derived_class : private parent_class{ int private3 ; parent_class private4 ; // Class data member public: derived_class ( ( int p1 , int p2int p1 , int p2 , int p3 , int p4 ,int p3 , int p4 , int p5 )

: parent_class ( p1 , p2 )parent_class ( p1 , p2 ) , private4 ( p3 , p4 )private4 ( p3 , p4 ) { private3 = p5 ; } int inc1 ( ) { return parent_class :: inc1 ( ) ; } int inc3 ( ) { return ++private3 ; } void display ( ) { parent_class :: display ( ) ; private4 . display ( ) ; cout << "private3=" << private3 << endl ; }} ;void main ( ){ derived_class d1 ( 17 , 18 , 1 , 2 , -5 ) ; d1 . inc1 ( ) ; d1 . display ( ) ;}

For class member private4 parent_class(p1,p2) : initialize

private4.private1 and private4.private2

Page 23: CHAPTER  10 Inheritance 继承

class derived_class : private parent_class{ int private3 ; parent_class private4 ; // Class data member public: derived_class ( ( int p1 , int p2int p1 , int p2 , int p3 , int p4 ,int p3 , int p4 , int p5int p5 )

: parent_class ( p1 , p2 )parent_class ( p1 , p2 ) , private4 ( p3 , p4 )private4 ( p3 , p4 ) { private3 = p5private3 = p5 ; } int inc1 ( ) { return parent_class :: inc1 ( ) ; } int inc3 ( ) { return ++private3 ; } void display ( ) { parent_class :: display ( ) ; private4 . parent_class :: display ( ) ; cout << "private3=" << private3 << endl ; }} ;void main ( ){ derived_class d1 ( 17 , 18 , 1 , 2 , -5 ) ; d1 . inc1 ( ) ; d1 . display ( ) ;}

For derived class Initialize data member private3

Page 24: CHAPTER  10 Inheritance 继承

class derived_class : private parent_class{ int private3 ; parent_class private4 ; // Class data member public: derived_class ( ( int p1 , int p2int p1 , int p2 , int p3 , int p4 ,int p3 , int p4 , int p5int p5 )

: parent_class ( p1 , p2 )parent_class ( p1 , p2 ) , private4 ( p3 , p4 )private4 ( p3 , p4 ) { private3 = p5private3 = p5 ; } int inc1 ( ) { return parent_class :: inc1 ( ) ; } int inc3 ( ) { return ++private3 ; } void display ( ) { parent_class :: display ( ) ; private4 . parent_class :: display ( ) ; cout << "private3=" << private3 << endl ; }} ;void main ( ){ derived_class d1 ( 17 , 18 , 1 , 2 , -5 ) ; d1 . inc1 ( ) ; d1 . display ( ) ;}

Output

private1 = 18 , private2 = 18

private1 = 1 , private2 = 2

private3 = -5

Page 25: CHAPTER  10 Inheritance 继承

• Data members and member functions can be cover in the derived class.

• The derived class member functions do not access to the private member

of base class.

10.4 PROTECTED MEMBERS AND INHERITANCE

保护成员与继承

Page 26: CHAPTER  10 Inheritance 继承

Exampleclass base{ public : int a , bb ; };class derived : public base { public : int bb , c ; } ;void f ( ){ derived d ; d . a = 1 ; d . base :: bbase :: b = 2 ; // use member b of base class d . bb = 3 ; // use member b of derived class d . c = 4 ;};

Page 27: CHAPTER  10 Inheritance 继承

Exampleclass X{ public : void get_ij ( ) ; void put_ij ( ) ; private : int i , jprivate : int i , j ;};class Y : public X{ public : int get_k ( ) ; void make_k ( ) ; private : int k ;};void Y :: make_k ( ) ;{ k = i * ji * j ; // illegal } ;

Page 28: CHAPTER  10 Inheritance 继承

Exampleclass X{ public : void get_ij ( ) ; void put_ij ( ) ; private : int i , jprivate : int i , j ;};class Y : public X{ public : int get_k ( ) ; void make_k ( ) ; private : int k ;};void Y :: make_k ( ) ;{ k = i * ji * j ; // illegal } ;

protected : int i , jprotected : int i , j ;

k = i * ji * j ; // legal

Page 29: CHAPTER  10 Inheritance 继承

10.5 CONTROLLING INHERITANCE 控制继承 10.5.1 Public Inheritance 公有继承

class DerivedClass : public BaseClass {

public :

// public section

private :

// private section

} ;

The public members and protected

members keep their character in

derived class.

Page 30: CHAPTER  10 Inheritance 继承

// Example13-3#include<iostream.h>class XX { public : void get_ij( ) { cout << "Enter two numbers:" ; cin >> i >> j ; }; void put_ij( ) { cout << i << " " << j << '\n' ; }; protected : int i , j ;};class YY : public X { public : int get_k( ) { return k ; }; void make_k( ) { k = i i * jj ; }; // use base class DM private : int k ;};class ZZ : public Y { public : void f( ) { ii = 2 ; jj = 3; }; // use base class DM};void main ( ){ Y var1Y var1; Z var2Z var2 ; var1var1.get_ij( )get_ij( ) ; var1var1.put_ij( )put_ij( ) ; var1.make_k( ) ; var2.f( ) ; var2var2.put_ij( )put_ij( ) ;}

class X

class Y

class Z

Page 31: CHAPTER  10 Inheritance 继承

10.5.2 Private inheritance 私有继承

class DerivedClass : private BaseClass {

public :

// public section

private :

// private section

} ;

The public members and protected

members of base class are private

member in derived class.

Page 32: CHAPTER  10 Inheritance 继承

// Example13-4#include<iostream.h>class XX { public : void get_ij( ) { cout << "Enter two numbers:" ; cin >> i >> j ; }; void put_ij( ) { cout << i << " " << j << '\n' ; }; protected : int i , j ;};class YY : public X { public : int get_k( ) { return k ; }; void make_k( ) { k = i i * jj ; }; // use base class DM private : int k ;};class ZZ : public Y { public : void f( ) { ii = 2 ; jj = 3; }; // use base class DM};void main ( ){ Y var1Y var1; Z var2Z var2 ; var1var1.get_ij( )get_ij( ) ; var1var1.put_ij( )put_ij( ) ; var1.make_k( ) ; var2.f( ) ; var2var2.put_ij( )put_ij( ) ;}

Page 33: CHAPTER  10 Inheritance 继承

// Example13-4#include<iostream.h>class XX { public : void get_ij( ) { cout << "Enter two numbers:" ; cin >> i >> j ; }; void put_ij( ) { cout << i << " " << j << '\n' ; }; protected : int i , j ;};class YY : privateprivate X { public : int get_k( ) { return k ; }; void make_k( ) { k = i i * jj ; }; // use base class DM private : int k ;};class ZZ : public Y { public : void f( ) { ii = 2 ; jj = 3; }; // use base class DM};void main ( ){ Y var1Y var1; Z var2Z var2 ; var1var1.get_ij( )get_ij( ) ; var1var1.put_ij( )put_ij( ) ; var1.make_k( ) ; var2.f( ) ; var2var2.put_ij( )put_ij( ) ;}

class X

class Y

class Z

private

public

OK i, j are private member in Y class

Page 34: CHAPTER  10 Inheritance 继承

// Example13-4#include<iostream.h>class XX { public : void get_ij( ) { cout << "Enter two numbers:" ; cin >> i >> j ; }; void put_ij( ) { cout << i << " " << j << '\n' ; }; protected : int i , j ;};class YY : privateprivate X { public : int get_k( ) { return k ; }; void make_k( ) { k = i i * jj ; }; // use base class DM private : int k ;};class ZZ : public Y { public : void f( ) { ii = 2 ; jj = 3; }; // use base class DM};void main ( ){ Y var1Y var1; Z var2Z var2 ; var1var1.get_ij( )get_ij( ) ; var1var1.put_ij( )put_ij( ) ; var1.make_k( ) ; var2.f( ) ; var2var2.put_ij( )put_ij( ) ;}

class X

class Y

class Z

private

public

Illegal Illegal Can not access private member of

base class

Page 35: CHAPTER  10 Inheritance 继承

// Example13-4#include<iostream.h>class XX { public : void get_ij( ) { cout << "Enter two numbers:" ; cin >> i >> j ; }; void put_ij( ) { cout << i << " " << j << '\n' ; }; protected : int i , j ;};class YY : privateprivate X { public : int get_k( ) { return k ; }; void make_k( ) { k = i i * jj ; }; // use base class DM private : int k ;};class ZZ : public Y { public : void f( ) { ii = 2 ; jj = 3; }; // use base class DM};void main ( ){ Y var1Y var1; Z var2Z var2 ; var1var1.get_ij( )get_ij( ) ; var1var1.put_ij( )put_ij( ) ; var1.make_k( ) ; var2.f( ) ; var2var2.put_ij( )put_ij( ) ;}

class X

class Y

class Z

private

public

Illegal Illegal main() can not call private MF of

any objects

Page 36: CHAPTER  10 Inheritance 继承

10.5.3 Protected inheritance 保护继承

class DerivedClass : protected BaseClass {

public :

// public section

private :

// private section

} ;

The public members and protected

members of base class are protected

member in derived class.

Protected inheritance is used rarely.

Page 37: CHAPTER  10 Inheritance 继承

Inheritance Type Base Class Member Access

Derived Class Member Access

public publicprotectedprivate

publicprotectedinaccessible

protected publicprotectedprivate

protectedprotectedinaccessible

private publicprotectedprivate

privateprivateinaccessible

Page 38: CHAPTER  10 Inheritance 继承

Do you know Do you know how access x, y and zhow access x, y and z

public : x class A protected : y

private : z

Such as : main()

Common functions

class B : public A

Derived

class C : private A

Derived

Page 39: CHAPTER  10 Inheritance 继承

A derived class can inherit from two or more base class.

10.6 MULTIPLE INHERITANCE 多继承

class C : public A , public B

class A class B

Page 40: CHAPTER  10 Inheritance 继承

A derived class can inherit from two or more base class.

10.6 MULTIPLE INHERITANCE 多继承

The syntax for declaring a class derived using multiple inheritance is

class DClass : publicpublic BClassBClass, , publicpublic BClassBClass {

public :// public section …

private :// private section…

} ;

Page 41: CHAPTER  10 Inheritance 继承

// Example13-4#include<iostream.h>class A { public : void setA ( int x ) { a = x ; } ; void showA( ) { cout << a << endl ; } ; private : int a ;} ;class B { public : void setB ( int x ) { b = x ; } ; void showB ( ) { cout << b << endl ; } ; private : int b ;} ;class C : public Apublic A , private Bprivate B { public : void setC ( int x , int y , int z ) { setA ( x ) ; setB ( y ) ; c = z ; } ; void showC ( ) { showA ( ) ; showB ( ) ; cout << c << endl ; } ; private : int c ;} ;void main ( ) { C obj ; obj . setA ( 5 ) ; obj . showA ( ) ; obj . setC ( 6 , 7 , 9 ) ; obj . showC ( ) ; //obj . setB ( 6 ) ;//obj . setB ( 6 ) ; // error, private inheritance //obj . showB ( ) ;//obj . showB ( ) ; // error}

class A class B

class C

Page 42: CHAPTER  10 Inheritance 继承

• If a derived class be derived from multiple base classes, and these base

have a common base class, then, when access members of this common

base class may be open to different interpretations.

10.7 VIRTUAL BASE CLASS

Page 43: CHAPTER  10 Inheritance 继承

Example

class B { public : int b ;} ;

class B1 : public B { private : int b1 ; } ;

class B2 : public B { private : int b2 ; } ;

class C : public B1 , public B2

{ public : int f ( ) ; private : int d ; } ;

C c ;

c . bc . b // error

c . B :: bc . B :: b // error , where from?

c . B1 :: b // ok , from B1

c . B2 :: b // ok , from B2

class C { f () , d }

class B1 { b1 } class B2 {b2}

class B { b } class B { b }

Page 44: CHAPTER  10 Inheritance 继承

// Example13-5

#include<iostream.h>

class B{public: int b;};

class B1:public B {public: int b1;};

class B2:public B {public: int b2;};

class C:public B1, public B2 {public: f(); private: int b1;};

void main()

{ C c;

c.B1::b=5;

c.B2::b=10;

cout<<"path B1==>"<<c.B1::b<<endl;

cout<<"path B2==>"<<c.B2::b<<endl;

}

Output:

path B1==> 5

path B2==> 10

class C { f () , d }

class B1 { b1 } class B2 {b2}

class B { b } class B { b }

Page 45: CHAPTER  10 Inheritance 继承

b

b1

b

b2

d

B

B

B1

B2C

Store of multiple derived class object C

When create object C, constructor of B is called two times. Once for B1,Others for B2. A C object contains two sub-object of B.

class C { f () , d }

class B1 { b1 } class B2 {b2}

class B { b } class B { b }Example

class B { public : int b ;} ;

class B1 : public B { private : int b1 ; } ;

class B2 : public B { private : int b2 ; } ;

class C : public B1 , public B2

{ public : int f ( ) ; private : int d ; } ;

Page 46: CHAPTER  10 Inheritance 继承

• If a derived class be derived from multiple base classes, and these base

have a common base class, then, when access members of this common

base class may be open to different interpretations.

10.7 VIRTUAL BASE CLASS

• For produce only one sub-object in deriver class object, the base classes

must be declared virtual inheritance the common base class.

Key word virtual

Page 47: CHAPTER  10 Inheritance 继承

Exampleclass B { public : int b ;} ;

class B1 : virtualvirtual public B { private : int b1 ; } ;

class B2 : virtualvirtual public B { private : int b2 ; } ;

class C : public B1 , public B2

{ private : float d ; } ;

As:C cc ;cc . bcc . b // OK

class C { d }

class B1 { b1} class B2 {b2}

class B { b }

cc . B1 :: b and cc . B2 :: b is same sub-object of class B

Page 48: CHAPTER  10 Inheritance 继承

Store of object C using virtual inheritance

b1

b2

d

b BB

B1B1

B2B2CC

class C { d }

class B1 { b1} class B2 {b2}

class B { b }Exampleclass B { public : int b ;} ;

class B1 : virtualvirtual public B { private : int b1 ; } ;

class B2 : virtualvirtual public B { private : int b2 ; } ;

class C : public B1 , public B2

{ private : float d ; } ;

Page 49: CHAPTER  10 Inheritance 继承

• If a derived class be derived from multiple base classes, and these base

have a common base class, then, when access members of this common

base class may be open to different interpretations.

10.7 VIRTUAL BASE CLASS

• For produce only one sub-object in deriver class object, the base classes

must be declared virtual inheritance the common base class.

• Using virtual inheritance, the constructor of common base class is called

only once.

Page 50: CHAPTER  10 Inheritance 继承

// Example13-6#include < iostream.h >class A{ public : A ( ) { cout << "class A" << endl ; } } ;class B : virtual public A{ public : B ( ) {cout << "class B" << endl ; } } ;class C : virtual public A{ public : C ( ) {cout << "class C" << endl ; } } ;class D : public B , public C{ public : D ( ) {cout << "class D" << endl ; } } ;void main ( ){ D dd ; }

Output class Aclass Bclass Cclass D

Page 51: CHAPTER  10 Inheritance 继承

// Example13-7#include < iostream.h >class A{ public : A ( ) { cout << "class A" << endl ; } } ;class B : /*virtual*/ public A{ public : B ( ) {cout << "class B" << endl ; } } ;class C : /*virtual*/ public A{ public : C ( ) {cout << "class C" << endl ; } } ;class D : public B , public C{ public : D ( ) {cout << "class D" << endl ; } } ;void main ( ){ D dd ; } Output class A

class Bclass Aclass Cclass D

Page 52: CHAPTER  10 Inheritance 继承

• Each object of a class has its own copy of all data members of the class.

In certain cases only one copy of a variable should be share by all objects

of a class. A static class variable is used for these and other reasons.

• The declaration of a static member begins with the keyword static.

10.8 STATIC CLASS MEMBERS 静态类成员

Page 53: CHAPTER  10 Inheritance 继承

• Static data members have class scope.

• Static data members must be initialized once (and only once) at file

scope.

• A class’s public static data members can be accessed through any object

of that class.

• A class’s private and protected static data members must be accessed

through public member functions of the class or through friends of the

class.

10.8.1 Static data members 静态数据成员

Page 54: CHAPTER  10 Inheritance 继承

class object { char ch ; static s ; …... };

object A , B , C , D ;

object B

char ch ;

object C

char ch ;

object D

char ch ;

ExampleExample

object A static s ;static s ;

char ch ;

Page 55: CHAPTER  10 Inheritance 继承

// Example13-8#include<iostream.h>class counter{ static int num1 ;static int num1 ; public : void setnum1 ( int i ) { num1 = i ; } void shownum1 ( ) { cout << num1 << " " ; } static int num2 ;static int num2 ;} ;int counter :: num1 = 0 ;int counter :: num1 = 0 ; // initial 0 can default int counter :: num2 = 5;int counter :: num2 = 5;void main ( ){ counter a , b ; cout<<"private static DM : \n" ; a . shownum1 ( ) ; b . shownum1 ( ) ; a . setnum1 ( 10 ) ; a . shownum1 ( ) ; b . shownum1 ( ) ; // cout<<counter::num1;// cout<<counter::num1; //error, can not access num1 cout<<“\n public static DM :\n "<< counter :: num2counter :: num2 <<endl;}

Page 56: CHAPTER  10 Inheritance 继承

// Example13-9 count the number of object# include < iostream.h >class counter{ static int num ; public : counter ( ) { cout << ++ num <<'\t' ; } ~counter( ) { cout << num -- <<'\t' ; } } ;int counter :: num = 0 ;void main ( ){ counter a , b , c ; cout<<endl;}

Output

1 2 3

3 2 1

Page 57: CHAPTER  10 Inheritance 继承

• Static member functions access static member only.

• Static member functions have no this pointer.

10.8.2 Static member functions 静态成员函数

Page 58: CHAPTER  10 Inheritance 继承

10.8.2 Static member functions 静态成员函数 Example class X{ int member ; public : static void func ( int i , X * ptr ) ;static void func ( int i , X * ptr ) ; }:

void g ( ){ X obj ; X :: func ( 1, & obj ) ;X :: func ( 1, & obj ) ; // OK obj . func ( 1 , & obj ) ;obj . func ( 1 , & obj ) ; // OK}

Page 59: CHAPTER  10 Inheritance 继承

10.8.2 Static member functions 静态成员函数 Example class X{ int member ; public : static void func ( int i , X * ptr ) ;static void func ( int i , X * ptr ) ; }:

void X :: func ( int i ; X * ptr )

{ member = i ; // Error , whose member ? ptr -> member = i ;ptr -> member = i ; // OK

}

Page 60: CHAPTER  10 Inheritance 继承

10.8.2 Static member functions 静态成员函数 Example class X{ int member ; public : static void func ( int i ,static void func ( int i , X aX a ) ;) ; }:

void X :: func ( int i ; void X :: func ( int i ; X aX a ) )

{ { cout << member ;cout << member ; // error, can not access non static member

cout << a . member ;cout << a . member ; // OK, pass parameter

}}

Page 61: CHAPTER  10 Inheritance 继承

// Example13-10 count goods#include<iostream.h>class Goods { public : static int totalWeight ;static int totalWeight ; Goods ( int w) { weight = w; totalWeight += w ; } ; ~ Goods ( ) { totalWeight -= weight ; } ; int Weight ( ) { return weight ; } ; static int TotalWeight ( ) { return totalWeight ; } ; private : int weight ;} ;int Goods :: totalWeight = 0 ;int Goods :: totalWeight = 0 ;void main ( ){ int w ; cin >> w ; Goods * g1 = new Goods ( w ) ; cin >> w ; Goods * g2 = new Goods ( w ) ; cout << Goods :: TotalWeight ( ) << endl ; delete g2 ; cout << Goods :: TotalWeight ( ) << endl ;}

Page 62: CHAPTER  10 Inheritance 继承

• A friend function of a class is defined outside that class’s scope.

• Friend functions have right access private members of the class.

• A function or an entire class may be declared to be a friend of another

class.

• Friendship is neither symmetric nor transitive.

10.9 FRIEND FUNCTION AND FRIEND CLASS

Page 63: CHAPTER  10 Inheritance 继承

10.9 FRIEND FUNCTION AND FRIEND CLASS

Example

class X

{ int i ;

friend void func ( X * , int ) ; // func is friend of X

void member_func ( int ) ;

};

Page 64: CHAPTER  10 Inheritance 继承

…...

void func ( X * xptr , int a )

{ xptr -> i = a ; }; // access private member

void X :: member_func ( int a )

{ i = a ; };

Example

class X

{ int i ;

friend void func ( X * , int ) ; // func is friend of X

void member_func ( int ) ;

};

Page 65: CHAPTER  10 Inheritance 继承

// Example13-11 Count Distance#include<iostream.h>#include<math.h>class Point { public : Point ( double xi , double yi ) { X = xi ; Y = yi ; } double GetX( ) { return X ; } double GetY( ) { return Y ; } friend double Distance ( Point & a , Point & b ) ;friend double Distance ( Point & a , Point & b ) ; private : double X , Y ;} ;double Distance ( Point & a , Point & b )double Distance ( Point & a , Point & b ) { double dx = a.X - b.X ; // access private data members double dy = a.Y - b.Y ; return sqrt ( dx * dx + dy * dy ) ;}void main ( ) { Point p1 ( 3.0 , 5.0 ) , p2 ( 4.0 , 6.0 ) ; double d = Distance ( p1 , p2 ) ; cout << "This distance is " << d << endl ;}

Page 66: CHAPTER  10 Inheritance 继承

// Example13-12 Friend Class#include<iostream.h>class A { friend class B ;friend class B ; // class B is friend of class A public: void Display ( ) { cout << x << endl ; }; private : int x ;};class Bclass B{ public: void Set ( int i ) { a . xa . x = i ; } ; // access private DM of a void Display ( ) { a. Display ( ) ; } ; private: A aA a ;} ;

void main ( ){ B b; b . Set ( 100 ) ; b . Display ( ) ; }<< endl ;}