Commit f4d0daf8 authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Tree - inorder/preorder mix up

parent 6f171a2a
......@@ -1696,17 +1696,17 @@ class Cat
{
catCount++;
}
Cat( string name )
{
catCount++;
m_name = name;
}
static int GetCount()
{
return catCount;
}
static int GetCount()
{
return catCount;
}
private:
string m_name;
......
......@@ -340,7 +340,7 @@ types of programs:
\begin{lstlisting}[style=code]
struct CoordinatePair
{
float x, y;
float x, y;
};
\end{lstlisting}
......@@ -349,26 +349,26 @@ float x, y;
\begin{lstlisting}[style=code]
struct Fraction
{
// Member variables
float numerator, denominator;
// Member variables
float numerator, denominator;
// Member functions (method)
void Display()
{
cout << numerator << "/" << denominator;
}
// Member functions (method)
void Display()
{
cout << numerator << "/" << denominator;
}
void Multiply( Fraction otherFraction )
{
numerator *= otherFraction.numerator;
denominator *= otherFraction.denominator;
}
void Multiply( Fraction otherFraction )
{
numerator *= otherFraction.numerator;
denominator *= otherFraction.denominator;
}
void Divide( Fraction otherFraction )
{
numerator *= otherFraction.denominator;
denominator *= otherFraction.numerator;
}
void Divide( Fraction otherFraction )
{
numerator *= otherFraction.denominator;
denominator *= otherFraction.numerator;
}
};
\end{lstlisting}
......@@ -410,6 +410,7 @@ for combining a small amount of member variables together under one name,
usually used for structures for math (coordinates, rectangles, etc.)
but more sophisticated objects ought to be created with a \textbf{class}.
\newpage
\section{Classes}
Traditionally, a \textbf{struct} is used to create small objects
......@@ -426,12 +427,12 @@ but more sophisticated objects ought to be created with a \textbf{class}.
\begin{lstlisting}[style=code]
class Player
{
public:
void SetPosition( int newX, int newY );
void Move();
public:
void SetPosition( int newX, int newY );
void Move();
private:
int x, y;
private:
int x, y;
};
\end{lstlisting}
......@@ -469,11 +470,11 @@ int x, y;
class Rectangle
{
public:
void SetPosition( int newX, int newY );
void SetDimensions( int newWidth, int newHeight );
private:
int x, int y, int width, int height;
public:
void SetPosition( int newX, int newY );
void SetDimensions( int newWidth, int newHeight );
private:
int x, int y, int width, int height;
};
#endif
......@@ -504,15 +505,15 @@ int x, int y, int width, int height;
void Rectangle::SetPosition( int newX, int newY )
{
x = newX;
y = newY;
x = newX;
y = newY;
}
void Rectangle::SetDimensions( int newWidth,
int newHeight )
{
width = newWidth;
height = newHeight;
width = newWidth;
height = newHeight;
}
\end{lstlisting}
......@@ -556,21 +557,21 @@ height = newHeight;
class Rectangle
{
public:
void SetPosition( int newX, int newY )
{
x = newX;
y = newY;
}
public:
void SetPosition( int newX, int newY )
{
x = newX;
y = newY;
}
void SetDimensions( int newWidth, int newHeight )
{
width = newWidth;
height = newHeight;
}
void SetDimensions( int newWidth, int newHeight )
{
width = newWidth;
height = newHeight;
}
private:
int x, int y, int width, int height;
private:
int x, int y, int width, int height;
};
#endif
......@@ -624,10 +625,10 @@ int x, int y, int width, int height;
\begin{lstlisting}[style=code]
void Rectangle::Setup( int newX, int newY, int newWidth, int newHeight )
{
x = newX;
y = newY;
width = newWidth;
height = newHeight;
x = newX;
y = newY;
width = newWidth;
height = newHeight;
}
\end{lstlisting}
......@@ -636,8 +637,8 @@ height = newHeight;
\begin{lstlisting}[style=code]
void Rectangle::SetPosition( int newX, int newY )
{
x = newX;
y = newY;
x = newX;
y = newY;
}
\end{lstlisting}
......@@ -650,7 +651,7 @@ y = newY;
\begin{lstlisting}[style=code]
void Rectangle::SetX( int newX )
{
x = newX;
x = newX;
}
\end{lstlisting}
......@@ -662,10 +663,10 @@ x = newX;
\begin{lstlisting}[style=code]
void Rectangle::SetX( int newX )
{
if ( newX >= 0 )
{
x = newX;
}
if ( newX >= 0 )
{
x = newX;
}
}
\end{lstlisting}
......@@ -683,7 +684,7 @@ if ( newX >= 0 )
\begin{lstlisting}[style=code]
int Rectangle::GetX()
{
return x;
return x;
}
\end{lstlisting}
......@@ -696,7 +697,7 @@ return x;
\begin{lstlisting}[style=code]
int Student::GetFullName()
{
return lastName + ", " + firstName;
return lastName + ", " + firstName;
}
\end{lstlisting}
......@@ -755,19 +756,19 @@ return lastName + ", " + firstName;
\begin{lstlisting}[style=code]
class Logger
{
public:
// constructors
Logger( string filename );
Logger();
public:
// constructors
Logger( string filename );
Logger();
// destructor
~Logger();
// destructor
~Logger();
// other method
void Write( string text );
// other method
void Write( string text );
private:
ofstream m_output;
private:
ofstream m_output;
};
\end{lstlisting}
......@@ -786,12 +787,12 @@ ofstream m_output;
\begin{lstlisting}[style=code]
Logger::Logger( string filename )
{
m_output.open( filename );
m_output.open( filename );
}
Logger::Logger()
{
m_output.open( "log.txt" );
m_output.open( "log.txt" );
}
\end{lstlisting}
......@@ -801,7 +802,7 @@ m_output.open( "log.txt" );
\begin{lstlisting}[style=code]
void Logger::Write( string text )
{
m_output << text << endl;
m_output << text << endl;
}
\end{lstlisting}
......@@ -809,7 +810,7 @@ m_output << text << endl;
\begin{lstlisting}[style=code]
Logger::~Logger()
{
m_output.close();
m_output.close();
}
\end{lstlisting}
......@@ -823,18 +824,18 @@ m_output.close();
int main()
{
// At this point,
// a constructor is called automatically
Logger log( "logfile.txt" );
// Writing to the text file
log.Write( "Hello!" );
log.Write( "How are you?" );
// Program ends here. The destructor
// is called automatically when the log variable
// goes out of scope and is destroyed.
return 0;
// At this point,
// a constructor is called automatically
Logger log( "logfile.txt" );
// Writing to the text file
log.Write( "Hello!" );
log.Write( "How are you?" );
// Program ends here. The destructor
// is called automatically when the log variable
// goes out of scope and is destroyed.
return 0;
}
\end{lstlisting}
......@@ -864,7 +865,7 @@ MyClass() // Default constructor
}
private:
int m_value;
int m_value;
};
\end{lstlisting}
~\\
......@@ -884,16 +885,16 @@ MyClass classVar;
\begin{lstlisting}[style=code]
class MyClass
{
public:
MyClass() { // Default constructor
m_value = 0;
}
public:
MyClass() { // Default constructor
m_value = 0;
}
MyClass( int value ) { // Parameterized constructor
m_value = value;
}
MyClass( int value ) { // Parameterized constructor
m_value = value;
}
// etc
// etc
};
\end{lstlisting}
......@@ -927,24 +928,24 @@ MyClass classVar( 100 );
\begin{lstlisting}[style=code]
class MyClass
{
public:
MyClass() // Default constructor
{
m_value = 0;
}
public:
MyClass() // Default constructor
{
m_value = 0;
}
MyClass( int value ) // Parameterized constructor
{
m_value = value;
}
MyClass( int value ) // Parameterized constructor
{
m_value = value;
}
MyClass( const MyClass& other ) // Copy constructor
{
m_value = other.m_value;
}
MyClass( const MyClass& other ) // Copy constructor
{
m_value = other.m_value;
}
private:
int m_value;
private:
int m_value;
};
\end{lstlisting}
~\\
......@@ -1073,18 +1074,18 @@ using namespace std;
class Room
{
public:
void Setup();
string GetName();
int GetWidth();
int GetLength();
int GetArea();
private:
string name;
int width;
int length;
public:
void Setup();
string GetName();
int GetWidth();
int GetLength();
int GetArea();
private:
string name;
int width;
int length;
};
#endif
......@@ -1108,34 +1109,34 @@ using namespace std;
void Room::Setup()
{
cout << "Enter room name: ";
cin >> name;
cout << "Enter room name: ";
cin >> name;
cout << "Enter width: ";
cin >> width;
cout << "Enter width: ";
cin >> width;
cout << "Enter length: ";
cin >> length;
cout << "Enter length: ";
cin >> length;
}
string Room::GetName()
{
return name;
return name;
}
int Room::GetWidth()
{
return width;
return width;
}
int Room::GetLength()
{
return length;
return length;
}
int Room::GetArea()
{
return width * length;
return width * length;
}
\end{lstlisting}
......@@ -1152,15 +1153,15 @@ const int MAX_ROOMS = 10;
class House
{
public:
House();
~House();
public:
House();
~House();
void AddRoom();
void AddRoom();
private:
Room rooms[MAX_ROOMS];
int roomCount;
private:
Room rooms[MAX_ROOMS];
int roomCount;
};
#endif
......@@ -1186,36 +1187,36 @@ using namespace std;
House::House()
{
roomCount = 0;
roomCount = 0;
}
House::~House()
{
ofstream output( "house.txt" );
for ( int i = 0; i < roomCount; i++ )
{
output << rooms[i].GetName() << "\t"
<< rooms[i].GetWidth() << "x"
<< rooms[i].GetLength() << "\t"
<< "(" << rooms[i].GetArea() << " sqft)"
<< endl;
}
output.close();
ofstream output( "house.txt" );
for ( int i = 0; i < roomCount; i++ )
{
output << rooms[i].GetName() << "\t"
<< rooms[i].GetWidth() << "x"
<< rooms[i].GetLength() << "\t"
<< "(" << rooms[i].GetArea() << " sqft)"
<< endl;
}
output.close();
cout << "Outputted to house.txt" << endl;
cout << "Outputted to house.txt" << endl;
}
void House::AddRoom()
{
if ( roomCount == MAX_ROOMS )
{
cout << "House is full!" << endl
<< "Cannot fit any more rooms!" << endl;
return; // exit this function
}
if ( roomCount == MAX_ROOMS )
{
cout << "House is full!" << endl
<< "Cannot fit any more rooms!" << endl;
return; // exit this function
}
rooms[ roomCount ].Setup();
roomCount++;
rooms[ roomCount ].Setup();
roomCount++;
}
\end{lstlisting}
......@@ -1230,28 +1231,28 @@ using namespace std;
int main()
{
House myHouse;
House myHouse;
cout << "HOUSE BUILDER" << endl;
bool done = false;
while ( !done )
{
cout << "(A)dd new room or (Q)uit: ";
char choice;
cin >> choice;
if ( toupper(choice) == 'A' )
cout << "HOUSE BUILDER" << endl;
bool done = false;
while ( !done )
{
myHouse.AddRoom();
cout << "(A)dd new room or (Q)uit: ";
char choice;
cin >> choice;
if ( toupper(choice) == 'A' )
{
myHouse.AddRoom();
}
else if ( toupper(choice) == 'Q' )
{
done = true;
}
cout << endl;
}
else if ( toupper(choice) == 'Q' )
{
done = true;
}
cout << endl;
}
return 0;
return 0;
}
\end{lstlisting}