Intermediate Business Programming with C++

Open table as spreadsheet

PROGRAM

DEMONSTRATES THE USE OF

bankdate.cpp

Shows a serial extension of nested classes

bubba.cpp

Shows how the operator ++() in a derived class can be base on the operator + in the base class..

cmlxasgn.cpp

Shows a serial public extension and the effect on operators in the base class.

consdest.cpp

Shows the effect of constructors and destructors in the base class on derived extensions.

constructor1.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor2.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor3.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor4.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor5.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor6.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor7.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor8.cpp

Shows the relationship between constructors in the base and constructors in the derived class

constructor9.cpp

Shows the relationship between constructors in the base and constructors in the derived class

date.h

Contains the definition of the class Date.

date.cpp

Contains the method definitions of the class Date that is defined in the file date.h.

dateprvt.cpp

Shows a base class with private members and the derived class has a private extension.

dateprot.cpp

Shows a base class with protected members and the derived class has a private extension.

datepubl.cpp

Showa a base class with protected and public members and the derived class has a public extension.

datereln.cpp

Serial extension of three classes with operators and using a header.

devi.cpp

Shows what would happen if a base member was raised to an access level in the derived class higher than its access level in the base.

don.cpp

Shows how the operator ++( ) in a derived class can be base on the operator + in the base class.

extdname.cpp

Serial extension with functions in the derived class with the same name as in the base class.

higherac.cpp

Shows how to call a base member function so that the derived function which is dependent on the base member function does not appear to be recursive.

invclass.cpp

Shows a class which is nested in another class.

multext2.cpp

Shows a multiple extension and the effect of having constructors in the base class.

multextn.cpp

Another example of a multiple extension.

nametest.cpp

A serial extension and the effect of functions with the same name in the derived class as in the base class.

newdate.h

Contains the definition of the class: newDate

newdate.cpp

Contains the definitions of the methods for the class newDate.

newdate.h

A header required in the program datereln.cpp and it contains the base class of the extension.

PrivateInheritance.cpp,

Illustrates a simple derived class NewClass derived from the base class BaseClass. The extension is a private derivation.

PublicInheritance.cpp

Illustrates a simple derived class: NewClass derived from the base class: BaseClass. The extension is a public derivation.

ProtectedInheritance.cpp.

Illustrates a simple derived class NewClass derived from BaseClass. The extension is a protected derivation.

usingDates.cpp

A program which uses: Date.h and Date.cpp.

testNewDates.cpp

A program which is a modification of usingDates.cpp and it uses the files: newdate.h and newdate.cpp

