Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. static const double PI = 3.14;
  6.  
  7.  
  8. /* BEGIN Zadanie 1 */
  9. class Shape{
  10. private:
  11.     string name;
  12. public:
  13.     Shape(string name) : name(name){
  14.         cout << "Tworze ksztalt" << endl;
  15.     }
  16.     virtual ~Shape(){
  17.         cout << "Usuwam ksztalt" << endl;
  18.     }
  19.     virtual void setName(string name){
  20.     }
  21.  
  22.     virtual string getName(){
  23.         return name;
  24.     }
  25.  
  26.     virtual double area(){
  27.         return 0;
  28.     }
  29. };
  30.  
  31. class Rectangle : public Shape{
  32. private:
  33.     double width;
  34.     double height;
  35. public:
  36.     Rectangle(string name, double width, double height) : Shape(name), width(width), height(height){
  37.         cout << "Tworze prostokat" << endl;
  38.     }
  39.     ~Rectangle(){
  40.         cout << "Usuwam prostokat" << endl;
  41.     }
  42.     double area(){
  43.         return width*height;
  44.     }
  45. };
  46.  
  47. class Square : protected Shape{
  48. private:
  49.     double width;
  50. public:
  51.     Square(string name, double width) : Shape(name), width(width){
  52.         cout << "Tworze kwadrat" << endl;
  53.     }
  54.     ~Square(){
  55.         cout << "Usuwam kwadrat" << endl;
  56.     }
  57.     double area(){
  58.         return width*width;
  59.     }
  60. };
  61.  
  62.  
  63. void zadanie1(){
  64.     Rectangle prost("prst1", 5.0, 6.2);
  65.     Square kwadr("kwadr1", 3.0);
  66.  
  67.     Shape &prostRef = prost; //rzutowanie przez referencje
  68.     Shape &kwadrRef = kwadr; //rzutowanie przez referencje
  69.  
  70.     cout << endl;
  71.     cout << "Rzutowanie przez referencje: " << endl;
  72.     cout << endl;
  73.     cout << "Nazwa prostokatu: " << prostRef.getName() << ", jego pole: " <<prostRef.area() << endl;
  74.     cout << "Nazwa kwadratu: " << kwadrRef.getName() << ", jego pole: " <<kwadrRef.area() << endl;
  75.     cout << endl;
  76.  
  77.  
  78.     Shape *prostPtr = &prost;
  79.     Shape *kwadrPtr = &kwadr;
  80.  
  81.     cout << endl;
  82.     cout << "Rzutowanie przez referencje: " << endl;
  83.     cout << endl;
  84.     cout << "Nazwa prostokatu: " << prostPtr -> getName() << ", jego pole: " <<prostPtr -> area() << endl;
  85.     cout << "Nazwa kwadratu: " << kwadrPtr -> getName() << ", jego pole: " <<kwadrPtr -> area() << endl;
  86.     cout << endl;
  87.  
  88.     delete prostPtr;
  89.     delete kwadrPtr;
  90.  
  91. }
  92.  
  93. /* END Zadanie 1 */
  94.  
  95. /* BEGIN Zadanie 2 */
  96.  
  97.  
  98. class Circle{
  99. private:
  100.     double radius;
  101. public:
  102.     Circle(double radius) : radius(radius){}
  103.     ~Circle(){}
  104.     virtual double area(){
  105.         return (PI*radius*radius);
  106.     }
  107.  
  108.     double getRadius(){
  109.         return radius;
  110.     }
  111. };
  112.  
  113. class Cylinder : public Circle{
  114. private:
  115.     double height;
  116. public:
  117.     Cylinder(double radius, double height) : Circle(radius), height(height){}
  118.  
  119.     double area(){
  120.         return ((2*PI*getRadius()*getRadius()) + (2*PI*getRadius()*height));
  121.     }
  122.  
  123.     double volume(){
  124.         return Circle::area() * height;
  125.     }
  126.  
  127. };
  128.  
  129.  
  130. void zadanie2(){
  131.     Cylinder walec(5, 10); //przykladowy obiekt walca z r=5, h=10
  132.     cout << "Pole powierzchni walca o r=5 i h=10: " << walec.area()<<". Objetosc tego walca wynosi: "<<walec.volume()<<endl;
  133. }
  134.  
  135.  
  136. /* END Zadanie 2 */
  137.  
  138. /* BEGIN Zadanie 3 */
  139.  
  140. class Funkcja{
  141. public:
  142.     Funkcja(){}
  143.     virtual double oblicz(float x){
  144.     }
  145. };
  146.  
  147. class FunkcjaLiniowa : public Funkcja{
  148. private:
  149.     double a;
  150.     double b;
  151. public:
  152.     FunkcjaLiniowa(double a, double b) : a(a), b(b){}
  153.     double oblicz(float x){
  154.         return a*x+b;
  155.     }
  156.  
  157. };
  158.  
  159. void zadanie3(){
  160.     FunkcjaLiniowa f1(5,6); //przykladowa funkcja a=5, b=6
  161.     cout << "Wartosc funkcji 5x+6 dla x=2: "<< f1.oblicz(2) << endl;
  162. }
  163.  
  164. /* END Zadanie 3 */
  165.  
  166. int main(){
  167.     int task;
  168.     bool stop = false;
  169.     do{
  170.         cout<<endl;
  171.         cout<<"#MENU#"<<endl;
  172.         cout << "#Ktore zadanie?(1, 2, 3) " << endl;
  173.         cout << "#Numer 4 by zakonczyc program " << endl;
  174.         cin>>task;
  175.  
  176.         switch(task){
  177.         case 1:
  178.             zadanie1();
  179.             break;
  180.         case 2:
  181.             zadanie2();
  182.             break;
  183.         case 3:
  184.             zadanie3();
  185.             break;
  186.         case 4:
  187.             stop = true;
  188.             break;
  189.         default: cout << "Zly numer zadania";
  190.  
  191.         }
  192.     } while (!stop);
  193.  
  194.  
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement