++ - . , : , .
12. ++ . .
++ - , , , , . ++ : -, - (). .
class Point2D
{
private:
double a,b;
public:
void read();
double mod() count
{
return sqrt(x*x+y*y);
}
void print() const;
Point2D . . .
void main()
{
Point2D p1,p2;
}
, :
- ,
- , , .
public private. protected. . . protected . . public, private. . , , . . private. private
class Point2D
{
private:
double x,y; //+ Point2D *const this;
public:
double mod()const
{
return (sqrt(x*x+y*y));
}
void read(); //Point2D *this
void print()const;
};
mod . , .. inline. . cpp.
(::)
//Point2D.cpp
#include <iostream.h>
#include <math.h>
#include point2d.h
/*_::__*/
void Point2D::read()
{
cout<<:;
cin>>x>>y;
}
void Point2D::print()const
{
cout<<: <<x<< <<y<<\n;
};
|
|
inline, . - (-> .). , - , (this). - . (my_class *const this).
const, this : const my_class *const this.
//main.cpp
#include <iostream.h>
#include point2d.h
Point2D p0; // . .
void main()
{
Point2D p1,
*p2=new Point2D,
&p3=*new Point2D; //
p1.read();
p2->read();
p3.read();
cout<<p1.mod()<< <<p2->mod()<< <<p3.mod()<<\n;
delete p2;
delete &p3;
};
//
class Point2D
{
double x,y;
double mod(const point2D *this)
{
return (sqrt(this->x*this->x+this->y*this->y));
}
void print()
{
...
}
, . ++ , . -, . . .
Point2D(double, double)
Point2D::Point2D(double x, double y)
{ x=_x;
y=_y;}
//main.cpp
Poind2D p10=Point2d(1,2);
p20=Poind2D(2,2);
Poind2D p10(1,2), p20(2,2); // . 2 : .
.. - , ( 0 , , . .. , . .
//point2d.h
Poind2D(double a=0)
{
x=a;
y=a;
}
Poind2D p1, p2(2); //
Point2D p3(5,6) // 2
p4=4.0; //
. : . , .
, . .
- ,
- ,
- ,
, . operator . :
|
|
. _::operator#(_)
{
//_...
}
# .
, : , .
Point2D Point2D::operator-(Point2D p2)
{
Point2D temp;
temp.x=x-p2.x;
temp.y=this->y-p2.y;
return temp;
}
, . , . this. , .
Point2D operator-(Point2D p2) const;
//Point2D.cpp
Point2D Point2D::operator-(Point2D p2) const
{
return Point2D(x-p2.x, y-p2.y);
}
-. , . , . -, , , -.
//Point2D.h
Point2D operator-() const;
//Point2D.cpp
Point2D Point2D::operator-() const
{
return Point2D(-x, -y);
}
void main()
{
const Point2D p1(1,2);
Point2D p2(2,3), p3;
(p1-p2).print(); //
p1.operator-(p2).print(); //
cout<<p1*p2<<\n;
(p1-20).print(); //
}
, , p1-20 . . -. 2.0-p1 , , .
++ . :
- .
- .*
- ::
- ?:
- sizeof
+ * / % ^ & |~!= <> += -= /= == && || ++ ->* -> [] () new delete
. , . , , . , . , . .
, , .
Point2D p1, p2(1,2), p3;
(p1+p2).print();
(p1+2.0).print();
(2.0+p2).print(); //
Point2D. , . ++ - . friend, , , .
//point2d.cpp
Point2D operator+(Point2D p1, Point2D p2)
{
return Point2D(p1.x+p2.x, p1.y+p2.y);}
|
|
//point2d.h
friend Point2D operator+(Point2D, Point2D);
- private, protected, public , .
- -
- , friend - . , .
- , .
ostream <<Point2D
ostreamf
friend ostream& operator <<(ostream &, Point2D);
friend istream& operator >>(istream&, Point2D&);
//point2d.cpp
ostreamf operator<<(ostream& out, Poind2D p)
{
return out<<:
<<p.x<<
<<p.y;
}
istream operator >>(istream &in, Point2D&)
{
cout<< ;
return in >>p.x>>p.y;
}
Point2D p2, const Point2D p1(1,2);
cin>>p2;
cout<<p1+p2<<\n
<<p1*p2<<\n;
cout<<p1+2<<2+p1<<\n<<p.y;
, - - .
class B;
class C;
class A
{
int m1;
char m2;
friend void ff(A); // ff
public
char fm(c);
char fm2(b,c);
};
class B
{
double mb2;
friend char A::fm2(B,C); // ,
...
}
class C
{
char mc1;
friend class A; // -
...
}
, , .. . , .