.


:




:

































 

 

 

 





__ _:: operator #( )

{ ,

};

class A

{char c; int i;

public:

A(int I = 0, char C = NULL);

A(const A&);

A operator + (A);

friend A operator - (A,A);

A operator = (A);

};

A::A(int I, char C)

{ i = I; c = C;}

 

A::A(A const &obj)

{ i=obj.i;

c=obj.c;

}

A A::operator + (A obj)

{

A tmp;

tmp.i = this->i + obj.i; //a+b

if(c>obj.c) tmp.c = c+1;

else tmp.c = obj.c +1;

return tmp;

}

A operator - (A obj1, A obj2)

{

A tmp;

tmp.i = obj1.i-obj2.i;

if(obj1.c<obj2.c) tmp.c = obj1.c -1;

else tmp.c = obj2.c -1;

return tmp;

}

A A::operator = (A obj)

{

this->i=obj.i;

this->c=obj.c;

return *this;

}

void main()

{

A a(2,'a'), b(4,'b'), c, d;

c = a+b;

d=c=a-b;

}

 

.

14. ++

__ _:: operator #( )

{ ,

};

class A

{ int a;

public:

A(int I = 0);

A(const A&);

A operator++();

A operator++(int);

friend A operator--(A &);

friend A operator--(A &,int);

A operator + (A);

};

A::A(int I)

{a = I;}

 

A::A(A const &obj)

{a=obj.a;}

 

A A::operator ++(int)

{ A tmp=*this; a++; return tmp;

}

 

A A::operator ++()

{ a++; return *this;

}

 

A operator --(A &obj)

{ obj.a--; return obj;

}

 

A operator --(A&obj,int)

{ A tmp=obj; obj.a--; return tmp;

}

 

A A::operator + (A obj)

{

A tmp;

tmp.a = this->a + obj.a;

return tmp;

}

 

void main()

{ A ob1(2),ob2(3);

ob1++ + ++ob2; //2+4

}

15.16.21. ().[].=

class A

{char *M; int len;

public:

A(const char * = "");

A(const A &);

~A();

char &operator[] (int);

A operator() (int,int);

A &operator=(const A &);

};

A::A(const char *s)

{ len = strlen(s); M = new char[len + 1]; strcpy(M, s);

}

A::A(const A &copy)

{ len = copy.len; M = new char[len + 1]; strcpy(M, copy.M);

}

A::~A()

{ delete [] M;

}

A A::operator () (int index, int len)

{ int j,i,len_new;

char x[100]="\0";

if(index>=0 && index<this->len)

{ if(this->len-index+1>=len) len_new = len;

else len_new = this->len-index+1;

 

for(j=0,i=index; j<len_new; i++,j++)

x[j]=M[i];

x[len_new] = '\0';

}

return A(x);

}

char &A::operator[] (int j)

{ assert(j >= 0 && j < len); return M[j];

}

A& A::operator=(const A &obj)

{ if(&obj!=this)

{

delete []M; this->len=obj.len; //new len stroki

M=new char[len+1]; strcpy(M, obj.M);

}

return *this;

}

void main()

{

A s01,s02;

s02=s01(3,10);

}

->.

*.

class A

{ int a,b;

public:

A(int I = 0, int J = 0);

A(const A&);

A *operator -> () {return this;} // obj

A &operator * () {return *this;}

int min();

};

A::A(int I, int J)

{a = I; b = J;}

 

A::A(A const &obj)

{a=obj.a; b=obj.b;}

 

A::min()

{

if(a<b) return a;

else return b;

}

 

void main()

{

A obj;

A *p; p=&obj;

p->min();

obj->min(); // . (&obj)->min();

}

 

 

new. delete.

void *operator new(size_t tip,int kol) // operator

{ cout << " NEW" <<endl;

return new char[tip*kol]; // new

}

class cls

{ char a[40];

public:

cls(char *aa)

{ cout<<" cls"<<endl;

strcpy(a,aa);

}

~cls(){}

void *operator new(size_t,int);

void operator delete(void *);

};

void *cls::operator new(size_t tip,int n) // operator

{ cout << " " <<endl;

return new char[tip*n]; // new

}

void cls::operator delete(void *p) // operator

{ cout << " DELETE" <<endl;

delete p; // delete

}

void operator delete[](void *p) // operator

{ cout << " DELETE" <<endl;

delete p; // delete

}

int main()

{ cls obj(" NEW DELETE");

float *ptr1;

ptr1=new (5) float; // . new

delete [] ptr1; // . delete

cls *ptr2=new (10) cls("aa"); //

// new ( cls)

delete ptr2; //

return 0; // delete ( cls)

}

Inline-.

, . , . . ( ) . . ,

, -

. :

class stroka

{ char str[100];

public:

..

int size()

{ for(int i=0; *(str+i); i++);

return i;

}

};

size() . , -

, , ,

inline:

class stroka

{ char str[100];

public:

..

int size();

};

inline int stroka::size()

{ for(int i=0; str[i]; i++);

return i;

}

inline , -

.

, -

:

class ext_class

{ class int_cls

{

};

public:

};

int_cls ext_class

().

, private,

-

, friend .

 

#include <iostream>

using namespace std;

class cls1 //

{ class cls2 //

{ int b; // private cls1 cls2

cls2(int bb): b(bb){} // cls2

};

public: // public cls1

int a;

class cls3 //

{ int c; // private cls1 cls3

public: // public- cls3

cls3(int cc): c(cc) {} // cls3

};

cls1(int aa): a(aa) {} // cls

};

void main()

{ cls1 aa(1980); //

//cls1::cls2 bb(456); // cls2 cannot access private

cls1::cls3 cc(789); //

cout << aa.a << endl; //

//cout << cc.c << endl; // 'c': cannot access private member

// declared in class 'cls1::cls3'

}

 





:


: 2016-07-29; !; : 475 |


:

:

.
==> ...

1653 - | 1627 -


© 2015-2024 lektsii.org - -

: 0.052 .