.


:




:

































 

 

 

 


operator _ (_);




 

:

++, ., .*, ?:, ::, sizeof;

( ++) ;

. , ++ ( ), + , ;

;

, , . this ( );

: + - * / % ++ -- ;

, = , , , ;

, .

 

 

4_2. 1

vector 1. h // ,

//

#ifndef _VECTOR1_H //

#define _VECTOR1_H //

#include <iostream>

using std::endl;

using std::cout;

class VECTOR1 //

{

//

private:

int x, y; //

//

public:

//

//

VECTOR1 (const int &x1=0, const int &y1=0): x(x1), y(y1){ }

// :

//VECTOR (const int &x1=0, const int &y1=0) { x = x1; y = y1; }

// -

// ,

//

//

// return.

//

VECTOR1 (const VECTOR1 &copy)//copy -

{

x = copy.x; y=copy.y;

}

// : .

void Assign(const int &x1, const int &y1)

{

x = x1; y = y1;

return;

}

// "++"

// :

// ( !)

// ,

// (this),

// .

VECTOR1 operator++(void)

{

x++; y++;

return *this; //

}

// "++"

// :

// .

// "operator++(int)"

// .

VECTOR1 operator++(int)

{

VECTOR1 vec_tmp = *this;

x++; y++;

return vec_tmp; //

}

// "+"

// :

// ( !). const ,

// , .

// .

VECTOR1 operator+(const VECTOR1 &) const;

// "=":

// (

// ).

// ,

//

//

//

VECTOR1& operator = (const VECTOR1 &v) //

// this -

// v -

{

this->x = v.x; this->y = v.y;

// : x = v.x; y = v.y;

return *this;

}

// : .

// .

void print(void) const;

};

void VECTOR1::print(void) const

{

cout<<"\n Coordinates of a vector: x= "

<<x<<" y= "<<y<<endl;

return;

}

// "+" , "operator+"

// e (this)

//

VECTOR1 VECTOR1:: operator+(const VECTOR1 &vec) const

{

//

//

VECTOR1 vec_tmp = *this;

//

vec_tmp.x += vec.x; vec_tmp.y += vec.y;

return vec_tmp; // vec_tmp,

// vec_tmp

}

#endif _VECTOR1_H

class _ vector 1. cpp // VECTOR1

#include "vector1.h"

const int N = 5; //

Int main()

{

// v1 v_a,

//

VECTOR1 v1, v_a[N];

//

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

{

v_a[i].Assign(i, i+1);

cout<<"\n Vector v_a["<<i<<"]:";

v_a[i].print();

}

//

//

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

{

++v_a[i];

cout<<"\n Vector v_a["<<i<<"]:";

v_a[i].print();

}

//

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

{

v1 = v1 + v_a[i];

}

cout<<"\n Summa - vector v1: "; v1.print();

cout<<endl;

system("pause");

return 0;

}

 

 

:

 

 


 

- - , .

4.1 Figure (), Circle (), Rectangle ().

4.1

Figure , Circle Rectangle .

 

++ :

, , . , ;

( ). , .

 

( 4.1) . , .

 

4_3. , ,

fiures. h // , ,

// ,

# ifndef _ FIGURES _ H //

#define _FIGURES_H //

#include <iostream>

using std::endl;

using std::cout;

class Figure //

{

//

public:

Figure(void) //

{

cout << "The constructor Figure" << endl;

}

virtual ~Figure(void) //

{

cout << "The destructor Figure" << endl;

}

// -

//

virtual void draw(void) = 0;

};

class Circle: public Figure // Circle Figure

{

//

private:

int x, // x-

y, // y-

R; //

//

public:

Circle(int CenterX, int CenterY, int Radius) //

{

cout << "The constructor Circle" << endl;

x = CenterX; y = CenterY; r = Radius;

}

virtual ~Circle(void) //

{

cout << "The destructor Circle" << endl;

}

// -

virtual void draw (void);

};

// , Circle

void Circle::draw(void)

{

cout << "The Circle draw" << endl;

}

class Rectangle: public Figure // Rectangle Figure

{

//

private:

int l, //

T, //

R, //

b; //

//

public:

Rectangle(int L, int T, int R, int B) //

{

cout << "The constructor Rectangle" << endl;

l = L; t = T; r = R; b = B;

}

virtual ~Rectangle(void) //

{

cout << "The destructor Rectangle" << endl;

}

// -

virtual void draw(void);

};

// , Rectangle

void Rectangle::draw(void)

{

cout << "The Rectangle draw" << endl;

}

# endif _ FIGURES _ H

 

 

der _ virt 1. cpp // , ,

//

#include "figures.h"

int main() // Figure, Circle Rectangle

{

// ,

// Circle Rectangle

Figure *figures[2];

figures[0] = new Circle(100, 100, 10);

figures [1] = new Rectangle (100, 100, 200, 250);

//

figures[0]->draw();

figures[1]->draw();

// .

// , ~Figure() -

// ,

delete figures[0]; delete figures[1];

cout<<endl;

system (" pause ");

return 0;

}

 

