kilolilo

sh

Mar 6th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5. class human{
  6. protected:
  7.     string name,surname;
  8.     int age;
  9. public:
  10.     human(string x,string y){
  11.         name=x;
  12.         surname=y;
  13.         age=0;
  14.     }
  15.     human(string x,string y,int age_1){
  16.         name=x;
  17.         surname=y;
  18.         age=age_1;
  19.     }
  20.     void say_hello(){
  21.         if(age>0){
  22.             cout<<"hello my name is "<<name<<"  "<<surname<<endl<<age<<" years old"<<endl;
  23.         }
  24.         else{
  25.           cout<<"hello my name is "<<name<<"  "<<surname<<endl;
  26.         }
  27.  
  28.     }
  29. };
  30.  
  31. class worker : public human{
  32. protected:
  33.     float salary;
  34.     string jobname;
  35. public:
  36.     worker(string x, string y, float z,string job) : human(x, y), salary(z),jobname(job){}
  37.     worker(string x, string y,int age_1, float z,string job) : human(x, y, age_1), salary(z),jobname(job){}
  38.     void say_hello(){
  39.         human::say_hello();
  40.         cout << "\t  salary - "<<salary<<"  job name is "<<jobname << endl;
  41.     }
  42. };
  43. class animal{
  44. public:
  45.     virtual void print()=0;
  46. };
  47. class cat:public animal{
  48. public:
  49.     void print(){
  50.         cout<<"cat"<<endl;
  51.     }
  52. };
  53. class dog:public animal{
  54. public:
  55.     void print(){
  56.         cout<<"dog"<<endl;
  57.     }
  58. };
  59.  
  60. class shape{
  61. public:
  62.     virtual float S() = 0;
  63. };
  64.  
  65. class square:public shape{
  66. public:
  67.     float a,s;
  68.     square(float x){
  69.         a=x;
  70.     }
  71.    float S(){
  72.        s=a*a;
  73.        return s;
  74.    }
  75. };
  76. class rectangle:public shape{
  77. public:
  78.     float a,b,s;
  79.     rectangle(float a1,float b1):a(a1),b(b1){
  80.     }
  81.     float S(){
  82.         s=a*b;
  83.         return s;
  84.     }
  85. };
  86. class circle:public shape{
  87. public:
  88.     float p=3.1415;
  89.     float r;
  90.     circle(float r1):r(r1){
  91.     }
  92.     float S(){
  93.         float s=p*(r*r);
  94.         return s;
  95.     }
  96. };
  97. class triangle:public shape{
  98. public:
  99.     float a,b,c;
  100.     triangle(float a1,float b1,float c1):a(a1),b(b1),c(c1){}
  101.     float S(){
  102.         float p=(a+b+c)/2;
  103.         float s=sqrt(p*(p-a)*(p-b)*(p-c));
  104.         return s;
  105.     }
  106. };
  107.  
  108. int main()
  109. {
  110.     shape *a=new square(5);
  111.     cout << a->S() << endl;
  112.     shape *b=new rectangle(5,10);
  113.     cout<< b->S()<<endl;
  114.     shape *c=new circle(12);
  115.     cout<< c->S()<<endl;
  116.     shape *t=new triangle(3,4,5);
  117.     cout<<t->S()<<endl;
  118. }
Add Comment
Please, Sign In to add comment