Difference: CodeClass (1 vs. 5)

Revision 52018-03-22 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Classes

Tic Tac Toe

Line: 11 to 11
 bool notValid(int r, int c); bool inUse(int r, int c, char board[3][3]); bool Win(char board[3][3]);
Added:
>
>
bool checkRowWin(char board[3][3], char &p); bool checkColWin(char board[3][3], char &p); bool checkDiaWin(char board[3][3], char &p);
  int main() { char player = 'X';
Line: 28 to 32
 }

void displayBoard(char board[3][3]) {

Changed:
<
<
for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { cout << board[row][col]; if (col < 2) { cout << "|";
>
>
for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { cout << board[row][col]; if (col < 2) { cout << "|";
  } }
Changed:
<
<
cout << endl; if (row < 2) { cout << "-----" << endl;
>
>
cout << endl; if (row < 2) { cout << "-----" << endl;
  } } }
Line: 45 to 49
 void nextMove(char player, char board[][3]) { int row, col; do {
Changed:
<
<
cout << "Player " << player << " what is your move (row column):"; cin >> row >> col;
>
>
cout << "Player " << player << " what is your move (row column):"; cin >> row >> col;
  } while (notValid(row, col)||inUse(row,col,board)); board[row - 1][col - 1] = player; }

bool notValid(int r, int c){

Changed:
<
<
bool result = r>3 || r3 || c<1;
>
>
bool result = r>3 || r<1 || c>3 || c<1;
  if (result) {
Changed:
<
<
cout << "Error, row and colum must be between 1 and 3!" << endl;
>
>
cout << "Error, row and colum must be between 1 and 3!" << endl;
  } return result; }
Line: 62 to 66
 bool inUse(int r, int c, char board[3][3]){ bool result = (board[r-1][c-1]!=' '); if (result) {
Changed:
<
<
cout << "Error, space in use!" << endl;
>
>
cout << "Error, space in use!" << endl;
  } return result; }
Deleted:
<
<
// Finish this function ... It may call other functions
 bool Win(char board[3][3]) {
Added:
>
>
char p; if (checkRowWin(board, p) || checkColWin(board, p) || checkDiaWin(board, p)) { cout << p << " Wins!!!" << endl; return true; } return false; }

bool checkRowWin(char board[3][3], char &p) { for(int row=0; row<3; row++) { if (board[row][0]!=' ') { if (board[row][0]==board[row][1] && board[row][1]==board[row][2]) { p = board[row][0]; return true; } } } return false; }

bool checkColWin(char board[3][3], char &p) { for(int col=0; col<3; col++) { if (board[0][col]!=' ') { if (board[0][col]==board[1][col] && board[1][col]==board[2][col]) { p = board[0][col]; return true; } } } return false; }

bool checkDiaWin(char board[3][3], char &p) { if (board[1][1]!=' ') { if (board[0][0]==board[1][1] && board[1][1]==board[2][2]) { p=board[0][0]; return true; } if (board[0][2]==board[1][1] && board[1][1]==board[2][0]) { p = board[0][0]; return true; } }

  return false; } %ENDCODE%</sticky>
Line: 120 to 171
  polls = pollNum; } void Candidate :: print(){
Changed:
<
<
cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl;
>
>
cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl;
 }

int main() {

Changed:
<
<
Candidate h("Bernie Sanders","Democrat",46); Candidate j("Trump","Republican",42); Candidate g("Gary Johnson","Libertarian",8);
>
>
Candidate s("Bernie Sanders","Democrat",46); Candidate t("Donald Trump","Republican",42); Candidate j("Gary Johnson","Libertarian",8);
 
Changed:
<
<
h.print();
>
>
s.print(); t.print();
  j.print();
Deleted:
<
<
g.print(); cout << endl; h.setPolls(12); j.setPolls(11); g.setPolls(77);
 
Changed:
<
<
h.print();
>
>
cout << endl; s.setPolls(12); t.setPolls(11); j.setPolls(77);

s.print(); t.print();

  j.print();
Deleted:
<
<
g.print();
  }

Revision 42018-03-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Classes

Added:
>
>

Tic Tac Toe

<-- SyntaxHighlightingPlugin -->
#include <iostream>
using namespace std;
void displayBoard(char board[3][3]);
void nextMove(char player, char board[][3]);
bool notValid(int r, int c);
bool inUse(int r, int c, char board[3][3]);
bool Win(char board[3][3]);

int main() {
    char player = 'X';
    char aBoard[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    while (!Win(aBoard)) {
        nextMove(player, aBoard);
        displayBoard(aBoard);
        if (player == 'X') {
            player = 'O';
        } else {
            player = 'X';
        }
    }
    return 0;
}

void displayBoard(char board[3][3]) {
    for (int row = 0; row &lt; 3; row++) {
        for (int col = 0; col &lt; 3; col++) {
            cout &lt;&lt; board[row][col];
            if (col &lt; 2) {
                cout &lt;&lt; "|";
            }
        }
        cout &lt;&lt; endl;
        if (row &lt; 2) {
            cout &lt;&lt; "-----" &lt;&lt; endl;
        }
    }
}

void nextMove(char player, char board[][3]) {
    int row, col;
    do {
        cout &lt;&lt; "Player " &lt;&lt; player &lt;&lt; " what is your move (row column):";
        cin &gt;&gt; row &gt;&gt; col;
    } while (notValid(row, col)||inUse(row,col,board));
    board[row - 1][col - 1] = player;
}

bool notValid(int r, int c){
    bool result = r&gt;3 || r3 || c&lt;1;
    if (result) {
        cout &lt;&lt; "Error, row and colum must be between 1 and 3!" &lt;&lt; endl;
    }
    return result;
}

bool inUse(int r, int c, char board[3][3]){
    bool result = (board[r-1][c-1]!=' ');
    if (result) {
        cout &lt;&lt; "Error, space in use!" &lt;&lt; endl;
    }
    return result;
}

// Finish this function ... It may call other functions
bool Win(char board[3][3]) {
    return false;
}
 
<-- end SyntaxHighlightingPlugin -->
</sticky>
 

Candidate Class

Added:
>
>
 %CODE{"c++"}% #include using namespace std;
Line: 48 to 120
  polls = pollNum; } void Candidate :: print(){
Changed:
<
<
cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl;
>
>
cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl;
 }

int main() {

Line: 61 to 133
  h.print(); j.print(); g.print();
Changed:
<
<
cout << endl;
>
>
cout << endl;
  h.setPolls(12); j.setPolls(11); g.setPolls(77);
Line: 72 to 144
  }
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%</sticky>
 

Class with a Member Function

%CODE{"c++"}%

Revision 32018-03-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Classes

Added:
>
>

Candidate Class

%CODE{"c++"}% #include using namespace std;

class Candidate { private: string name; string party; int polls; public: Candidate(); Candidate(string aName, string aParty, int aPoll); void setPolls(int polls);

string getName(); string getParty(); int getPolls(); void print(); };

//#include "candidate.h" #include using namespace std;

Candidate :: Candidate() { name = ""; party = ""; polls = 0; } Candidate :: Candidate(string aName, string aParty, int aPoll) { name = aName; party = aParty; polls = aPoll; }

string Candidate :: getName() { return name; } int Candidate :: getPolls() { return polls; } void Candidate :: setPolls(int pollNum) { polls = pollNum; } void Candidate :: print(){ cout << "Name : " << name << ", "; cout << "Party : " << party << ", "; cout << "Polls : " << polls << "%" << endl; }

int main() { Candidate h("Bernie Sanders","Democrat",46); Candidate j("Trump","Republican",42); Candidate g("Gary Johnson","Libertarian",8);

h.print(); j.print(); g.print(); cout << endl; h.setPolls(12); j.setPolls(11); g.setPolls(77);

h.print(); j.print(); g.print();

}

 
Added:
>
>
%ENDCODE%
 

Class with a Member Function

%CODE{"c++"}%

Revision 22016-11-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Classes

Revision 12015-11-01 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Classes

Class with a Member Function

<-- SyntaxHighlightingPlugin -->
//DISPLAY 10.3 Class with a Member Function 
//Program to demonstrate a very simple example of a class. 
//A better version of the class DayOfYear will be given in Display 10.4.
#include <iostream>
using namespace std;

class DayOfYear
{
public:
    void output( );
    int month;
    int day;
};

int main( )
{
    DayOfYear today, birthday;

    cout << "Enter today's date:\n";
    cout << "Enter month as a number: ";
    cin >> today.month;
    cout << "Enter the day of the month: ";
    cin >> today.day;
    cout << "Enter your birthday:\n";
    cout << "Enter month as a number: ";
    cin >> birthday.month;
    cout << "Enter the day of the month: ";
    cin >> birthday.day;

    cout << "Today's date is ";
    today.output( );
    cout << "Your birthday is ";
    birthday.output( );

    if (today.month = = birthday.month
        && today.day = = birthday.day)
        cout << "Happy Birthday!\n";
    else
        cout << "Happy Unbirthday!\n";

    return 0;
}
//Uses iostream:
void DayOfYear::output( )
{
    cout << "month = " << month
         << ", day = " << day << endl;
}


 
<-- end SyntaxHighlightingPlugin -->

<-- SyntaxHighlightingPlugin -->
//DISPLAY 10.4 Class with Private Members 
//Program to demonstrate the class DayOfYear.
#include <iostream>
using namespace std;

class DayOfYear
{
public:
    void input( );
    void output( );

    void set(int new_month, int new_day);
    //Precondition: new_month and new_day form a possible date.
    //Postcondition: The date is reset according to the arguments.

    int get_month( );
    //Returns the month, 1 for January, 2 for February, etc.

    int get_day( );
    //Returns the day of the month.
private:
    void check_date( );
    int month;
    int day;
};

int main( )
{
    DayOfYear today, bach_birthday;
    cout << "Enter today's date:\n";
    today.input( );
    cout << "Today's date is ";
    today.output( );

    bach_birthday.set(3, 21);
    cout << "J. S. Bach's birthday is ";
    bach_birthday.output( );

    if ( today.get_month( ) == bach_birthday.get_month( ) &&
               today.get_day( ) == bach_birthday.get_day( ) )
        cout << "Happy Birthday Johann Sebastian!\n";
    else
        cout << "Happy Unbirthday Johann Sebastian!\n";
        
    system("pause");
    return 0;
}
//Uses iostream:
void DayOfYear::input( )
{
    cout << "Enter the month as a number: ";


cin >> month;
    cout << "Enter the day of the month: ";
    cin >> day;
    check_date( );
}

void DayOfYear::output( )
{
   cout<<"month = "<<month<<",  day = "<<day<<endl;
            
} 
  
  

void DayOfYear::set(int new_month, int new_day)
{
    month = new_month;
    day = new_day;
    check_date();
}

void DayOfYear::check_date( )
{
    if ((month < 1) || (month > 12) || (day < 1) || (day > 31))
    {
        cout << "Illegal date. Aborting program.\n";
        system("pause");
        exit(1);
    }
}

int DayOfYear::get_month( )
{
    return month;
}

int DayOfYear::get_day( )
{
    return day;
}


 
<-- end SyntaxHighlightingPlugin -->

Class with Private Members

<-- SyntaxHighlightingPlugin -->
//DISPLAY 10.4 Class with Private Members 
//Program to demonstrate the class DayOfYear.
#include <iostream>
using namespace std;

class DayOfYear
{
public:
    void input( );
    void output( );

    void set(int new_month, int new_day);
    //Precondition: new_month and new_day form a possible date.
    //Postcondition: The date is reset according to the arguments.

    int get_month( );
    //Returns the month, 1 for January, 2 for February, etc.

    int get_day( );
    //Returns the day of the month.
private:
    void check_date( );
    int month;
    int day;
};

int main( )
{
    DayOfYear today, bach_birthday;
    cout << "Enter today's date:\n";
    today.input( );
    cout << "Today's date is ";
    today.output( );

    bach_birthday.set(3, 21);
    cout << "J. S. Bach's birthday is ";
    bach_birthday.output( );

    if ( today.get_month( ) == bach_birthday.get_month( ) &&
               today.get_day( ) == bach_birthday.get_day( ) )
        cout << "Happy Birthday Johann Sebastian!\n";
    else
        cout << "Happy Unbirthday Johann Sebastian!\n";
        
    system("pause");
    return 0;
}
//Uses iostream:
void DayOfYear::input( )
{
    cout << "Enter the month as a number: ";


cin >> month;
    cout << "Enter the day of the month: ";
    cin >> day;
    check_date( );
}

void DayOfYear::output( )
{
   cout<<"month = "<<month<<",  day = "<<day<<endl;
            
} 
  
  

void DayOfYear::set(int new_month, int new_day)
{
    month = new_month;
    day = new_day;
    check_date();
}

void DayOfYear::check_date( )
{
    if ((month < 1) || (month > 12) || (day < 1) || (day > 31))
    {
        cout << "Illegal date. Aborting program.\n";
        system("pause");
        exit(1);
    }
}

int DayOfYear::get_month( )
{
    return month;
}

int DayOfYear::get_day( )
{
    return day;
}


 
<-- end SyntaxHighlightingPlugin -->

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

META TOPICMOVED by="JimSkon" date="1446397208" from="KenyonCpp.CodeStructClass" to="KenyonCpp.CodeClass"
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback