Programare Orientata pe Obiecte


Clase Abstracte

Sa se implementeze clasele Cerc si Dreptunghi derivate din FiguraGeometrica. Clasa FiguraGeometrica va fi definita ca si clasa abstractă având declarată metoda getPerimetru() ca funcție virtuală pură.

#include <math.h>
#include <conio.h>
#include <iostream>
#define M_PI 3.14159265
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 float getPerimetrul() = 0;
         virtual void print();

};

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

class Cerc: public FiguraGeometrica {
        Punct centru;
        float raza;
      public:       
        Cerc (Punct c, float r);  
        float getPerimetrul();   
        void print();
};
Cerc::Cerc(Punct c, float r):centru(c), raza(r){
}

float Cerc::getPerimetrul() {
      return 2*M_PI*raza;
}
void Cerc::print(){
     cout<<"Cerc ("<<centru<<","<<raza<<")"<<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); 
      float getPerimetrul();
      void print();      
};      

Dreptunghi::Dreptunghi(Punct c_s_s, Punct c_d_j): colt_s_s(c_s_s), colt_d_j(c_d_j){
}
float Dreptunghi::getPerimetrul() {
      return 2*(colt_d_j.getX()-colt_s_s.getX())+2*(colt_s_s.getY()-colt_d_j.getY());
}
void Dreptunghi::print(){
     cout<<"Dreptunghi ["<<colt_s_s<<","<<colt_d_j<<"]"<<endl;;
}

int main(){    
    FiguraGeometrica *f[]={new Cerc(Punct(0,0),1), new Dreptunghi(Punct(0,1),Punct(2,0))}; 
    for(int i=0;i<2;i++){
         f[i]->print();
         cout<<"Perimetrul = "<<f[i]->getPerimetrul()<<endl;   
    }
    getch();
}      

Realizati o clasa abstracta pentru rezolvarea problemelor ce utilizeaza metoda backtracking. Sa se utilizeze aceasta clasa pentru rezolvarea problemei damelor.

#include <math.h>
#include <conio.h>
#include <iostream>

using namespace std;

class Dame;
class Backtracking{
   protected:
      int x[100];
      int k;
   public:
   void solve();
   virtual void initLevel() = 0;
   virtual  int succesor() = 0;
   virtual  int  validate() const=0;
   virtual  int  testSolution() const=0;
   virtual  void printSolution() const=0;
};

void Backtracking::solve() {
      int is,iv=0;
      k=1;
      initLevel();
      while (k>0){
         do{
            is=succesor();
            if (is)
               iv=validate();
         } while (!((!is)||((is)&&(iv))));
         if(is)
            if(testSolution()) printSolution();
            else{
               k++;
               initLevel();
            }
            else k--;
      }
}


class Dame: public Backtracking{
   int table;
public:
   Dame(int n);
   void initLevel();
   int succesor();
   int validate() const;
   int testSolution() const;
   void printSolution() const;
};

Dame::Dame(int n){
 table=n;
 for (int i=1;i<=table;i++)
   x[i]=0;
}
void Dame::initLevel() {
      x[k]=0;
   }

int Dame::succesor() {
      if (x[k]<table){
         x[k]+=1;
         return 1;
      } else {
       return 0;
      }
   }

int Dame::validate() const{
      for (int i=1;i<k;i++)
         if ((x[k]==x[i])||(abs(x[k]-x[i])==abs(k-i)))
           return 0;
      return 1;
}
int Dame::testSolution() const{
      if (k==table) return 1;
      return 0;
   }

void Dame::printSolution() const{
      for (int i=1;i<=table;i++)
         cout<<x[i]<<" ";
      cout<<"\n";
}

int main (){
   Dame *d=new Dame(4);

   d->solve();
   getch();
   return 0;
}

Tema

  1. Eliminati cuvantul cheie virtual din declaratia functiei print() a clasei FiguraGeometrica. Rulati programul si explicati output-ul.

  2. Adaugati la clasa abstracta FiguraGeometrica definita anterior metoda getAria(). Actualizati corespunzator clasele Cerc si Dreptunghi.

  3. Sa se realizeze o clasa abstracta ce implementeaza notiunea de Lista.Sa se realizeze doua implementari ale acestei clase ListaStatica si ListaInlantuita.