.


:




:

































 

 

 

 


. #include <iostream>




#include <iostream>

using namespace std;

class TA{

int a;

int b;

public:

TA (int x=3,int y=4){a=x;b=y;}

friend void print(TA&);

//

/* -, , , , :

*/

TA& operator++(){a++;b++;return *this;}

// :

const TA& operator=(const TA& operand2)

{if(&operand2==this) return *this;

//

//

//

a=operand2.a;b=operand2.b;

}

TA operator+(TA& operand2){

TA sum;

sum.a=a+operand2.a;

sum.b=b+operand2.b;

return sum;

}

bool operator>(TA& operand2){

return (a>operand2.a&&b>operand2.b)? true: false;

}

//

//

//

friend ostream& operator<<(ostream& out,TA &obj)

{out<<"a= "<<obj.a<<"\tb="<<obj.b;

return out;}

friend istream& operator>>(istream& in,TA &obj)

{

cout<<"Vvedite a=";

in>>obj.a;

cout<<"Vvedite b=";

in>>obj.b;

return in;}

};

int main()

{

TA b(4,5),c(1,1);

print(b);

b++;

print(b);

TA s=b+c;

print(s);

if (s>b)cout<<">"<<endl;

cin>>s;

cout<<s<<endl;//a=6 b=7

cin.get();

cin.get();

retrun 0;

}

//---------------------------------------------------------------------------

void print(TA &obj)

{cout<<obj.a<<"\t"<<obj.b<<endl;}

/*

""

,

- ;

-

:

- ( , );

- ;

- (">>") "<<".

.

*/

#include "stdafx.h"

#include <iostream>

#include <time.h>

class Tvec

{

int *vec;

int n;

public:

Tvec();//

Tvec(int n);//

Tvec(Tvec &);//

~Tvec(){delete [] vec;}//

void Rand(int);// 0

int Sum();//

bool operator==(Tvec&);// "=="

Tvec& operator=(Tvec&);// "="

Tvec operator+(Tvec&);// "+"

//

friend std::istream& operator>>(std::istream&,Tvec&);

friend std::ostream& operator<<(std::ostream&,Tvec&);

};

int main()

{

Tvec vec1, vec2(10);

vec2.Rand(5);

vec1.Rand(10);

std::cout<<"Ischodnie dannie:"<<std::endl;

std::cout<<"vec1 = "<<vec1<<std::endl;

std::cout<<"vec2 = "<<vec2<<std::endl;

std::cout<<"Sum1 = "<<vec1.Sum()<<std::endl;

std::cout<<"Sum2 = "<<vec2.Sum()<<std::endl;

if(vec1==vec2) std::cout<<"vec1==vec2"<<std::endl;

else std::cout<<"vec1!=vec2"<<std::endl;

Tvec vec3(vec2);

if(vec3==vec2) std::cout<<"vec3==vec2"<<std::endl;

else std::cout<<"vec3!=vec2"<<std::endl;

vec1=vec2;

std::cout<<"Vec1 = "<<vec1<<std::endl;

vec3.Rand(8);

std::cout<<"Vec3 = "<<vec3<<std::endl;

std::cout<<"Vec1+Vec3 = "<<vec1+vec3<<std::endl;

std::cin.get();

return 0;

}

 

Tvec::Tvec()

{ this->n=5;

vec=new int[n];

}

Tvec::Tvec(int n=5)

{ this->n=n;

vec=new int[n];

}

Tvec::Tvec(Tvec &obj)

{this->n=obj.n; vec=new int[n];

for(int i(0);i<n;i++)

vec[i]=obj.vec[i];

};

void Tvec::Rand(int k)

{

srand(time(NULL));

for(int i(0);i<n;i++)

vec[i]=rand()%k;

}

int Tvec::Sum()

{

int S(0);

for(int i(0);i<n;i++)

S+=vec[i];

return S;

}

bool Tvec::operator==(Tvec& obj)

{

int S1=this->Sum();

int S2=obj.Sum();

return (S1==S2);

}

Tvec& Tvec::operator=(Tvec& obj)

{

if(this==&obj) return *this;

this->~Tvec();

n=obj.n;

vec=new int[n];

for(int i(0);i<n;i++)

vec[i]=obj.vec[i];

return *this;

}

Tvec Tvec::operator+(Tvec& obj)

{

int m=n+obj.n;

Tvec Merg(m);

int i(0);

for(;i<n;i++)

Merg.vec[i]=vec[i];

for(;i<m;i++)

Merg.vec[i]=obj.vec[i-n];

return Merg;

}

 

 

std::istream& operator>>(std::istream& in,Tvec& obj)

{

for(int i(0);i<obj.n;i++)

in>>obj.vec[i];

return in;

}

std::ostream& operator<<(std::ostream& out,Tvec& obj)

