Advertisement
xTheEc0

4. Class inheritance

Mar 24th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <cstdlib>  // Standard General Utilities Library
  2. #include <cstdio>   // Input/Output operations (C)
  3. #include <iostream> // Input/Output stream objects
  4. #include <fstream>  // File input and output
  5. #include <ctime>    // Date and time information
  6. #include <cmath>    // Mathematical operations and transformations
  7. using namespace std;
  8.  
  9. #define pi 3.14
  10.  
  11. // Classes
  12. class line {
  13. protected:
  14.     double a;
  15. public:
  16.  
  17.     line() {a = 1;}
  18.     line(double var1) {a = var1;}
  19. };
  20.  
  21. class circle: public line {
  22. protected:
  23.     double r;
  24. public:
  25.     double area() {return pi * pow(r, 2);}
  26.  
  27.     circle():line() {r = a;}
  28.     circle(double var1) {r = var1;}
  29. };
  30.  
  31. class cylinder: public circle {
  32. private:
  33.     double h;
  34. public:
  35.     void assign(double var1, double var2) {r = var1; h = var2;}
  36.     double volume() {return area() * h;}
  37.     void print() {  cout << "Cylinder information:" << endl;
  38.                     cout << "r = " << r << ", h = " << h << endl;
  39.                     cout << "Volume: " << volume() << endl << endl;}
  40.  
  41.     cylinder() {h = 1;}
  42.     cylinder(double var1) {h = var1;}
  43.     cylinder(double var1, double var2):circle(var1) {h = var2;}
  44. };
  45.  
  46. class cone: public circle {
  47. private:
  48.     double h;
  49. public:
  50.     void assign(double var1, double var2) {r = var1; h = var2;}
  51.     double volume() {return 1.0 / 3.0 * area() * h;}
  52.     void print() {  cout << "Cone information:" << endl;
  53.                     cout << "r = " << r << ", h = " << h << endl;
  54.                     cout << "Volume: " << volume() << endl << endl;}
  55.  
  56.     cone() {h = 1;}
  57.     cone(double var1) {h = var1;}
  58.     cone(double var1, double var2):circle(var1) {h = var2;}
  59. };
  60.  
  61. class rectangle: public line {
  62. protected:
  63.     double b;
  64. public:
  65.     double area() {return a * b;}
  66.  
  67.     rectangle():line() {b = 1;}
  68.     rectangle(double var1):line(var1) {b = var1;}
  69.     rectangle(double var1, double var2):line(var1) {b = var2;}
  70. };
  71.  
  72. class parallelepiped: public rectangle{
  73. private:
  74.     double c;
  75. public:
  76.     double volume() {return area() * c;}
  77.     void print() {  cout << "Parallelepiped rectangle information:" << endl;
  78.                     cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
  79.                     cout << "Volume: " << volume() << endl << endl;}
  80.  
  81.     parallelepiped():rectangle() {c = 1;}
  82.     parallelepiped(double var1):rectangle(var1) {c = var1;}
  83.     parallelepiped(double var1, double var2):rectangle(var1) {c = var2;}
  84.     parallelepiped(double var1, double var2, double var3):rectangle(var1, var2) {c = var3;}
  85. };
  86.  
  87. // Main program
  88. int main(int argc, char *argv[]) {
  89.  
  90.     parallelepiped G(4, 2, 3);
  91.     G.print();
  92.  
  93.     cylinder R(2, 3);
  94.     R.print();
  95.  
  96.     cone K(2, 3);
  97.     K.print();
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement