++ --.
:
Tstud& operator ++(Tstud &Stud)
{
++Stud.bal;
return Stud;
}
Tstud& operator --(Tstud &Stud)
{
--Stud.bal;
return Stud;
}
() Tstud :
Tstud Student={"Ivanov",3};
++Student;// Student={"Ivanov",4}
--Student--;// Student={"Ivanov",2}
+ TAB. :
TAB operator +(const TAB &operand1,const TAB &operand2)
{TAB AB;
AB.A=operand1.A+operand2.A;
AB.B=operand1.B+operand2.B;
return AB;}
:
TAB AB1={1.3,2},AB2={2.1,1};
TAB AB3=AB1+AB2; // AB3={3.4,3};
> Tstud. , bol bol . :
bool operator >(const Tstud &Stud1, const Tstud &Stud2)
{return Stud1.bal>Stud2.bal;}
:
Tstud Stud1={"Petrov",3};
Tstud Stud2={"Sidorov",2};
if(Stud1>Stud2)cout<<">"<<endl;
else cout<<"<"<<endl;
-, . :
operator _ ();
.
:
operator () ( );
int , :
struct Tstud
{
char FIO[20];
int bal;
operator int() {return (bal); }
void operator()(char* str,int mybal)
{
strcpy(FIO,str);
bal=mybal;
}
};
:
Stud2("Petja",5); // Stud2={"Petja",5}
cout<<(int)Stud2<<endl;// 5
,
<< >>. . , , . , -, - , . , , .
:
|
|
ostream& operator<<(ostream& _, )
{
return _;
}
:
istream& operator>>(istream& _, )
{
return _;
}
Tstud:
ostream& operator<<(ostream& out,const Tstud &Stud)
{
out<<setw(10)<<setfill('.')<<setiosflags(ios::left)<<Stud.FIO
<<Stud.bal<<endl;
return out;
}
istream& operator>>(istream& in,Tstud &Stud)
{
cout<<"Vvedite FIO\n";
in>>Stud.FIO;
cout<<"Vvedite Bal\n";
in>>Stud.bal;
return in;
}
:
Tstud Stud;
cin>>Stud;
cout<<Stud;
.
4.4
, .
4.6
2
++, --, >, +, (), int, .
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Tstud{
char FIO[20];
int bal;
operator int() const {return (bal); }
void operator()(char* str,int mybal)
{
strcpy(FIO,str);
bal=mybal;
}
};
Tstud& operator ++(Tstud &Stud)
{
++Stud.bal;
return Stud;
}
Tstud& operator --(Tstud &Stud)
{
--Stud.bal;
return Stud;
}
Tstud operator +(const Tstud &Stud1, const Tstud &Stud2)
{
Tstud Stud=Stud1;
strcat(Stud.FIO,Stud2.FIO);
Stud.bal+=Stud2.bal;
return Stud;
}
bool operator >(const Tstud &Stud1, const Tstud &Stud2)
{
return Stud1.bal>Stud2.bal;
}
ostream& operator<<(ostream& out,Tstud &Stud)
{
out<<setw(20)<<setfill('.')<<setiosflags(ios::left)<<Stud.FIO<<Stud.bal;
return out;
}
istream& operator>>(istream& in,Tstud &Stud)
{
cout<<"Vvedite FIO\n";
in>>Stud.FIO;
cout<<"Vvedite Bal\n";
in>>Stud.bal;
return in;
}
int main()
{
Tstud Student1={"Ivanov",3};
Tstud Student2={"Petrov",5};
Tstud Student3;
Student1--;//Student1={"Ivanov",2};
Student2++;//Student2={"Petrov",6};
cout<<Student1<<endl;
cout<<Student2<<endl;
Student3=Student1+Student2;//Student3={"IvanovPetrov",8};
cout<<Student3<<endl;
Student3("Sidorov",5);//Student3={"Sidorov",5};
cout<<Student3<<endl;
if(Student1>Student2)
cout<< Student1.FIO<<" u4itsj lu4she "<<Student2.FIO<<endl;
else cout<< Student2.FIO<<" u4itsj lu4she "<<Student1.FIO<<endl;
|
|
const int n=3;
Tstud StudentMas[n];
//
for(int i=0;i<n;i++)
cin>>StudentMas[i];
//
for(int i=0;i<n;i++)
cout<<StudentMas[i]<<endl;
//
ofstream out("f.txt");
for(int i=0;i<n;i++)
out<<StudentMas[i]<<endl;
cin.get();
return 0;
}
:
Ivanov.2
Petrov..6
Ivanov Petrov..8
Sidorov5
Petrov u4itsj lu4she Ivanov
Vvedite FIO
Vvedite Bal
Vvedite FIO
Vvedite Bal
Vvedite FIO
Vvedite Bal
5 ++
5.2
, , , . , , .
++ , , -, -, .
. . , , . , , .
ß
class date
{
public:
int day, month, year;
void set (int, int, int);
void get (int&, int&, int&);
void next ();
};
- . inline. .
.
. - date::set, .
void date::set (int d, int m, int y) {
day = d;
month = m;
year = y;
}
. . . , , ,
date d;
, .
ß
cout << d.day;
d.set(19,10,2001);
- . ( , , , , ). , .
public private .
. date , .
ß
class date
{
private:
int day, month, year;
public:
void set (int,int,int);
...
};
, , ,
int day, month, year;
int date::day, date::month, date::year;
.
. , . , . .
. .
ß
class date {
int day, month, year;
public:
date(int,int,int);
date(char*);
date();
};
, . , ++ .
:
|
|
int main(int argc, char* argv[])
{
date d1 = date(19, 10, 2001);
date d2(19, 10, 2001); // ,
date d3 = date("19-Oct-2001");
date d4 = "19-Oct-2001"; // 1
date d5; //
return 0;
}
, , . .
, . , . ~ . ,
~date().
. , . , .
date* p date. p , (*p).set(),
p->set()
.
ß
date d1;
date* p = &d1;
, this. -. X this :
X* const this;
- ,
int readme() const {/* */};
this, , :
const X *const this.
new delete
new.
ß
date* p = new date(19, 10, 2001);
, , 0.
. new , , .
delete.
ß
delete p;
!