// program_id bankdate.cpp // author don voils // date written 9/23/2006 // // description This program simulates a bank // account using the class // bank_account. The class // bank_account has a nested class, // the class date. // #include<iostream> using namespace std; class Date { private: int month, day, year; public: Date() { char dash; cout << "What is the date? "; cin >> month >> dash >> day >> dash >> year; cout << endl << endl; } void displayDate() { if(month>=10) cout << month; else cout << "0" << month; cout << "/"; if(day >=10) cout << day; else cout << "0" << day; cout << "/"; cout << year; } }; class BankAccount { private: Date statementDate; float amount; public: BankAccount(float startingAmount=0) { amount = startingAmount; } void depositSlip(float deposit) { amount += deposit; } void withdrawalSlip(float checkAmount) { amount -= checkAmount; } float showBalance(void) { return amount; } void displaySDate(void) { statementDate.displayDate(); } }; void main() { BankAccount checkingAccount; cout << "I started my checking account at the bank" << endl << endl; cout << "I deposited $2,000.00 in my checking account" << endl << endl; checkingAccount.depositSlip(2000.00); cout << "I wrote checks for $700.00 against my account." << endl <<endl; checkingAccount.withdrawalSlip(700.00); cout << "My checking account balance is $" << checkingAccount.showBalance() << endl << endl; cout << "I started my savings account at the bank with " << "$5,000" << endl << endl; BankAccount savingsAccount(5000); cout << "I desposited $3,000.00 in my savings account" << endl << endl; savingsAccount.depositSlip(3000.00); cout << "I withdrew $1,700.00 from my savings account" << endl << endl; savingsAccount.withdrawalSlip(1700.00); cout << "My savings balance is $" << savingsAccount.showBalance() << endl << endl; cout << "I will receive a checking account statement on "; checkingAccount.displaySDate(); cout << endl << endl << "I will receive a savings account statement on "; savingsAccount.displaySDate(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id bubba.cpp // written_by don voils // date_written 3/2/2006 // description This program demonstrates operator overloading with ++() // based upon the this pointer. // #include<iostream> using namespace std; #include"newdate.h" class TheDate : public NewDate { public: TheDate(){} TheDate(NewDate a) { month = a.showMonth(); day = a.showDay(); year = a.showYear(); } TheDate operator ++() { *this = TheDate(*this + 1); return *this; } }; void main() { TheDate bubba; bubba.getDate(); TheDate bubba1; bubba1 = ++bubba; cout << endl << bubba1.showMonth()<<'/'<< bubba1.showDay()<<'/' << bubba1.showYear()<< endl << endl; cout << bubba.showMonth()<<'/'<< bubba.showDay()<<'/' << bubba.showYear()<< endl << endl; char resp; cout << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id CMLXASGN.CPP // author don voils // date written 10/22/2006 // // description This program illustrates the overloading // of the binary operations: +, -, *, and / // AND the assignment operators: +=, -=, /=, // and *= to the complex numbers of the form // a+bi. It does this by defining a base class // complex and then a second class by the name // of cmlxasgn is a derived class of the base // class complex. Note the use of the protected // access specifier in class and the use of the // : as and indication that cmlxasgn is a derived // class of complex. // // The operators of the base class would not work // on the derived class if it were not for the // constructor of the derived class whose argument // is an object of the base class. // #include<iostream> using namespace std; class Complex { protected: double real, imaginary; public: Complex() { } Complex(double r, double i) { real = r; imaginary=i; } double showReal() { return real; } double showImaginary() { return imaginary; } Complex operator +(Complex a) { Complex temp; temp.real = real+a.real; temp.imaginary = imaginary+a.imaginary; return temp; } Complex operator -(Complex a) { Complex temp; temp.real = real-a.real; temp.imaginary = imaginary-a.imaginary; return temp; } Complex operator *(Complex a) { Complex temp; temp.real = real*a.real-imaginary*a.imaginary; temp.imaginary=real*a.imaginary+imaginary*a.real; return temp; } Complex operator /(Complex a) { Complex temp1, temp2(real,imaginary); temp1 = temp2 * a.reciprical(); return temp1; } Complex reciprical() { Complex temp; temp.real = real/(real*real + imaginary*imaginary); temp.imaginary = (-imaginary)/(real*real + imaginary*imaginary); return temp; } }; class Cmlxasgn : public Complex { public: Cmlxasgn() : Complex() { } Cmlxasgn(double a,double b) : Complex(a,b) { } Cmlxasgn(Complex theOne) { real = theOne.showReal(); imaginary = theOne.showImaginary(); } void operator +=(Cmlxasgn a) { real += a.real; imaginary += a.imaginary; } void operator -=(Cmlxasgn a) { real -= a.real; imaginary -=a.imaginary; } void operator *=(Cmlxasgn a) { double r, i; r = real; i = imaginary; real = r*a.real-i*a.imaginary; imaginary=r*a.imaginary+i*a.real; } void operator /=(Cmlxasgn a) { Complex temp1; temp1 = a.reciprical(); Cmlxasgn temp2(temp1.showReal(),temp1.showImaginary()); Cmlxasgn temp3(real, imaginary); temp3 *= temp2; real = temp3.real; imaginary = temp3.imaginary; } }; void main() { char i, op; double real, imaginary; cout << "What is the first complex number? (Enter in the form a + bi) "; cin >> real >> op >> imaginary >> i; Cmlxasgn complex1(real,imaginary); cout << endl << endl << "What is the second complex number? (Enter in the form a + bi) "; cin >> real >> op >> imaginary >> i; Cmlxasgn complex2(real,imaginary); Cmlxasgn complex6; // The following would not be possible without the defining of // the derived class constructor whose argument is an object of the base class i // complex6= (complex1 + complex2); cout << endl << endl << "If (" << complex1.showReal() << " + " << complex1.showImaginary() << "i) + (" << complex2.showReal() << " + " << complex2.showImaginary() << "i) then the result is " << complex6.showReal() << " + " << complex6.showImaginary() << "i"; // The following would not be possible without the defining of // the derived class constructor whose argument is an object of the base class i // complex6= (complex1 - complex2); cout << endl << endl << "If (" << complex1.showReal() << " + " << complex1.showImaginary() << "i) - (" << complex2.showReal() << " + " << complex2.showImaginary() << "i) then the result is " << complex6.showReal() << " + " << complex6.showImaginary() << "i"; Cmlxasgn complex3 = complex1; complex3 += complex2; cout << endl << endl << "If (" << complex1.showReal() << " + " << complex1.showImaginary() << "i) += (" << complex2.showReal() << " + " << complex2.showImaginary() << "i) then the result is " << complex3.showReal() << " + " << complex3.showImaginary() << "i"; complex3 = complex1; complex3 -= complex2; cout << endl << endl << "If (" << complex1.showReal() << " + " << complex1.showImaginary() << "i) -= (" << complex2.showReal() << " + " << complex2.showImaginary() << "i) then the result is " << complex3.showReal() << " + " << complex3.showImaginary() << "i"; complex3 = complex1; complex3 *= complex2; cout << endl << endl << "If (" << complex1.showReal() << " + " << complex1.showImaginary() << "i) X= (" << complex2.showReal() << " + " << complex2.showImaginary() << "i) then the result is " << complex3.showReal() << " + " << complex3.showImaginary() << "i"; complex3 = complex1; complex3 /= complex2; cout << endl << endl << "If (" << complex1.showReal() << " + " << complex1.showImaginary() << "i) /= (" << complex2.showReal() << " + " << complex2.showImaginary() << "i) then the result is " << complex3.showReal() << " + " << complex3.showImaginary() << "i" << endl << endl; } // program_id CONSDEST.CPP // author don voils // date written 10/31/2006 // // description This program illustrates how constructors // in a derived class call the constructors // in the base class. If a base class has a // constructor and data members and the // constructor has an argument, then the // derived class must have a constructor. // The constructor in the base class is // called before a constructor in a derived // derived class. // // In addition to constructors, this program // demonstrates how destructors are called. // Notice that the destructors are called in // reverse order to the constructors. That is // the destructors in the derived class are // called before the destructor in the base // class is called. // #include<iostream> #include<conio.h> using namespace std; class Base { protected: int intBase; public: Base() { intBase = 0; cout << "Calling the Base no argument constructor." << endl; } Base(int a) { intBase = a; cout << "Calling the Base argument constructor." << endl; } ~Base() { cout << "When intBase = " << intBase << " calling the Base destructor." << endl; } void print() { cout << "through Base intBase = " << intBase << endl; } }; class Derived1 : public Base { public: Derived1() : Base() { cout << "Calling the Derived1 no argument constructor." << endl; } Derived1(int a) : Base(a) { cout << "When intBase = " << intBase << " calling the Derived1 argument constructor." << endl; } ~Derived1() { cout << "When intBase = " << intBase << " calling the Derived1 destructor." << endl; } void print() { cout << "Through Derived1 intBase = " << intBase << endl; } }; class Derived2 : public Base { }; void main() { char resp; cout << "Defining objectA here with an argument. " << endl; Base objectA(5); cout << endl << "Defining objectB here with an argument. " << endl; // Notice that both the Derived1 and the Base constructors are called. // The Base constructor is called first. // Derived1 objectB(15); cout << endl << "Defining objectC here with no argument. " << endl; Derived2 objectC; cout << endl << endl << "For objectA "; objectA.print(); cout << "For objectB "; objectB.print(); cout << "For objectC "; objectC.print(); // Since the class Derived2 has no constructor, this line is legal. // cout << endl << "Defining objectD here with no argument. " << endl; Derived2 objectD; // While Base has a constructor for this case, stuffc does not. // Therefore this line is not legal. // // Derived2 objectE(25); cout << "For objectD "; objectD.print(); // // Notice that as the program ends it calls the destructors. // The destructors are called in reverse order from the constructors. // The Derived1 will be called before the Base destructor. // cout << endl << "Press Y enter to end. "; cin >> resp; cout << endl << endl; } //Program_id constructor1.cpp //written_by don voils //date_written 5/20/2006 //Description The following program is an example of class inheritance // where neither the base class or the derived class have // explicit constructors. // #include<iostream> using namespace std; class Base { private: short member; public: void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived : public Base { private: short member1; public: void setMember1(short a) { member1 = a; } short showMember1() { return member1; } }; void main() { // When object1 is defined below, the // consturctor of both the base class // and the derived class are called. // Both constructors in this case are // the implicit default constructors. // Derived object1; object1.setMember(10); object1.setMember1(15); cout << "Member from Base is " << object1.showMember() << endl; cout << "Member from Derived is " << object1.showMember1() << endl << endl << endl; } // Program_id constructor2.cpp // written_by don voils // date_written 5/20/2006 // Desctiption In this example the base class has // the implicit default constructor while // the derived class has a non-default // constructor. Therefore when the // derived class' constructor is called // the implicit default construcotr of the // base is called first followed by the // non-default constructor of the derived class. // #include<iostream> using namespace std; class Base { private: short member; public: void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived : public Base { private: short member1; public: Derived(short a) { member1 = a; } short showMember1() { return member1; } }; void main() { // When object11 is defined below, the impilict default constructor // of the base class as well as the explicit non-default construcotr // of the base class are called. // Derived object1(15); object1.setMember(10); // Notice that object1 has memory for the base class data attribute // member and it can be initialized and accessed using // methods of the base class. // cout << "member from Base is " << object1.showMember() << endl; cout << "member1 from Derived is " << object1.showMember1() << endl << endl << endl; } // Program_id constructor3.cpp // written_by don voils // date_written 5/20/2006 // Description: In this example the base class has // an explicit default constructor and // the derived class has an explicity // non-default constructor. #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; cout << endl << "The value of member is " << member << endl; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1(short a) { member1 = a; cout << endl << "The value of member1 is " << member1 << endl; } short showMember1() { return member1; } }; void main() { // When object1 is defined below, the constructor // of the base and the constructor of the derived // class are both called. // cout << "The derived object: object1 is defined " << endl; Derived1 object1(15); cout << endl << "On definition the attribute: member from Base is " << object1.showMember() << endl << "On definition the attribute: member1 from Derived1 is " << object1.showMember1() << endl; object1.setMember(10); cout << endl << "After the method: setMember() is called " << "the attribute: member from Base is " << object1.showMember() << endl << endl; } // Program_id constructor4.cpp // written_by don voils // date_written 5/20/2006 // Description: In this example the base class has // an explicit default and non-default // constructor. When a object of the // derived class is defined, the default // constructor of the base class is called. // #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; } Base(short a) { member = a; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1(short a) { member1 = a; } Derived1(short a, short b) { member1 = a; setMember(b); } short showMember1() { return member1; } }; void main() { // The object object1 is defined and the default constructor // for the base class as well as the non-default constructor // of the derived class is called. // Derived1 object1(15); cout << "On definition of object1 the attribute: member from Base is " << object1.showMember() << endl; cout << "On definition of object1 the attribute: member1 from Derived1 is " << object1.showMember1() << endl << endl; object1.setMember(10); cout << "After the method: setMember() is called by object1 the attribute:" << endl << "member from Base is " << object1.showMember() << endl << endl; Derived1 object2(25,35); cout << "On definition of object2 the attribute: member from Base is " << object2.showMember() << endl; cout << "On definition of object2 the attribute:" << endl << "member1 from Derived1 is " << object2.showMember1() << endl << endl; } // Program_id constructor5.cpp // written_by don voils // date_written 5/20/2006 // Description In this example both the base class and the // derived class have two constructors. One of // the constructors of the derived class would // call the default constructor of the base class // while the other constructor of the derived // class calls the non-default constructor of // the base class. // #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; } Base(short a) { member = a; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1(short a) { member1 = a; } // Notice that this constructor calls // the non-default constructor of the // base class. Derived1(short a, short b) : Base(b) { member1 = a; } short showMember1() { return member1; } }; void main() { // The object: object1 calls the default constructor // of the base class. // Derived1 object1(15); cout << "On definition of object1 the attribute: member from Base is " << object1.showMember() << endl; cout << "On definition of object1 the attribute: member1 from Derived1 is " << object1.showMember1() << endl; object1.setMember(10); cout << endl << "After setMember() is called by object1, the attribute: member from Base is " << object1.showMember() << endl; // The object: object2 calls the non-default constructor // of the base class. // Derived1 object2(25,35); cout << endl << "On definition of object2 the attribute: member from stuff is " << object2.showMember() << endl; cout << "On definition of object2 the attribute: member1 from stuff1 is " << object2.showMember1() << endl << endl; } // Program_id constructor6.cpp // written_by don voils // date_written 5/20/2006 // COMMENT: This program does not compile. Why not? // #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; } Base(short a) { member = a; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1(short a) { member1 = a; } Derived1(short a, short b) : Base(b) { member1 = a; } short showMember1() { return member1; } }; class Derived2 : public Derived1 { private: short member2; public: Derived2(short a) { member2 = a;} short showMember2() { return member2; } }; void main() { Derived2 object1(15); cout << "On definition of object1 the Member from stuff is " << object1.showMember() << endl; cout << "On definition of object1 the Member1 from Derived1 is " << object1.showMember1() << endl; cout << "On definition of object1 the Member2 from Derived2 is " << object1.showMember2() << endl; object1.setMember(10); cout << endl << "After setMember() on object1 the Member from Base is " << object1.showMember() << endl; } // Program_id constructor7.cpp // written_by don voils // date_written 5/20/2006 // Description: This is an example of serial inheritance // where one class is derived from another // class which is in turn derived from a // third class. // #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; } Base(short a) { member = a; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1() { member1 = 2; } Derived1(short a) { member1 = a; } Derived1(short a, short b) : Base(b) { member1 = a; } short showMember1() { return member1; } }; class Derived2 : public Derived1 { private: short member2; public: Derived2(short a) { member2 = a; } short showMember2() { return member2; } }; void main() { cout << "An object of Derived2: object1 is defined using the non-default constructor" << endl << "from the derived class: Derived2 which in turn calls the default" << endl << "contructors of the class Base and Derived1" << endl << endl; Derived2 object1(15); cout << "On definition of object1 the attribute: member from Base is " << object1.showMember() << endl << endl; cout << "On definition of object1 the attribute: member1 from Derived1 is " << object1.showMember1() << endl << endl; cout << "On definition of object1 the attribute: member2 from Derived2 is " << object1.showMember2() << endl << endl << endl; } // Program_id constructor8.cpp // written_by don voils // date_written 5/20/2006 // Description: This program shows an example of // defining objects that are dependent // of base classes where default and // non-default constructors of the // base classes are called. // #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; } Base(short a) { member = a; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1() { member1 = 2; } Derived1(short a) { member1 = a; } Derived1(short a, short b) : Base(b) { member1 = a; } short showMember1() { return member1; } }; class Derived2 : public Derived1 { private: short member2; public: Derived2(short a) { member2 = a; } Derived2(short a, short b, short c): Derived1(a,b) { member2 = c; } short showMember2() { return member2; } }; void main() { // When the class Derived2 object: object1 is defined, the non-default // constructor of Derived2 with only one argument is called that is dependent on the // default constructors of the classes: Base and Derived1. // Derived2 object1(15); cout << "On definition of object1 the attribute: member from Base is " << object1.showMember() << endl; cout << "On definition of object1 the attribute: member1 from Derived1 is " << object1.showMember1() << endl; cout << "On definition of object1 the attribute: member2 from Derived2 is " << object1.showMember2() << endl << endl; // When the class Derived2 object: object2 is defined, the non-default // constructor of Derived2 with three arguments is called that is dependent on the // non-default constructors of the classes: Base and Derived1. // Derived2 object2(5, 10 ,15); cout << "On definition of object2 the attribute: member from Base is " << object2.showMember() << endl; cout << "On definition of object2 the attribute: member1 from stuff1 is " << object2.showMember1() << endl; cout << "On definition of object2 the attribute: member2 from stuff2 is " << object2.showMember2() << endl << endl; } // Program_id constructor9.cpp // written_by don voils // date_written 5/20/2006 // description Demonstrates constructors for derived classes. // #include<iostream> using namespace std; class Base { private: short member; public: Base() { member= 1; } Base(short a) { member = a; } void setMember(short a) { member = a; } short showMember() { return member; } }; class Derived1 : public Base { private: short member1; public: Derived1() { member1 = 2; } Derived1(short a) { member1 = a; } Derived1(short a, short b) : Base(b) { member1 = a; } short showMember1() { return member1; } }; class Derived2 : public Derived1 { private: short member2; public: Derived2(short a) { member2 = a; } Derived2(short a, short b, short c): Derived1(a,b) { member2 = c; } short showMember2() { return member2; } }; void main() { Derived2 object1(15); cout << "On definition of object1 member from Base is " << object1.showMember() << endl << endl; cout << "On definition of object1 member1 from Derived1 is " << object1.showMember1() << endl << endl; cout << "On definition of object1 member2 from Derived2 is " << object1.showMember2() << endl << endl << endl; } // program-id date.cpp // written_by don voils // date_written 5/20/2006 // description This file contains the method definitions // of the class Date that is defined in the // file date.h // #include<iostream> #include<string> using namespace std; #include"date.h" Date::Date() { month = 1; day = 1; year = 1970; } Date::Date(short theMonth,short theDay,short theYear) { month = theMonth; day = theDay; year = theYear; } void Date::setDate() { char dash; cout << "What is the date? (e.g. 8/31/2000) "; cin >> month >> dash >> day >> dash >> year; if(year < 1000) if(year>10) year += 1900; else year += 2000; } void Date::setDay(short theDay) { day = theDay; } void Date::setMonth(short theMonth) { month = theMonth; } void Date::setYear(short theYear) { year = theYear; } short Date::getDay() { return day; } short Date::getMonth() { return month; } short Date::getYear() { return year; } void Date::showMonthName() { string monthName[] = {"","JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE","JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"}; cout << endl<< endl << "The date is " << monthName[month] << " " << day << ", " << year; } // program_id date.h // written_by don voils // date_written 5/20/2006 // description This file contains the definition of the class Date. // #ifndef DATE #define DATE #include<iostream> #include<string> using namespace std; class Date { private: short month, day, year; public: Date(); Date(short,short,short); void setDate(); void setDay(short); void setMonth(short); void setYear(short); short getDay(); short getMonth(); short getYear(); void showMonthName(); }; #endif // program_id DATEPROT.CPP // author don voils // date written 10/25/2006 // // description This program demonstrates an extension of // the class date to a derived class new_date. // The derived class is declared a private // extension. // The only real change from DATEPRVT.CPP is // that the members: month, day, and year of // the base class are now treated as private // members of the derived class new_date. // // #include<iostream> using namespace std; // USER CREATED DATA TYPES // class Date { // Since these are protected members of the base class date // they are accessible by a derived class new_date objects // depending on the type of extension. // protected: long month, day, year; // // Since these are the public members of the base class // they are accessible by a derived class members depending // on the type of extension which is declared. // public: void getDate(); void setDate(int m,int d,int y); int lastDay(int m,int y); int showDay(); int showMonth(); int showYear(); bool leapYear(int y); long daysSince(int m, int d, int y); long f(int m, int y); long g(int m); bool incorrectDate(int m, int d, int y); }; class NewDate : private Date { public: void getNewDate() { getDate(); } void setNewDate(int m,int d,int y) { month=m, day=d, year=y; } int showNewDay() { return day; } int showNewMonth() { return month; } int showNewYear() { return year; } // Could have also used the public members of date to show // these values since the are public in a private extension // they would have had to have been included in public members // of new_date like: // int showNewDay() // { // return showDay(); // } NewDate operator +(int numberDays); NewDate operator -(int numberDays); int operator - (NewDate otherDate); }; // MAIN PROGRAM // void main() { NewDate today, future, invDate, newInvDate; bool badDate; int numberDays; do { cout << "How many days in the future do you want to go from today? "; cin >> numberDays; }while(numberDays < 0); cout << endl << endl; cout << "What is today's date? "; today.getNewDate(); future = today + numberDays; cout << endl<< endl << numberDays << " day(s) after " << today.showNewMonth() << "/" << today.showNewDay() << "/" << today.showNewYear() << " will be " << future.showNewMonth() << "/" << future.showNewDay() << "/" << future.showNewYear() << endl << endl; do { badDate = TRUE; cout << "What was the invoice date? "; invDate.getNewDate(); if ((today - invDate) < 0) cout << endl << endl << "Invoice date cannot be after today!" << endl << endl; else badDate = false; }while(badDate); cout << endl << endl << "The invoice has been open " << today - invDate << " days." << endl << endl; do { cout << "How many days in the past do you want the invoice dated? "; cin >> numberDays; }while(numberDays<0); cout << endl << endl; newInvDate = invDate - numberDays; cout << endl<< endl << numberDays << " day(s) before " << invDate.showNewMonth() << "/" << invDate.showNewDay() << "/" << invDate.showNewYear() << " will give an invoice date of " << newInvDate.showNewMonth() << "/" << newInvDate.showNewDay() << "/" << newInvDate.showNewYear() << endl << endl; } int Date::showDay() { return day; } int Date::showMonth() { return month; } int Date::showYear() { return year; } void Date::getDate() { char dash; do { cin >> month >> dash >> day >> dash >> year; }while(incorrectDate(month, day, year)); } void Date::setDate(int m, int d, int y) { month = m; day = d; year = y; } int Date::lastDay(int m, int y) { int lastDay; int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (m != 2) lastDay = daysInMonth[m]; else if (leapYear(y)) lastDay =29; else lastDay=daysInMonth[m]; return lastDay; } bool Date::leapYear(int y) { bool leapTest; if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0)) leapTest=TRUE; else leapTest=false; return leapTest; } bool Date::incorrectDate(int m, int d, int y) { bool notCorrect; if ((m>=1) && (m<=12) && (d>=1) && (d<=lastDay(m,y))) notCorrect = false; else notCorrect = true; return notCorrect; } long Date::daysSince(int m, int d, int y) { long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d; return(number); } long Date::f(int m, int y) { long number = (m<=2L) ? y - 1L :y; return(number); } long Date::g(int m) { long number = (m<=2L) ? m + 13L : m + 1L; return(number); } NewDate NewDate::operator + (int numberDays) { NewDate nextDay; nextDay.setNewDate(showNewMonth(),showNewDay(),showNewYear()); for(int index = 1; index <=numberDays; ++index) { if (nextDay.showNewDay() < lastDay(nextDay.showNewMonth(), nextDay.showNewYear())) { nextDay.setNewDate(nextDay.showNewMonth(), nextDay.showNewDay()+1, nextDay.showNewYear()); } else { if(nextDay.showNewMonth()<12) { nextDay.setNewDate(nextDay.showNewMonth()+1,1, nextDay.showNewYear()); } else { nextDay.setNewDate(1, 1, nextDay.showNewYear()+1); } } } return nextDay; } NewDate NewDate::operator - (int numberDays) { NewDate nextDay; nextDay.setNewDate(showNewMonth(),showNewDay(),showNewYear()); for(int index=1; index <= numberDays; ++index) { if (nextDay.showNewDay() != 1) { nextDay.setNewDate(nextDay.showNewMonth(), nextDay.showNewDay()-1, nextDay.showNewYear()); } else { if(nextDay.showNewMonth() != 1) { int newMonth=nextDay.showNewMonth() - 1, newYear = nextDay.showNewYear(), newDay = lastDay(newMonth,newYear); nextDay.setNewDate(newMonth,newDay,newYear); } else { nextDay.setNewDate(12, 31, nextDay.showNewYear() - 1); } } } return nextDay; } int NewDate::operator - (NewDate bDate) { int days; days = daysSince(showNewMonth(), showNewDay(), showNewYear()) - daysSince(bDate.showNewMonth(), bDate.showNewDay(), bDate.showNewYear()); return days; } // program_id DATEPRVT.CPP // author don voils // date written 10/25/2006 // // description This program demonstrates an extension of // the class date to a derived class new_date. // The derived class is declared a private // extension. // #include<iostream> using namespace std; // USER CREATED DATA TYPES // class Date { // Since these are private members of the base class // they are not accessible by a derived class no matter // what type of an extension is declared. // private: long month, day, year; // // Since these are the public members of the base class // they are accessible by a derived class members depending // on the type of extension which is declared. // public: void getDate(); void setDate(int m, int d, int y); int lastDay(int m, int y); int showDay(); int showMonth(); int showYear(); bool leapYear(int y); long daysSince(int m, int d, int y); long f(int m, int y); long g(int m); bool incorrectDate(int m, int d, int y); }; class NewDate : private Date { public: void getNewDate(void) { getDate(); } void setNewDate(int m, int d, int y) { setDate(m, d, y); } int showNewDay() { return showDay(); } int showNewMonth() { return showMonth(); } int showNewYear() { return showYear(); } NewDate operator +(int numberDays); NewDate operator -(int numberDays); int operator - (NewDate otherDate); }; // MAIN PROGRAM // void main() { NewDate today, future, invDate, newInvDate; bool badDate; int numberDays; do { cout << "How many days in the future do you want to go from today? "; cin >> numberDays; }while(numberDays < 0); cout << endl << endl; cout << "What is today's date? "; today.getNewDate(); future = today + numberDays; cout << endl<< endl << numberDays << " day(s) after " << today.showNewMonth() << "/" << today.showNewDay() << "/" << today.showNewYear() << " will be " << future.showNewMonth() << "/" << future.showNewDay() << "/" << future.showNewYear() << endl << endl; do { badDate = true; cout << "What was the invoice date? "; invDate.getNewDate(); if ((today - invDate) < 0) cout << endl << endl << "Invoice date cannot be after today!" << endl << endl; else badDate = false; }while(badDate); cout << endl << endl << "The invoice has been open " << today - invDate << " days." << endl << endl; do { cout << "How many days in the past do you want the invoice dated? "; cin >> numberDays; }while(numberDays < 0); cout << endl << endl; newInvDate = invDate - numberDays; cout << endl<< endl << numberDays << " day(s) before " << invDate.showNewMonth() << "/" << invDate.showNewDay() << "/" << invDate.showNewYear() << " will give an invoice date of " << newInvDate.showNewMonth() << "/" << newInvDate.showNewDay() << "/" << newInvDate.showNewYear() << endl << endl; } int Date::showDay() { return day; } int Date::showMonth() { return month; } int Date::showYear() { return year; } void Date::getDate() { char dash; do { cin >> month >> dash >> day >> dash >> year; if(year <= 10) year += 2000; else if(year < 100) year += 1900; }while(incorrectDate(month, day, year)); } void Date::setDate(int m, int d, int y) { month = m; day = d; year = y; } int Date::lastDay(int m, int y) { int lastDay; int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (m != 2) lastDay=daysInMonth[m]; else if (leapYear(y)) lastDay =29; else lastDay=daysInMonth[m]; return lastDay; } bool Date::leapYear(int y) { bool leapTest; if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0)) leapTest=true; else leapTest=false; return leapTest; } bool Date::incorrectDate(int m,int d,int y) { bool notCorrect; if ((m>=1) && (m<=12) && (d>=1) && (d<=lastDay(m,y))) notCorrect = false; else notCorrect = true; return notCorrect; } long Date::daysSince(int m, int d, int y) { long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d; return(number); } long Date::f(int m, int y) { long number = (m<=2L) ? y - 1L :y; return(number); } long Date::g(int m) { long number = (m<=2L) ? m + 13L : m + 1L; return(number); } NewDate NewDate::operator + (int numberDays) { NewDate nextDay; nextDay.setNewDate(showNewMonth(),showNewDay(),showNewYear()); for(int index=1; index <=numberDays; ++index) { if (nextDay.showNewDay() < lastDay(nextDay.showNewMonth(), nextDay.showNewYear())) { nextDay.setNewDate(nextDay.showNewMonth(), nextDay.showNewDay()+1, nextDay.showNewYear()); } else { if(nextDay.showNewMonth()<12) { nextDay.setNewDate(nextDay.showNewMonth()+1,1, nextDay.showNewYear()); } else { nextDay.setNewDate(1, 1, nextDay.showNewYear()+1); } } } return nextDay; } NewDate NewDate::operator - (int numberDays) { NewDate nextDay; nextDay.setNewDate(showNewMonth(), showNewDay(), showNewYear()); for(int index=1; index <= numberDays; ++index) { if (nextDay.showNewDay() != 1) { nextDay.setNewDate(nextDay.showNewMonth(), nextDay.showNewDay()-1, nextDay.showNewYear()); } else { if(nextDay.showNewMonth() != 1) { int newMonth = nextDay.showNewMonth() - 1, newYear = nextDay.showNewYear(), newDay=lastDay(newMonth, newYear); nextDay.setNewDate(newMonth, newDay, newYear); } else { nextDay.setNewDate(12, 31, nextDay.showNewYear() - 1); } } } return nextDay; } int NewDate::operator - (NewDate bDate) { int days; days = daysSince(showNewMonth(), showNewDay(), showNewYear()) -daysSince(bDate.showNewMonth(), bDate.showNewDay(), bDate.showNewYear()); return days; } // program_id DATEPUBL.CPP // author don voils // date written 10/25/2006 // // description This program demonstrates an extension of // the class Date to a derived class NewDate. // The derived class is declared a public // extension. // The only real change from DATEPROT.CPP is // that the public members of the base class // Date are now treated as public members // of the derived class NewDate. // // #include<iostream> using namespace std; // USER CREATED DATA TYPES // class Date { // Since these are protected members of the base class date // they are accessible by a derived class new_date objects // depending on the type of extension. // protected: long month, day, year; // // Since these are the public members of the base class // they are accessible by a derived class members depending // on the type of extension which is declared. // public: void getDate(void); void setDate(int m, int d, int y); int lastDay(int m, int y); int showDay(); int showMonth(); int showYear(); bool leapYear(int y); long daysSince(int m, int d, int y); long f(int m,int y); long g(int m); bool incorrectDate(int m, int d, int y); }; class NewDate : public Date { public: // Notice the difference between this public extension and the // private of DATEPRVT.CPP and the protected of DATEPROT.CPP. // Since this is a public extension all of the protected members // of Date become protected members of NewDate. All of the public // members of Date become public members of NewDate. As a result // of this last point, the number of public members of NewDate // were greatly reduced since the public members of Date are now // public members of NewDate. // NewDate operator +(int numberDays); NewDate operator -(int numberDays); int operator - (NewDate otherDate); }; // MAIN PROGRAM // void main() { NewDate today, future, invDate, newInvDate; bool badDate; int numberDays; do { cout << "How many days in the future do you want to go from today? "; cin >> numberDays; }while(numberDays<0); cout << endl << endl; cout << "What is today's date? "; today.getDate(); future = today + numberDays; cout << endl<< endl << numberDays << " day(s) after " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << " will be " << future.showMonth() << "/" << future.showDay() << "/" << future.showYear() << endl << endl; do { badDate = true; cout << "What was the invoice date? "; invDate.getDate(); if ((today - invDate) < 0) cout << endl << endl << "Invoice date cannot be after today!" << endl << endl; else badDate = false; }while(badDate); cout << endl << endl << "The invoice has been open " << today - invDate << " days." << endl << endl; do { cout << "How many days in the past do you want the invoice dated? "; cin >> numberDays; }while(numberDays<0); cout << endl << endl; newInvDate = invDate - numberDays; cout << endl<< endl << numberDays << " day(s) before " << invDate.showMonth() << "/" << invDate.showDay() << "/" << invDate.showYear() << " will give an invoice date of " << newInvDate.showMonth() << "/" << newInvDate.showDay() << "/" << newInvDate.showYear() << endl << endl; } int Date::showDay() { return day; } int Date::showMonth() { return month; } int Date::showYear() { return year; } void Date::getDate() { char dash; do { cin >> month >> dash >> day >> dash >> year; if(year <=10) year += 2000; else if(year < 100) year+=1900; }while(incorrectDate(month, day, year)); } void Date::setDate(int m, int d, int y) { month = m; day = d; year = y; } int Date::lastDay(int m, int y) { int lastDay; int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (m != 2) lastDay=daysInMonth[m]; else if (leapYear(y)) lastDay =29; else lastDay=daysInMonth[m]; return lastDay; } bool Date::leapYear(int y) { bool leapTest; if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0)) leapTest=true; else leapTest=false; return leapTest; } bool Date::incorrectDate(int m, int d, int y) { bool notCorrect; if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m, y))) notCorrect = false; else notCorrect = true; return notCorrect; } long Date::daysSince(int m, int d, int y) { long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d; return(number); } long Date::f(int m, int y) { long number = (m<=2L) ? y - 1L :y; return(number); } long Date::g(int m) { long number = (m<=2L) ? m + 13L : m + 1L; return(number); } NewDate NewDate::operator + (int numberDays) { NewDate nextDay; nextDay.setDate(showMonth(),showDay(),showYear()); for(int index=1; index <=numberDays; ++index) { if (nextDay.showDay() < lastDay(nextDay.showMonth(), nextDay.showYear())) { nextDay.setDate(nextDay.showMonth(), nextDay.showDay()+1, nextDay.showYear()); } else { if(nextDay.showMonth()<12) { nextDay.setDate(nextDay.showMonth()+1,1, nextDay.showYear()); } else { nextDay.setDate(1, 1, nextDay.showYear()+1); } } } return nextDay; } NewDate NewDate::operator - (int numberDays) { NewDate nextDay; nextDay.setDate(showMonth(),showDay(),showYear()); for(int index=1;index <= numberDays;++index) { if (nextDay.showDay() != 1) { nextDay.setDate(nextDay.showMonth(),nextDay.showDay()-1, nextDay.showYear()); } else { if(nextDay.showMonth() != 1) { int newMonth=nextDay.showMonth()-1, newYear=nextDay.showYear(), newDay=lastDay(newMonth,newYear); nextDay.setDate(newMonth,newDay,newYear); } else { nextDay.setDate(12, 31, nextDay.showYear()-1); } } } return nextDay; } int NewDate::operator - (NewDate bDate) { int days; days = daysSince(showMonth(),showDay(),showYear()) -daysSince(bDate.showMonth(), bDate.showDay(), bDate.showYear()); return days; } // program_id datereln.cpp // author don voils // date written 10/15/2006 // // description This program illustrates a serial extension // of classes. The base class is date. It has a // derived class new_date which is a public // extension of date. new_date is then a base class // for the derived class datereln. daterelen is // a public extension of new_date. // #include<iostream> using namespace std; #include "newdate.h" class DateReln : public NewDate { public: // Since the operators + and - from the class NewDate had output of // a NewDate object, it was not possible to use these function // without change. This is handled here using constructors. // DateReln() { } DateReln(NewDate aDate) { month = aDate.showMonth(); day = aDate.showDay(); year = aDate.showYear(); } bool operator <(DateReln aDate); bool operator <=(DateReln aDate); bool operator ==(DateReln aDate); bool operator >(DateReln aDate); bool operator >=(DateReln aDate); }; // MAIN PROGRAM // void main() { DateReln today, future, invDate, newInvDate; bool badDate; int numberDays; do { cout << "How many days in the future do you want to go from today? "; cin >> numberDays; }while(numberDays<0); cout << endl << endl; cout << "What is today's date? "; today.getDate(); future = today + numberDays; cout << endl<< endl << numberDays << " day(s) after " << today.showMonth() << "/" << today.showDay() << "/" << today.showYear() << " is " << future.showMonth() << "/" << future.showDay() << "/" << future.showYear() << endl << endl; do { badDate = TRUE; cout << "What was the invoice date? "; invDate.getDate(); if ((today < invDate)) cout << endl << endl << "Invoice date cannot be after today!" << endl << endl; else if (today == invDate) cout << endl << endl << "Invoice date cannot be today!" << endl << endl; else badDate = false; }while(badDate); cout << endl << endl << "The invoice has been open " << today - invDate << " days." << endl << endl; do { cout << "How many days in the past do you want the invoice dated? "; cin >> numberDays; }while(numberDays<0); cout << endl << endl; newInvDate = invDate - numberDays; cout << endl<< endl << numberDays << " day(s) before " << invDate.showMonth() << "/" << invDate.showDay() << "/" << invDate.showYear() << " will give an invoice date of " << newInvDate.showMonth() << "/" << newInvDate.showDay() << "/" << newInvDate.showYear() << endl << endl; } bool DateReln::operator >(DateReln aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month,day,year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate > secondDate)? true : false; return resp; } bool DateReln::operator >=(DateReln aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month,day,year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate >= secondDate) ? true : false; return resp; } bool DateReln::operator ==(DateReln aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month,aDate.day,aDate.year); resp = (firstDate == secondDate)? true : false; return resp; } bool DateReln::operator <(DateReln aDate) { bool resp; long firstDate, secondDate; firstDate = daysSince(month, day, year); secondDate=daysSince(aDate.month,aDate.day,aDate.year); resp = (firstDate < secondDate)? true : false; return resp; } bool DateReln::operator <= (DateReln aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month,aDate.day,aDate.year); resp = (firstDate <= secondDate)? true : false; return resp; } // program_id devi.cpp // written_by don voils // date_written 5/20/2006 // description This program tries to look at the question as // to whether when a member of a class is raised // to a level of derived extension can exceed the access // level it held in the base class. #include<iostream> using namespace std; class Base { private: int member; public: Base() {member = 0;} Base(int a) { member = a; } int showBase() { return member; } }; class Derived : protected Base { protected: int member1; // This member was in the private access section in the base class. // The question is can it be raised to the protected access section // in the derived class. The answer seems to be no because when the // comment symbol below is removed an error occurrs. // // Base::member; public: Derived(int a, int b):Base(a) { member1 = b; } int showDerived() { return member1; } Base::showBase; }; void main() { Derived object(10,15); cout << "The value of member is " << object.showBase() << endl; cout << "The value of member1 is " << object.showDerived() << endl; cout << endl << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id don.cpp // written_by don voils // date_written 5/20/2006 // description Demonstrates a way to define the ++() operator. // #include<iostream> using namespace std; #include"newdate.h" class TheDate : public NewDate { public: TheDate(){} TheDate(NewDate a) { month = a.showMonth(); day = a.showDay(); year = a.showYear(); } TheDate operator ++() { TheDate temp; temp.setDate(month,day,year); temp = temp + 1; month = temp.month; day = temp.day; year = temp.year; return temp; } }; void main() { TheDate don1; don1.getDate(); TheDate don2; don2 = ++don1; cout << endl << don1.showMonth()<<'/'<< don1.showDay()<<'/' << don1.showYear()<< endl << endl; cout << don2.showMonth()<<'/'<< don2.showDay()<<'/' << don2.showYear()<< endl << endl << endl; } // program_id EXTDNAME.CPP // author don voils // date written 10/31/2006 // // description This program illustrates how functions // in a derived class with the same name // as a function in the base class handles // a call from a derived class object. // #include<iostream> using namespace std; class Base { protected: int intBase; public: void setInt(int a) { intBase = a; } void print() { cout << "through Base intBase = " << intBase << endl << endl; } }; // Since Derived is a protected extension of Base, the objects // of Derived can not access the public member functions of Base // using the dot operator. As a result, member functions in Derived // must be defined to accomplish these tasks. Sometimes it is desirable // to use the same names for both functions. But this is not possible // without using the scope specifier as illustrated in the definitions // below. // class Derived : protected Base { public: void setInt(int a) { Base::setInt(a); cout << endl<< "Initialized through Derived." << endl; } void print() { cout << " printed through Derived." << endl ; Base::print(); } }; void main() { Base ObjectA; ObjectA.setInt(5); Derived ObjectB; ObjectB.setInt(15); cout << "For ObjectA "; ObjectA.print(); cout << "For ObjectB "; ObjectB.print(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } //program_id HIGHERAC.CPP //author don voils //date written 10/01/2006 //description Demonstrates that the // inheritance access specifier may be over // ridden through redeclaration for both data // members and member functions. // #include<iostream> using namespace std; class Date { protected: int month, day, year; public: char aFlag; Date() {} Date(int m, int d, int y,char f='A') { month = m; day = d; year = y; aFlag = f; } int showMonth() { return month; } int showDay() { return day; } int showYear() { return year; } void print() { cout << endl << "Date: " << month << "/" << day << "/" <<year << endl << endl << "The aFlag is " << aFlag << endl; } }; class NewDate : private Date { protected: // notice that these data members of the base class // are redeclared here but no data types are specified. Date::month; Date::day; Date::year; char bFlag; public: Date::aFlag; // notice that these member fuctions of the base class // are redeclared here but not output data type, no arguments // nor no parenthesis are listed. Date::showMonth; Date::showDay; Date::showYear; NewDate() {} // although the derivation was private and the data members // month, day, and year were declared in the protected section // a member function of the derived class can access these // base data members. NewDate(int m, int d, int y,char f='B',char g='C') { month = m; day = d; year = y; aFlag = f; bFlag = g; } // notice that the following member function has the same // name as the member function in the base class. void print() { Date::print(); cout << endl << "The bFlag is " << bFlag << endl; } }; void main() { char slash; int m,d,y; cout << "What is the date? "; cin >> m >> slash >> d >> slash >> y; NewDate aDate(m,d,y); aDate.print(); // Notice that the derivation was private yet the // member functions in the public access section // of the base are being accessed by // an object of the derived class in main() cout << endl << endl << "Did you say the date was:" << aDate.showMonth()<< "/" << aDate.showDay() << "/" << aDate.showYear() << endl << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id INVCLASS.CPP // author don voils // date written 10/09/2006 // // description This program demonstrates the // concept of nested classes. // #include<iostream> #include<iomanip> using namespace std; class Date { private: int month, day, year; public: void setDate(int m, int d, int y) { month = m; day = d; year = y; } int getMonth() { return month; } int getDay() { return day; } int getYear() { return year; } }; class Invoice { private: float invAmount; Date invDate; public: void setDate(int m, int d, int y) { invDate.setDate(m, d, y); } void setAmount(float amount) { invAmount = amount; } int getInvMonth() { return invDate.getMonth(); } int getInvDay() { return invDate.getDay(); } int getInvYear() { return invDate.getYear(); } float getInvAmount() { return invAmount; } }; void main() { char dash; int month, day, year; float amount; Invoice invoice1234; cout << "What was the date? "; cin >> month >> dash >> day >> dash >> year; invoice1234.setDate(month, day, year); cout <<endl<<endl<< "What was the amount of the invoice? "; cin >> amount; invoice1234.setAmount(amount); for(int index=1;index<=250;++index) cout << endl; cout << "Did you say that the invoice date was: " << invoice1234.getInvMonth() << "/" << invoice1234.getInvDay() << "/" << invoice1234.getInvYear() << endl << endl << "Did you say that the invoice amount was: $" << setiosflags(ios::fixed) << setprecision(2) << setiosflags(ios::showpoint) << invoice1234.getInvAmount(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id multext2.cpp // author don voils // date written 11/10/2006 // // description This program illustrates how a class // can be a derived extension of three or // more other classes. Notice that the base // classes BaseA, BaseB and BaseD each have two // constructors. // // The derived class has two constructors as well, one // with no argument and one with // arugments. However the // constructors for Derived do not // contain a reference to the class BaseD. // // While this extension is a private // extension of one class and a public // extension of the other two, a multiple extension // could have any combination of types // of extensions. // #include<iostream> using namespace std; class BaseA { protected: int intBaseA; public: BaseA() { intBaseA = 10; } BaseA(int a) { intBaseA= a; } int showintBaseA() { return intBaseA; } void doStuff() { intBaseA +=10; } }; class BaseB { protected: int intBaseB; public: BaseB() { intBaseB = 10; } BaseB(int b) { intBaseB = b; } int showintBaseB() { return intBaseB; } void doStuff() { intBaseB -=10; } }; // Since class BaseD has no constructor with an argument, // Derived as a derived class of the base class BaseD is // not required to include reference to this class in its // constructor. // class BaseD { private: int intBaseD; public: // This constructor is legal when the constructor of // class Derived a derived class of this class, does // not include a reference to this constructor. BaseD() { intBaseD = 10; } // The constructor below is legal even when the constructor // of class Derived does not include a reference to this // constructor in its header. BaseD(int d) { intBaseD = d; } // Since the constructor above is not referred // to by the constructor of Derived with an argument, it is // necessary to define the function below to // enable objects of both classes to "load" the // int_stuffd with a value. // void setintBaseD(int d) { intBaseD = d; } int showintBaseD() { return intBaseD; } void doStuff() { intBaseD *=10; } }; class Derived : private BaseA, public BaseB, public BaseD { private: int intDerived; public: // No references to the non-argument constructors of // the classes BaseA, BaseB, or BaseD need to be listed // below. The non-argument constructors are called automatically // when an object of Derived is defined. This is done below // with objectA which takes all default values. // Derived() { intDerived = 10; } // Notice there is no reference to a constructor // from class BaseD even though there is a constructor of // class BaseD that does have an argument. Derived(int a, int b, int c): BaseA(a), BaseB(b) { intDerived = c; } void showMembers() { cout << "intBaseA = " << showintBaseA() << endl << "intBaseB = " << showintBaseB() << endl << "intBaseD = " << showintBaseD() << endl << "intDerived = " << intDerived << endl; } void doStuff() { BaseA::doStuff(); BaseB::doStuff(); BaseD::doStuff(); intDerived *= -10; } }; void main() { Derived objectA, objectB(1,1,1); objectB.setintBaseD(1); cout << "Showing the Derived object objectA constructed with no argument." << endl; objectA.showMembers(); cout << endl << endl << "Showing the Derived object objectB constructed with an argument" << endl; objectB.showMembers(); objectB.doStuff(); cout << endl << endl << "Showing the stuffc object objectb after doStuff() " << endl; objectB.showMembers(); cout << endl << endl; } // program_id multextn.cpp // author don voils // date written 10/31/2006 // // description This program illustrates how a class // can be a derived extension of two or // more other classes. Notice that each // base class has two constructors. // Therefore the derived class has to // have two constructors as well, one // with no argument and one with // arugments. // // While this extension is a private // extension of one class and a public // extension of the second, an extension // could have any combination of types // of extensions. // #include<iostream> using namespace std; class BaseA { protected: int intBaseA; public: BaseA() { intBaseA = 0; } BaseA(int a) { intBaseA = a; } int showBaseA() { return intBaseA; } void doStuff() { intBaseA +=10; } }; class BaseB { protected: int intBaseB; public: BaseB() { intBaseB = 0; } BaseB(int b) { intBaseB = b; } int showBaseB() { return intBaseB; } void doStuff() { intBaseB -=10; } }; class Derived : private BaseA, public BaseB { private: int intDerived; public: Derived() : BaseA(), BaseB() { intDerived = 0; } Derived(int a, int b, int c): BaseA(a), BaseB(b) { intDerived = c; } void showMembers() { cout << "intBaseA = " << showBaseA() << endl << "intBaseB = " << showBaseB() << endl << "intDerived = " << intDerived << endl; } void doItAll() { BaseA ::doStuff(); BaseB::doStuff(); intDerived *= 10; } }; void main() { Derived objectA, objectB(1,1,1); cout << "Showing the Derived object objectA constructed with no argument." << endl; objectA.showMembers(); cout << endl << endl << "Showing the Derived object objectB constructed with an argument" << endl; objectB.showMembers(); objectB.doItAll(); cout << endl << endl << "Showing the Derived object objectB after doItAll()." << endl; objectB.showMembers(); } // program_id NAMETEST.CPP // author don voils // date written 10/31/2006 // // program description This program illustrates how functions // in a derived class with the same name // as a function in the base class handles // a call from a derived class object. // #include<iostream> using namespace std; class BaseClass { protected: int theMember; public: void setInt(int a) { theMember = a; } void print() { cout << "through BaseClass theMember = " << theMember << endl << endl; } }; class DerivedB : public BaseClass { public: void print() { cout << "through DerivedB theMember = " << theMember << endl << endl; } }; class DerivedC : public BaseClass { }; void main() { BaseClass objectA; objectA.setInt(5); DerivedB objectB; objectB.setInt(15); DerivedC objectC; objectC.setInt(25); cout << "For objectA "; objectA.print(); cout << "For objectB "; objectB.print(); cout << "For objectC "; objectC.print(); cout << "For theMember using the print() from BaseClass "; objectB.BaseClass::print(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id newdate.h // written_by don voils // date_written 5/20/2006 // description Contains the definition of the classes: Date and NewDate // where Date is the base of NewDate. // // USER CREATED DATA TYPES // class Date { // Since these are protected members of the base class date // they are accessible by a derived class new_date objects // depending on the type of extension. // protected: long month, day, year; // // Since these are the public members of the base class // they are accessible by a derived class members depending // on the type of extension which is declared. // public: void getDate(); void setDate(int m, int d, int y); int lastDay(int m, int y); int showDay(); int showMonth(); int showYear(); bool leapYear(int y); long daysSince(int m, int d, int y); long f(int m, int y); long g(int m); bool incorrectDate(int m, int d, int y); }; class NewDate : public Date { public: NewDate operator +(int numberDays); NewDate operator -(int numberDays); int operator - (NewDate otherDate); }; int Date::showDay() { return day; } int Date::showMonth() { return month; } int Date::showYear() { return year; } void Date::getDate() { char dash; do { cout << "Enter date: m/d/y "; cin >> month >> dash >> day >> dash >> year; if(year< 1000) if(year < 10) year += 2000; else year += 1900; }while(incorrectDate(month, day, year)); } void Date::setDate(int m, int d, int y) { month = m; day = d; year = y; } int Date::lastDay(int m, int y) { int lastDay; int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (m != 2) lastDay=daysInMonth[m]; else if (leapYear(y)) lastDay =29; else lastDay=daysInMonth[m]; return lastDay; } bool Date::leapYear(int y) { bool leapTest; if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0)) leapTest=true; else leapTest=false; return leapTest; } bool Date::incorrectDate(int m, int d, int y) { bool notCorrect; if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m,y))) notCorrect = false; else notCorrect = true; return notCorrect; } long Date::daysSince(int m, int d, int y) { long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d; return(number); } long Date::f(int m,int y) { long number = (m <= 2L) ? y - 1L :y;3 return(number); } long Date::g(int m) { long number = (m <= 2L) ? m + 13L : m + 1L; return(number); } NewDate NewDate::operator + (int numberDays) { NewDate nextDay; nextDay.setDate(showMonth(),showDay(),showYear()); for(int index=1; index <=numberDays; ++index) { if (nextDay.showDay() < lastDay(nextDay.showMonth(), nextDay.showYear())) { nextDay.setDate(nextDay.showMonth(), nextDay.showDay()+1, nextDay.showYear()); } else { if(nextDay.showMonth()<12) { nextDay.setDate(nextDay.showMonth()+1, 1, nextDay.showYear()); } else { nextDay.setDate(1, 1, nextDay.showYear() + 1); } } } return nextDay; } NewDate NewDate::operator - (int numberDays) { NewDate nextDay; nextDay.setDate(showMonth(),showDay(),showYear()); for(int index=1; index <= numberDays; ++index) { if (nextDay.showDay() != 1) { nextDay.setDate(nextDay.showMonth(), nextDay.showDay() - 1, nextDay.showYear()); } else { if(nextDay.showMonth() != 1) { int newMonth = nextDay.showMonth() - 1, newYear = nextDay.showYear(), newDay=lastDay(newMonth,newYear); nextDay.setDate(newMonth,newDay,newYear); } else { nextDay.setDate(12, 31, nextDay.showYear() - 1); } } } return nextDay; } int NewDate::operator - (NewDate bDate) { int days; days = daysSince(month,day,year) -daysSince(bDate.month,bDate.day, bDate.year); return days; } // file-id newdate.cpp // written_by don voils // date_written 5/20/2006 // contents: This file contains the defintions of the // methods for the class newDate. // // #include"newdate.h" void newDate::setNewDate(short theMonth,short theDay,short theYear) { setMonth(theMonth); setDay(theDay); setYear(theYear); } short newDate::lastDay(short theMonth,short theYear) { short lastDay; int daysInMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if (theMonth!=2) lastDay=daysInMonth[theMonth]; else if (leapYear(theYear)) lastDay =29; else lastDay=daysInMonth[theMonth]; return lastDay; } bool newDate::leapYear(short theYear) { bool leapTest; if (((theYear%4==0) && (theYear%100!=0)) || (theYear%400==0)) leapTest=true; else leapTest=false; return leapTest; } long newDate::daysSince(short theMonth,short theDay, short theYear) { long theNumber = 1461L*f(theMonth,theYear)/4L + 153L*g(theMonth)/5L + theDay; return(theNumber); } bool newDate::incorrectDate(short theMonth,short theDay,short theYear) { bool notCorrect; if ((theMonth>=1) && (theMonth<=12) && (theDay>=1) && (theDay<=lastDay(theMonth,theYear))) notCorrect = false; else notCorrect = true; return notCorrect; } long newDate::f(short theMonth,short theYear) { long theNumber = (theMonth<=2L) ? theYear - 1L :theYear; return(theNumber); } long newDate::g(short theMonth) { long theNumber = (theMonth<=2L) ? theMonth + 13L : theMonth + 1L; return(theNumber); } newDate newDate::operator +(short numberDays) { newDate nextDay; nextDay.setNewDate(getMonth(),getDay(),getYear()); for(int index=1;index <=numberDays;++index) { if (nextDay.getDay() < lastDay(nextDay.getMonth(), nextDay.getYear())) { nextDay.setNewDate(nextDay.getMonth(), nextDay.getDay()+1, nextDay.getYear()); } else { if(nextDay.getMonth()<12) { nextDay.setNewDate(nextDay.getMonth()+1,1, nextDay.getYear()); } else { nextDay.setNewDate(1,1,nextDay.getYear()+1); } } } return nextDay; } short newDate::operator - (newDate datePaid) { short theDays; theDays = (short)(daysSince(getMonth(),getDay(),getYear()) -daysSince(datePaid.getMonth(),datePaid.getDay(), datePaid.getYear())); return theDays; } ostream &operator << (ostream &stream, newDate aDate) { stream << aDate.getMonth() << "/"; stream << aDate.getDay() << "/"; stream << aDate.getYear() << endl; return stream; } istream &operator >> (istream &stream, newDate &aDate) { char slash; short theMonth, theDay, theYear; stream >> theMonth >> slash >> theDay >> slash >> theYear; if(theYear< 100) if(theYear >10) theYear += 1900; else theYear += 2000; aDate.setMonth(theMonth); aDate.setDay(theDay); aDate.setYear(theYear); return stream; } // file_id newdate.h // writtin_by don voils // date_written 5/20/2006 // program_id Contains the definition of the class: newDate // #ifndef NEWDATE #define NEWDATE #include"date.h" class newDate : public Date { public: void setNewDate(short,short,short); short lastDay(short,short); bool leapYear(short); long daysSince(short,short,short); long f(short,short); long g(short); bool incorrectDate(short,short,short); newDate operator +(short); short operator -(newDate); friend ostream &operator << (ostream &stream, newDate aDate); friend istream &operator >> (istream &stream, newDate &aDate); }; #endif // program_id PrivateInheritance.CPP // author don voils // date written 10/25/2006 // // program description This program illustrates a simple derived // class NewClass derived from the base class // BaseClass. The extension is a private derivation. // #include<iostream> using namespace std; class BaseClass { private: int theInteger; float theFloat; protected: int theInteger2; public: void setPrivate(int i, float f) { theInteger=i; theFloat=f; } void setProtected(int j) { theInteger2=j; } int getPrivateInteger() { return theInteger;} float getPrivateFloat() { return theFloat;} int getProtectedInteger() { return theInteger2;} }; class NewClass : private BaseClass { // Since this is a private extension all protected and public members // act like private members of NewClass. Therefore they are accessible // within NewClass by public members. Since they act like private members // within NewClass, the protected and the public members of stuff are not // accessible by a class derived from NewClass. // private: char newChar; public: void setNewAttributes(char c, int i, float f, int j) { setPrivate(i, f); // // Can access the base class protected member j // in the following manner // theInteger2 = j; // // Or could have used this public function instead // // setProtected(int j); // newChar = c; } void showNewClass() { cout << "theInteger = " << getPrivateInteger() << endl << "theFloat = " << getPrivateFloat() << endl << "theInteger2 = " << getProtectedInteger() << endl << "newChar = " << newChar; } }; void main() { NewClass object1; object1.setNewAttributes('a',1,10.1f,2); // // The following lines are non-accessible during a private extension // // object1.theInteger = 23; // This line is trying to access a base class // private member. // object1.theInteger2 = 55; // This line is trying to access a base class // protected memeber. // object1.getPrivateInteger(); // This line is trying to access a base class // public member. object1.showNewClass(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id ProtectedInheritance.CPP // author don voils // date written 10/25/2006 // // program description This program illustrates a simple derived // class NewClass derived from BaseClass // The extension is a protected derivation. // #include<iostream> using namespace std; class BaseClass { private: int theInteger; float theFloat; protected: int theInteger2; public: void setPrivate(int i, float f) { theInteger=i; theFloat=f; } void setProtected(int j) { theInteger2=j; } int getPrivateInteger() { return theInteger;} float getPrivateFloat() { return theFloat;} int getProtectedInteger() { return theInteger2;} }; class NewClass : protected BaseClass { // Since this is a protected extension all protected and public members // act like protected members of NewClass. Therefore they are accessible // within NewClass by public members. Since they act like protected members // within NewClass, the protected and the public members of BaseClass are // accessible by a class NewClass2 derived from NewClass. // private: char theChar; public: void setNewClass(char c, int i, float f, int j) { // Can not access the base class private members // directly so must initialize in the following manner. // setPrivate(i, f); // // Can access the base class protected member j // in the following manner // theInteger2 = j; // // Or could use this public function instead // setProtected(j); theChar = c; } void showNewClass() { cout << "theInteger = " << getPrivateInteger() << endl << "theFloat = " << getPrivateFloat() << endl << "theInteger2 = " << getProtectedInteger() << endl << "theChar = " << theChar << endl << endl; } }; class NewClass2: public NewClass { private: char newChar; public: void setNewClass2(char c1, char c2, int i, float f, int j) { setNewClass(c2, i, f, j); newChar = c1; } char getnewChar() { return newChar; } }; void main() { NewClass object1; cout << "Initialized object11:" << endl; object1.setNewClass('a',1,10.1f,2); // // The following lines are non-accessible during a protected extension // // object1.theInteger = 23; // This line is trying to access a base class // private member. // object1.theInteger2 = 55; // This line is trying to access a base class // protected memeber. // object1.show_private_int(); // This line is trying to access a base class // public member. object1.showNewClass(); NewClass2 object2; cout << endl << endl << "Initialized object2: " << endl; object2.setNewClass2('A','B',5,10.5f,6); object2.showNewClass(); cout << "and the real new character is " << object2.getnewChar(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id PublicInheritance.CPP // author don voils // date written 10/25/2006 // // program description This program illustrates a simple derived // class: NewClass derived from the base class: // BaseClass. The extension is a public derivation. // #include<iostream> using namespace std; class BaseClass { private: int theInteger; float theFloat; protected: int theInteger2; public: void setPrivate(int i, float f) { theInteger=i; theFloat=f; } void setProtected(int j) { theInteger2=j; } int getPrivateInteger() { return theInteger;} float getPrivateFloat() { return theFloat;} int getProtected() { return theInteger2;} }; // and all of the public members // of the base act like public members of new_stuff class NewClass : public BaseClass { // Since this is a public extension, all protected members of the // base class act like protected members of NewClass Therefore // they are accessible within NewClass by public members. Since // they act like protected members within NewClass, they are // accessible by any class derived from NewClass through the // public members of NewClass. The public members of the base // class act like public members of NewClass. Since they act like // public members within NewClass, they are accessible by any // object of NewClass. In addition they can be accessed by an // extension of NewClass like any public member of NewClass. // private: char theChar; public: void setNewClass(char c, int i, float f, int j) { setPrivate(i, f); // // Can access the base class protected member j // in the following manner // theInteger2 = j; // // Or could have used by this public function instead // // get_protected_stuff(int j); // theChar = c; } void showNewClass() { cout << "theInteger = " << getPrivateInteger() << endl << "theFloat = " << getPrivateFloat() << endl << "theInteger2 = " << getProtected() << endl << "theChar = " << theChar << endl; } }; void main() { NewClass object1; object1.setNewClass('a',1,10.1f,2); // // The following lines are non-accessible during a private extension // // object1.theInteger = 23; // // This line is trying to access a base class // private member. // // object1.theInteger2 = 55; // // This line is trying to access a base class // protected member. // cout << "theInteger = " << object1.getPrivateInteger() << endl << endl; // This line can access a base class public member. object1.showNewClass(); char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program-id testNewDates.cpp // written_by don voils // date_written 5/20/2006 // description This program tests the class newDate. // #include<iostream> using namespace std; #include "newDate.h" void main() { newDate invoiceDate; newDate dueDate; newDate paidDate; cout << "What is the invoice date? "; cin >> invoiceDate; dueDate = invoiceDate + 30; cout << endl << endl << "The invoice due date is " << dueDate << endl << endl; cout << endl << endl << "When was the invoice paid? "; cin >> paidDate; short numberDaysOpen = paidDate - invoiceDate; cout << endl << endl << "The number of days the invoice was open was " << numberDaysOpen << endl << endl; } // program_id usingDates.cpp // written_by don voils // date_written 5/20/2006 // description Contains a program that interacts with the header Date.h and // the file: Date.cpp // #include<iostream> using namespace std; #include"date.h" void main() { Date today(2,29,2006), springBreak; char slash; short theMonth, theDay, theYear; cout << "What will Spring Break begin? (format is: mm/dd/yyyy) "; cin >> theMonth >> slash >> theDay >> slash >> theYear; today.showMonthName(); cout << endl << endl << "Did you say that spring break is: " << springBreak.getMonth() << "/" << springBreak.getDay() << "/" << springBreak.getYear() << endl; springBreak.setDay(theDay); springBreak.setMonth(theMonth); springBreak.setYear(theYear); cout << endl << endl << "Or did you say that spring break will be: " << springBreak.getMonth() << "/" << springBreak.getDay() << "/" << springBreak.getYear() << endl; cout << endl << endl << "Continue? "; cin >> slash; }

Категории