ctorDemo.cpp

URL: http://147.64.242.52/~bennett/class/cmsc2040/fall2026/notes/inherit/code/preClass/ctorDemo.cpp
 
#include <iostream>

using namespace std;

class ParT{
   public:
      ParT(string word): theWord{word} {
         cout << "\tIn the ParT constructor with " << word << endl;
      }

      string Word() const {
         return theWord;
      }

   private:
      string theWord;
};

ostream & operator << (ostream & s, const ParT & data) {
     s << data.Word();
     return s;
}

class DemoT{
   public:
      DemoT(int newA=0, float newB=1.0, string newC = "default") :
              a{newA}, b{newB}, c{newC} 
              ,x{a+ static_cast<int>(b)} 
              ,p{"Hello World "+ c}
      {
         //x = 5;
         cout << "\tIn the DemoT constructor" << endl;
      }

      DemoT(const DemoT & other):
         a{other.a}, b{other.b}, c{other.c}, x{other.x}, p{other.p}
      {
      }

      DemoT & operator = (const DemoT & other)  = delete;

/*
      DemoT & operator = (const DemoT & other) {
          a = other.a;
          b = other.b;
          c = other.c;
          p = other.p;
          // x = other.x;

          return *this;
      }
*/
      void Tell() {
        cout << " a  = " << a << endl ;
        cout << " b  = " << b << endl ;
        cout << " c  = " << c << endl ;
        cout << " x  = " << x << endl ;
        cout << " p  = " << p << endl ;

      }

           
   private:
      int a{10};
      float b;
      string c;
      const int x{-99};
      //ParT   p{"hi there"};
      ParT   p;
};



int main() {

    cout << "Before declaring a DemoT" << endl;
    DemoT  one;
    cout << "After declaring a DemoT" << endl;
    

    cout << " one : default" << endl;
    one.Tell();
    cout << endl << endl;;

    DemoT  two{1,2," bob"};

    cout << "two (1,2,bob)" << endl;
    two.Tell();
    cout << endl << endl;;

    DemoT three{two};

    cout << " three{two}" << endl;
    three.Tell();
    cout << endl << endl;;

/*
    DemoT four;
    four = two;

    cout << "four = two " << endl;
    four.Tell();
    cout << endl << endl;;
*/

    return 0;
}