Информатика и технология программирования


Назад: Си++ = Си + классы + объектно-ориентированное - часть 4


};
class bb: public aa
{
public: int inc() { int nn = aa::inc(); return (nn-1); }
bb(int n) : aa(n+1) {}
};
void z2()
{ aa a1(10); bb b1(12);
int x = a1.inc(); int y = b1.inc() + a1.inc();
}
//------------------------------------------------------- 3


class aaa
{ int x;
public: aaa() { x = 0; }
aaa(int n) { x = n; }
int inc() { return(++x); }
};
class bbb : public aaa
{
public: int inc() { int nn = aaa::inc(); return (nn-1); }
bbb(int n) : aaa(n+1) {}
};
void z3()
{ aaa a1(10); bbb b1(12);
aaa *pa = &#38b1; bbb *pb = &#38b1;
int x = a1.inc();
int y = b1.inc() + pa-&#62inc();
int z = pb-&#62inc();
}
//------------------------------------------------------ 4


class ax
{ int x;
public:
virtual int out() { return(x); }
ax(int n) { x = n; }
};
class bx : public ax
{
public: int out() { return (ax::out()+1); }
bx(int n) : ax(n) { }
};
class cx : public ax
{
public: cx(int n) : ax(n) { }
};
void z4()
{ ax A1(5);
bx B1(6);
cx C1(10);
ax *p[] = { &#38A1, &#38B1, &#38C1 };
int r1 = p[0]-&#62out() + p[1]-&#62out() + p[2]-&#62out();
int r2 = A1.out() + B1.out() + C1.out();
}
//------------------------------------------------------ 5


class ay
{
public: virtual int put()=0;
ay() {};
};
class integer : public ay
{ int val;
public: int put() { return(val); }
integer(int n) { val=n; }
};
class string : public ay
{
char *str;
public: int put() { return(strlen(str)); }
string(char *s) { str = s; }
};
void z5()
{
integer a1(12);
integer a2(24);
string a3("aaaa");
ay *p[4]= { &#38a1, &#38a2, &#38a3, &#38a1 };
int i,x;
for (x = i = 0; i &#60 4; i++) x += p[i]-&#62put();
}
//------------------------------------------------------ 6


class mem
{
protected:
void *addr;
public: mem() {}
virtual int put() { return(0);}
};
class integers : public mem
{
public: int put() { return (*(int*)addr); }
integers(int &#38d) { addr = (void *)&#38d; }
};
class strings : public mem
{
public: int put() { return(strlen((char*)addr)); }
strings (char *p) { addr = (void*)p; }
};
void z6()
{
int x=12;
integers i0(x),i1(x);
strings s0("aaaa"),s1("bb");
mem *p[4] = { &#38i0, &#38i1, &#38s0, &#38s1 };
int n1,n2,i;
n1 = i0.put() + s0.put();
for (i=n2=0; i&#60 4; i++) n2 += p[i]-&#62put();
}






Начало  Назад  Вперед