Valentino Vranić - Objektovo-orientovane programovanie 2013/14 Prednáška 10: Programovací jazyk C++ 30. apríl 2014 Kód zo slajdov Pôvod tried v jazyku C++ typedef struct { char[20] meno; char[20] priezvisko; int rocnik; } Student; . . . Student x; strcpy(x.meno,"Milan"); strcpy(x.priezvisko,"Milov"); x.rocnik = 1; . . . Student y = ("Dana", "Petrova", 2); Členské premenné a funkcie class Student { char[20] meno; char[20] priezvisko; int rocnik; public: void zapis(int rocnik); . . . }; void Student::zapis(int rocnik) { this->rocnik = rocnik; } Inštanciácia Student s; s.zapis(3); Student* p = new Student(); p->zapis(3); Konštruktory class Student { char[20] meno; char[20] priezvisko; int rocnik; public: void zapis(int rocnik); Student(int rocnik); . . . }; . . . Student::Student(int rocnik) { this->rocnik = rocnik; } Student* p = new Student(3); Student s(2); Deštruktory Student* p = new Student(3); . . . delete p; class Student { char[20] meno; char[20] priezvisko; int rocnik; char* poznamka; public: void zapis(int rocnik); Student(int rocnik); ~Student(); . . . }; Student::Student(int rocnik) { this->rocnik = rocnik; poznamka = new char[200]; } Student::~Student() { delete poznamka; } Modifikátory prístupu class Student { private: char[20] meno; char[20] priezvisko; int rocnik; protected: char* poznamka; public: void zapis(int rocnik); Student(int rocnik); ~Student(); . . . }; Dedenie public class GUtvar { . . . }; class Kruh : public GUtvar { private: int r; public: . . . }; Virtuálne funkcie class A { public: virtual void m(); }; void A::m() { cout << "toto je A" << endl; // štandardný výstup } class B : public A { public: virtual void m(); }; void B::m() { cout << "toto je B" << endl; } . . . A* x = new B(); x->m(); // "toto je B" Čisto virtuálne funkcie class A { public: virtual void m() = 0; }; Dedenie private a protected class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A { // x is private // y is private // z is not accessible from D }; Viacnásobné dedenie class A { public: virtual int aaa(); }; class B { public: virtual void bbb(int i); }; class M : public A, public B { . . . public: virtual int aaa(); virtual void bbb(int i); . . . }; Použitie viacnásobného dedenia class ObservedClock : public Clock, public Subject { public: virtual void Tick() { Clock::Tick(); Notify(); } }; Virtuálne dedenie class X : virtual public B { . . . }; class Y : virtual public B { . . . }; class M : public A, public B { . . . }; B* x = new M(); x->m(); Šablóny template T max(T a, T b) { return a>b ? a : b; } . . . max(10, 15); Metaprogramovanie pomocou šablón template struct Factorial { enum { RET = Factorial::RET * n }; }; template<> struct Factorial<0> { enum { RET = 1 }; }; void main() { cout << Factorial<7>::RET << endl; // 5040 } Referencie a parametre void duplicate (int& a, int& b, int& c) { a *= 2; b *= 2; c *= 2; } int main () { int x = 1, y = 3, z = 7; duplicate (x, y, z); cout << "x = " << x << ", y = " << y << ", z = " << z; return 0; } // Výstup: x = 2, y = 6, z = 14 Referencie a návratová hodnota int& preinc(int& x) { ++x; return x; } ... preinc(y) = 5; // 5 bude priradené y Preťaženie operátorov -- príklad template T nsd(T i1, T i2) { T i; while(i2 > 0) { i = i1 % i2; i1 = i2; i2 = i; } return i1; } template class Zlomok { protected: T citatel, menovatel; public: Zlomok& Zjednodus() { T NSD = nsd(citatel, menovatel); citatel /= NSD; menovatel /= NSD; return *this; } Zlomok (T _citatel=0, T _menovatel=1) { citatel = _citatel; menovatel = _menovatel; } friend Zlomok operator+(Zlomok &z1, Zlomok &z2); friend Zlomok operator*(Zlomok &z1, Zlomok &z2); friend ostream& operator<<(ostream& s, Zlomok z); }; template ostream& operator<<(ostream& s, Zlomok z) { return (s << z.citatel << '/' << z.menovatel ); } template Zlomok operator+(Zlomok &z1, Zlomok &z2) { return Zlomok(z1.citatel * z2.menovatel + z2.citatel * z1.menovatel, z1.menovatel *z2.menovatel).Zjednodus(); } template Zlomok operator*(Zlomok &z1, Zlomok &z2) { return Zlomok(z1.citatel * z2.citatel, z1.menovatel * z2.menovatel).Zjednodus(); } void main() { Zlomok a(1,2), b(2,1), c; c=a*b; cout << c; c=a+b; cout << c; }