• Definiowanie prostych klas. Przykłady.
• Przypomnienie: typy referencyjne, domyślne wartości Programowanie Obiektowe
argumentów, przeciążanie funkcji.
• Konstruktory, destruktory. Definiowanie operatorów.
(język C++)
Strumieniowe operacje we/wy.
• Wskaźnik this. Operatory new, delete. Funkcje zaprzyjaźnione.
• Klasa z dynamicznie alokowaną tablicą składowych.
Wykład 1.
• Pola i metody statyczne. Dostępność składowych klasy.
• Klasa z polem obiektowym.
-1-
-2-
Literatura
Program przedmiotu c.d.:
B. Stroustrup – Język C++ (The C++ Programming Language), WNT 2002
• Dziedziczenie, polimorfizm i metody wirtualne.
• Jak realizowany jest mechanizm wirtualności? Metody
S.B. Lippman – Podstawy języka C++ (C++ Primer), WNT 1997
czysto wirtualne i ATD.
• Obsługa błędów. Obsługa wyjątków. Wyjątki jako mechanizm sterujący.
J. Grę bosz – Symfonia C++ standard, Edition 2005
• Konwersje, operatory konwersji i konwertery.
• Definiowanie i wykorzystanie szablonów funkcji.
J. Liberty – Poznaj C++ w 10 minut, Intersoftland 1999
• Definiowanie i wykorzystanie szablonów klas.
• Elementy biblioteki STL.
Nicolai M. Josuttis - C++ Biblioteka standardowa. Podręcznik programisty, Helion 2003
-3-
-4-
Zadanie na początek
#include <iostream>
#include <cmath>
using namespace std;
struct CMPLX
• Chcemy napisać program, który umożliwi
{
double Re, Im;
};
wykonywanie prostych obliczeń na liczbach
void Read ( CMPLX& a ) {
zespolonych.
cin >> a.Re;
cin >> a.Im;
}
double Abs ( CMPLX a ) { return sqrt ( a.Re * a.Re + a.Im * a.Im ); }
• Zadanie zrealizujemy najpierw wykorzystując środki CMPLX Add ( CMPLX a, CMPLX b ) {
a.Re += b.Re; a.Im += b.Im;
języka C++ znane nam już z wykładu AiPP.
return a;
}
int main ( ) {
CMPLX x,y,z;
double result;
• Następnie zobaczymy, jak może to wyglądać w Read ( x );
języku C++ z wykorzystaniem pojęcia klasy.
Read ( y );
z = Add ( x, y );
result = Abs ( z );
cout << "Result = ” << result;
}
-5-
-6-
#include <iostream>
double Abs ( CMPLX a )
#include <cmath>
{
using namespace std;
return sqrt ( a.Re * a.Re + a.Im * a.Im );
}
struct CMPLX
{
int main ( )
double Re, Im;
{
};
CMPLX x, y, z;
double result;
void Read ( CMPLX& a )
{
cin >> a.Re;
Read ( x );
cin >> a.Im;
Read ( y );
}
z = Add ( x, y );
CMPLX Add ( CMPLX a, CMPLX b )
{
result = Abs ( z );
a.Re += b.Re;
a.Im += b.Im;
return a;
cout << ”Result = ” << result;
}
}
-7-
-8-
Przykład #2
Struktura programu Przykład #1
#include <iostream>
#include <cmath>
using namespace std;
struct CMPLX {
double Re, Im;
double Abs ( ) { return sqrt ( Re * Re + Im * Im ); }
void Read ( ) { cin >> Re; cin >> Im; }
};
CMPLX Add ( CMPLX a, CMPLX b ) {
a.Re += b.Re;
a.Im += b.Im;
return a;
}
int main ( ) {
CMPLX x, y, z;
x.Read ( );
y.Read ( );
z = Add ( x, y );
cout << ”\nResult = ” << z.Abs ( ) << endl;
}
-9-
-10-
#include < iostream >
#include < cmath >
CMPLX Add ( CMPLX a, CMPLX b )
{
using namespace std;
a.Re += b.Re;
a.Im += b.Im;
struct CMPLX
return a;
{
}
double Re, Im;
int main ( )
double Abs ( )
{
{
CMPLX x, y, z;
return sqrt ( Re * Re + Im * Im );
}
x.Read ( );
y.Read ( );
void Read ( )
{
z = Add ( x, y );
cin >> Re;
cin >> Im;
cout << ”\nResult = ” << z.Abs ( ) << endl;
}
}
};
-11-
-12-
Przykład #2
Przykład #2.
#include <iostream>
#include <iostream>
#include <cmath>
#include <cmath>
using namespace std;
using namespace std;
struct CMPLX {
struct CMPLX {
double Re, Im;
double Re, Im;
double Abs ( ) { return sqrt ( Re * Re + Im * Im ); }
double Abs ( ) const { return sqrt ( Re * Re + Im * Im ); }
void Read ( ) { cin >> Re; cin >> Im; }
void Read ( ) { cin >> Re >> Im; }
};
};
CMPLX Add ( CMPLX a, CMPLX b ) {
CMPLX Add ( CMPLX a, CMPLX b ) {
a.Re += b.Re;
a.Re += b.Re;
a.Im += b.Im;
a.Im += b.Im;
return a;
return a;
}
}
int main ( ) {
int main ( ) {
CMPLX x, y, z;
CMPLX x, y, z;
x.Read ( );
x.Read ( );
y.Read ( );
y.Read ( );
z = Add ( x, y );
z = Add ( x, y );
cout << ”\nResult=” << z.Abs ( ) << endl; cout << ”\nResult=” << z.Abs ( ) << endl;
}
}
-13-
-14-
Przykład #2..
#include <iostream>
Struktura programu Przykład #2
#include <cmath>
using namespace std;
struct CMPLX {
double Re, Im;
double Abs ( ) const { return sqrt ( Re * Re + Im * Im ); }
void Read ( ) { cin >> Re >> Im; }
};
CMPLX operator+ ( CMPLX a, CMPLX b ) {
a.Re += b.Re;
a.Im += b.Im;
return a;
}
int main ( ) {
CMPLX x, y, z;
x.Read ( );
y.Read ( );
z = x + y;
// równoważne: z = operator+ ( x, y );
cout << ”\nResult=” << z.Abs ( ) << endl;
}
-15-
-16-
• Nasz program przykładowy możemy
Struktura programu Przykład #3
zapisać w trzech plikach:
cmplx.h – plik nagłówkowy klasy (definicja klasy CMPLX i deklaracje funkcji wspomagających),
cmplx.cpp – plik implementacyjny klasy (definicje metod klasy CMPLX i definicje funkcji wspomagających), progmain.cpp – główny plik aplikacji (definicja funkcji main i ewentualnie definicje innych funkcji programu).
-17-
-18-
Plik nagłówkowy (definicja klasy CMPLX)
Plik implementacyjny klasy CMPLX
// cmplx.cpp
// cmplx.h
#include < iostream >
#include < cmath >
struct CMPLX
{
using namespace std;
double Re, Im;
#include ”cmplx.h”
double Abs ( ) const;
double CMPLX :: Abs ( ) const
void Read ( );
{
return sqrt ( Re * Re + Im * Im );
};
}
CMPLX operator+ ( CMPLX, CMPLX );
void CMPLX :: Read ( )
{
cin >> Re >> Im;
}
CMPLX operator+ ( CMPLX a, CMPLX b )
{
a.Re += b.Re;
a.Im += b.Im;
return a;
}
-19-
-20-
Główny plik aplikacji
Struktura programu Przykład #3
// myprog.cpp
#include < iostream >
using namespace std;
#include ”cmplx.h”
int main ( )
{
CMPLX x, y, z;
x.Read ( );
y.Read ( );
z = x + y;
cout << ”\nResult = ” << z.Abs ( ) << endl;
}
-21-
-22-
Koniec wykładu 1.
-23-