Advertisement
frentzy

bilet nr 21 , aproape complet(POO)

May 18th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #include <conio.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. using namespace std;
  7.  
  8. class Planeta {
  9. protected:
  10.     char *nume;
  11.     int raza;
  12.     int distantaS;
  13.     int durata;
  14.     int rotatie;
  15. public:
  16.     Planeta() {
  17.         //asta e doar un alt constructor de initializare, si e gol pentru ca
  18.         //atunci cand creez vectorul Planeta *planete in cealalta clasa
  19.         //nu te lasa sa creezi decat daca initializezi, de asta faci un
  20.         //constructor Gol pentru ca uneori vrei "2" constructori
  21.         //nu se stie cand vrei sa initializezi mai tarziu ceva(printr-o functie de initializare)
  22.     }
  23.     Planeta(char *nume, int raza, int distantaS, int durata, int rotatie) {
  24.         this->nume = new char[30];
  25.         this->nume = nume;
  26.         this->raza = raza;
  27.         this->distantaS = distantaS;
  28.         this->durata = durata;
  29.         this->rotatie = rotatie;
  30.     }
  31.     ~Planeta();
  32.     Planeta(Planeta& v);
  33.     void afisare();
  34.     void  setNume(char *p);
  35.     void setRaza(int a);
  36.     void setdistantaS(int a);
  37.     void setdurata(int a);
  38.     void setrotatie(int a);
  39.     char * getNume();
  40.     void citire();//asta am facut-o eu
  41.  
  42. };
  43.  
  44. char * Planeta::getNume() {
  45.     return this->nume;
  46. }
  47.  
  48. Planeta::~Planeta() {
  49.     cout << "destructor";
  50. }
  51.  
  52. Planeta::Planeta(Planeta& v) {
  53.     nume = new char[20];
  54.     this->nume = v.nume;
  55.     this->raza = v.raza;
  56.     this->distantaS = v.distantaS;
  57.     this->durata = v.durata;
  58.     this->rotatie = v.rotatie;
  59. }
  60.  
  61. void Planeta::afisare() {
  62.     cout << this->nume << ": \n";
  63.     cout << "raza = " << this->raza << endl;
  64.     cout << "distanta fata de soare = " << this->distantaS << endl;
  65.     cout << "durata =  " << this->durata << endl;
  66.     cout << "perioada de rotatie = " << this->rotatie << endl;
  67.     cout << " \n#####################\n";
  68.  
  69. }
  70.  
  71. void Planeta::setNume(char *p) {
  72.     this->nume = p;
  73. }
  74.  
  75. void Planeta::setRaza(int a) {
  76.     this->raza = a;
  77. }
  78.  
  79. void Planeta::setdistantaS(int a) {
  80.     this->distantaS = a;
  81. }
  82.  
  83. void Planeta::setdurata(int a) {
  84.     this->durata = a;
  85. }
  86. void Planeta::setrotatie(int a) {
  87.     this->rotatie = a;
  88. }
  89.  
  90. void Planeta::citire() {
  91.     cout << "\ndistantaS = "; cin >> this->distantaS;
  92.     cout << "durata = "; cin >> this->durata;
  93.     this->nume = new char[50];//aloc memorie pt nume planeta , ca e pointer
  94.     char numeTemp[50]; // creez un vector de char (string) temporar ca sa pot folosii functia gets_s
  95.     cout << "Nume Planeta = ";
  96.     gets_s(numeTemp);//functia asta e ca sa citesti stringuri de la tastatura, merge si cin>>nume dar... e o mica problema
  97.                      //functia cin nu citeste "spatiu", daca scrii " Frentzy are mere", nu retine decat Frentzy
  98.                     // iar functia get_s retine toata propozitia, doar ca get_s nu merge pe pointeri =))))
  99.                     // trebuie folosit o variabila normala si fac un "temp" in care retin numele planetei
  100.     gets_s(numeTemp);// de ce sunt 2 get_s? , pentru ca cel putin la mine ,compilatorul ignora pe primul ca si cum nu ar exista asa ca am pus 2
  101.     strcpy(this->nume, numeTemp);// pun in pointer(this->nume) ce se afla in temp-ul respectiv(numeTemp)
  102.     cout << "raza = "; cin >> this->raza;
  103.     cout << "rotatie = "; cin >> this->rotatie;
  104.    
  105. }
  106.  
  107. class SistemSolar  {
  108. protected:
  109.     char *name;
  110.     Planeta *planete;
  111.     int nrPlanete;
  112. public:
  113.     SistemSolar(char *name,int nrPlanete);
  114.     ~SistemSolar();
  115.     void afisare();
  116.     void citire();
  117.  
  118. };
  119.  
  120. SistemSolar::SistemSolar(char *name, int nrPlanete) {
  121.     this->nrPlanete = nrPlanete;
  122.     this->name = name;
  123.     /*sau mai poti face
  124.     this->name = new char[strlen(name)+1];
  125.     strcpy(this->name,name);
  126.     */
  127. }
  128.  
  129. SistemSolar::~SistemSolar() {
  130.     cout << "\n apel destructor";
  131. }  
  132.  
  133. void SistemSolar::citire() {
  134.     cout << "\nIntroducem date despre planetele astea: \n";
  135.     this->planete = new Planeta[this->nrPlanete];
  136.     for (int i = 0; i < this->nrPlanete; i++) {
  137.         this->planete[i].citire();
  138.     }
  139. }
  140.  
  141. void SistemSolar::afisare() {
  142.     cout << endl << "Nume Sistem Solar:" << this->name << endl;
  143.     for (int i = 0; i < this->nrPlanete; i++) {
  144.         this->planete[i].afisare();
  145.     }
  146.    
  147. }
  148.  
  149.  
  150.  
  151. void main() {
  152.     /*Planeta nr1("Pamant", 25, 123456, 24, 1456574);
  153.     nr1.afisare();
  154.     nr1.setNume("Marte");
  155.     nr1.setdistantaS(1131241341);
  156.     nr1.setdurata(12);
  157.     nr1.setRaza(321434);
  158.     nr1.setrotatie(4);
  159.     nr1.afisare();
  160.     */
  161.     Planeta Frentzy;
  162.     Frentzy.citire();
  163.     //cout << Frentzy.getNume();
  164.     cout << endl;
  165.     Frentzy.afisare();
  166.     cout << endl;
  167.     SistemSolar hahalera("Jeg", 2);
  168.     hahalera.citire();
  169.     hahalera.afisare();
  170.     _getch();
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement