Advertisement
totobac

Untitled

Mar 9th, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. enum Rank { A,B,C,D};
  5. class Planet {
  6. private:
  7.     char* name;
  8.     char* system;
  9.     char* republic;
  10.  
  11. public:
  12.  
  13.     Planet();
  14.     Planet(const char*, const char*, const char*);
  15.     Planet(Planet& otherPlanet);
  16.  
  17.     void print();
  18.     char* getName();
  19.     char* getSystem();
  20.     char* getRepublic();
  21.     void setName(const char*);
  22.     void setSystem(const char*);
  23.     void setRepublic(const char*);
  24.     //Planet operator= (const Planet& otherPlanet);
  25.  
  26.     ~Planet();
  27. };
  28.  
  29. //Planet::Planet operator= (const Planet otherPlanet) {
  30. //
  31. //}
  32.  
  33.  
  34. class Jedi {
  35. private:
  36.     char* name;
  37.     Rank rank;
  38.     double midichlorian;
  39.     Planet planet;
  40.     char* spicies;
  41.     char* militaryRank;
  42. public:
  43.     Jedi();
  44.     Jedi(const char*, Rank, double, Planet, const char*, const char*);
  45.     Jedi(Jedi& other);
  46.     ~Jedi();
  47.  
  48.     void print();
  49.     char* getName();
  50.     Rank getRank();
  51.     double getMidichlorian();
  52.     Planet getPlanet();
  53.     char* getSpicies();
  54.     char* getMilitaryRank();
  55.     void setName(const char*);
  56.     void setRank(Rank);
  57.     void setMidichlorian(double);
  58.     void setPlanet(Planet);
  59.     void setSpicies(const char*);
  60.     void setMilitaryRank(const char*);
  61.  
  62. };
  63. class Stormtrooper {
  64.     private:
  65.     char* id;
  66.     Rank rank;
  67.     char* type;
  68.     Planet planet;
  69.     public:
  70.  
  71.         Stormtrooper();
  72.         Stormtrooper(const char*, Rank, const char*, Planet);
  73.         Stormtrooper(Stormtrooper& otherStormtrooper);
  74.         ~Stormtrooper();
  75.  
  76.         void print();
  77.         char* getId();
  78.         Rank getRank();
  79.         char* getType();
  80.         Planet getPlanet();
  81.         void setId(const char*);
  82.         void setRank(Rank);
  83.         void setType(const char*);
  84.         void setPlanet(Planet);
  85.  
  86.  
  87. };
  88.  
  89.  
  90. int main()
  91. {
  92.     Planet fst("Earth", "Dominac", "Tangiers");
  93.  
  94.     //Jedi firstJed("Ivan", B, 10.31, fst, "Athletic ", "frontLiner");
  95.     Stormtrooper firstTroop("184293", C, "fighter", fst);
  96.     //firstJed.print();
  97. }
  98.  
  99. // DEFAULT PLANET ?
  100. Jedi::Jedi() {
  101.     name = spicies = militaryRank = nullptr;
  102.     rank = A;
  103.     midichlorian = 0;
  104.     planet.setName("");
  105.     planet.setRepublic("");
  106.     planet.setSystem("");                
  107. }
  108. Jedi::Jedi(const char* _name, Rank _rank, double _midichlorian, Planet _planet, const char* _spicies, const char* _militaryRank) {
  109.  
  110.     name = new char[strlen(_name) + 1];
  111.     strcpy_s(name, strlen(_name) + 1, _name);
  112.  
  113.     rank = _rank;
  114.     midichlorian = _midichlorian;
  115.     planet = _planet;
  116.  
  117.     spicies = new char[strlen(_spicies) + 1];
  118.     strcpy_s(spicies, strlen(_spicies) + 1, _spicies);
  119.  
  120.     militaryRank = new char[strlen(_militaryRank) + 1];
  121.     strcpy_s(militaryRank, strlen(_militaryRank) + 1, _militaryRank);
  122. }
  123. Jedi::Jedi(Jedi& other) {
  124.     this->name = new char[strlen(other.name + 1)];
  125.     strcpy_s(name, strlen(other.name + 1), other.name);
  126.  
  127.     rank = other.rank;
  128.     midichlorian = other.midichlorian;
  129.     planet = other.getPlanet();
  130.  
  131.     this->spicies = new char[strlen(other.spicies + 1)];
  132.     strcpy_s(spicies, strlen(other.spicies + 1), other.spicies);
  133.  
  134.     this->militaryRank = new char[strlen(other.militaryRank + 1)];
  135.     strcpy_s(militaryRank, strlen(other.militaryRank + 1), other.militaryRank);
  136.  
  137. }
  138. // planet = other.getplanet problem
  139. Jedi::~Jedi() {
  140.     delete[] name;
  141.     delete[] spicies;
  142.     delete[] militaryRank;
  143.     rank = A;
  144.     midichlorian = 0;
  145. }
  146.  
  147.  
  148.  
  149. void Jedi::print() {
  150.     std::cout << "Name: " << name << std::endl;
  151.     std::cout << "Rank: " << rank << std::endl;
  152.     std::cout << "Midichlorian: " << midichlorian << std::endl;
  153.     std::cout << "Planet: " << planet.getName() << std::endl;
  154.     std::cout << "Spicies: " << spicies << std::endl;
  155.     std::cout << "militaryRank: " << militaryRank << std::endl;
  156. }
  157. char* Jedi::getName() {
  158.     return name;
  159. }
  160. Rank Jedi::getRank(){
  161.     return rank;
  162. }
  163. double Jedi::getMidichlorian() {
  164.     return midichlorian;
  165. }
  166. Planet Jedi::getPlanet() {              
  167.     return planet;
  168. }
  169. // strange
  170. char* Jedi::getSpicies() {
  171.     return spicies;
  172. }
  173. char* Jedi::getMilitaryRank() {
  174.     return militaryRank;
  175. }
  176.  
  177. void Jedi::setName(const char* _name) {
  178.     if (name != nullptr)
  179.         delete[] name;
  180.  
  181.     name = new char[strlen(_name) + 1];
  182.     strcpy_s(name, strlen(_name) + 1, _name);
  183. }
  184. void Jedi::setRank(Rank _rank) {
  185.     rank = _rank;
  186. }
  187. void Jedi::setMidichlorian(double _midichlorian) {
  188.     midichlorian = _midichlorian;
  189. }
  190. void Jedi::setPlanet(Planet _planet) {          // problem
  191.     planet = _planet;
  192. }
  193.  
  194. void Jedi::setSpicies(const char* _spicies) {
  195.     if (spicies != nullptr)
  196.         delete[] spicies;
  197.  
  198.     spicies = new char[strlen(_spicies) + 1];
  199.     strcpy_s(spicies, strlen(_spicies) + 1, _spicies);
  200. }
  201. void Jedi::setMilitaryRank(const char* _militaryRank) {
  202.     if (militaryRank != nullptr)
  203.         delete[] militaryRank;
  204.  
  205.     militaryRank = new char[strlen(_militaryRank) + 1];
  206.     strcpy_s(militaryRank, strlen(_militaryRank) + 1, _militaryRank);
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213. Stormtrooper::Stormtrooper() {
  214.     id = type = nullptr;
  215.     rank = A;
  216.     planet.setName("");
  217.     planet.setRepublic("");
  218.     planet.setSystem("");
  219. }
  220. Stormtrooper::Stormtrooper(const char* _id, Rank _rank, const  char* _type, Planet _planet) {
  221.     id = new char[strlen(_id) + 1];
  222.     strcpy_s(id, strlen(_id) + 1, _id);
  223.     rank = _rank;
  224.     type = new char[strlen(_type) + 1];
  225.     strcpy_s(type, strlen(_type) + 1, _type);
  226.     planet = _planet;
  227. }
  228. Stormtrooper::Stormtrooper(Stormtrooper& other) {
  229.     this->id = new char[strlen(other.id + 1)];
  230.     strcpy_s(id, strlen(other.id + 1), other.id);
  231.     rank = other.rank;
  232.     this->type = new char[strlen(other.type + 1)];
  233.     strcpy_s(type, strlen(other.type + 1), other.type);
  234.     planet = other.planet;
  235.  
  236.  
  237. }
  238. Stormtrooper ::~Stormtrooper() {
  239.     delete[] id;
  240.     delete[]type;
  241.     rank = A;
  242.     planet.setName("");
  243.     planet.setRepublic("");
  244.     planet.setSystem("");
  245. }
  246.  
  247.  
  248. void Stormtrooper::print()
  249. {
  250.     std::cout << "Id: " << id << std::endl;
  251.     std::cout << "Rank: " << rank << std::endl;
  252.     std::cout << "Type: " << type << std::endl;
  253.     std::cout << "Planet: " << planet.getName() << std::endl;
  254. }
  255. char* Stormtrooper::getId()
  256. {
  257.     return this->id;
  258. }
  259. Rank Stormtrooper::getRank()
  260. {
  261.     return this->rank;
  262. }
  263. char* Stormtrooper::getType() {
  264.     return this->type;
  265. }
  266. Planet Stormtrooper::getPlanet()
  267. {
  268.     return  planet;
  269. }          ////////// problem
  270.  
  271. void Stormtrooper::setId(const char* _id) {
  272.     if (id != nullptr)
  273.         delete[] id;
  274.  
  275.     id = new char[strlen(_id) + 1];
  276.     strcpy_s(id, strlen(_id) + 1, _id);
  277. }
  278. void Stormtrooper::setRank(Rank _rank) {
  279.     rank = _rank;
  280. }
  281. void Stormtrooper::setType(const char* _type) {
  282.     if (type != nullptr)
  283.         delete[] type;
  284.  
  285.     type = new char[strlen(_type) + 1];
  286.     strcpy_s(type, strlen(_type) + 1, _type);
  287. }
  288. void Stormtrooper::setPlanet(Planet _planet) {
  289.     planet = _planet;
  290. }
  291.  
  292.  
  293.  
  294.  
  295. Planet::Planet() {
  296.     name = system = republic = nullptr;
  297. }
  298. // default constructor
  299. Planet::Planet(const char* _name, const char* _system, const char* _republic ) {
  300.     this->name = new char[strlen(_name) + 1];
  301.     strcpy_s(name, strlen(_name) + 1, _name);
  302.    
  303.     this->system = new char[strlen(_system) + 1];
  304.     strcpy_s(system, strlen(_system) + 1, _system);
  305.  
  306.     this->republic = new char[strlen(_republic) + 1];
  307.     strcpy_s(republic, strlen(_republic) + 1, _republic);
  308. } //
  309. //parameter constructor
  310. Planet::Planet(Planet& otherPlanet) {
  311.     this->name = new char[strlen(otherPlanet.name) + 1];
  312.     strcpy_s(name, strlen(otherPlanet.name) + 1, otherPlanet.name);
  313.  
  314.     this->system = new char[strlen(otherPlanet.system) + 1];
  315.     strcpy_s(system, strlen(otherPlanet.system) + 1, otherPlanet.system);
  316.  
  317.     this->republic = new char[strlen(otherPlanet.republic) + 1];
  318.     strcpy_s(republic, strlen(otherPlanet.republic) + 1, otherPlanet.republic);
  319. }
  320. //copy constructor
  321. Planet::~Planet() {
  322.     delete[] name;
  323.     delete[] system;
  324.     delete[] republic;
  325. }
  326. //destructor
  327.  
  328.  
  329. void Planet::print() {
  330.     std::cout << "Planet's name: " << this->name << std::endl;
  331.     std::cout << "Planet's System: " << this->system << std::endl;
  332.     std::cout << "Planet's Republic: " << this->republic << std::endl;
  333. }
  334.  
  335. void Planet::setName(const char* _name) {
  336.     if (name != nullptr)
  337.         delete[] name;
  338.  
  339.     int length = strlen(_name) + 1;
  340.     this->name = new char[length];
  341.     strcpy_s(name, length, _name);
  342. }
  343. void Planet::setSystem(const char* _system) {
  344.     if (system != nullptr)
  345.         delete[] system;
  346.        
  347.     int length = strlen(_system) + 1;
  348.     this->system = new char[length];
  349.     strcpy_s(system, length, _system);
  350. }
  351. void Planet::setRepublic(const char* _republic) {
  352.     if (republic != nullptr)
  353.         delete[] republic;
  354.  
  355.     int length = strlen(_republic) + 1;
  356.     this->republic = new char[length];
  357.     strcpy_s(republic, length, _republic);
  358. }
  359.  
  360. char* Planet::getName() {
  361.     return this->name;
  362. }
  363. char* Planet::getSystem() {
  364.     return this->system;
  365. }
  366. char* Planet::getRepublic() {
  367.     return this->republic;
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement