组合类及一般类 的构造函数,析构函数,复制构造函数,友元类,友元函数
#include <iostream>
#include <cmath>
using namespace std;
class point {
int x, y;
public :
friend class line ;
point ( )
{
x= y= 0 ;
}
point ( int px, int py) : x ( px) , y ( py) { }
void print ( ) ;
int getx ( ) { return x; }
int gety ( ) { return y; }
} ;
void point:: print ( )
{
cout<< "(" << x<< "," << y<< ")" ;
}
class line
{
point a, b;
float distance;
public :
line ( point pa, point pb) ;
line ( const line & copyone) ;
void print ( ) ;
float length ( )
{
return distance;
}
} ;
line:: line ( const line & copyone) : a ( copyone. a) , b ( copyone. b) , distance ( copyone. distance)
{
cout<< "success copy" << endl;
cout<< "(" << a. x<< "," << a. y<< ")\t" ;
cout<< "(" << b. getx ( ) << "," << b. gety ( ) << ")" << endl;
}
line:: line ( point pa, point pb) : a ( pa) , b ( pb)
{
double x= pa. getx ( ) - pb. getx ( ) ;
double y= pa. y- pb. y;
distance= sqrt ( x* x+ y* y) ;
}
void line:: print ( )
{
cout<< "length:" << distance<< endl;
}
int main ( void )
{
point a ( 2.3 , 9.8 ) , b ( 4.2 , 5.9 ) ;
cout<< "L start point :" << "(" << a. getx ( ) << "," << a. gety ( ) << ")" << endl;
cout<< "L end point :" << "(" << b. getx ( ) << "," << b. gety ( ) << ")" << endl;
line line1 ( a, b) ;
line1. print ( ) ;
line line2 ( line1) ;
line2. print ( ) ;
return 0 ;
}