Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
229 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <string> | ||
|
||
/** Represent an individuals bank account **/ | ||
class BankAccount | ||
{ | ||
private: | ||
float balance; | ||
|
||
public: | ||
static int number; // see the end of this file for the initialisation of number | ||
std::string name; | ||
|
||
/** Takes account name as input **/ | ||
BankAccount( std::string name ) | ||
{ | ||
this->name = name; | ||
balance = 0.f; | ||
number = number +1; | ||
} | ||
|
||
float get_balance() | ||
{ | ||
return balance; | ||
} | ||
|
||
void deposit( float amount ) | ||
{ | ||
balance += amount; | ||
} | ||
|
||
void withdraw( float amount ) | ||
{ | ||
if( amount <= balance ) | ||
balance -= amount; | ||
} | ||
|
||
/*def transfer(self, other, amount): | ||
if amount <= self.__balance: | ||
self.__balance -= amount | ||
other.__balance += amount*/ | ||
}; | ||
|
||
int BankAccount::number = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
|
||
/** Represnt a fraction **/ | ||
class Fraction | ||
{ | ||
public: | ||
int n, d; | ||
|
||
Fraction( int n, int d ) | ||
{ | ||
this->n = n; | ||
this->d = d; | ||
} | ||
|
||
Fraction operator+( Fraction other ) | ||
{ | ||
int n = ( this->n * other.d ) + ( other.n * this->d ); | ||
int d = this->d * other.d; | ||
|
||
return Fraction( n, d ); | ||
} | ||
|
||
friend std::ostream& operator<<( std::ostream &out, Fraction &other ); | ||
}; | ||
|
||
/** How should Fraction be printed to the screen? **/ | ||
std::ostream& operator<<( std::ostream &out, Fraction &frac ) | ||
{ | ||
out << frac.n << "/" << frac.d; | ||
return out; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "bankaccount.h" | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
BankAccount a( "Bob" ); | ||
|
||
std::cout << a.number << std::endl; | ||
|
||
BankAccount b( "Bankyaccount McBankaccountface" ); | ||
|
||
/* static class attributes can be accessed either through an object | ||
i.e. b.number | ||
or through the class itself | ||
i.e. BankAccount::number */ | ||
std::cout << b.number << std::endl; | ||
std::cout << BankAccount::number << std::endl; | ||
|
||
a.deposit( 100 ); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <iostream> | ||
#include "fraction.h" | ||
|
||
int main() | ||
{ | ||
Fraction f1( 1, 2 ); | ||
Fraction f2( 5, 6 ); | ||
|
||
Fraction f3 = f1 + f2; | ||
|
||
std::cout << f3 << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
#include "pointarithmetic.h" | ||
|
||
int main() | ||
{ | ||
Point pt1( 1, 2 ); | ||
Point pt2( 3, 3 ); | ||
Point pt3 = pt1 + pt2; | ||
|
||
std::cout << pt3 << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
#include "rectangle.h" | ||
|
||
int main() | ||
{ | ||
Rectangle r1( 3, 4 ); | ||
std::cout << r1.perimeter() << ", " << r1.area() << std::endl; | ||
|
||
r1.scale( 3 ); | ||
std::cout << r1.perimeter() << ", " << r1.area() << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <iostream> | ||
|
||
/** Class for points in the (x,y) plane **/ | ||
class Point | ||
{ | ||
public: | ||
int x, y; | ||
|
||
Point( int x, int y ) | ||
{ | ||
this->x = x; | ||
this->y = y; | ||
} | ||
|
||
Point operator+( Point other ) const | ||
{ | ||
int newX = x + other.x; | ||
int newY = y + other.y; | ||
return Point( newX, newY ); | ||
} | ||
|
||
friend std::ostream& operator<<( std::ostream &out, Point &other ); | ||
}; | ||
|
||
/** How should Point be printed to the screen? **/ | ||
std::ostream& operator<<( std::ostream &out, Point &point ) | ||
{ | ||
out << "(" << point.x << ", " << point.y << ")"; | ||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <exception> | ||
|
||
/** Class to store rectangles. | ||
Every rectangle has two numbers which define the side lengths. | ||
We provide methods to calculate various properties. **/ | ||
class Rectangle | ||
{ | ||
private: | ||
float side1, side2; | ||
|
||
public: | ||
Rectangle( float x, float y ) | ||
{ | ||
if( x<=0 || y<=0 ) | ||
throw std::range_error( "Sides must be positive values" ); | ||
|
||
side1 = x; | ||
side2 = y; | ||
} | ||
|
||
float area() const | ||
{ | ||
return side1 * side2; | ||
} | ||
|
||
float perimeter() const | ||
{ | ||
return 2*side1 + 2*side2; | ||
} | ||
|
||
void scale( float scaleFactor ) | ||
{ | ||
if( scaleFactor <= 0 ) | ||
throw std::range_error( "Scale Factor must be positive" ); | ||
|
||
side1 = side1 * scaleFactor; | ||
side2 = side2 * scaleFactor; | ||
} | ||
}; |