Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Box.h"
- int Box::nr = 0;
- int Box::nrkopii = 0;
- int main()
- {
- cout << "*** Instancje tworzone dynamicznie:\n";
- const int ile = 10;
- Box* aBox[ile] = { 0 };
- aBox[0] = new Box;
- aBox[1] = new Box();
- aBox[2] = new Box("box2", 2.5, 1, .8);
- aBox[3] = new Box("box3", 3.1);
- aBox[4] = new Box("box4");
- aBox[5] = new Box(*aBox[1]);
- cout << "\n*** Instancje tworzone statycznie:\n";
- Box b6;
- Box b7;
- b7 = Box();
- Box b8("box8", 1.25, 1, 6.4);
- Box b9("box9", 9.1);
- Box b10("box10");
- Box b11 = b10;
- Box b12(b10);
- Box b13;
- b13 = Box("box13");
- b12 = b6;
- b13 = Box();
- cout << endl;
- for (int i = 0; i < ile; i++)
- {
- if (aBox[i])
- {
- aBox[i]->printVolume();
- }
- }
- cout << endl;
- b6.printVolume(); b7.printVolume();
- b8.printVolume(); b9.printVolume();
- b10.printVolume(); b11.printVolume();
- b12.printVolume(); b13.printVolume();
- cout << "\n*** Czyszczenie instancji dynamicznych:\n";
- for (int i = 0; i < ile; i++) if (aBox[i]) delete aBox[i];
- cout << "\n*** Koniec main()\n";
- }
- /*
- 1) Ile razy uruchomiony został konstruktor klasy Box?
- 2) Ile razy uruchomiony został konstruktor kopiujący klasy Box?
- 3) Ile razy uruchomiona została funkcja printVolume() klasy Box?
- */
- ///////////////////////////////////////////////////////////////////////////////////////////////
- #pragma once
- #include <iostream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- class Box
- {
- private:
- string name;
- float length;
- float width;
- float height;
- public:
- Box();
- Box(string name);
- Box(string name, float length);
- Box(string name, float length, float width, float heigth);
- Box(Box & box);
- void printVolume();
- static int nr;
- static int nrkopii;
- };
- //////////////////////////////////////////////////////////////
- #include "Box.h"
- Box::Box()
- {
- this->name = "i";
- this->name.append(to_string(nr));
- this->name.append("-");
- this->name.append("def");
- this->length = 1;
- this->width = 1;
- this->height = 1;
- cout << "K: tworze " << this->name << endl;
- nr++;
- }
- Box::Box(string name)
- {
- this->name = "i";
- this->name.append(to_string(nr));
- this->name.append("-");
- this->name.append(name);
- this->length = 1;
- this->width = 1;
- this->height = 1;
- cout << "K: tworze " << this->name << endl;
- nr++;
- }
- Box::Box(string name, float length)
- {
- this->name = "i";
- this->name.append(to_string(nr));
- this->name.append("-");
- this->name.append(name);
- this->length = length;
- this->width = 1;
- this->height = 1;
- cout << "K: tworze " << this->name << endl;
- nr++;
- }
- Box::Box(string name, float length, float width, float height)
- {
- this->name = "i";
- this->name.append(to_string(nr));
- this->name.append("-");
- this->name.append(name);
- this->length = length;
- this->width = width;
- this->height = height;
- cout << "K: tworze " << this->name<< endl;
- nr++;
- }
- Box::Box(Box & box)
- {
- this->name = "i";
- this->name.append(to_string(nr));
- this->name.append("-");
- this->name.append("kopia");
- this->name.append(to_string(nrkopii));
- this->name.append("_");
- this->name.append(box.name);
- this->length = box.length;
- this->width = box.width;
- this->height = box.height;
- cout << "KK: tworze " << name << endl;
- nrkopii++;
- }
- void Box::printVolume()
- {
- cout <<"V(" << name << ")= " << length * width * height << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement