home *** CD-ROM | disk | FTP | other *** search
/ Sams Teach Yourself C in 21 Days (6th Edition) / STYC216E.ISO / mac / Examples / Day22 / cube.cpp / cube.cpp
Encoding:
C/C++ Source or Header  |  2002-05-24  |  1.7 KB  |  93 lines  |  [TEXT/LMAN]

  1. // A C++ program with square and cube classes
  2. #include <iostream.h>
  3.  
  4. // Simple square class
  5. class square {
  6.   public:
  7.     square();
  8.     square(int);
  9.     int length;
  10.     long area();
  11.     int draw();
  12. };
  13.  
  14. //simple cube class inheriting from the square class
  15. class cube: public square {
  16.   public:
  17.     cube( int );
  18.     long area();
  19. };
  20.  
  21. //constructor for square
  22. square::square()
  23. {
  24.     length = 4;
  25. }
  26.  
  27. //overloaded constructor for square
  28. square::square( int init_length )
  29. {
  30.     length = init_length;
  31. }
  32.  
  33. //square class' area function
  34. long square::area( void )
  35. {
  36.     return((long) length * length);
  37. }
  38.  
  39. //square class' draw function
  40. int square::draw()
  41. {
  42.    int ctr1 = 0;
  43.    int ctr2 = 0;
  44.  
  45.    for (ctr1 = 0; ctr1 < length; ctr1++ )
  46.    {
  47.        cout << "\n";  /* new line */
  48.        for ( ctr2 = 0; ctr2 < length; ctr2++)
  49.        {
  50.            cout << "*";
  51.        }
  52.    }
  53.    cout << "\n";
  54.  
  55.    return 0;
  56. }
  57.  
  58. //cube class' constructor
  59. cube::cube( int init_length)
  60. {
  61.     length = init_length;
  62. }
  63.  
  64. //cube class' area function
  65. long cube::area()
  66. {
  67.     return((long) length * length * length);
  68. }
  69.  
  70. int main()
  71. {
  72.     square square1;
  73.     square1.length = 5;
  74.     square square2(3);
  75.     square square3;
  76.     cube cube1(4);
  77.  
  78.     cout << "\nDraw square 1 with area of " << square1.area() << "\n";
  79.     square1.draw();
  80.  
  81.     cout <<"\nDraw square 2 with area of " << square2.area() << "\n";
  82.     square2.draw();
  83.  
  84.     cout << "\nDraw square 3 with area of " << square3.area() << "\n";
  85.     square3.draw();
  86.  
  87.     cout << "\nDraw cube 1 with area of " << cube1.area() << "\n";
  88.     cube1.draw();  //Actually uses square's draw function
  89.  
  90.     return 0;
  91. }
  92.  
  93.