{

for(int i(0);i<obj.n;i++)

out<< obj.vec[i]<<" ";

out<<'\n';

return out;

}

Ischodnie dannie:

vec1 = 7 4 8 5 3

vec2 = 2 4 3 0 3 4 0 1 3 1

Sum1 = 27

Sum2 = 21

vec1!=vec2

vec3==vec2

Vec1 = 2 4 3 0 3 4 0 1 3 1

Vec3 = 1 0 4 1 1 7 1 7 4 3

Vec1+Vec3 = 2 4 3 0 3 4 0 1 3 1 1 0 4 1 1 7 1 7 4 3

 

 

 

 

5.4

 

 

 

. . : *.h *.cpp. :

 

1.

2.

3.

4.

5.

6.

7.

8.

9.

 

 

5.6

 

1

. :

1) , ;

2) , .

2

. , .

. , .

3

. :

1) , ;

2) , .

4

. :

1) , ;

2) , .

5

. :

1) , ;

2) , .

6

. :

1) , ;

2) .

7

8 8 k, k- k- .

, .

8

. , .

, .

9

- i-1 i+1, j-1 j+1, (, ) (i, j). , . 10 10. , .

10

, . 10 10.

, .

11

. .

, .

12

, , . , .

13

( ), .

14

MxN k : 1- , , , ; .

15

. , .

. , .

16

.

, .

17

, , (2,2), (3,3) . ., .

, .

18

. :

1) , ;

2) , .

19

. :

1) , ;

2) , .

20

. :

1) , ;

2) .

 

 

Tmatr.cpp

/*

" "

.

: *.h *.cpp.

:

1.

2.

3.

4.

5.

6.

7. ( )

8.

:

1. ;

2. .

*/

#include "stdafx.h"

#include "TmatrH.h"

int main()

{

 

Nmatr::Tmatr matr1, matr2(10,5);

matr1.Rand(5);

matr2.Rand(10);

std::cout<<"Ischodnie dannie:"<<std::endl;

std::cout<<"matr1 = \n"<<matr1<<std::endl;

std::cout<<"matr2 = \n"<<matr2<<std::endl;

std::cout<<"Sum1 = \n"<<matr1.Sum()<<std::endl;

std::cout<<"Sum2 = \n"<<matr2.Sum()<<std::endl;

// "<"

if(matr1<matr2) std::cout<<"matr1<matr2"<<std::endl;

else std::cout<<"matr1>=matr2"<<std::endl;

Nmatr::Tmatr matr3(matr2);

if(!(matr3<matr2)||(matr2<matr3)) std::cout<<"matr3==matr2"<<std::endl;

matr1=matr2;

std::cout<<"matr1 = \n"<<matr1<<std::endl;

Nmatr::Tmatr matr4;

matr4.LoadFromFile("TextMatr.txt");

std::cout<<"matr4 = \n"<<matr4<<std::endl;

matr4.SaveFromFile("TextMatrOut.txt");

//

matr4.SortStr(2);

std::cout<<"matr4 = \n"<<matr4<<std::endl;

 

return 0;

}

 

 

TmatrH.h

#ifndef TmatrH

#define TmatrH

#include <iostream>

#include <fstream>

#include <time.h>

namespace Nmatr{

class Tmatr

{

int **matr;

int n,m;

public:

Tmatr();//

Tmatr(int,int);//

Tmatr(Tmatr &);//

~Tmatr();//

void Rand(int);// 0

int Sum();//

bool operator<(Tmatr&);// "<"

Tmatr& operator=(Tmatr&);// "="

//

friend std::istream& operator>>(std::istream&,Tmatr&);

friend std::ostream& operator<<(std::ostream&,Tmatr&);

void LoadFromFile(char*);//

void SaveFromFile(char*);//

void SortStr(int);//

};

//

std::istream& operator>>(std::istream&,Tmatr&);

std::ostream& operator<<(std::ostream&,Tmatr&);

 

}//end namespace Nmatr

 

#endif

 

Tmatr.cpp

#include "stdafx.h"

#include "TmatrH.h"

Nmatr::Tmatr::Tmatr()

{ this->n=5;

this->m=5;

matr=new int*[n];

for(int i(0);i<n;i++)

matr[i]=new int[m];

}

Nmatr::Tmatr::Tmatr(int n, int m)

{

this->n=n;

this->m=m;

matr=new int*[n];

for(int i(0);i<n;i++)

matr[i]=new int[m];

}

Nmatr::Tmatr::Tmatr(Tmatr &obj)

{

this->n=obj.n;

this->m=obj.m;

matr=new int*[n];

for(int i(0);i<n;i++)

matr[i]=new int[m];

for(int i(0);i<n;i++)

for(int j(0);j<m;j++)

matr[i][j]=obj.matr[i][j];

};

