Lecture 10 Examples


image from book

Open table as spreadsheet

Program

Demonstrates

friend.cpp

Shows an example of friend classes.

friend0.cpp

Shows the need for friend classes because it requires each member function to be a friend function to access the private members of another class.

friend1.cpp

Shows the use of mutual friends.

friend2.cpp

Same as friend.cpp except it is declared in the private access section.

isvirtual.cpp

Same as notvirtual.cpp except that the extensions are virtual extensions and it will compile.

lotsdat1.cpp

Shows along with lotsdat2.cpp how adding the keyword virtual increases the size of lotsdat2.cpp more than adding one word would.

lotsdat2.cpp

Same as lotsdat1.cpp but adding the keyword virtual increases the size of lotsdat2.cpp.

lotsdat3.cpp

Shows why virtual functions are needed so that pointers to the base class may be used to point to objects of the derived class but access functions in the derived class.

lotsdat4.cpp

Shows how adding the keyword virtual overcomes the problem of lotsdat3.cpp accessing derived member functions with base class pointers to derived class objects.

lotsdat5.cpp

Shows the used of abstract virtual functions rather than the way it was done in lotsdat4.cpp.

lotsdat7.cpp

Shows what happens when a base class and it derived class have functions with the same name while another class derived from the first derive class does not.

lotsdat8.cpp

It does not compile since it is attempting to define an object of the abstract class.

lotsdat10.cpp

This program does not compile because an abstract class has a derived that does not have a function with the same name as the abstract virtual function of the base.

menus.cpp

Shows the use of late binding functions.

menus2.cpp

This program contains the menus for all of modules of the accounting system. From these menus, the user can select which of the accounting modules he/she wants to use.

nofriend.cpp

Shows the need for friend functions because it will not compile when one class tries to access the data members of another class.

notvirtual.cpp

Demonstrates the need for virtual class inheritance. It will not compile.

object.cpp

Shows the use of functions with arguments which are references to a base class being used by objects of a derived class.

ptr_base1.cpp

Shows the use of pointers to a base class being used with objects of a derived class.

ptr_base2.cpp

Shows what would happen if a pointer to an object of a derived class was use to point to an object of the base class.

ptr_base3.cpp

Shows what would happen if a pointer of a base class that points to an object of a derived class is incremented.

prt_base4.cpp

Shows that a pointer to a derived class cannot contain the address of a base class object. It will not compile.

virtual.cpp

Shows how by using virtual classes the problems of virtual0.cpp are overcome.

virtual0.cpp

Shows the need for virtual classes.

