-

        - .

  1. - . .

#include <iostream.h>

class point

{ protected:

int x,y;

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

void out() {cout<<:<<x<< <<y<<\n; }

};

class pointcol:public point

{ short color;

public:

pointcol(int a=0,int b=0,short cl=1):point(a,b)

{ color=cl; }

void out()

{ cout<< :<<x<< <<y<< :<<color<,\n; }

};

void main()

{ point p(3,5),*adp=&p;

pointcol pc(8,6,2),*adpc=&pc; //

adp->out(); // out point :3 5

adpc->out();// out pointcol :8 6 2

adp=adpc; //

adp->out(); // adp point :3 5

adpc->out();// :8 6 2

}

 

  1. - . , . . virtual.

 

#include <iostream.h>

class point

{ protected:

int x,y;

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

virtual void out() {cout<<:<<x<< <<y<<\n; }

};

class pointcol:public point

{ short color;

public:

pointcol(int a=0,int b=0,short cl=1):point(a,b)

{ color=cl; }

void out() // virtual( )

{ cout<< :<<x<< <<y<< :<<color<,\n;}

};

void main()

{ point p(3,5),*adp=&p;

pointcol pc(8,6,2),*adpc=&pc; //

adp->out(); // out point :3 5

adpc->out();// out pointcol :8 6 2

adp=adpc; //

adp->out(); //adp pointcol :8 6 2

adpc->out();// :8 6 2

}

- () ( virtual).

-

#include <iostream.h>

class animal

{ protected:

int age;

public:

animal(): age(1) { }

virtual void speak() { cout<<Animal speaks.\n; }

};

class dog:public animal

{ public:

void speak() { cout<<Baow.\n; }

};

class cat:public animal

{ public:

void speak() { cout<<Meow.\n; }

};

class pig:public animal

{ public:

void speak() { cout<<Grouch.\n; }

};

void main()

{ animal *a[4],*ptr;//

int choice;

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

{ cout<<(1)dog (2)cat (3)pig:;

cin>>choice;

switch(choice)

{ case 1: ptr=new dog; break; //ptr

case 2: ptr=new cat; break; //

case 3: ptr=new pig; break;

default:ptr=new animal;

}

a[i]=ptr;

}

for(i=0;i<4;i++) a[i]->speak();

}

 

(1)dog(2)cat(3)pig:1

(1)dog(2)cat(3)pig:2

(1)dog(2)cat(3)pig:3

(1)dog(2)cat(3)pig:4

Baow.

Meow.

Grouch.

Animal speaks.

speak virtual .

 

:

class point

{ int x,y;

..

};

class color

{ int cl;

..

};

class pointcol:public point,public color

{};

:

pointcol:pointcol(1):point(2),color(3){ }

:

: 1) point; 2) color; 3) pointcolor

 

 


 

 

 

 

 

 

 

 

- Base Drv3 ( Drv1 Drv2). - .

.

1.     

class base

{ protected: int i;

.

};

class drv1:public base

{ protected: int j;

.

};

class drv2:public base

{ protected: int k;

};

class drv3:public drv1,public drv2

{ protected: int sum

..

};

void main()

{ drv3 ob;

ob.i=10; //!

//( i - drv1 drv2?)

ob.j=20; // - j drv3

ob.k=30; // - k drv3

ob.sum=ob.j+ob.k;

cout<<ob.sum; // 50

}

2.     

class base

{ protected: int i;

.

};

class drv1:public base

{ protected: int j;

.

};

class drv2:public base

{ protected: int k;

};

class drv3:public drv1,public drv2

{ protected: int sum

..

};

void main()

{ drv3 ob;

ob.drv1::i=10; //

ob.j=20; // - j drv3

ob.k=30; // - k drv3

ob.sum=ob.j+ob.k+ob.drv1::i;

cout<<ob.sum; // 60

}

3.      drv3. .

, , . drv3 - base.

 

class base

{ protected: int i;

.

};

class drv1:virtual public base

{ protected: int j;

.

};

class drv2:virtual public base

{ protected: int k;

};

class drv3:public drv1,public drv2

{ protected: int sum

..

};

void main()

{ drv3 ob;

ob.i=10; // drv3

ob.j=20; // - j drv3

ob.k=30; // - k drv3

ob.sum=ob.j+ob.k+ob.i;

cout<<ob.sum; // 60

drv1 my;

my.i=80; //virtual base drv1

}

 

 

 

 

 

 

 

 

 

 


#include <iostream.h>

enum COLOR {red,green,white,black,brown};

class animal

{ protected: int age

public:

animal(int age_n):age(age_n)

{ cout<< animal\n; }

virtual ~animal() { cout<< animal\n; }

virtual int get_age() { return age; }

virtual void set_age(int age_n) { age=age_n; }

};

class horse:virtual public animal

{ protected:

int height;

COLOR col;

public:

horse(int age_n,COLOR c,int h)://

animal(age_n),col(c),height(h) // -

{ cout<< horse\n; }

~horse() { cout<< horse\n; }

};

class bird:virtual public animal

{ protected: COLOR col;

public:

bird(int age_n,COLOR c):animal(age_n),col(c)

{ cout<< bird\n; }

~bird() { cout<< bird\n; }

};

class pegasus:public horse,public bird

{ long nbr_believers;

public:

pegasus(COLOR c,int h,long n,int age_n):

horse(age_n,c,h),bird(age_n,c),animal(age_n*2),nbr_beleivers(n)

{ cout<< pegasus\n; } //animal (virtual)

}; //

void main()

{ pegasus *p=new pegasus(white,5,100,2);

int age=p->get_age();

cout<< <<age<< \n;

delete p;

}

animal

horse

bird

pegasus

4

pegasus

bird

horse

animal