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