Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication(CPP Kaioh planet).cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #define NDEBUG
- #define _CRTDBG_MAP_ALLOC
- #include <stdlib.h>
- #include <crtdbg.h>
- #include "pch.h"
- #include <iostream>
- #include <cassert>
- class IndexIsOutOfBoundsException {};
- class IntArray {
- private:
- int * m_ptr{ nullptr };
- int m_size{ 0 };
- public:
- bool IsValidIndex(int index) const { //Bounds check
- return (index >= 0) && (index < m_size);
- }
- IntArray() = default; // This will simply create a IntArray with pointer set to null and size set to zero
- //Another constructor that takes the size of array as an input parameter
- explicit IntArray(int size) { //use explicit with one argument constructor, unless I want automatically implicity conversions
- if (size != 0) {
- m_ptr = new int[size] {}; //Allocate array memory block on the heap (The brackets putted over there is to properly initialize the elements to zero. Without them you will get some garbage in the array memory block
- m_size = size;
- }
- }//Without the use of "explicit" the compiler would automattically use this constructor to implicity convert integer numbers
- //to objects of this IntArray class, witch doesnt make sense from an array semantic perspective, and would trigger subtle bugs.
- //Ready only properties
- int Size() const {
- return m_size;
- }
- bool IsEmpty() const {
- return (m_size == 0);
- }
- int &operator[](int index) { //Overloaded operator //And if it is without the & he will point to an int and not to the element address causing an error
- if (!IsValidIndex(index)) {
- throw IndexIsOutOfBoundsException{};
- }
- return m_ptr[index]; //Return the ptr index as an int type
- }
- /*
- int operator[](int index) const {
- if (!IsValidIndex(index)) {
- throw IndexIsOutOfBoundsException{};
- }
- return m_ptr[index];
- }*/
- ~IntArray() {
- delete[]m_ptr;
- }
- };
- std::ostream& operator<<(std::ostream& os, IntArray& a) {
- os << "[";
- for (int i = 0; i < a.Size(); i++) {
- os << a[i] << ' ';
- }
- os << "]";
- return os;
- };
- int main()
- {
- using std::cout;
- _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
- cout << "Creating an empty array.\n";
- IntArray a{};
- cout << "a.Size() is " << a.Size() << '\n';
- assert(a.IsEmpty());
- cout << "-------------------------------------------\n";
- cout << "Creating an array that contains elements\n";
- IntArray b{10};
- cout << "b.Size() is " << b.Size() << '\n';
- assert(!b.IsEmpty());
- cout << "-------------------------------------------\n";
- cout << "Creating an array and getting their value\n";
- IntArray c{3};
- c[1] = 666;
- cout << "c[1] is " << c[1] << '\n';
- cout << "-------------------------------------------\n";
- cout << "Creating an array for bound checks\n";
- IntArray d{ 3 };
- d[1];
- cout << "d[1] is " << d[1] << '\n';
- cout << "-------------------------------------------\n";
- IntArray e{ 15 };
- for (int i = 0; i < e.Size(); i++) {
- e[i] = (i + 1) * 10;
- }
- cout << "Array elements: " << e << "\n";
- //_CrtDumpMemoryLeaks();
- //An array of elements of type char can be declared like this
- //char v[6] = {'XXX', 'CCC', 'VVX', 'F', 'X', 'XVX'};
- //char *p = &v[3]; // p points to vās four th element
- //char x = *p;
- ////1 Allocate big chunk of memory using new[]
- //int *p = new int[1000000]{};
- ////2 Use the memory
- //std::cout << "Before delete!\n " << &p[500] <<"\n";
- ////3 Must release it!
- //p = nullptr;
- ////delete[]p;
- //std::cout << "After delete!\n " << &p[500] << "\n";
- };
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement