CPP_Liczby_zespolone_i_obwod_trojkata.pdf

(67 KB) Pobierz
398371414 UNPDF
Programowanie Obiektowe
1. Podać w języku C++ przykładowy kod operatora dodawania dwóch obiektów klasy:
class complex {
public :
double re , im ;
};
Odp.
class complex
{
public :
double re , im ;
complex operator +( const complex & b )
{
return complex ( re + b . re , im + b . im ) ;
}
};
Pełny kod programu:
#include < iostream >
using namespace std ;
/*#########################_KLASA_COMPLEX_########################*/
class complex
{
public :
double re , im ; //re - część rzeczywista | im - część zespolona
complex ( void ) : re ( 0 ), im ( 0 ) {}
complex ( double r , double i = 0 ) : re ( r ), im ( i ) {}
~ complex () {}
complex operator +( const complex & b )
{
return complex ( re + b . re , im + b . im ) ;
}
friend ostream & operator <<( ostream & out , const complex & c ) ;
};
/*#######################_DEFINICJE_FUNKCJI_######################*/
//-przeciążenie operatora (wypisanie na ekran)
ostream & operator <<( ostream & out , const complex & c )
{
out << c . re << " + " << c . im << " i " ;
return out ;
}
//---------------------------------------------------
/*########################_PROGRAM_GŁÓWNY_########################*/
int main ()
{
complex a ( 2 , 4 ), b ( 3 , 7 ), c ;
c = a + b ;
cout << " ( " << a << " ) + ( " << b << " ) = ( " << c << " ) \n " ;
}
398371414.010.png 398371414.011.png 398371414.012.png 398371414.013.png 398371414.001.png
2. Uzupełnić kod poniższego programu a następnie zrobić z niego kompletnie funkcjonujący
program.
...
class punkt {
public :
double x , y ;
double odleglosc ( punkt ) ;
...
};
...
class trojkat {
punkt p1 , p2 , p3 ;
public :
...
};
ostream & operator <<( ostream & strumien , punkt & p )
{
strumien << " ( " << p . x << " , " << p . y << " ) " ;
}
...
int main ( int argc , char * argv [])
{
punkt p1 , p2 ( 2 ., 0 .), p3 ( 2 ., 3 .) ;
trojkat ( p1 , p2 , p3 ) ;
cout << " p1: " << p1 << endl ;
cout << " p2: " << p2 << endl ;
cout << " p3: " << p3 << endl ;
cout << " trojkat " << t1 << " ma obwod " << t1 . obwod () << endl ;
}
Pełny kod programu:
#include < iostream >
#include < cmath >
using namespace std ;
/*#######################_DEFINICJE_KLAS_#######################*/
//---------------------------------------------
class punkt {
public :
double x , y ;
punkt () : x ( 0 ), y ( 0 ) {}
punkt ( double Ax , double Ay ) : x ( Ax ), y ( Ay ) {}
~ punkt () {}
double odleglosc ( punkt a , punkt b ) ;
friend ostream & operator <<( ostream & strumien , punkt & p ) ;
};
//---------------------------------------------
class trojkat : public punkt
{
private :
punkt p1 , p2 , p3 ;
public :
trojkat () {}
trojkat ( punkt P1 , punkt P2 , punkt P3 ) : p1 ( P1 ), p2 ( P2 ), p3 ( P3 ) {}
~ trojkat () {}
double obwod () ;
friend ostream & operator <<( ostream & strumien , trojkat & t ) ;
};
//---------------------------------------------
398371414.002.png 398371414.003.png 398371414.004.png 398371414.005.png
/*######################_DEFINICJE_FUNKCJI_#####################*/
//---------------------------------------------
ostream & operator <<( ostream & strumien , punkt & p )
{
strumien << " ( " << p . x << " , " << p . y << " ) " ;
}
//---------------------------------------------
ostream & operator <<( ostream & strumien , trojkat & t )
{
strumien << " A = " << t . p1 << " , B = " << t . p2 << " , C = " << t . p3 ;
}
//---------------------------------------------
double punkt :: odleglosc ( punkt a , punkt b )
{
int p1 ; int p2 ;
( a . x > b . x ) ? p1 = a . x - b . x : p1 = b . x - a . x ;
( a . y > b . y ) ? p2 = a . y - b . y : p2 = b . y - a . y ;
return sqrt (( p1 * p1 ) + ( p2 * p2 )) ;
}
//---------------------------------------------
double trojkat :: obwod ()
{
return odleglosc ( p1 , p2 ) + odleglosc ( p2 , p3 ) + odleglosc ( p3 , p1 ) ;
}
//---------------------------------------------
/*#######################_PROGRAM_GŁÓWNY_#######################*/
int main ( int argc , char * argv [])
{
punkt p1 , p2 ( 2 ., 0 .), p3 ( 2 ., 3 .) ;
trojkat t1 ( p1 , p2 , p3 ) ;
cout << " p1: " << p1 << endl ;
cout << " p2: " << p2 << endl ;
cout << " p3: " << p3 << endl ;
cout << " trojkat " << t1 << " ma obwod " << t1 . obwod () << endl ;
}
398371414.006.png 398371414.007.png 398371414.008.png 398371414.009.png
Zgłoś jeśli naruszono regulamin