Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added practice for template functions and classes #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions fraction3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Specify the minimum version of Cmake that can run this script
cmake_minimum_required(VERSION 3.19)

# Project name and version
project(fraction3 VERSION 1.0)
# Request C++ 11 standard features
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
include_directories(include)
file(GLOB SOURCES "src/*cpp")

# Compile the passed source files into an executable
add_executable(fraction3 ${SOURCES})

# Set the output director for executables
set_target_properties(fraction3 PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin/$<CONFIG>")
target_compile_features(fraction3 PUBLIC cxx_std_17)

# add compiler warning flags just when building this project
# via the BUILD_INTERFACE generator expression
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

set(gcc_flags "-Wall;-Werror;-Wunused;-Wextra;-Wshadow;-Wshadow;-Wformat=2;-pedantic-errors;-std=c++17")
set(msvc_flags "-W3")

target_compile_options(fraction3 PRIVATE
"$<${gcc_like_cxx}:$<BUILD_INTERFACE:${gcc_flags}>>"
"$<${msvc_cxx}: $<BUILD_INTERFACE:${msvc_flags}>>")


1 change: 1 addition & 0 deletions fraction3/compile_commands.json
109 changes: 109 additions & 0 deletions fraction3/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <exception>
#include <ios>
#include <iostream>
#include <istream>
#include <stdexcept>
#include <string>
#include <limits>

class Fraction{
private:
int m_numerator;
int m_deonominator;
public:
Fraction(int n=1, int d=1):
m_numerator{n} {
// this will automatically be called by operators
// that are overloaded because they return a new
// Fraction object
setD(d);
reduce();
}

void print() const{
std::cout << m_numerator << '/' << m_deonominator << '\n';
}

// this is to practice throwing exceptions.
// usually this would just return a success or failuer (0, 1)
// but we want to practice exceptions
void setD(const int d) {
if(d == 0){
throw std::runtime_error("Invalid denominator");
}
else{
m_deonominator = d;
}
}


int gcd(int a, int b) {
return (b == 0) ? (a > 0 ? a : -a) : gcd(b, a % b);
}

void reduce(){
if (m_numerator != 0 && m_deonominator != 0){
int gcd { Fraction::gcd(m_numerator, m_deonominator) };
m_numerator /= gcd;
m_deonominator /= gcd;
}
}

friend Fraction operator*(const Fraction &f1, const Fraction &f2);
friend Fraction operator*(const Fraction &f1, const int i);
friend Fraction operator*(const int i,const Fraction &f1);
friend std::ostream& operator<< (std::ostream &out, const Fraction &fraction);
friend std::istream& operator>> (std::istream &in, Fraction &fraction);
};

// learning how to overload operators using the friend function
// the const is important because it's impossible to bind again
// non-const temp reference objects
Fraction operator*(const Fraction &f1, const Fraction &f2){
return Fraction{
f1.m_numerator * f2.m_numerator,
f1.m_deonominator * f2.m_deonominator
};
}

Fraction operator*(const Fraction &f1, const int i){
return Fraction{
f1.m_numerator * i,
f1.m_deonominator * i,
};
}

Fraction operator*(const int i,const Fraction &f1){
return Fraction{
f1.m_numerator * i,
f1.m_deonominator * i,
};
}

std::ostream& operator<< (std::ostream &out, const Fraction &fraction){
out << fraction.m_numerator << "/" << fraction.m_deonominator;
return out;
}

std::istream& operator>> (std::istream &in, Fraction &fraction){
in >> fraction.m_numerator;
int d{0};
in >> d;
fraction.setD(d);
return in;
}

int main()
{
try{
Fraction f1{};
std::cout << "Enter fraction 1: ";
std::cin >> f1;
}
catch(std::exception& e){
std::cout << e.what() << '\n';
}


return 0;
}
32 changes: 32 additions & 0 deletions pairs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Specify the minimum version of Cmake that can run this script
cmake_minimum_required(VERSION 3.19)

# Project name and version
project(pairs VERSION 1.0)
# Request C++ 11 standard features
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
include_directories(pairs "${PROJECT_SOURCE_DIR}/include/")
file(GLOB SOURCES "src/*cpp")

# Compile the passed source files into an executable
add_executable(pairs ${SOURCES})

# Set the output director for executables
set_target_properties(pairs PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin/$<CONFIG>")
target_compile_features(pairs PUBLIC cxx_std_17)

# add compiler warning flags just when building this project
# via the BUILD_INTERFACE generator expression
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

set(gcc_flags "-Wall;-Werror;-Wunused;-Wextra;-Wshadow;-Wshadow;-Wformat=2;-pedantic-errors;-std=c++17")
set(msvc_flags "-W3")

target_compile_options(pairs PRIVATE
"$<${gcc_like_cxx}:$<BUILD_INTERFACE:${gcc_flags}>>"
"$<${msvc_cxx}: $<BUILD_INTERFACE:${msvc_flags}>>")


1 change: 1 addition & 0 deletions pairs/compile_commands.json
64 changes: 64 additions & 0 deletions pairs/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <string>
#include <iostream>

template <class T>
class Pair1{
private:
T m_a;
T m_b;
public:
Pair1 (const T& a, const T& b )
: m_a{a}, m_b{b} {

}

const T& first() const { return m_a; }
const T& second() const { return m_b; }

};

template <class T1, class T2>
class Pair{
private:
T1 m_a;
T2 m_b;
public:
Pair (const T1& a, const T2& b )
: m_a{a}, m_b{b} {

}

const T1& first() const { return m_a; }
const T2& second() const { return m_b; }

};

template<class T>
class StringValuePair : public Pair<std::string, T>{
public:
StringValuePair(const std::string& key, const T& value)
: Pair<std::string, T>(key, value){

}
};


int main(){
Pair1<int> p1(5, 8);
std::cout << "Pair: " << p1.first() << ' ' << p1.second() << '\n';

const Pair1<double> p2(2.3, 4.5);
std::cout << "Pair: " << p2.first() << ' ' << p2.second() << '\n';

Pair<int, double> p3(5, 8.6);
std::cout << "Pair: " << p3.first() << ' ' << p3.second() << '\n';

const Pair<double, int> p4(2.3, 5);
std::cout << "Pair: " << p4.first() << ' ' << p4.second() << '\n';

StringValuePair<int> svp("Hello", 5);
std::cout << "Pair: " << svp.first() << ' ' << svp.second() << '\n';


return 0;
}
Empty file added pairs/src/Pairs.inl
Empty file.
31 changes: 31 additions & 0 deletions serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Specify the minimum version of Cmake that can run this script
cmake_minimum_required(VERSION 3.19)

# Project name and version
project(serialization VERSION 1.0)
# Request C++ 11 standard features
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include_directories(serialization "${PROJECT_SOURCE_DIR}/src/")
file(GLOB SOURCES "src/*cpp")

# Compile the passed source files into an executable
add_executable(serialization ${SOURCES})

# Set the output director for executables
set_target_properties(serialization PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin/$<CONFIG>")
target_compile_features(serialization PUBLIC cxx_std_11)

# add compiler warning flags just when building this project
# via the BUILD_INTERFACE generator expression
set(gcc_like_cxx "$<COMPILE_LANG_AND_ID:CXX,ARMClang,AppleClang,Clang,GNU>")
set(msvc_cxx "$<COMPILE_LANG_AND_ID:CXX,MSVC>")

set(gcc_flags "-Wall;-Werror;-Wunused;-Wextra;-Wshadow;-Wshadow;-Wformat=2;-pedantic-errors;-std=c++17")
set(msvc_flags "-W3")

target_compile_options(serialization PRIVATE
"$<${gcc_like_cxx}:$<BUILD_INTERFACE:${gcc_flags}>>"
"$<${msvc_cxx}: $<BUILD_INTERFACE:${msvc_flags}>>")


1 change: 1 addition & 0 deletions serialization/compile_commands.json
66 changes: 66 additions & 0 deletions serialization/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
#include <cstring>


#define INITIAL_SIZE 32

struct Buffer{
void *data;
size_t next;
size_t size;
};

struct Buffer* newBuffer() {
struct Buffer *b = new Buffer;

b->data = malloc(INITIAL_SIZE);
b->size = INITIAL_SIZE;
b->next =0;

return b;
}

void reserveSpace(Buffer* b, size_t bytes){
if((b->next + bytes) > b->size){
// double size to enforce 0(lg N) rallocs
b->data = realloc(b->data, b->size * 2);
b->size *=2;
}
}

void serializeInt(int x, Buffer* b){
// assume int == long
// x = htonl(x);

reserveSpace(b, sizeof(int));
memcpy((char*)b->data + b->next, &x, sizeof(int));
b->next += sizeof(int);
}



struct intFloatString{
int i;
// float f;
// char* s;
};

void serializeIntFloatString(struct intFloatString* toSerialize, struct Buffer* output){
serializeInt(toSerialize->i, output);

}



int main()
{
intFloatString* toSerialize = new intFloatString;
Buffer* buf = newBuffer();

toSerialize->i = 42;

serializeIntFloatString(toSerialize, buf);

std::cout << "Hello World" << "\n";
}