Programare Orientata pe Obiecte


Clase Virtuale

Sa se implementeze o clasa ce exemplifica conceptul de mostenire virtuala.

#include <conio.h>
#include <iostream>
using namespace std;

class B {
  protected:
      int b;
  public:   
      B(int b = 0){
         this->b=b;   
         cout <<"Apel constructor clasa B("<<b<<")"<<endl;
      }       
      void print(){
         cout<<"In B b="<<b<<endl;
      }
};

class B1: virtual public B{
  protected:
      int b1;
  public:
       B1(int b1, int b=1):B(b){
         this->b1 = b1;     
         cout <<"Apel constructor clasa B1"<<endl;         
       }
      void print(){
         cout<<"In B1 b="<<b<<", b1="<<b1<<endl;
      }    
};


class B2: virtual public B{
  protected:
      int b2;
  public:
       B2(int b2, int b=2):B(b){
         this->b2 = b2;        
         cout <<"Apel constructor clasa B2"<<endl;
       }   
       void print(){
         cout<<"In B2 b="<<b<<", b2="<<b2<<endl;
      }     
};

class D: public B1, public B2 {
  public:
   
      D(int b, int b1, int b2): B1(b1), B2(b2), B(b){
        cout <<"Apel constructor clasa D"<<endl;
      }       
      void print(){
         cout<<"In D b="<<b<<", b1="<<b1<<", b2="<<b2<<endl;
      }    
};



int main (){
    D d(0,1,2);
    d.print();
    d.B1::print();
    d.B2::print();
}

In urma rularii programului se obtine urmatorul output:

Apel constructor clasa B(0)
Apel constructor clasa B1
Apel constructor clasa B2
Apel constructor clasa D
In D b=0, b1=1, b2=2
In B1 b=0, b1=1
In B2 b=0, b2=2

Functii virtuale

Sa se implementeze o clasa ce reprezinta notiunea de "plansa de desen". O plansa de desen este formata dintr-o multime de figuri geometrice precum: cercuri, deptunghiuri, patrate, etc.

#include <conio.h>
#include <iostream>
using namespace std;

class Punct {
      float x,y;
   public:
      Punct(float x = 0, float y = 0);          
      void setX(float x);
      float getX();
      void setY(float y);
      float getY();
      friend ostream & operator << (ostream &out, Punct &p);
};

Punct:: Punct(float x, float y){
      this->x = x; this->y = y; 
}

void Punct::setX(float x){
      this->x = x;
}

float Punct::getX(){
      return x;
}

void Punct::setY(float y){
      this->y = y;
}

float Punct::getY(){
      return y;
}

ostream & operator << (ostream &out, Punct &p){
    out<<"("<<p.x<<","<<p.y<<")";
    return out;
}
class FiguraGeometrica {
      public:
         virtual void print();
         virtual ~FiguraGeometrica();        
};


void FiguraGeometrica::print(){
     cout<<"Figura Geometrica Oarecare"<<endl;
}
FiguraGeometrica::~FiguraGeometrica(){
     cout<<"Apel destructor FiguraGeometrica"<<endl;
}

class Cerc: public FiguraGeometrica {
        Punct centru;
        float raza;
      public:       
        Cerc (Punct c, float r);  
        Cerc::~Cerc();
        void print();   
};
Cerc::Cerc(Punct c, float r):centru(c), raza(r){
}
void Cerc::print(){
     cout<<"Cerc ("<<centru<<","<<raza<<")"<<endl;
}
Cerc::~Cerc(){
     cout<<"Apel destructor Cerc"<<endl;
}

class Dreptunghi: public FiguraGeometrica {
      Punct colt_s_s; //colt stanga sus
      Punct colt_d_j; //colt dreapta jos
     public:
      Dreptunghi (Punct c_s_s, Punct c_d_j); 
      ~Dreptunghi();
      void print();      
};      

Dreptunghi::Dreptunghi(Punct c_s_s, Punct c_d_j): colt_s_s(c_s_s), colt_d_j(c_d_j){
}

void Dreptunghi::print(){
     cout<<"Dreptunghi ["<<colt_s_s<<","<<colt_d_j<<"]"<<endl;;
}
Dreptunghi::~Dreptunghi(){
     cout<<"Apel destructor Dreptunghi"<<endl;
}

class Plansa {
      FiguraGeometrica **pFiguri;
      int nrFiguri;
   public:
       Plansa(int n, FiguraGeometrica *f[]);
       Plansa(const Plansa &plansa);
       ~Plansa();
       void print();     
};
Plansa::Plansa(int n, FiguraGeometrica *f[]){
     nrFiguri=n;            
     pFiguri = new FiguraGeometrica*[nrFiguri];
     for(int i=0;i<nrFiguri;i++){
       pFiguri[i] = f[i];
     }           
}

Plansa::Plansa(const Plansa &plansa){
     nrFiguri=plansa.nrFiguri;            
     pFiguri = new FiguraGeometrica*[nrFiguri];
     for(int i=0;i<nrFiguri;i++){
       pFiguri[i] = plansa.pFiguri[i];
     }           
}
Plansa::~Plansa(){
    if (pFiguri != 0){
        delete pFiguri;
        pFiguri = 0;        
    }              
}

void Plansa::print(){
     cout<<"Plansa:"<<endl;
     cout<<"======================================="<<endl;
     for(int i=0;i<nrFiguri;i++){
       if (pFiguri[i]!=0){
          pFiguri[i]->print();
       }
     }           
    cout<<"======================================="<<endl;
}
int main(){
    Cerc c(Punct(0,0),1);
    c.print();
    Dreptunghi d(Punct(0,1),Punct(2,0));
    d.print();
    FiguraGeometrica *f[]={&c,&d}; 
    Plansa plansa(2,f);
    plansa.print();    
}      

Cerc ((0,0),1)
Dreptunghi [(0,1),(2,0)]
Plansa:
=======================================
Cerc ((0,0),1)
Dreptunghi [(0,1),(2,0)]
=======================================
Apel destructor Dreptunghi
Apel destructor FiguraGeometrica
Apel destructor Cerc
Apel destructor FiguraGeometrica       

Tema

  1. Explicati output-ul programelor prezentate anterior.

  2. Implementati urmatoarea ierahie de clase: Animal, Mamifer, AnimalZburator, Liliac.

  3. Imlementati o ierarhie de clase care reprezinta aticolele dintr-o biblioteca.