Advertisement
madalinaradu

Figura Geometrica

May 27th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4.  
  5. class Punct {
  6. protected:
  7.     float x;
  8.     float y;
  9. public:
  10.     Punct(float x=0, float y=0) {
  11.         this->x=x;
  12.         this->y=y;
  13.     }
  14.  
  15.     void afisare() {
  16.         //out<<"Punct ( "<<x<<","<< y<<" )"<<endl;
  17.     }
  18.     friend ostream& operator <<(ostream &out, const Punct &p);
  19.     friend float distanta(Punct& a, Punct& b);
  20. };
  21. float distanta(Punct& a, Punct& b) {
  22.     return sqrt((a.x-b.x)*(a.x-b.x)+ (a.y-b.y)*(a.y-b.y));
  23. }
  24. ostream& operator <<(ostream &out, const Punct &p) {
  25.     out<<"Punct ( "<<p.x<<","<< p.y<<" )";
  26.     return out;
  27.  
  28. }
  29.  
  30. class FiguraGeometrica {
  31. public:
  32.     virtual float getPerimetrul()=0;
  33.     virtual float getAria()=0;
  34.     virtual void print();
  35. };
  36. void FiguraGeometrica:: print() {
  37.     cout<<"Figura geometrica oarecare"<<endl;
  38. }
  39.  
  40. class Triunghi: public FiguraGeometrica {
  41.     Punct a,b,c;
  42.  
  43. public:
  44.     Triunghi(Punct a, Punct b,Punct c);
  45.     float getPerimetrul();
  46.     float getAria();
  47.     void print();
  48.  
  49. };
  50.  
  51. Triunghi::Triunghi(Punct _a, Punct _b,Punct _c): a(_a), b(_b), c(_c) {
  52. }
  53. float Triunghi:: getPerimetrul() {
  54.     return distanta(a,b)+distanta(b,c)+ distanta(a,c);
  55. }
  56. float Triunghi:: getAria() {
  57.     ///sqrt(p*(p-a)*(p-b)*(p-c));
  58.     float x=distanta(a,b);
  59.     float y=distanta(a,c);
  60.     float z=distanta(b,c);
  61.     float p=(x+y+z)/2;
  62.     return sqrt(p*(p-x)*(p-y)*(p-z));
  63. }
  64. void Triunghi:: print() {
  65.     cout<<"Triunghi ("<<a<<" , "<<b<<" , "<<c<<")"<<endl;
  66. }
  67.  
  68.  
  69.  
  70. class Cerc: public FiguraGeometrica {
  71.     Punct centru;
  72.     float raza;
  73. public:
  74.     Cerc(Punct c, float r=0);
  75.     float getPerimetrul();
  76.     float getAria();
  77.     void print();
  78.  
  79. };
  80. Cerc::Cerc(Punct c, float r): centru(c), raza(r) {
  81. }
  82.  
  83. float Cerc:: getPerimetrul(){
  84.     return 2*3.14*raza;
  85. }
  86.     float Cerc:: getAria(){
  87.         return 3.14*raza*raza;
  88.     }
  89.     void Cerc::print(){
  90.       cout<<"Cerc ("<<centru<<" , "<<raza<<")"<<endl;
  91.     }
  92.  
  93. int main() {
  94.     Punct a(1,1);
  95.     cout<<a<<endl;
  96.     Triunghi t(Punct(0,0), Punct(0,3), Punct(4,0));
  97.     t.print();
  98.     cout<<"Perimetru: ";
  99.     cout<<t.getPerimetrul()<<" "<<endl;
  100.     cout<<"Aria: ";
  101.     cout<<t.getAria()<<"";
  102.     cout<<endl;
  103.     Cerc c(a,1);
  104.     c.print();
  105.     cout<<"Perimetru: ";
  106.     cout<<c.getPerimetrul()<<" "<<endl;
  107.     cout<<"Aria: ";
  108.     cout<<c.getAria()<<"";
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement