Difference: CodeClass2 (1 vs. 7)

Revision 72018-03-27 - JimSkon

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

Classes

Line: 187 to 187
  date4.display();

}

Added:
>
>
%ENDCODE%
 

The BankAccount Class

%CODE{"c++"}%
Line: 560 to 561
  %CODE{"c++"}%
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%

</sticky>

Revision 62017-10-19 - JimSkon

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

Classes

Line: 27 to 27
  void print(); }; %ENDCODE%
Changed:
<
<

Candidate.h

>
>

Candidate.cpp

  %CODE{"c++"}% #include
Line: 68 to 68
 

main.cpp

%CODE{"c++"}%
Added:
>
>
#include "candidate.h"
 using namespace std;

int main() {

Revision 52017-10-19 - JimSkon

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

Classes

The Candidate Class

Added:
>
>

Candidate.h

  %CODE{"c++"}% #include
Line: 25 to 26
  int getPolls(); void print(); };
Changed:
<
<
//#include "candidate.h"
>
>
%ENDCODE%

Candidate.h

%CODE{"c++"}%
 #include
Added:
>
>
#include "candidate.h"
 using namespace std;

Candidate :: Candidate() {

Line: 59 to 64
  cout << "Party : " << party << endl; cout << "Polls : " << polls << "%" << endl; }
Added:
>
>
%ENDCODE%

main.cpp

%CODE{"c++"}% using namespace std;
  int main() { Candidate h("Hillary Clinton","Democrat",46);
Line: 549 to 559
  %CODE{"c++"}%
Changed:
<
<
%ENDCODE%
>
>
%ENDCODE%
 \ No newline at end of file

Revision 42017-04-11 - JimSkon

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

Classes

Line: 78 to 78
  %ENDCODE%

The Date Class

Changed:
<
<
#include < stream.h > #include < math.h > #include < stdlib.h >
>
>
%CODE{"c++"}% #include

using namespace std;

  int max (int a, int b) {
Line: 127 to 128
  void Date::display() {
Changed:
<
<
static char *name[] = {"nothing", "January", "February", "March", "April",
>
>
static string name[] = {"nothing", "January", "February", "March", "April",
  "May", "June", "July", "August", "September", "October", "November", "December" };
Line: 161 to 162
  month = min(month, 12); }
Changed:
<
<
void main()
>
>
int main()
 { Date mydate(1, 2, 1993);
Changed:
<
<
Date date2(4,12,1994); Date date4; Date *date3; Date *date6; Date date5(1,1,1);
>
>
Date date2(4,11,2017); Date date3;
 
Changed:
<
<
date3 = new Date(45,45,44545);
>
>
Date date5(34,25,2034);
  mydate.display(); date2.display();
Changed:
<
<
date3->display();

cout << "OK: " << date3->GetMonth() << '\n'; date3->SetMonth(4); cout << "OK: " << date3->GetMonth() << '\n';

delete(date3); date6 = new Date(1,1,1);

cout << "OK: " << date3->GetMonth() << '\n'; date3->SetMonth(4); cout << "OK: " << date3->GetMonth() << '\n';

date3->display();

>
>
date3.display();
  date4.display();
Deleted:
<
<
date5.SetMonth(3); delete(date6);
 }
Deleted:
<
<
%ENDCODE%
 

The BankAccount Class

%CODE{"c++"}%

Revision 32017-04-11 - JimSkon

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

Classes

Line: 76 to 76
 }

%ENDCODE%

Added:
>
>

The Date Class

#include < stream.h > #include < math.h > #include < stdlib.h >

int max (int a, int b) { if (a>b) return(a) ; else return (b); }

int min (int a, int b) { if (a>b) return(b); else return (a); }

class Date { public: Date (); Date ( int mn, int day, int yr); // constructor void display(); // function to display date int GetMonth(); void SetMonth(int mn); ~Date(); private: int month, day, year; int DaysSoFar(); };

// constructor definition Date::Date () { month = day = year = 1; }

Date::Date (int mn, int dy, int yr) { static int length[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

month = max(1, mn); month = min(month,12);

day = max(1,dy); day = min(day, length[month]);

year = max(1, yr); }

void Date::display() { static char *name[] = {"nothing", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

cout << '\n' << name[month] << ' ' << day << "," << year << '\n'; cout << "Days so far: " << DaysSoFar() << '\n'; }

 
Added:
>
>
Date::~Date() { cout << "Thank You for using DateLine Services and have a nice date\n"; }

int Date::DaysSoFar() { int total = 0; static int length[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

for (int i=1; i < month; i++) total += length[i]; total += day; return (total); }

int Date::GetMonth() { return month; }

void Date::SetMonth(int mn) { month = max(1, mn); month = min(month, 12); }

void main() { Date mydate(1, 2, 1993); Date date2(4,12,1994); Date date4; Date *date3; Date *date6; Date date5(1,1,1);

date3 = new Date(45,45,44545);

mydate.display(); date2.display(); date3->display();

cout << "OK: " << date3->GetMonth() << '\n'; date3->SetMonth(4); cout << "OK: " << date3->GetMonth() << '\n';

delete(date3); date6 = new Date(1,1,1);

cout << "OK: " << date3->GetMonth() << '\n'; date3->SetMonth(4); cout << "OK: " << date3->GetMonth() << '\n';

date3->display();

date4.display();

date5.SetMonth(3); delete(date6); } %ENDCODE%

 

The BankAccount Class

%CODE{"c++"}%

Revision 22016-11-08 - JimSkon

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

Classes

Changed:
<
<

The BankAccount Class

>
>

The Candidate Class

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

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

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

 
Added:
>
>
//#include "candidate.h" #include using namespace std;

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

int main() { Candidate h("Hillary Clinton","Democrat",46); Candidate j("Jill Stein","Green",3);

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

h.setPolls(12); j.setPolls(52);

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

}

%ENDCODE%

The BankAccount Class

  %CODE{"c++"}% //DISPLAY 10.5 The BankAccount Class

Revision 12015-11-09 - JimSkon

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

Classes

The BankAccount Class

<-- SyntaxHighlightingPlugin -->
//DISPLAY 10.5 The BankAccount Class 
//Program to demonstrate the class BankAccount.
#include <iostream>
using namespace std;

//Class for a bank account:
class BankAccount
{
public:
    void set(int dollars, int cents, double rate);
    //Postcondition: The account balance has been set to $dollars.cents;
    //The interest rate has been set to rate percent.

    void set(int dollars, double rate);
    //Postcondition: The account balance has been set to $dollars.00.
    //The interest rate has been set to rate percent.

    void update( );
    //Postcondition: One year of simple interest has been
    //added to the account balance.

    double get_balance( );
    //Returns the current account balance.

    double get_rate( );
    //Returns the current account interest rate as a percentage.

    void output(ostream& outs);
    //Precondition: If outs is a file output stream, then
    //outs has already been connected to a file.
    //Postcondition: Account balance and interest rate have  
    //been written to the stream outs.
private:
    double balance;
    double interest_rate;

    double fraction(double percent);
    //Converts a percentage to a fraction. For example, fraction(50.3) 
    //returns 0.503.
};

int main( )
{
    BankAccount account1, account2;
    cout << "Start of Test:\n";
    account1.set(123, 99, 3.0);
    cout << "account1 initial statement:\n";
    account1.output(cout);
    account1.set(100, 5.0);



cout << "account1 with new setup:\n";
    account1.output(cout);

    account1.update( );
    cout << "account1 after update:\n";
    account1.output(cout);

    account2 = account1;
    cout << "account2:\n";
    account2.output(cout);
    return 0;
}

void BankAccount::set(int dollars, int cents, double rate)
{
    if ((dollars < 0) || (cents < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
    }

    balance = dollars + 0.01*cents;
    interest_rate = rate;
}

void BankAccount::set(int dollars, double rate)
{
    if ((dollars < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
    }

    balance = dollars;
    interest_rate = rate;
}

void BankAccount::update( )
{
    balance = balance + fraction(interest_rate)*balance;
}

double BankAccount::fraction(double percent_value)
{
    return (percent_value/100.0);
}



double BankAccount::get_balance( )
{
    return balance;
}

double BankAccount::get_rate( )
{
    return interest_rate;
}

//Uses iostream:
void BankAccount::output(ostream& outs)
{
    outs.setf(ios::fixed);
    outs.setf(ios::showpoint);
    outs.precision(2);
    outs << "Account balance $" << balance << endl;
    outs << "Interest rate " << interest_rate << "%" << endl;
}

 
<-- end SyntaxHighlightingPlugin -->

Alternative BankAccount Class Implementation

<-- SyntaxHighlightingPlugin -->
//DISPLAY 10.6 /Program to demonstrate the class BankAccount.
//Demonstrates an alternative implementation of the class BankAccount.
#include <iostream>
using namespace std;

//Class for a bank account:
class BankAccount
{
public:
    BankAccount(int dollars, int cents, double rate);
    //Initializes the account balance to $dollars.cents and
    //initializes the interest rate to rate percent.

    BankAccount(int dollars, double rate);
    //Initializes the account balance to $dollars.00 and
    //initializes the interest rate to rate percent.

    BankAccount( );
    //Initializes the account balance to $0.00 and the interest rate to 0.0%.

    void update( );
    //Postcondition: One year of simple interest has been added to the account 
    //balance.

    double get_balance( );
    //Returns the current account balance.

    double get_rate( );
    //Returns the current account interest rate as a percentage.

    void output(ostream& outs);
    //Precondition: If outs is a file output stream, then
    //outs has already been connected to a file.
    //Postcondition: Account balance and interest rate 
    //have been written to the stream outs.
private:
    double balance;
	double interest_rate;
	double fraction(double percent);
	//Converts a percentage to a fraction. For example, fraction(50.3)
	//returns 0.503.

};

int main( )
{
    BankAccount account1(100, 2.3), account2;

    cout << "account1 initialized as follows:\n";
    account1.output(cout);
    cout << "account2 initialized as follows:\n";
    account2.output(cout);

    account1 = BankAccount(999, 99, 5.5);
    cout << "account1 reset to the following:\n";
    account1.output(cout);
    return 0;
}

BankAccount::BankAccount(int dollars, int cents, double rate)
{
    if ((dollars < 0) || (cents < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
    }
 	balance = dollars + 0.01*cents;
	interest_rate = rate;
}

BankAccount::BankAccount(int dollars, double rate)
{
	if ((dollars < 0) || (rate < 0))
	{
		cout << "Illegal values for money or interest rate.\n";
	}
	balance = dollars;
	interest_rate = rate;
}


BankAccount::BankAccount( )  : balance(0), interest_rate(0.0)
{
    //Body intentionally empty.
}

//Uses iostream:
void BankAccount::output(ostream& outs)
{
    outs.setf(ios::fixed);
    outs.setf(ios::showpoint);
    outs.precision(2);
    outs << "Account balance $" << balance << endl;
    outs << "Interest rate " << interest_rate << "%" << endl;
}



 
<-- end SyntaxHighlightingPlugin -->

Alternative BankAccount Class Implementation

<-- SyntaxHighlightingPlugin -->
//DISPLAY 10.7 Alternative BankAccount Class Implementation
//Demonstrates an alternative implementation of the class BankAccount.
#include <iostream>
#include <cmath>
using namespace std;

//Class for a bank account:
class BankAccount
{
public:
    BankAccount(int dollars, int cents, double rate);
    //Initializes the account balance to $dollars.cents and
    //initializes the interest rate to rate percent.

    BankAccount(int dollars, double rate);
    //Initializes the account balance to $dollars.00 and
    //initializes the interest rate to rate percent.

    BankAccount( );
    //Initializes the account balance to $0.00 and the interest rate to 0.0%.

    void update( );
    //Postcondition: One year of simple interest has been added to the account 
    //balance.

    double get_balance( );
    //Returns the current account balance.

    double get_rate( );
    //Returns the current account interest rate as a percentage.

    void output(ostream& outs);
    //Precondition: If outs is a file output stream, then
    //outs has already been connected to a file.
    //Postcondition: Account balance and interest rate 
    //have been written to the stream outs.
private:
    int dollars_part;
    int cents_part;
    double interest_rate;//expressed as a fraction, for example, 0.057 for 5.7%

    double fraction(double percent);
    //Converts a percentage to a fraction. For example, fraction(50.3) 
    //returns 0.503.

    double percent(double fraction_value);
    //Converts a fraction to a percentage. For example, percent(0.503)
    //returns 50.3.
};

int main( )
{
    BankAccount account1(100, 2.3), account2;

    cout << "account1 initialized as follows:\n";
    account1.output(cout);
    cout << "account2 initialized as follows:\n";
    account2.output(cout);

    account1 = BankAccount(999, 99, 5.5);
    cout << "account1 reset to the following:\n";
    account1.output(cout);
    return 0;
}

BankAccount::BankAccount(int dollars, int cents, double rate)
{
    if ((dollars < 0) || (cents < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
    }
    dollars_part = dollars;
    cents_part = cents;
    interest_rate = fraction(rate);
}

BankAccount::BankAccount(int dollars, double rate)
{
    if ((dollars < 0) || (rate < 0))
    {
        cout << "Illegal values for money or interest rate.\n";
    }
    dollars_part = dollars;
    cents_part = 0;
    interest_rate = fraction(rate);
}

BankAccount::BankAccount( ) : dollars_part(0), cents_part(0), interest_rate(0.0)
{
    //Body intentionally empty.
}


double BankAccount::fraction(double percent_value)
{
    return (percent_value/100.0);
}

//Uses cmath:
void BankAccount::update( )
{
    double balance = get_balance( );
    balance = balance + interest_rate*balance;
    dollars_part = floor(balance);
    cents_part = floor((balance - dollars_part)*100);
}

double BankAccount::get_balance( )
{
    return (dollars_part + 0.01*cents_part);
}

double BankAccount::percent(double fraction_value)
{
    return (fraction_value*100);
}

double BankAccount::get_rate( )
{
    return percent(interest_rate);
}

//Uses iostream:
void BankAccount::output(ostream& outs)
{
    outs.setf(ios::fixed);
    outs.setf(ios::showpoint);
    outs.precision(2);
    outs << "Account balance $" << get_balance( ) << endl;
    outs << "Interest rate " << get_rate( ) << "%" << endl;
}

 
<-- end SyntaxHighlightingPlugin -->

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->

<-- SyntaxHighlightingPlugin -->

<-- end SyntaxHighlightingPlugin -->
 
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