From 642720852ec17b68d8d2bbad300202604c0bb63d Mon Sep 17 00:00:00 2001 From: Oskar Date: Tue, 30 Jul 2024 17:01:04 +0200 Subject: more exercises --- .idea/.gitignore | 8 +++++ .idea/editor.xml | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++ .idea/misc.xml | 18 ++++++++++ .idea/vcs.xml | 6 ++++ 2p14.cpp | 22 ++++++++++++ 2p15.cpp | 18 ++++++++++ 2p16-2p17.cpp | 28 +++++++++++++++ 2p18.cpp | 23 ++++++++++++ 2p20.cpp | 20 +++++++++++ 2p21.cpp | 20 +++++++++++ 2p25.cpp | 27 ++++++++++++++ 2p30-2p31.cpp | 30 ++++++++++++++++ const-tests.cpp | 25 +++++++++++++ constexpr-test.cpp | 22 ++++++++++++ pointertest.cpp | 21 +++++++++++ 15 files changed, 391 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/editor.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 2p14.cpp create mode 100644 2p15.cpp create mode 100644 2p16-2p17.cpp create mode 100644 2p18.cpp create mode 100644 2p20.cpp create mode 100644 2p21.cpp create mode 100644 2p25.cpp create mode 100644 2p30-2p31.cpp create mode 100644 const-tests.cpp create mode 100644 constexpr-test.cpp create mode 100644 pointertest.cpp diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/editor.xml b/.idea/editor.xml new file mode 100644 index 0000000..855412d --- /dev/null +++ b/.idea/editor.xml @@ -0,0 +1,103 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..53624c9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2p14.cpp b/2p14.cpp new file mode 100644 index 0000000..d3973a6 --- /dev/null +++ b/2p14.cpp @@ -0,0 +1,22 @@ +#include +#include "sales_item.hpp" + +/* + * Yes, it is legal if you aren't using compilerflags like me. + * 2.14 + * + * + */ + +int main () { + /* + int i = 100; + int sum = 0; + for(int i = 0 ; i != 10 ; i++) { + sum += i; + } + + std::cout << i << " " << sum << std::endl; + */ + return 0; +} diff --git a/2p15.cpp b/2p15.cpp new file mode 100644 index 0000000..1e13ede --- /dev/null +++ b/2p15.cpp @@ -0,0 +1,18 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.15 + * + * + */ + +int main () { + + //int ival = 1.01; // Valid + //int &rval1 = 1.01; // Invalid + //int &rval2 = ival; // Valid + //int &rval3; // Invalid + return 0; +} diff --git a/2p16-2p17.cpp b/2p16-2p17.cpp new file mode 100644 index 0000000..8a7b8eb --- /dev/null +++ b/2p16-2p17.cpp @@ -0,0 +1,28 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.16, 2.17 + * + * + */ + +int main () { + + int i = 0, &r1 = i; + double d = 0, &r2 = d; + + r2 = 3.14159; // Valid + r2 = r1; // Invalid + i = r2; // Invalid + r1 = d; // Invalid + //All valid? + std::cout << " "; + int u = 0; + int &ru = u; + u = 5; + ru = 10; + std::cout << u << " " << ru << std::endl; + return 0; +} diff --git a/2p18.cpp b/2p18.cpp new file mode 100644 index 0000000..845ac8e --- /dev/null +++ b/2p18.cpp @@ -0,0 +1,23 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.18 + * + * + */ + +int main () { + + int myval = 10; + int *point = nullptr; + std::cout << "myval: " << myval << std::endl; + point = &myval; + *point = 1000; + std::cout << "myval: " << myval << "\n*point: " << *point + << "\npoint: " << point << "\n&myval: " << &myval + << std::endl; + + return 0; +} diff --git a/2p20.cpp b/2p20.cpp new file mode 100644 index 0000000..6e85ad8 --- /dev/null +++ b/2p20.cpp @@ -0,0 +1,20 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.20 + * + * + */ + +int main () { + + int i = 42; + int *p = &i; + *p = *p * *p; // Same as i = i * i; + std::cout << "i: " << i + << "\n*p: " << *p << std::endl; + + return 0; +} diff --git a/2p21.cpp b/2p21.cpp new file mode 100644 index 0000000..746ce34 --- /dev/null +++ b/2p21.cpp @@ -0,0 +1,20 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.21 + * + * + */ + +int main () { + + int i = 0; + //double *dp = &i; // Invalid + //int *ip = i; // Invalid + int *p = &i; + std::cout << i << " " << p << std::endl; + + return 0; +} diff --git a/2p25.cpp b/2p25.cpp new file mode 100644 index 0000000..e97dec3 --- /dev/null +++ b/2p25.cpp @@ -0,0 +1,27 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.25 + * + * + */ + +int main () { + + int *p = nullptr; + int i = 10; + int &r = i; + // addr 10 10 + std::cout << &p << " " << i << " " << r << std::endl; + + int i2 = 11; + int *ip = 0; // AKA nullptr AKA NULL + std::cout << i2 << " " << &ip << std::endl; + + int *ip2 = nullptr; + int ip3 = 99; + std::cout << ip3 << " " << &ip2 << std::endl; + return 0; +} diff --git a/2p30-2p31.cpp b/2p30-2p31.cpp new file mode 100644 index 0000000..eecbdbd --- /dev/null +++ b/2p30-2p31.cpp @@ -0,0 +1,30 @@ +#include +#include "sales_item.hpp" + +/* + * + * 2.30, 2.31 + * + * + */ + +int main () { + //int i; + //const int v2 = 0; // top level ?? + //int v1 = v2; + //int *p1 = &v1, &r1 = v1; + //const int *p2 = &v2, *const p3 = &i, &r2 = v2; // p2 low level + // p3 top level ?? and low level?? + // r2 low level ? + //r1 = v2; low level + //p1 = p2; low level? + //p1 = p3; no? + //p2 = p1; ok? + //p2 = p3; ok? + int v1 = 2; + const int v2 = 0; + int &r = v1; + r = v2; + std::cout << r << std::endl; + return 0; +} diff --git a/const-tests.cpp b/const-tests.cpp new file mode 100644 index 0000000..6f07b05 --- /dev/null +++ b/const-tests.cpp @@ -0,0 +1,25 @@ +#include +#include "sales_item.hpp" + +/* + * + * Just me testing a bit + * + * + */ + +int main () { + + int i = 10; + const int *p = &i; + const int ci = 42; + const int &r = ci; + const int &r2 = i; + std::cout << *p << " " << i << std::endl; + std::cout << r << std::endl; + i = 20; + std::cout << *p << " " << i << std::endl; + i = 3443; + std::cout << r2 << std::endl; + return 0; +} diff --git a/constexpr-test.cpp b/constexpr-test.cpp new file mode 100644 index 0000000..ba88142 --- /dev/null +++ b/constexpr-test.cpp @@ -0,0 +1,22 @@ +#include +#include "sales_item.hpp" + +/* + * + * A little test + * + * + */ + +constexpr int get_number() { + + return 999; +} + +int main () { + + constexpr int i = get_number(); + std::cout << i << std::endl; + + return 0; +} diff --git a/pointertest.cpp b/pointertest.cpp new file mode 100644 index 0000000..2a2210d --- /dev/null +++ b/pointertest.cpp @@ -0,0 +1,21 @@ +#include +#include "sales_item.hpp" + +/* + * + * Description + * + * + */ + +int main () { + + int val = 929992; + int *pi = nullptr; + int **pi2 = nullptr; + pi = &val; + pi2 = π + std::cout << &pi << " " << pi2 << std::endl; + std::cout << val << " " << *pi << " " << **pi2 << std::endl; + return 0; +} -- cgit v1.2.3