Skip to content
Permalink
Browse files
added cpp
  • Loading branch information
David committed Oct 19, 2018
1 parent 89cfde5 commit 51bcc176e6f7f3fd5d3920b118763b9507c0d40b
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 7 deletions.
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 14)



project(cppintro CXX)


@@ -32,3 +34,24 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)

#add_executable( age lab_age.cpp )

#file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/our_*.cpp")
#foreach(filename ${files})
# get_filename_component(EXEC ${filename} NAME_WE)
# string(REPLACE "lib_" "" LIBRARY ${EXEC})

# add_library(${LIBRARY} STATIC ${filename})
# install(TARGETS ${LIBRARY} DESTINATION /lib)
# install(FILES ${EXEC}.h DESTINATION /include)
#endforeach()

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/pre_*.cpp")
foreach(filename ${files})
get_filename_component(EXEC ${filename} NAME_WE)
add_executable(${EXEC} ${filename} ${HEADERS})
endforeach()

file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/lab_*.cpp")
foreach(filename ${files})
get_filename_component(EXEC ${filename} NAME_WE)
add_executable(${EXEC} ${filename} ${HEADERS})
endforeach()
@@ -31,12 +31,5 @@ foreach(filename ${files})
# say that this executable is a test so that ctest will run it
add_test(NAME ${THIS_TEST}
COMMAND ${TEST_RUNNER} )
#set_target_properties(${TEST_RUNNER} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TEST_DIR})

#add_custom_target(${THIS_TEST} ALL
# COMMAND bin/${TEST_RUNNER}
# WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
# COMMENT "Run bin/${THIS_TEST}"
# DEPENDS ${TEST_RUNNER} Catch Capio)
endforeach()

@@ -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;
@@ -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;
}

@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -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;
}
};

0 comments on commit 51bcc17

Please sign in to comment.