Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- // Klasa o nazwie ClassA (jest to nowy typ dostępny dla funkcji).
- class ClassA
- {
- public:
- // Konstruktor domyślny obiektu ClassA.
- ClassA() {
- mProtected = 0;
- mPrivate = 1;
- }
- // Konstruktor z argumentami.
- ClassA(int pro, int prv) {
- mProtected = pro;
- mPrivate = prv;
- }
- // Konstruktor kopiujący - kopiowanie obiektu.
- ClassA(const ClassA &a) {
- mProtected = a.mProtected + 1;
- mPrivate = a.mPrivate + 2;
- }
- // Destruktor - niszczenie obiektu.
- ~ClassA(void) {
- mProtected = 0;
- mPrivate = 0;
- }
- // Metoda klasy (funkcja mająca dostęp do składowych publicznych i prywatnych).
- void print(void) const {
- cout << "mProtected = " << mProtected << endl;
- cout << "mPrivate = " << mPrivate << endl;
- }
- protected:
- // Składowa chroniona.
- int mProtected;
- private:
- // Składowa prywatna.
- int mPrivate;
- };
- // Klasa z dynamicznie alokowaną pamięcią danych.
- class Dynamic {
- public:
- Dynamic(const char *str = "x") {
- mStr = new char[strlen(str) + 1];
- strcpy(mStr, str);
- }
- Dynamic(const Dynamic& d) {
- mStr = new char[strlen(d.mStr) + 1];
- strcpy(mStr, d.mStr);
- }
- ~Dynamic() {
- delete [] mStr;
- }
- void print(void) {
- cout << "mStr = " << mStr << endl;
- }
- private:
- char *mStr;
- };
- int main()
- {
- ClassA a;
- ClassA b(10, 11);
- ClassA c(a);
- a.print();
- b.print();
- c.print();
- Dynamic d;
- Dynamic e("nicość");
- Dynamic *f = new Dynamic("nic");
- Dynamic *g = new Dynamic(*f);
- d.print();
- e.print();
- g->print();
- delete f;
- g->print();
- return 0;
- }
Add Comment
Please, Sign In to add comment