madalinaradu

POO telefon

May 27th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include<iostream>
  2. #include<string.h>
  3. #include<conio.h>
  4. using namespace std;
  5. class Telefon{
  6. protected:
  7.     char* numar;
  8.     char* marca;
  9. public:
  10.     Telefon(char *numar, char *marca);
  11.     Telefon(const Telefon &t);
  12.     ~Telefon();
  13.     void setNumar(char *numar);
  14.     void apeleaza(char *numar);
  15.     void raspunde();
  16.     Telefon & operator=(Telefon &n);
  17.     void afisare();
  18. };
  19. Telefon::Telefon(char *numar, char *marca){
  20.         this->numar=new char[strlen(numar)+1];
  21.         strcpy(this->numar, numar);
  22.         this->marca=new char[strlen(marca)+1];
  23.         strcpy(this->marca, marca);
  24.         cout<<"Apel constructor cu param Telefon"<<endl;
  25.     }
  26. Telefon::Telefon(const Telefon &t){
  27.     numar=new char[strlen(t.numar)+1];
  28.     strcpy(this->numar, t.numar);
  29.     marca=new char[strlen(t.marca)+1];
  30.     strcpy(marca, t.marca);
  31.     cout<<"Apel constructor copiere Telefon"<<endl;
  32. }
  33. Telefon::~Telefon(){
  34.     delete[]numar;
  35.     delete[]marca;
  36.     cout<<"Apel destructor Telefon"<<endl;
  37. }
  38.  
  39. void Telefon::setNumar(char *numar){
  40. if(strlen(this->numar)<strlen(numar)) {
  41.         delete this->numar;
  42.         this->numar=new char[strlen(numar)+1];
  43.     }
  44.     strcpy(this->numar,numar);
  45. }
  46. void Telefon::apeleaza(char *numar){
  47.     cout<<"suna "<<numar<<endl;
  48. }
  49. void Telefon::raspunde(){
  50. cout<<"te suna cineva";
  51. }
  52.  
  53. Telefon& Telefon:: operator=(Telefon &n){
  54.     if(this!=&n){
  55.         delete[]numar;
  56.         delete[]marca;
  57.         numar=new char[strlen(n.numar)+1];
  58.         strcpy(numar, n.numar);
  59.         marca=new char[strlen(n.marca)+1];
  60.         strcpy(marca, n.marca);
  61.     }
  62. }
  63.  
  64. void Telefon::afisare(){
  65.     cout<<"numar "<<numar<<", marca "<<marca<< endl;
  66. }
  67. ///*******************
  68. class TelefonFix: public Telefon{
  69. protected:
  70.     bool whireless;
  71. public:
  72.     TelefonFix(char *n, char *m, bool whir);
  73.     void afisare();
  74. };
  75. TelefonFix::TelefonFix(char *n, char *m, bool whir): Telefon(n,m){
  76.     this->whireless=whir;
  77. }
  78.  
  79. void TelefonFix::afisare(){
  80.     Telefon::afisare();
  81.     cout<<"whireless "<<whireless<<endl;
  82. }
  83. ///****************
  84. class TelefonMobil:public Telefon{
  85. protected:
  86.     int autonomie;
  87.    // char *Operator; redefinim supraincarcarea =
  88.  
  89. public:
  90.     TelefonMobil(char *n, char *m, int autonomie );
  91.     void afisare();
  92. };
  93. TelefonMobil::TelefonMobil(char *n, char *m, int autonomie ):Telefon(n, m){
  94.     this->autonomie=autonomie;
  95. }
  96. void TelefonMobil::afisare(){
  97.     Telefon::afisare();
  98.     cout<<"Autonomie "<<autonomie<<endl;
  99. }
  100.  
  101. int main(){
  102.     Telefon t("0744501110", "Motorola");
  103.     t.afisare();
  104.     cout<<endl;
  105.     TelefonFix t1("0351411111", "rds", true);
  106.     t1.afisare();
  107.     cout<<endl;
  108.      TelefonMobil t2("0721444444", "Vodafone", 24);
  109.     t2.afisare();
  110.     cout<<endl;
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment