madalinaradu

galaxie

Jun 21st, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1.  
  2. #include <string.h>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. #define NR_MAX_OB 100
  7. #define MAX 40
  8.  
  9. class ObiectCosmic {
  10. protected:
  11.     char nume[MAX];
  12.     double masa;
  13. public:
  14.     ObiectCosmic(const char *nume = "",  double masa = 0);
  15.     void setNume(const char *nume);
  16.     char* getNume();
  17.     void setMasa(double masa);
  18.     double getMasa();
  19.     virtual void afisare();
  20. };
  21.  
  22.  
  23. ObiectCosmic::ObiectCosmic(const char *nume, double masa) {
  24.     strncpy(this -> nume, nume, MAX);
  25.     this -> masa = masa;
  26. }
  27.  
  28. void ObiectCosmic::setNume(const char *nume) {
  29.     strncpy(this -> nume, nume, MAX);
  30. }
  31. char* ObiectCosmic::getNume() {
  32.     return nume;
  33. }
  34.  
  35. void ObiectCosmic::setMasa(double masa) {
  36.     this->masa = masa;
  37. }
  38. double ObiectCosmic::getMasa() {
  39.     return masa;
  40. }
  41.  
  42. void ObiectCosmic::afisare() {
  43.     cout<<"Nume: "<<nume<<endl;
  44.     cout<<"Masa: "<<masa<<endl;
  45. }
  46.  
  47.  
  48.  
  49. ///*********************************************************************
  50. class Planeta: public ObiectCosmic {
  51. protected:
  52.     int perRotatie;
  53. public:
  54.     Planeta(const char *nume = " ",  double masa = 0, int perRotatie=0);
  55.  
  56.     void setPerRotatie( int perRotatie){
  57.         this->perRotatie=perRotatie;
  58.     }
  59.  
  60.     int getPerRotatie(){
  61.         return perRotatie;
  62.     }
  63.     void afisare();
  64. };
  65.  
  66. Planeta::Planeta(const char *nume,  double masa, int perRotatie ):ObiectCosmic(nume, masa){
  67.         this->perRotatie=perRotatie;
  68. }
  69.  
  70.  
  71. void Planeta::afisare() {
  72.     cout<<"Planeta"<<endl;
  73.     cout<<"---------------------------------"<<endl;
  74.     ObiectCosmic::afisare();
  75.     cout<<"Perioada de rotatie: "<<perRotatie<<endl;
  76.     cout<<"---------------------------------"<<endl;
  77. }
  78.  
  79.  
  80.  
  81. ///*********************************************************************
  82. class Stea: public ObiectCosmic {
  83. protected:
  84.     int magnitudine;
  85. public:
  86.     Stea(const char *nume = "",  double masa = 0, int magnitudine = 0);
  87.     void setMagnitudine(int magnitudine){
  88.         this->magnitudine=magnitudine;
  89.     }
  90.     int setMagnitudine(){
  91.         return magnitudine;
  92.     }
  93.     void afisare();
  94. };
  95.  
  96. Stea::Stea(const char *nume,  double masa, int magnitudine):ObiectCosmic(nume, masa) {
  97.     this->magnitudine = magnitudine;
  98. }
  99.  
  100. void Stea::afisare() {
  101.     cout<<"Stea"<<endl;
  102.     cout<<"---------------------------------"<<endl;
  103.     ObiectCosmic::afisare();
  104.     cout<<"Magnitudine: "<<magnitudine<<endl;
  105.     cout<<"---------------------------------"<<endl;
  106. }
  107.  
  108.  
  109. class Galaxie {
  110.     char nume[MAX];
  111.      ObiectCosmic *G[NR_MAX_OB];
  112.     int nrObiecteCosmice;
  113. public:
  114.     Galaxie(const char *nume = "");
  115.     void adaugaObiecteCosmice(ObiectCosmic *p);
  116.     void afisare();
  117. };
  118.  
  119. Galaxie::Galaxie(const char *nume) {
  120.     strncpy(this -> nume, nume, MAX);
  121.     nrObiecteCosmice = 0 ;
  122. }
  123.  
  124. void Galaxie::adaugaObiecteCosmice(ObiectCosmic *p) {
  125.     if (nrObiecteCosmice < NR_MAX_OB) {
  126.         G[nrObiecteCosmice]=p;
  127.         nrObiecteCosmice++;
  128.     } else {
  129.         throw out_of_range("Depasire capacitate");
  130.     }
  131. }
  132.  
  133. void Galaxie::afisare() {
  134.     cout<<"Galaxie: "<<nume<<endl;
  135.     cout<<"================================="<<endl;
  136.     for(int i=0; i<nrObiecteCosmice; i++) {
  137.         G[i]->afisare();
  138.  
  139.     }
  140. }
  141. int main() {
  142.     Galaxie galaxie("Calea Lactee");
  143.     galaxie.adaugaObiecteCosmice(new Planeta("Marte", 100,687 ));
  144.     galaxie.adaugaObiecteCosmice(new Stea("Sirius", 10, 1.5));
  145.     galaxie.afisare();
  146. }
  147.  
  148. ///judet-localitati.cpp
  149. ///Se afișează judet-localitati.cpp.
Add Comment
Please, Sign In to add comment