:

 

, , , .

 

( draw) !

 

4.4.1

 

, .

( ) . . , (, a = b = c) !

, , . , , , !

assert () assert. h. , , .

 

 

4_4_1.

DinArray 1. h //

 

#ifndef _DIN_ARRAY1_ //

#define _DIN_ARRAY1_ //

#include "assert.h"

#include <iostream>

using std::endl;

using std::cout;

class Array1D //

{

//

private:

int *p; //

int size; //

//

public:

Array1D(int n=3); // ( = 3)

Array1D(const Array1D& a); //

~Array1D() {delete [] p;} // ,

//

void print()const; //

// :

int& operator [](int i)const; //

Array1D& operator =(const Array1D& a); //

Array1D operator +(const Array1D& a)const; // ( )

};

// Array 1 D

Array 1 D:: Array 1 D (int n): size (n) //

{

assert (n >0); //

p = new int [ n ]; //

assert (p!=0); // ,

};

Array1D::Array1D(const Array1D& a): size(a.size) //

{

p = new int [ size ]; //

assert (p!=0); // ,

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

p [ i ] = a. p [ i ]; //

}

void Array1D::print() const

{

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

cout<<p[i]<<endl;

cout<<endl;

}

int & Array1D::operator [](int i) const

{

assert (i >=0 && i < size); // ,

return p [ i ]; // i

}

Array1D& Array1D::operator =(const Array1D& a)

{

if (this!= &a){ // ,

assert (size == a. size); // ,

//

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

p[i] = a.p[i];

}

return *this; // Array1D

}

Array1D Array1D::operator +(const Array1D& a) const

{

assert (size == a. size); // ,

//

Array 1 D sum (size); // sum size

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

sum.p[i] = p[i] + a.p[i]; // sum.p[i]=this->p[i] +a.p[i];

return sum; // ,

// sum e

}

# endif _ DIN _ ARRAY 1_

 

DinArray 1. cpp // Array1D

 

#include "DinArray1.h"

Int main()

{

Array1D a, b, c; // = 3

for (int i=0; i<3; ++i) a[i]=i+1; //

cout<<"Array1D a: "<<endl; //

A.print();

b = a; // b

// a

cout<<"Array1D b: "<<endl; // b

B.print();

a = a + b + (c = a + b); // ,

//

cout<<"Array1D c: "<<endl; //

c.print();

cout <<" Array 1 D a: " << endl; //

A.print();

system("pause");

return 0;

}

 

 


:

 

 

4.4.2

 

, .

< cstring >:

strcpy _ s (s, len, str) len str s

strcat _ s (s, len, str) len str s

strlen (str) str. size _ t

strcpy _ s, strcat _ s ( ) strcpy, strcat, :

This function or variable may be unsafe. Consider using strcpy_s instead.

 

assert () assert. h.

 

4_4_2.

DinString. h //

#ifndef _DIN_STRING_ //

#define _DIN_STRING_ //

#include <iostream>

using std::endl;

using std::cout;

#include <cstring>

#include "assert.h"

class DinString //

{

//

private:

char *s; //

int len; //

//

public:

DinString(): len(0){ //

s=new char[1]; assert(s!=0); s[0]=0;

}

DinString(const DinString& str); //

DinString(const char* str); // -

// char* DinString

~DinString() {delete [] s;} // ,

//

void print()const { cout<<s<<endl;}//

//

DinString& operator =(const DinString& str); //

DinString operator + (const DinString& str) const; //

};

// DinString

DinString::DinString(const char* str) //

{

len = static_cast<int>(strlen(str));//

// strlen size _ t

// int

s=new char[len+1]; //

assert(s!=0); // ,

strcpy_s(s, len+1, str);

};

DinString::DinString(const DinString& str): len(str.len) //

{

s=new char[len+1]; //

assert(s!=0); // ,

strcpy_s(s, len+1, str.s);

}

DinString& DinString::operator =(const DinString& str)

{

if (this!= &str){ // ,

delete [] s; // ""

len = str.len; //

s=new char[len+1]; //

assert(s!=0); // ,

strcpy_s(s, len+1, str.s);

}

return *this; //

}

DinString DinString::operator + (const DinString& str) const

{

DinString tmp;

tmp.s =new char[len + str.len +1];

tmp.len=len + str.len;

strcpy_s(tmp.s, tmp.len+1, s);

strcat_s(tmp.s, tmp.len+1, str.s);

return tmp; // ,

// tmp

}

#endif _DIN_STRING_

 

 

DinSring. cpp // DinString

 

#include "DinString.h"

Int main()

{

DinString s1("123"), s2("456"), s3(s1), s4;

s4=s3 + s2;

cout<<"s1: "; s1.print(); cout<<"s2: "; s2.print();

cout<<"s3: "; s3.print(); cout<<"s4: "; s4.print();

cout<<endl;

system("pause");

return 0;

}

 

 

:

 

 





:


: 2018-10-14; !; : 305 |


:

:

! . .
==> ...

1533 - | 1325 -


© 2015-2024 lektsii.org - -

: 0.266 .