Nmatr::Tmatr::~Tmatr()

{

for(int i(0);i<n;i++)

delete[]matr[i];

delete[]matr;

 

}

void Nmatr::Tmatr::Rand(int k)

{

srand(time(NULL));

for(int i(0);i<n;i++)

for(int j(0);j<m;j++)

matr[i][j]=rand()%k;

}

int Nmatr::Tmatr::Sum()

{

int S(0);

for(int i(0);i<n;i++)

for(int j(0);j<m;j++)

S+=matr[i][j];

return S;

}

bool Nmatr::Tmatr::operator<(Nmatr::Tmatr& obj)

{

int S1=this->Sum();

int S2=obj.Sum();

return (S1<S2);

}

Nmatr::Tmatr& Nmatr::Tmatr::operator=(Nmatr::Tmatr& obj)

{

if(this==&obj) return *this;

this->~Tmatr();

this->n=obj.n;

this->m=obj.m;

matr=new int*[n];

for(int i(0);i<n;i++)

matr[i]=new int[m];

for(int i(0);i<n;i++)

for(int j(0);j<m;j++)

matr[i][j]=obj.matr[i][j];

return *this;

}

 

std::istream& Nmatr::operator>>(std::istream& in,Nmatr::Tmatr& obj)

{

for(int i(0);i<obj.m;i++)

for(int j(0);j<obj.m;j++)

in>>obj.matr[i][j];

return in;

}

std::ostream& Nmatr::operator<<(std::ostream& out,Nmatr::Tmatr& obj)

{

for(int i(0);i<obj.n;out<<'\n',i++)

for(int j(0);j<obj.m;j++)

{

out.width(5);

out<<obj.matr[i][j];

}

out<<'\n';

return out;

}

void Nmatr::Tmatr::LoadFromFile(char* FilaName)//

{

this->~Tmatr();

matr=new int*[n];

for(int i(0);i<n;i++)

matr[i]=new int[m];

std::ifstream in(FilaName);

in>>this->n;

in>>this->m;

for(int i(0);i<n;i++)

for(int j(0);j<m;j++)

in>>matr[i][j];

}

 

void Nmatr::Tmatr::SaveFromFile(char* FilaName)//

{ std::ofstream out(FilaName);

out<<this->n<<' ';

out<<this->m<<'\n';

for(int i(0);i<n;out<<'\n',i++)

for(int j(0);j<m;j++)

{

out.width(5);

out<<matr[i][j];

}

out<<'\n';

}

void Nmatr::Tmatr::SortStr(int k)//

{

if((k<0)||(k>=n))return;

for(int i(0);i<m;i++)

for(int j(0);j<m-1-i;j++)

if(matr[k][j]>matr[k][j+1])

{

int tmp=matr[k][j];

matr[k][j]=matr[k][j+1];

matr[k][j+1]=tmp;

}

}


6 , ,

 

 

6.2

 

() . , , .

 

: date birthday.

ß

class date { int day, month, year; public: date(int,int,int); date(char*); };

 

class birthday: public date { public: string name; };

 

. , . .

- .

:

private () - ;

protected () private, - ;

public () , .

 

, , .

.

class Derived: _ Base1 [, _ Base2,...] {...};

. , .

. - , , (private < protected < public).

 

. private public .

 

private, private . , public .

. .

ß

class Base { public: void f1(); };

 

class Derived: private Base { public: Base::f1; // f1() };

 

private, public .

, .

 

. .

date

ß


 

class date { protected: int day, month, year; public: date(int,int,int); void set_year(int y); void print(); };   date::date(int d, int m, int y) { day = d; month = m; year = y; }   void date::set_year(int y) { year = y; print(); }   void date::print() { printf("%d-%d-%d\n", day, month, year); }

 

birthday:

ß

class birthday: public date { public: char name[80]; birthday(int d, int m, int y, char* n); void print(); };   birthday::birthday(int d, int m, int y, char* n): date(d, m, y) { strcpy(name, n); }   void birthday::print() { printf("%d-%d-%d, dear %s\n", day, month, year, name); }

 

ß

date* d = new date(19, 10, 1951); birthday* b = new birthday(19, 10, 1951, "Bond"); d->set_year(2001); b->set_year(2001);

 

: "19-10-1951" .

 

date::print() ,

ß

virtual void print(int y);

 

:

 

"19-10-1951"

"19-10-1951, dear Bond".

 

, virtual.

 

:

 

1) static;.

2) virtual , .

3) , , .. .

virtual void print() = 0;

 

, , , . .





:


: 2017-02-25; !; : 326 |


:

:

80% - .
==> ...

1753 - | 1606 -


© 2015-2024 lektsii.org - -

: 0.246 .