This presentation is the property of its rightful owner.
Sponsored Links
1 / 47

第六章进一步使用成员函数 PowerPoint PPT Presentation


  • 70 Views
  • Uploaded on
  • Presentation posted in: General

第六章进一步使用成员函数. 静态成员 友元函数 const 对象和 volatile 对象 转换函数 指向类成员的指针 数组和类. 静态成员. 静态成员:使用关键字 static 修饰的类的数据成员和成员函数 格式: static 数据类型 数据成员名; 举例: class MyClass{ public: static int count; // 引用性说明 static void func(); int x; };. 静态数据成员. 特点:.

Download Presentation

第六章进一步使用成员函数

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


4583440

constvolatile


4583440

  • static

  • static

    • class MyClass{

      public:

      static int count; //

      static void func();

      int x;

      };


4583440

  • Test b,c;

  • b.x=25;

  • cout<<c.x;

  • ,

  • int Test::x;

  • ::


4583440

  • main()static ::

    • ::

    • .

    • ->


4583440

class Myclass{

int i;

static int j;

public:

Myclass();

void Print();

~Myclass();

};

int Myclass::j=0;

Myclass::Myclass(){j+=10;}

void Myclass::Print()

{ cout<<j;}

void main()

{ Myclass first,second;

first.Print();

second.Print();}


4583440

class CStatic{

public:

CStatic(){val++;}

static int val;

};

int CStatic::val=0;

void main

{

cout<< CStatic::val=<<CStatic::val<<endl;

CStatic cs1;

cout<< cs1.val=<<cs1.val<<endl;

CStatic cs2;

cout<< cs2.val=<<cs2.val<<endl;

CStatic cs3,cs4;

cout<< cs1.val=<<cs1.val<<endl;

cout<< cs2.val=<<cs2.val<<endl;

}

Cstatic::val=0

cs1.val=1

cs2.val=2

cs1.val=4

cs2.val=4


4583440

  • static

    • static

  • static

  • ::

      • const


  • 4583440

    int ex=6;

    const int cx=7;

    class MyClass{

    public:

    static int f_1(MyClass m); //

    int x; //

    static int y; //

    };

    int MyClass::y=8;

    int MyClass::f_1(MyClass m)

    {

    m.x=9;

    return y+ex+cx+m.x;

    }


    4583440

    #include <iostream.h>

    class X{

    int member_int;

    public:

    static void func(int i,X *ptr);

    };

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

    { ptr->member_int=i;}

    void main()

    {

    X obj;

    X::func(1,&obj);

    obj.func(1,&obj);

    }


    4583440

    ::

    class Base{

    public:

    static void a();

    void b();};

    class Derived:private Base{};

    class DD:public Derived{

    void func();};

    void DD::func()

    { Base::a();

    a();

    b();}


    4583440

    class MyClass{

    public:

    static void f();

    private:

    static int Globe;

    };

    void MyClass::f()

    { Globe=100;}


    4583440

    class MyClass{

    public:

    static void f(MyClass my);

    private:

    int Globe;

    };

    void MyClass::f()

    { cout<<Globe;}

    • f

    • Globe


    4583440

    VectorMatrixfloatelemV[i]M[i][j]

    float &Vector::elem(int i)

    {return V[i];}

    float &Matrix::elem(int i,int j)

    {return M[i][j];}

    elem

    Vector multiply(Matrix &m,Vector &v)

    { Vector r;

    for(int i=0;i<5;i++)

    { r.elem(i)=0;

    for(int j=0;j<5;j++)

    r.elem(i)+=m.elem(i,j)*v.elem(j);}

    return r;}


    4583440

    class girl{

    char *name;

    int age;

    public:

    girl(char *n,int d)

    {name=new char[strlen(n)+1];

    strcpy(name,n);

    age=d;}

    friend void disp(girl &);

    ~girl( ){delete name;}};

    void disp(girl &x)

    { cout<<"girl\'s name is:"<<x.name<<",age:"<<x.age<<"\n";}

    void main()

    { girl e("Chen Xingwei",18);

    disp(e);}


    4583440

    • friend friend void disp(girl &)

    • void disp(girl &)

    • ::


    4583440

    class two;

    class one{

    int a;

    public:

    void f(two &);

    };

    class two{

    int b;

    friend void one::f(two &);

    };

    void f(two &r)

    {a=r.b;}


    4583440

    class C2{

    friend class C1;

    };

    • XYYZ

    • XY


    4583440

    class one{

    protected:

    int a;

    public:

    one(){a=20;}};

    class two:public one{

    int b;

    friend void f(two &t)

    {cout<<'a'<<t.a<<'b'<<t.b;}

    public:

    two(){b=10;}};

    void main()

    { two t1;

    f(t1);}


    4583440

    class one{

    protected:

    int a;

    public:

    friend class two;

    };

    class two{

    public:

    void func(one &s){cout<<s.a;}

    };

    class three:public two{

    public:

    void seta(){a=10;}

    };


    4583440

    class MyClass{

    char my_char1;

    char my_char2;

    public:

    friend void SetMember(MyClass &my,char);

    };

    void SetMember(MyClass &my,char mem1)

    { my.my_char1=mem1;}

    void SetMember(MyClass &my,char mem1,char mem2)

    { my.my_char1=mem1;

    my.my_char2=mem1;}

    void main()

    { MyClass obj;

    SetMember(obj,5);

    SetMember(obj,7,9);}


    Const

    const

    • constconst

      • const Date MyBirthday(8,13,1970);

    • const,constconst

      • int GetMonth() const{return month;}

    • constconstconstconst


    Volatile

    volatile

    • volatilevolatilevolatile

    • volatilevolatile


    4583440

    this

    constconstconstthis

    class ConstFun{

    int val;

    public:

    ConstFun(int i){val=i;}

    int Getval() const;

    };

    int ConstFun::GetVal() const{return val;}

    void main()

    { ConstFun s(98);

    cout<<s.Getval()<<endl;}

    const ConstFun * const this;


    4583440

    • class A{

    • operator type( );

    • };

    • type*([ ] constvolatile

    • type

    • Atypetype


    4583440

    class number{

    int val;

    public:

    number(int i){val=i;}

    operator int( );};

    number::operator int( )

    { return val;}

    void main()

    { number n(15);

    int i=n;

    cout<<i<<endl;

    i=i+n;

    cout<<i<<endl;

    cout<<int(n)<<endl;(cout<<n<<endl;)

    }

    i=n.operator int( );

    n,operator int()


    4583440

    class num:public number{

    public:

    num(int i):number(i){ }

    };

    void main( )

    { num n(15);

    int i=n;

    cout<<i+n<<endl;}


    4583440

    class A{

    public:

    virtual operator const char *( ){return A;}};

    class B:public A{

    public:

    virtual operator const char *( ){return B;}};

    void main()

    { A *p=new B;

    cout<<*p<<endl;}

    <<,:

    Boolcharint long floatdouble(void *)(const char *)


    4583440

    • type

    number n(15.0)15.0val

    operator int( )valint


    4583440


    4583440

    • Xtype

      type X::*pointer;

      Xmembertype

      pointer=&X::member;pointer

    • pointer.*->*


    4583440

    class A{

    public:

    int a,b,c;

    void fa(void){ }

    void fb(void){ }

    };

    void main( )

    { int A::*p;

    p=&A::b;

    A x;

    A *px=new A;

    x.*p=1;

    p=&A::c;

    px->*p=2;

    px->b=8;}

    .*

    ->*


    4583440

    type (X::pointer)(list);

    Xlisttype

    void (A::*pfan)(void);

    pfanA

    pfan=A::fa; Afa()pfan,pfan

    A x;

    A *px=new A;

    (x.*pfan)(); Axfa

    (px->*pfan)(); Afa


    4583440

    #include <iostream.h>

    class A{

    private:

    int val;

    public:

    A(int i){val=i;}

    int value(int a){return val+a;}

    };

    void main()

    { int(A:: *pfun)(int);

    pfun=A::value;

    A obj(10);

    cout<<(obj.*pfun)(15)<<endl;

    A *pc=&obj;

    cout<<(pc->*pfun)(15)<<endl;}


    4583440

    #include <iostream.h>

    class base{

    public:

    virtual void print(){cout<<In Base<<endl;}

    };

    class derived:public base{

    public:

    void print(){cout<<In Derived<<endl;}

    };

    void display(base *pb,void(base::*pf)())

    { (pb->*pf)();}

    void main()

    { derived d;

    display(&d,base::print);}

    base::::pfbase


    4583440

    #include <iostream.h>

    class A{

    public:

    static int num;

    };

    int A::num;

    void main()

    { int *p=&A::num;

    *p=56;

    cout<<A::num<<endl;

    cout<<*p<<endl;

    cout<<p<<endl;

    A a,b;

    cout<<a.num<<endl;

    cout<<b.num<<endl;

    }


    4583440


    4583440

    exam0~3x

    class exam{

    int x;

    public:

    void set_x(int n){x=n;}

    int get_x( ) {return x;}

    };

    main()

    {exam ob[4]; //4,exam

    int i;

    for(i=0;i<4;i++)ob[i].set_x(i);

    for(i=0;i<4;i++) cout<<ob[i].get_x()<<;

    cout<<\n ;

    return 0;

    }


    4583440

    #include <iostream.h>

    class exam{

    int x;

    public:

    exam(int n)

    {x=n;}

    int get_x()

    {return x;}

    };

    main()

    {exam ob[4]={11,22,33,44};

    int i;

    for(i=0;i<4;i++)cout<<ob[i].get_x()<<' ';

    cout<<'\n';

    return 0;

    }


    4583440

    1

    op1[0].x=1,op1[1].x=4

    op1[2].x=5,op1[2].y=5

    2

    op1[0].x=2,op1[0].y=3

    op1[1].x=3,op1[1].y=4

    op2[0].x=5,op2[1].y=5

    class point{

    int x,y;

    public:

    point() {x=5;y=5;}

    point(int a,int b) {x=a;y=b;}

    point(int a) {x=a;}

    int get_x() {return x;}

    int get_y() {return y;}};

    void main()

    { point op(3,4);

    point op1[3]={1,4};

    point op2[4]={point(2,3),point(3,4)};

    }


    4583440

    • ,

      • []

      • []={(1),(2)};


    4583440

    • C++

    #include <iostream.h>

    class test{

    int num;

    public:

    test(){num=0;}

    test(int n){num=n;}

    ~test(){cout<<"Destructor"<<endl;}

    };

    void main()

    {test a[5];}


    4583440

    pob[0]

    pob[1]

    class exe{

    int x;

    public:

    void set_a(int a) {x=a;}

    void show_a() {cout<<x<<endl;}

    };

    main()

    {exe ob[2],*p;

    ob[0].set_a(10);

    ob[1].set_a(20);

    p=ob;

    p->show_a();

    p++;

    p->show_a();

    return 0;

    }


    4583440

    class base{

    int x,y;

    public:

    base(int m,int n){x=m;y=n;}

    void print();

    void print() const;

    };

    void base::print()

    {cout<<x<<","<<y<<endl;}

    void base::print() const

    {cout<<x<<","<<y<<" const"<<endl;}

    void main()

    {base a(55,84);

    a.print();

    const base b(32,25);

    b.print();

    }

    55,84

    32,25 const


    4583440

    class base{

    const int a;

    static const int b;

    public:

    base(int i);

    void print();

    const int &r;};

    const int base::b=128;

    base::base(int i):a(i),r(a){}

    void base::print()

    {cout<<a<<","<<b<<","<<r<<endl;}

    void main()

    {base a1(125),a2(119);

    a1.print();

    a2.print();}

    125,128,125

    119,128,119


    4583440

    class test{

    int n;

    public:

    test(int i){n=i;cout<<"constructor is called."<<endl;}

    ~test(){cout<<"destructor is called."<<endl;}

    int getn(){return n;}

    void inc(){++n;}

    };

    void main()

    { static test a(3); //

    for(int i=0;i<3;i++)

    {a.inc();

    cout<<a.getn()<<endl;

    }

    cout<<"Exit main()"<<endl;

    }

    Constructor is called

    4

    5

    6

    Exit main()

    Destructor is called


    4583440

    class test{

    private:

    int n;

    public:

    test(int i){n=i;cout<<"constructor is called."<<endl;}

    ~test(){cout<<"destructor is called."<<endl;}

    int getn(){return n;}

    void inc(){++n;}

    };

    void main()

    { for(int i=0;i<3;i++)

    {static test a(3);

    a.inc();

    cout<<a.getn()<<endl;

    }

    cout<<"Exit main()"<<endl;

    }

    Constructor is called

    4

    5

    6

    Exit main()

    Destructor is called


    4583440

    class test{

    private:

    int n;

    public:

    test(int i){n=i;cout<<"constructor is called."<<endl;}

    ~test(){cout<<"destructor is called."<<endl;}

    int getn(){return n;}

    void inc(){++n;}

    };

    void main()

    { for(int i=0;i<3;i++)

    { test a(3);

    a.inc();

    cout<<a.getn()<<endl;

    }

    cout<<"Exit main()"<<endl;

    }

    constructor is called.

    4

    destructor is called.

    constructor is called.

    4

    destructor is called.

    constructor is called.

    4

    destructor is called.

    Exit main()

    Press any key to continue

    • afor


  • Login