Help Please this assignment is due by Sunday 7/20/2014
We will use one source code file to learn how to program object abstractions (commonly referred to as object-oriented classes). We will see how encapsulation, object abstractions, and information hiding are distinct and very significant concepts in C/C++. This should clarify the misunderstandings that result from using object-oriented terms that are appropriate specifically for Object-Oriented Analysis, versus Object-Oriented Design, and Object-Oriented Implementation in C++ .
Please, pay close attention to the threaded discussion posts as they provide key definitions necessary for successfully accomplishing the labs in both coming weeks.
Lab for Week #2
Concerning encapsulation:
Best Definition for OO Design phase:
Encapsulation is the packaging into a single unit highly related states and methods of an entity.
Best definition for implementation phase:
An encapsulation is a proscriptive boundary in the source code such that any interaction across the boundary is allowed by the compiler only if performed in accordance with a formally defined interface. (COMP220 class definition)
The act of proscription is implemented by the compiler's refusal to emit code that access members inside the encapsulation. Instead, the compiler issues a diagnostic: is private or it is not accessible
The formal interface is the class definition. A class definition always creates a collective type handle known as the name of an OO class. The C++ keywords used to define OO classes are class and struct. Because, classes may be nested,
class1::class2::class3 is a perfectly valid collective type handle.
WARNING: There are special rules for using handles with the same name but of different kind. For example, 1) the name of a constructor is also the name of the class, 2) an object (Object Handle) named identically to a class name (collective type handle) hides the class (never can be instantiated again) for the rest of the program. This is an excellent security feature of C++.
Lab Procedure Week #2
1) Download the source code file named EncapInherit.cpp.
2) As appropriate prepare files required from your development environment.
3) Compile the file and execute it.
4) Study the source code
5) Create a backup copy of the file EncapInherit.cpp.
6) Using the operating system, find out the size of the executable file created by compiling the source code file EncapInherit.cpp.
7) Change all member access specifiers from private or protected to public. Do not change the inheritance access specifiers.
8) (If in an IDE clean the project) recompile and link the program.
9) Repeat step 6 and describe any size changes.
*/
#include "iostream"
using namespace std;
class WritingInstruments
{
protected:
unsigned int RemainingWritingSubstance; // measured in # of chars
public:
explicit WritingInstruments // Do not allow for conversion purposes
( unsigned int AmountOfWritingSubstance = 5 )
{ RemainingWritingSubstance = AmountOfWritingSubstance; }
bool Write( unsigned int NumberOfCharacters2Write )
{
bool Rtn = true;
if( NumberOfCharacters2Write <= RemainingWritingSubstance )
RemainingWritingSubstance -= NumberOfCharacters2Write;
else
{
Rtn = false;
RemainingWritingSubstance = 0;
}
return Rtn;
}
unsigned int LookAtIt() { return RemainingWritingSubstance; }
};
class Pens : public WritingInstruments
{
bool CapIsOn;
public:
explicit Pens(unsigned int Ink = 5, bool CapPassed = true) : WritingInstruments(Ink)
{ CapIsOn = CapPassed;
}
bool Write( unsigned int NumberOfCharacters2Write )
{
if(CapIsOn) return false;
return WritingInstruments::Write( NumberOfCharacters2Write );
// Notice that here we fail to distinguish a failure to write because
// the cap is on from a failure to write because the pen ran out of ink
// Clearly, despite our efforts this inheritance does not meet Liskov's
// sustitution principle. It is possible to meet Liskov if all Pens
// objects are created with the cap off.
}
void LookAtIt() // I violate the encapsulation here
{
cout << "The cap is " << (CapIsOn ? " on " : " off ") << endl;
cout << "and there is enough ink left to write "
<< RemainingWritingSubstance << " characters" << endl;
}
void CapOn(){ CapIsOn = true; }
void CapOff(){ CapIsOn = false; }
};
class Pencils : public WritingInstruments
{
unsigned int RemainingB4Sharpening; // zero means cannot write
public:
const unsigned int CharactersPerSharpenning;
explicit Pencils(unsigned int Lead = 5U, bool TipIsSharp = false,
unsigned int ChPerSharp = 3U)
: WritingInstruments(Lead), CharactersPerSharpenning(ChPerSharp)
{
RemainingB4Sharpening = 0U;
}
bool Write( unsigned int NumberOfCharacters2Write )
{
while (
RemainingB4Sharpening-- &&
RemainingWritingSubstance-- &&
NumberOfCharacters2Write--
);
return !NumberOfCharacters2Write;
}
void Sharpen() { RemainingB4Sharpening = CharactersPerSharpenning; }
};
//------------------------- MAIN ------------------------------
int main()
{
WritingInstruments MyPen(8), YourPen;
// Pens MyPen(8), YourPen;
// MyPen.CapOff(); // Open my pen for writing
for( int i = 1; i <= 5; i++)
{
cout << "\n---------writing for the " << i << "th time ---------\n";
cout << "MyPen: ";
MyPen.LookAtIt();
cout << "\nYourPen: ";
YourPen.LookAtIt();
cout << endl;
if( MyPen.Write(2) )
cout << "\nMy pen successfully wrote 2 more charactrers" << endl;
else cout << "\nMy pen ran out of ink " << endl;
if( YourPen.Write(2) )
cout << "Your pen successfully wrote 2 more charactrers" << endl;
else cout << "Your pen ran out of ink " << endl;
}
system("pause");
return 0;
}