image from book

 // program_id          friend0.cpp // author              don voils  //date written        04/17/2006 // // program description    This program compiled in previous //                         versions of C++ but it no longer //                         compiles in Visual Studio .NET //                         version of C++. It appears that //                         while a class may have friend //                         functions they can not be //                         methods for another class. // #include<iostream> using namespace std; class A {  private:   float value;  public:   A(){value=3.1415F;}   friend float showValue();   friend void setValue(float amount);   friend void addValue(float amount); }; class B {     private:           A a;     public:           float showValue(){return a.value;}           void setValue(float amount){a.value = amount;}           void addValue(float amount){a.value += amount;} }; void main() {     B b;     b.setValue(500);     b.addValue(100);     cout << "The current value is " << b.showValue(); } // program_id    friend1.cpp // author        don voils // date written 04/17/2006 // // program description     This program demonstrates the use //                          mutual friend classes. // #include<iostream> using namespace std; // The line below is no longer required. // In earlier versions of C++ this was needed. // // class B; class A {    private:       float value;    public:       A()       {        value=3.1415F;       }     friend class B; }; class C {    private:       float otherValue;    public:      C()      {        otherValue = 234.45F;      }      friend class B; }; class B {    private:       A a;       C c;    public:       float showValue()       {         return a.value;       }       float showOtherValue()       {         return c.otherValue;       }       void setValue(float amount)       {         a.value = amount;       }       void addValue(float amount)       {         a.value += amount;       }       void transferValue()       {         float temp;         temp = a.value;         a.value = c.otherValue;         c.otherValue = temp;       } }; void main() {    B b;    b.setValue(500);    b.addValue(100);    cout << "The current value is " << b.showValue()         << endl << endl         << "The current other value is " << b.showOtherValue()         << endl << endl;    cout << "The transfer is made." << endl << endl;    b.transferValue();    cout << "The current value is " << b.showValue()         << endl << endl         << "The current other value is " << b.showOtherValue()         << endl << endl; } // program_id    friend2.cpp // author        don voils // date written 04/17/2006 // // description   This program demonstrates the use //                friend classes. It is the same as //                friend.cpp except in this example //                the friend is declared in the //                private access section. // #include<iostream> using namespace std; //   The line below is no longer required. //   In earlier versions of C++ this was needed. // // class B; class A {    private:       float value;       friend class B;    public:       A()       {         value=3.1415F;       } }; class B {    private:       A a;    public:       float showValue()       {         return a.value;       }       void setValue(float amount)       {         a.value = amount;       }       void addValue(float amount)       {         a.value += amount;       } }; void main() {    B b;    b.setValue(500);    b.addValue(100);    cout << "The current value is " << b.showValue()         << endl << endl; } // program_id    friend.cpp // author       don voils // date written 04/17/2006 // // program description   This program demonstrates the use //                       friend classes. // #include<iostream> using namespace std; //   The line below is no longer required. // In earlier versions of C++ this was needed. // // class B; class A {    private:       float value;    public:       A()       {         value=3.1415F;       }    friend class B; }; class B {    private:       A a;    public:       float showValue()       {         return a.value;       }       void setValue(float amount)       {         a.value = amount;       }       void addValue(float amount)       {         a.value += amount;       } }; void main() {    B b;    b.setValue(500);    b.addValue(100);    cout << "The current value is " << b.showValue()         << endl << endl; } // program_id    isvirtual.cpp // written_by    don voils // date_written  3/16/2006 // // description   This program is the same as notvirtual.cpp except that //                the extensions of stuff2 and stuff3 from stuff1 are //               virtual extensions and it will therefore compile. // #include<iostream> using namespace std; class Base {      private:                int number1;      public:                 Base()                {                 number1 = 1;                }                int showNumber1() const                {                 return number1;                } }; class Derived1 : virtual public Base {      private:                int number2;      public:                Derived1()                {                   number2 = 2;                }                int showNumber2() const                {                   return number2;                } }; class Derived2: virtual public Base {      private:               int number3;             public:               Derived2()               {                  number3 = 3;               }               int showNumber3() const               {                   return number3;               } }; class Derived3:public Derived1, public Derived2 {            private:        int number4;            public:        Derived3()              {                number4 = 4;             }             int showNumber4() const             {               return number4;             } }; void main() {      Base object1;      Derived1 object2;      Derived2 object3;      Derived3 object4;      cout << endl << endl << "object1 has number1 = " << object1.showNumber1()           << endl << "and the size of object1 is = " << sizeof(object1);      cout << endl << endl << "object2 has number1 = " << object2.showNumber1()                << endl << "and number2 = " << object2.showNumber2()                << endl << "and the size of object2 is = " << sizeof(object2);      cout << endl << endl << "object3 has number1 = " << object3.showNumber1()                 << endl << "and number3 = " << object3.showNumber3()                 << endl << "and the size of object3 is = " << sizeof(object3);      cout << endl << endl << "object4 has number1 = " << object4.showNumber1()<< endl                 << endl << "and number2 = " << object4.showNumber2() << endl                 << "and number3 = " << object4.showNumber3() << endl                 << "and number4 = " << object4.showNumber4() << endl                 << "and the size of object4 is = " << sizeof(object4)                 << endl << endl; } // program_id    lotsdat1.cpp // author         don voils // date written 12/6/2006 // // description  This program is used in coordination //              with the program lotsdat12.cpp. They //              are both the same program except that //              the other program has added the keyword //              virtual to the function show() in the //              base class. #include<iostream> #include<iomanip> using namespace std; class Date {      private:                    int month, day, year;      public:                    void getDate()                    {                      char dash;                      cin >> month >> dash >> day >> dash >> year;                    }                    void show()                    {                      cout << month <<"/" << day << "/" << year;                    } }; class Invoice : public Date {       private:                         int number;                         float amount;       public:          void getInvoice()                        {                          cout << fixed;                          cout << setprecision(2);                          cout << "What is the invoice date? ";                          getDate();                          cout << endl << endl << "What is the invoice number?";                          cin >> number;                          cout << endl << endl << "What is the invoice amount?";                          cin >> amount;            }                        void show()                        {                          cout << endl << "The invoice date was: ";                          Date::show();                          cout << endl << "Invoice #: " << number << endl                               << "Invoice amount: $" << amount << endl;            } }; class PurchaseOrder : public Date {      private:                     int number;      public:                     void getPurchaseOrder()                     {                       cout << endl << "What is the Purchase Order date? ";                       getDate();                       cout << endl << endl << "What is the Purchase Order number? ";                       cin >> number;                     }                     void show()                     {                       cout << endl << "The PO date was: ";                       Date::show();                       cout << endl << "PO #: " << number << endl;                     } }; class ShippingSlip : public Date {     private:                  int number;     public:                  void getShippingSlip()                  {                   cout << endl << "What is the date of the shiping slip? ";                   getDate();                   cout << endl << "What is the shiping slip number? ";                   cin >> number;                  } }; void main() {   Invoice invoice1234;   PurchaseOrder purchaseOrder1234;   ShippingSlip shippingSlip1234;   invoice1234.getInvoice();   purchaseOrder1234.getPurchaseOrder();   shippingSlip1234.getShippingSlip();   invoice1234.show();   purchaseOrder1234.show();   cout << endl << "The shiping slip date was ";   shippingSlip1234.show();   cout << endl << endl; } // program_id          lotsdat2.cpp // author            don voils // date written       12/6/2006 // // description        This program is used in coordination //                    with the program lotsdat2.cpp. They //                    are both the same program except that //                    this program has added the keyword //                    virtual to the function show() in the //                    base class. #include<iostream> #include<iomanip> using namespace std; class Date {      private:           int month,                            day,                            year;      public:           void getDate()           {                           char dash;                           cin >> month >> dash >> day >> dash >> year;           }           virtual void show()           {                           cout << month <<"/" << day << "/" << year;           } }; class Invoice : public Date {      private:           int number;           float amount;      public:           void getInvoice()           {                            cout << "What is the invoice date? ";                            getDate();                            cout << endl << endl << "What is the invoice number? ";                            cin >> number;                            cout << endl << endl << "What is the invoice amount? ";                            cin >> amount;           }           void show()           {                            cout << endl << "The invoice date was: ";                            Date::show();                            cout << endl << "Invoice #: " << number << endl                                << "Invoice amount: $ " << fixed << setprecision(2) << amount << endl;           } }; class PurchaseOrder : public Date {       private:            int number;       public:            void getPurchaseOrder()            {               cout << endl << "What is the Purchase Order date? ";               getDate();               cout << endl << endl << "What is the Purchase Order number? ";               cin >> number;            }            void show()            {                           cout << endl << "The Purchase Order date was: ";                           Date::show();                           cout << endl << "Purchase Order #: " << number << endl;            } }; class ShippingSlip : public Date {       private:            int number;       public:            void getShippingSlip()            {                           cout << endl << "What is the date of the Shipping Slip? ";                           getDate();                           cout << endl << "What is the Shipping Slip number? ";                           cin >> number;            } }; void main() {      Invoice invoice1234;      PurchaseOrder purchaseOrder1234;      ShippingSlip shippingSlip1234;      invoice1234.getInvoice();      purchaseOrder1234.getPurchaseOrder();      shippingSlip1234.getShippingSlip();             for(int index=0; index < 250; ++index)                cout << endl;      invoice1234.show();      purchaseOrder1234.show();      cout << endl << "The shiping slip date was ";      shippingSlip1234.show();      cout << endl << endl; } //  program_id          lotsdat3.cpp //  author              don voils //  date written       12/6/2006 // //  description   This program is used in coordination //                with the program lotsdat1.cpp. They //                are both the same program except that //                this program has added an array of pointers to the //                base class to call the function show(). //                As you will notice from the output, //                the function show() of the base class //                is called even though the pointers were //                pointing to the objects from the derived //                classes. // #include<iostream> #include<iomanip> using namespace std; class Date {      private:           int  month,                day,                year;      public:           void getDate()           {                char dash;                cin >> month >> dash >> day >> dash >> year;           }           void show()           {                cout << month <<"/" << day << "/" << year;           } }; class Invoice : public Date {       private:            int   number;            float amount;       public:            void getInvoice()            {              cout << "What is the invoice date? ";              getDate();              cout << endl << "What is the invoice number? ";              cin >> number;              cout << endl << "What is the invoice amount? ";              cin >> amount;            }            void show()            {              cout << endl << "The invoice date was: ";              Date::show();              cout << endl << "Invoice #: " << number << endl                   << "Invoice amount: $ " << setprecision(2) << fixed                   << amount << endl;            } }; class PurchaseOrder : public Date {      private:           int number;      public:           void getPurchaseOrder()           {              cout << endl << "What is the Purchase Order date? ";              getDate();              cout << endl << "What is the Purchase Order number? ";              cin >> number;           }           void show()           {               cout << endl << "The Purchase Order date was: ";               Date::show();               cout << endl << "Purchase Order #: " << number << endl;           } }; class ShippingSlip : public Date {      private:           int number;      public:           void getShippingSlip()           {    cout << endl << "What is the date of the Shipping Slip? ";                getDate();                cout << endl << "What is the shiping Shipping Slip number? ";                cin >> number;           } }; void main() {      Invoice invoice1234;      PurchaseOrder purchaseOrder1234;      ShippingSlip shippingSlip1234;      Date *pointer[3];      char *pointerWord[]={ "The invoice date was ",                            "The purchase order date was ",                            "The shiping slip date was "};      pointer[0] = &invoice1234;      pointer[1] = &purchaseOrder1234;      pointer[2] = &shippingSlip1234;      invoice1234.getInvoice();      purchaseOrder1234.getPurchaseOrder();      shippingSlip1234.getShippingSlip();      for(int index = 0; index < 250;++index)         cout << endl;      invoice1234.show();      purchaseOrder1234.show();      cout << endl << "Shiping slip date is ";      shippingSlip1234.show();      cout << endl << endl           << "Notice that the output above is different than below:"           << endl << endl;      //      // Notice that even though the pointer array ptr[] is pointing      // to objects of the classes: invoice and purchase_order the      // output below deals with show() for the base class instead.      //      for(int index=0;index < 3;++index)      {           cout << endl <<pointerWord[index];           pointer[index] -> show();      }      cout << endl << endl << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } //  program_id         lotsdat4.cpp //  author             don voils //  date written      12/6/2006 // //  program description   This program is used in coordination //                        with the program lotsdat3.cpp. They //                        are both the same program except that //                        this program has added the keyword //                        virtual to the base class show(). //                        As you will notice from the output, //                        the function show() of the base class //                        is only called the objects from the derived //                        classes that has its own show() to use. // #include<iostream> #include<iomanip> using namespace std; class Date {      private:           int  month,                day,                year;      public:           void getDate()           {                char dash;                cin >> month >> dash >> day >> dash >> year;           }           virtual void show()           {              cout << month <<"/" << day << "/" << year;           } }; class Invoice : public Date {           private:                int   number;                float amount;           public:                void getInvoice()                {                     cout << "What is the invoice date? ";                     getDate();                     cout << endl << "What is the invoice number? ";                     cin >> number;                     cout << endl << "What is the invoice amount? ";                     cin >> amount;                }                void show()                {                     cout << endl << "The invoice date was: ";                     Date::show();                     cout << endl << "Invoice #: " << number << endl                          << "Invoice amount: $ " << setprecision(2) << fixed << amount << endl;                } }; class PurchaseOrder : public Date {      private:           int number;      public:           void getPurchaseOrder()           {              cout << endl << "What is the PurchaseOrder date? ";              getDate();              cout << endl << "What is the PurchaseOrder number? ";              cin >> number;           }           void show()           {    cout << endl << "The PurchaseOrder date was: ";                Date::show();                cout << endl << "PurchaseOrder #: " << number << endl;           } }; class ShippingSlip : public Date {      private:           int number;      public:           void getShippingSlip()           {    cout << endl << "What is the date of the Shipping Slip? ";                getDate();                cout << endl << "What is the Shipping Slip number? ";                cin >> number;           } }; void main() {      Invoice invoice1234;      PurchaseOrder purchaseOrder1234;      ShippingSlip shippingSlip1234;      Date *pointer[3];      char *pointerWord[]={ "The invoice date was ",                            "The purchase order date was ",                            "The shiping slip date was "};      pointer[0] = &invoice1234;      pointer[1] = &purchaseOrder1234;      pointer[2] = &shippingSlip1234;      invoice1234.getInvoice();      purchaseOrder1234.getPurchaseOrder();      shippingSlip1234.getShippingSlip();      for(int index = 0; index < 250; ++index)         cout << endl;      invoice1234.show();      purchaseOrder1234.show();      cout << endl << "Shiping slip date is ";      shippingSlip1234.show();      cout << endl << endl           << "Notice that the output above is the same as that below."           << endl << endl;      for(int index=0;index < 3;++index)      { //         Only need a modification for the object shippingSlip1234. //            if(index==2)                 cout << endl << "Shiping slip date is ";            pointer[index] -> show();      }      cout << endl << endl << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } //  program_id        lotsdat5.cpp //  author            don voils //  date written      12/6/2006 // //  program description   This program is used in coordination //                        with the program lotsdat4.cpp. They //                        are both the same program except that //                        this program not only added the keyword //                        virtual to the base class show() but //                        made the function an abstract virtual function. //                        Notice that the derived classes each have //                        a function show(). // #include<iostream> #include<iomanip> using namespace std; class Date {      protected:           int  month,                day,                year;      public:           void getDate()           {                char dash;                cin >> month >> dash >> day >> dash >> year;           }           virtual void show()=0; // //   Since the function above is an abstract virtual function, //   date is an abstract base class. As a result, the class date //   can not have objects. // }; class Invoice : public Date {      private:           int  number;           float amount;      public:           void getInvoice()           {                cout << "What is the invoice date? ";                getDate();                cout << endl << "What is the invoice number? ";                cin >> number;                cout << endl << setprecision(2) << fixed << "What is the invoice amount? ";                cin >> amount;           }           void show()           {                cout << endl << "The invoice date was: ";                cout << month <<"/" << day << "/" << year;                cout << endl << "Invoice #: " << number << endl                     << "Invoice amount: $" << amount << endl;           } }; class PurchaseOrder : public Date {      private:           int number;      public:           void getPurchaseOrder()           {              cout << endl << "What is the PO date? ";              getDate();              cout << endl << "What is the PO number? ";              cin >> number;           }           void show()           {                cout << endl << "The PurchaseOrde date was: ";                cout << month <<"/" << day << "/" << year;                cout << endl << "PurchaseOrde #: " << number << endl;           } }; class ShippingSlip : public Date {      private:           int number;      public:           void getShippingSlip()           {                cout << endl << "What is the date of the shiping slip? ";                getDate();                cout << endl << "What is the shiping slip number? ";                cin >> number;           }           void show()           {     cout << endl << "The Shiping date was: ";                cout << month <<"/" << day << "/" << year;                cout << endl << "Shiping #: " << number << endl;           } }; void main() {      Invoice invoice1234;      PurchaseOrder purchaseOrder1234;      ShippingSlip shippingSlip1234;      Date *thePointer[3];      thePointer[0] = &invoice1234;      thePointer[1] = &purchaseOrder1234;      thePointer[2] = &shippingSlip1234;      invoice1234.getInvoice();      purchaseOrder1234.getPurchaseOrder();      shippingSlip1234.getShippingSlip();      for(int index = 0; index < 250; ++index)         cout << endl;      invoice1234.show();      purchaseOrder1234.show();      shippingSlip1234.show();      // Notice that the output above is the same as that below.      //      cout << endl << endl << "Using prointers the information is: " << endl;      for(int index=0;index < 3;++index)      {           thePointer[index] -> show();      }      cout << endl << endl << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } // program_id    lotdats7.cpp // written by   don voils // date_written 4/2/2006 // description   This program demonstrates what would happen //               to the objects of a class C that is derived //               from a class B which in turn is derived from //               a class A. Both A and B have a function show() //               while C does not. In addition the function show() //               in the class A is virtual while the function show() //               in the class B is not. // #include<iostream> using namespace std; class A {    private:       short member1;    public:       A(short a)       {         member1 = a;       }       virtual short show()       {         return member1;       } }; class B: public A {    private:       short member2;    public:       B(short a, short b): A(a)       {         member2 = b;       }       short show()       {         return member2;       } }; class C : public B {    private:       short member3;    public:       C(short a, short b, short c): B(a,b)       {         member3 = c;       } }; void main() {   A *pointer0,     *pointer1,     *pointer2;   A object0(25);   cout << endl << "object0 has member1 = 25" << endl;   B object1(1,2);   cout << endl << "object1 has member1 = 1 and member2 = 2 " << endl;   C object2(3,4,5);   cout << endl << "object2 has member1 = 3, member2 = 4 and member3 = 5 " << endl << endl;   pointer0 = &object0;   pointer1 = &object1;   pointer2 = &object2;   cout << "The address of object0 in the pointer pointer0 is " << pointer0 << endl << endl;   cout << "The address of object1 in the pointer pointer1 is " << pointer1 << endl << endl;   cout << "The address of object2 in the pointer pointer2 is " << pointer2 << endl << endl;   cout << "The output of the function show() acting on pointer0 is "        << pointer0->show() << endl << endl;   cout << "The output of the function show() acting on pointer1 is "        << pointer1->show() << endl << endl;   cout << "The output of the function show() acting on pointer2 is "        << pointer2->show();   cout << endl << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id     lotsdat8.cpp // written by    don voils // date_written  1/2/2006 // description    This program demonstrates is the same //                a lotsdat7.cpp except the function //                show() in the class A is an abstract //                virtual function which makes A //                an abstract class. This program will //                not compile because it attempts to //                define an object of A as was done //                in lotsdat7.cpp except an abstract class //                can not have an object. // #include<iostream> using namespace std; class A {    private:       short member1;    public:       A(short a)       {         member1 = a;       }       virtual short show()=0; }; class B: public A {    private:       short member2;    public:       B(short a, short b): A(a)       {         member2 = b;       }       short show()       {         return member2;       } }; class C : public B {    private:       short member3;    public:       C(short a, short b, short c): B(a,b)       {         member3 = c;       } }; void main() {   A *pointer0,     *pointer1,     *pointer2;   A object0(25);   cout << endl << "object0 has member1 = 25" << endl;   B object1(1,2);   cout << endl << "object1 has member1 = 1 and member2 = 2 " << endl;   C object2(3,4,5);   cout << endl << "object2 has member1 = 3, member2 = 4 and member3 = 5 "        << endl << endl;   pointer0 = &object0;   pointer1 = &object1;   pointer2 = &object2;   cout << "The address of object0 in the pointer ptr0 is " << pointer0        << endl << endl;   cout << "The address of object1 in the pointer ptr1 is " <<pointer1        << endl << endl;   cout << "The address of object2 in the pointer ptr2 is " << pointer2        << endl << endl;   cout << "The output of the function show() acting on ptr0 is "        << pointer0->show() << endl << endl;   cout << "The output of the function show() acting on ptr1 is "        << pointer1->show() << endl << endl;   cout << "The output of the function show() acting on ptr2 is "        << pointer2->show();   cout << endl << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } //   program_id          lotsdat10.cpp //   author              don voils //   date written       12/6/2006 // //   program description    This program is used in coordination //                          with the program lotsdat4.cpp. They //                          are both the same program except that //                          this program has made show() an abstract //                          function while the class ShippingSlip //                          does not have such a function. As a result //                          this program does not compile // #include<iostream> using namespace std; class Date {       protected:            int month, day, year;       public:           void getDate()           {    char dash;                cin >> month >> dash >> day >> dash >> year;           }           virtual void show()=0; // //   Since the function above is an abstract virtual function, //   date is an abstract base class. As a result, the class date //   can not have objects. // }; class Invoice : public Date {       private:            int number;            float amount;       public:            void getInvoice()            {    cout << "What is the invoice date? ";                 getDate();                 cout << endl << "What is the invoice number? ";                 cin >> number;                 cout << endl << "What is the invoice amount? ";                 cin >> amount;            }            void show()            {    cout << endl << "The invoice date was: ";                 cout << month <<"/" << day << "/" << year;                 cout << endl << "Invoice #: " << number << endl                      << "Invoice amount: " << amount << endl;            } }; class PurchaseOrder : public Date {      private:           int   number;      public:           void getPurchaseOrder()           {              cout << endl << "What is the Purchase Order date? ";              getDate();              cout << endl << "What is the Purchase Order number? ";              cin >> number;           }           void show()           {    cout << endl << "The Purchase Order date was: ";                cout << month <<"/" << day << "/" << year;                cout << endl << "PurchaseOrder #: " << number << endl;           } }; class ShippingSlip : public Date {      private:           int number;      public:          void getShippingSlip()          {    cout << endl << "What is the date of the shipping slip? ";               getDate();               cout << endl << "What is the shipping slip number? ";               cin >> number;          } }; void main() {      Invoice invoice1234;      PurchaseOrder purchaseOrder1234;      ShippingSlip shippingSlip1234;      Date *thePointer[3];      thePointer[0] = &invoice1234;      thePointer[1] = &purchaseOrder1234;      thePointer[2] = &shippingSlip1234;      invoice1234.getInvoice();      purchaseOrder1234.getPurchaseOrder();      shippingSlip1234.getShippingSlip();      invoice1234.show();      purchaseOrder1234.show();      shippingSlip1234.show();      cout << endl;      // Notice that the output above is the same as that below.      //      for(int index=0;index < 3;++index)      {          thePointer[index] -> show();      }      cout << endl << endl << "Continue? (Y/N) ";      char resp;      cin >> resp;      cout << endl << endl; } // program-id           menus2.cpp // author               DON VOILS // date written        SEPTEMBER 21, 1988 // program description    THIS PROGRAM CONTAINS THE MENUS //                        FOR ALL MODULES OF THE ACCOUNTING //                        SYSTEM. FROM THESE MENUS THE USER //                        CAN SELECT WHICH OF THE ACCOUNTING //                        MODULES HE/SHE WANTS TO USE. // #include <iostream> using namespace std; void strtclnt() { } void expense() { } void pandl() { } void endytd() { } void sales() { } void main() { short menu_1 = 0,       menu_2; while(menu_1 == 0) {     for(int loopCounter=0;loopCounter<=24;++loopCounter)          cout << endl;     cout  << "          Main Menu" << endl << endl << endl          << "   1.  Start New Customer" << endl << endl          << "   2.  Enter Expense Ledger" << endl << endl          << "   3.  Enter Sales Ledger" << endl << endl          << "   4.  Enter P&L" << endl << endl          << "   5.  Zero YTD values" << endl << endl          << "   6.  Exit" << endl << endl          << "        Which? ";     cin >> menu_1;     for(int loopCounter=0;loopCounter<=24;++loopCounter)          cout << endl;     switch (menu_1)     {          case 1:          //      THIS MODULE PROCESSES THE INSTALLATION          //      OF NEW CLIENTS.          //          //          // strtclnt();          //            menu_1 = 0;            break;          case 2:          //      THIS MODULE HELP THE USER TO SELECT WHICH          //      OF THE EXPENSE MODULES TO USE.          //          //          //  expense();          //            menu_1 = 0;            break;     case 3:          //      THIS MODULE HELP THE USER TO SELECT WHICH          //      OF THE SALES MODULES TO USE.          //          //             menu_2 = 0;             while (menu_2==0)             {               for(int loopCounter=0;loopCounter<=24;++loopCounter)              cout << endl;             cout << "         Sales Menu" << endl << endl << endl                << "   1.  Enter Daily Sales" << endl << endl                << "   2.  Edit Daily Sales" << endl << endl                << "   3.  Print Daily Sales Report" << endl << endl                << "   4.  Print MTD Sales Report" << endl << endl                << "   5.  Zero MTD Sales Data" << endl << endl                << "   6.  Exit" << endl << endl                << "        Which? ";               cin >> menu_2;               for(int loopCounter=0;loopCounter<=24;++loopCounter)                  cout << endl;               switch (menu_2)               {                 case 1:                 //       THIS MODULE PROCESSES THE ENTRY                 //       OF NEW SALES.                 //                 //                 // entrsales();                 //                   menu_2 = 0;                   break;                 case 2:                 //       THIS MODULE HELPS THE USER TO                 //       EDIT SALES ALREADY ENTERED.                 //                 //                 // editsales();                 //                   menu_2 = 0;                   break;                 case 3:                 //       THIS MODULE HELP THE USER TO DISPLAY                 //       THE SALES FOR A PARTICULAR DAY.                 //                 //                 //  displaysales();                 //                   menu_2 = 0;                   break;                 case 4:                 //       THIS MODULE PERMITS THE USER TO PRINT                 //       THE MTD SALES DATA.                 //                 // printsales();                 //                   menu_2 = 0;                   break;                 case 5:                 //       THIS MODULE PERMITS THE ZEROING OF MTD                 //       SALES DATA TO START A NEW MONTH.                 //                 //  endmtdsales();                 //                   menu_2 = 0;                   break;                 case 6:                 //       THIS SECTION PERMITS THE USER TO EXIT          //                   menu_2 = 5;                   break;                 default:                 //       IN CASE THE USER SELECTED A NUMBER OTHER                 //       THAN 1–5 THIS MODULE WILL FORCE THE USER                 //       TO CHOOSE AN ACCEPTABLE NUMBER.                 //                    menu_2 = 0;                    break;              }           }     //       menu_1 = 0;       break;     case 4:          //      THIS MODULE PERMITS THE USER TO PROCESS          //      THE P & L STATEMENT BY MERGING THE MTD          //      FIGURES WITH THE YTD FIGURES. IF THE          //      USER CHOOSES, THE MTD MAY BE ADDED TO          //      THE YTD FIGURES FOR STORAGE FOR THE          //      NEXT MONTH.          //          //  pandl();          //         menu_1 = 0;         break;     case 5:          //      THIS MODULE PERMITS THE ZEROING OF YTD          //      FIGURES SO THAT A NEW YEAR CAN BE PROCESSED.          //          //  endytd();          //         menu_1 = 0;         break;     case 6:          //      THIS SECTION PERMITS THE USER TO EXIT          //         menu_1 = 5;         break;      default:           //     IN CASE THE USER SELECTED A NUMBER OTHER           //     THAN 1–5 THIS MODULE WILL FORCE THE USER           //     TO CHOOSE AN ACCEPTABLE NUMBER.           //          menu_1 = 0;          break;       }    } } // program-id            menus.cpp // author                DON VOILS // program description   THIS PROGRAM CONTAINS THE MENUS //                       FOR ALL MODULES OF THE ACCOUNTING //                       SYSTEM. FROM THESE MENUS THE USER //                       CAN SELECT WHICH OF THE ACCOUNTING //                       MODULES HE/SHE WANTS TO USE. // //                One of the new modifications was the //                creation of arrays of pointers to functions. //                The individual menus were then changed so //                that these arrays would call the individual //                functions required by the menus. This was //                done by using the ability of C++ to permit //                late binding of function. Notice that in //                each case the exact function to be used //                on the menu choice is not determined //                until the program is executed. This enables //                the elimination of the switch. Late binding //                causes the program to be larger but the //                elimination of the switches makes it smaller. // #include<iostream> using namespace std; //  FUNCTION PROTOTYPES // void expense_ldgr(); void sales_ldgr(); //  Empty functions // void strtclnt(){}; void expenseout(){} void expenseedit(){ void expenseprt(){} void eomexpenses(){} void quit_expenses(){} void salesout(){}; void salesprt(){}; void salesedit(){}; void eomsales(){}; void quit_sales(){}; void pandl(){}; void eoy(){}; void quit() {      for(int loopCounter=0;loopCounter<=24;++loopCounter)          cout << endl;      exit(1); } //   The following are arrays of pointers to functions which are //   used to make the individual menus work without the switch. // void (*menu1[])()={strtclnt, expense_ldgr, sales_ldgr, pandl, eoy, quit}; void (*menu2[])()={expenseout, expenseedit, expenseprt, eomexpenses, quit_expenses}; void (*menu3[])()={salesout, salesedit, salesprt, eomsales, quit_sales}; void main() {      char index;      do      {          for(int loop_counter=0;loop_counter<=24;++loop_counter)                cout << endl;          cout << "           Main Menu" << endl << endl << endl                              << "  1.  Start New Client" << endl << endl                              << "  2.  Expense Ledger Menu" << endl << endl                              << "  3.  Sales Ledger Menu" << endl << endl                              << "  4.  Print P&L" << endl << endl                              << "  5.  End of Year Processing" << endl << endl                              << "  6.  Exit" << endl << endl                              << "       Which? ";           cin.get(index).get(); //   This is an example of late binding. The compiler does not specify //   the location of the jump until the execution of the program. //           menu1[(index-'0')-1]();           for(int loopCounter=0;loopCounter<=24;++loopCounter)                 cout << endl;      }while (1); } /*   program-id               EXPENSE_LDGR()      author                  DON VOILS      date written            SEPTEMBER 21, 1988      program description     THIS FUNCTION CONTAINS THE MENU                                    FOR THE EXPENSES. */ void expense_ldgr() {      char index;      /*   THIS MODULE HELPS THE USER TO SELECT WHICH                OF THE EXPENSE MODULES TO USE.      */      do      {           for(int loopCounter=0;loopCounter<=24;++loopCounter)                     cout << endl;           cout << flush;           cout << "          Expense Ledger Menu" << endl << endl                              <<    " 1. Enter Expenses" << endl << endl                              <<    " 2. Change Expenses" << endl << endl                              <<    " 3. Print Expense Ledger" << endl << endl                              <<    " 4. End of Month Processing for Expenses" << endl << endl                              <<    " 5. Return to Main Menu" << endl << endl                              <<    "     Which? ";           cin.get(index).get();                          menu2[(index-'0')-1]();                          if (index=='5')                              break;    } while (1); } /*   program-id              SALES_LDGR()      author                DON VOILS      date written           SEPTEMBER 21, 1988      program description   THIS FUNCTION CONTAINS THE MENU                          FOR THE SALES. */ void sales_ldgr() {      char index;      /*   THIS MODULE HELP THE USER TO SELECT WHICH           OF THE SALES MODULES TO USE.      */      do      {           for(int loopCounter=0;loopCounter<=24;++loopCounter)                     cout << endl;           cout << flush;           cout << "          Sales Ledger Menu" << endl << endl             << "      1. Enter Sales" << endl << endl             << "      2. Change Sales" << endl << endl             << "      3. Print Sales" << endl << endl             << "      4. End of Month Processing for Sales" << endl << endl             << "      5. Return to Main Menu" << endl << endl             << "          Which? ";           cin.get(index).get();           menu3[(index-'0')-1]();           if (index=='5')                break;      } while (1); } // program_id          nofriend.cpp // author              don voils // date written       04/17/2006 // // program description   This program demonstrates the //                        need to use friend classes. Notice //                        that even though the object a is //                        a data member of the class B, //                        it can not be used in the class B //                        to access its data member value //                        because of the way the class A is defined //                        with value defined in the private //                        access section. // #include<iostream> using namespace std; class A {  private:    float value;  public:    A()    {      value=3.1415F;    } }; class B {  private:     A a;  public:    float showValue()    {       return a.value;    } }; void main() {  B b;  cout << b.showValue(); } //  program_id      notvirtual.cpp //  written_by     don voils //  date_written   3/16/2006 // //  description       This program demonstrates the need for virtual class inheritance. //                    It will not compile. #include<iostream> using namespace std; class Base {      private:           int number1;      public:           Base()           {                          number1 = 1;                       }           int showNumber1() const           {                         return number1;                       } }; class Derived1 :  public Base {      private:           int number2;      public:           Derived1()           {                        number2 = 2;                      }           int showNumber2() const           {                         return number2;                      } }; class Derived2:  public Base {       private:            int number3;              public:            Derived2()            {                          number3 = 3;                       }            int showNumber3() const            {                          return number3;                       } }; class MutuallyDerived:public Derived1, public Derived2 { private:      int number4; public:      MutuallyDerived()      {              number4 = 4;            }      int showNumber4() const      {               return number4;            } }; void main() {      Base object1;      Derived1 object2;      Derived2 object3;      MutuallyDerived object4;      cout << endl << endl << "object1 has number1 = " << object1.showNumber1()                 << endl << "and the size of object1 is = " << sizeof(object1);      cout << endl << endl << "object2 has number1 = " << object2.showNumber1()                 << endl << "and number2 = " << object2.showNumber2()                 << endl << "and the size of object2 is = " << sizeof(object2);      cout << endl << endl << "object3 has number1 = " << object3.showNumber1()                 << endl << "and number3 = " << object3.showNumber3()                 << endl << "and the size of object3 is = " << sizeof(object3);      cout << endl << endl << "object4 has number1 = " << object4.showNumber1()<< endl                 << endl << "and number2 = " << object4.showNumber2() << endl                 << "and number3 = " <<object4.showNumber3() << endl                 << "and number4 = " << object4.showNumber4() << endl;            cout << endl << endl << "and the size of object4 is = " << sizeof(stuffit4)                 << endl << endl; } // program_id    object.cpp // written by   don voils // date_written 6/2/2006 // description  This program demonstrates that an object //              of a derived class may be used as an argument where a //              reference to a base class object is called for //              In addition a base class pointer to the derived class //              object may also be used as ab argynebt where a pointer //              to the base class is called for. // #include<iostream> using namespace std; class A {  private:   short member1;  public:    A(short a)    {      member1 = a;    }    short doIt(A& a)    {      return member1*a.member1;    }    short gotIt(A* thePointer)    {      return member1*(thePointer -> member1);    }    short showA()    {      return member1;    } }; class B: public A {  public:   B(short a): A(a)   { } }; void main() {   B object1(10);   B object2(30);   A *anotherPointer;   anotherPointer = &object2;   cout << endl << "The value of member1 in object1 is " << object1.showA();   cout << endl << "The value of member1 in object2 is " << object2.showA();   cout << endl << "The value of object1.doIt(object2) is " << object1.doIt(object2) << endl;   cout << endl << "The value of object1.gotIt(anotherPointer) is " << object1.gotIt(anotherPointer) << endl;   cout << endl << endl << "Continue? (Y/N)";   char resp;   cin >> resp;   cout << endl << endl; } // program_id     prt_base1.cpp // written by     don voils // date written   6/3/2006 // Descriptioin   The program shows how pointers to a base //                class can be used to access the base class //                part of an object of the derived class. // #include<iostream> using namespace std; class A {  private:   short member1;  public:   A(short a)   {     member1 = a;   }   short showA()   {     return member1;   } }; class B: public A {  private:   short member2;  public:   B(short a, short b): A(a)   {     member2 = b;   }   short showB()   {     return member2;   } }; void main() {   A *thePointer;   B object(1,2);   thePointer = &object;   cout << "The address of object is " << thePointer << endl;   cout << "The value of member1 is " << thePointer -> showA() << endl; // //   The following line will not compile. Why? // //  cout << "The value of member2 is " << thePointer -> showB() << endl;   cout << endl << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id    prt_base2.cpp // written by    don voils // date written  6/3/2006 // description   This program demonstrates that while //               in general a pointer to a base class //               that points to an object of a derived //               class can not be used to access members //               of the derived class, by type casting, //               this restriction can be overcome. // #include<iostream> using namespace std; class A {  private:   short member1;  public:   A(short a)   {     member1 = a;   }   short showA()   {     return member1;   } }; class B: public A {  private:   short member2;  public:   B(short a, short b): A(a)   {     member2 = b;   }   short showB()   {     return member2;   } }; void main() {   A *thePointer;   B object(1,2);   thePointer = &object;   cout << "The address of object is " << thePointer << endl;   cout << "The value of member1 is " << thePointer -> showA() << endl;   // Notice how typecasting was needed to access this B class method   //   cout << "The value of member2 is " << ((B*)thePointer) -> showB() << endl;   // If you remove the comments from the code below,   // the program will not compile because ptr is a pointer   // to the base class A and the method is a B class method   // even though it contains the address of a B class object.   //   //cout << "The value of member2 is " << thePointer -> showB() << endl;   cout << endl << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id    prt_base3.cpp // written by    don voils // date_written  4/2/2006 // // description   This program demonstrates that while //               in general a pointer to a base class //               can point to an object of a derived //               class, the pointer should not be //               incremented because the resulting //               address advances the size of the //               base class rather than the size //               of the derived class even though //               the pointer contains the address //               of a derived class object. // #include<iostream> using namespace std; class A {  private:   short member1;  public:   A(short a)   {     member1 = a;   }   short showA()   {     return member1;   } }; class B: public A {  private:   short member2;  public:   B(short a, short b): A(a)   {     member2 = b;   }   short showB()   {     return member2;   } }; void main() {   A *thePointer;   B object(1,2);   thePointer = &object;   cout << "The address of object is " << thePointer << endl << endl;   cout << "The incrementing of the address of object is " << thePointer +1 << endl << endl;   cout << "The size of an object of A is " << sizeof(A) << endl << endl ;   cout << "The size of an object of B is " << sizeof(B) << endl << endl;   cout << endl << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id    prt_base4.cpp // written by    don voils // date written  6/3/2006 // Descriptioin  The program shows that a pointer to a //               derived class cannot contain the address //               of a base class object. It will not compile. // #include<iostream> using namespace std; class A {  private:   short member1;  public:   A(short a)   {     member1 = a;   }   short showA()   {     return member1;   } }; class B: public A {  private:   short member2;  public:   B(short a, short b): A(a)   {     member2 = b;   }   short show_B()   {     return member2;   } }; void main() {   B *thePointer;   A object(1);   // The program will not compile because in the following'   // statement the address of an object of the base class   // is being assigned to a pointer to the derived class.   //   thePointer = &object;   cout << "The value of member1 is " << thePointer -> showA() << endl;   cout << endl << endl << "Continue? (Y/N) ";   char resp;   cin >> resp;   cout << endl << endl; } // program_id        virtual0.cpp // author           don voils // date written    04/17/2006 // // program description   This program demonstrates a problem with //                        multiple inheritance. Notice that it will //                        not compile because the compiler does not //                        know where the class Golden inherits access //                        to the methods of the class Account. Notice //                        that there is a problem with the constructor //                        of the class Golden as well. The constructors //                        of both the classes Stock and Mutual have //                        constructors that are dependent on different //                        implimentations of the constructor of //                        Account. Therefore a constructor of Golden //                        can not be dependent on both the constructors //                        of its base classes Stock and Mutual as //                        one would expect. // #include<iostream> using namespace std; class Account {       private:               float balance;       public:               Account(float startBalance=0.00)               {                 balance = startBalance;               }               float showBalance()               {                 return balance;               }               void deposit(float money)               {                 balance += money;               }               void interest(float amount)               {                 balance *= amount;               } }; class Stock : public Account {       public:                      Stock() : Account(100.00)                      {}                      void interest(float percentage=1.06)                      {                        Account::interest(percentage);                      } }; class Mutual : public Account {       public:                    Mutual() : Account(10000.00)                    {}                    void premium(float amount)                    {                      deposit(amount);                    } }; class Golden : public Stock, public Mutual {    public:         Golden(){} }; void main() {    Account bill;    Stock sally;    Mutual jerrie;    Golden goldie;    bill.deposit(100);    sally.interest();    jerrie.premium(100);    goldie.deposit(100);    goldie.interest();    goldie.premium(100);    cout << "Bill's balance is " << bill.showBalance() << endl;    cout << "Sally's balance is " << sally.showBalance() << endl;    cout << "Jerrie's balance is " << jerrie.showBalance() << endl;    cout << "Goldie's balance is " << goldie.showBalance() << endl; } // program_id        virtual.cpp // author           don voils // date written    04/17/2006 // // program description   This program demonstrates the use //                        of declaring classes as virtual //                        classes. // #include<iostream> #include<iomanip> using namespace std; class Account {    private:             float balance;    public:             Account(float start_balance=0.00)             {               balance = start_balance;             }             float showBalance()             {               return balance;             }             void deposit(float money)             {               balance += money;             }             void interest(float amount)             {               balance *= amount;             } }; class Stock : virtual public Account {    public:               Stock() : Account(100.00)               {}               void interest(float percentage=1.06)               {                 Account::interest(percentage);               } }; class Mutual : virtual public Account {    public:              Mutual() : Account(10000.00)              {}              void premium(float amount)              { deposit(amount);              } }; class Golden : public Stock, public Mutual {    public:                Golden()                {} }; void main() {   cout << fixed << setprecision(2);   Account bill;   Stock sally;   Mutual jerrie;   Golden goldie;   bill.deposit(100);   sally.interest();   jerrie.premium(100);   goldie.deposit(100);   goldie.interest();   goldie.premium(100);   cout << "Bill's balance is $" << bill.showBalance() << endl;   cout << "Sally's balance is $" << sally.showBalance() << endl;   cout << "Jerrie's balance is $" << jerrie.showBalance() << endl;   cout << "Goldie's balance is $" << goldie.showBalance() << endl;   cout << endl << "The size of the account object bill is " << sizeof(bill) << endl;   cout << "The size of the stock object sally is " << sizeof(sally) << endl;   cout << "The size of the mutual object jerrie is " << sizeof(jerrie) << endl;   cout << "The size of the golden object goldie is " << sizeof(goldie) << endl << endl; } 




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net