Skip to content

Commit d27d2ac

Browse files
committed
Initial Commit
0 parents  commit d27d2ac

File tree

9 files changed

+491
-0
lines changed

9 files changed

+491
-0
lines changed

build/.build/DynamicArray.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#ifndef DYNAMIC_CLASS_DEF
2+
#define DYNAMIC_CLASS_DEF
3+
4+
#include <DynamicArray.h>
5+
#include <stdlib.h>
6+
#include <iostream>
7+
8+
using namespace Scrappers;
9+
10+
template <class T>
11+
DynamicArray<T>::DynamicArray(int length){
12+
this->length = length;
13+
ptr = new T[length];
14+
}
15+
template <class T>
16+
DynamicArray<T>::~DynamicArray(){
17+
destroyMemory();
18+
}
19+
template <class T>
20+
const int DynamicArray<T>::getLength(){
21+
return length;
22+
}
23+
template <class T>
24+
T DynamicArray<T>::add(T object){
25+
if(position < length){
26+
ptr[position++] = object;
27+
}else{
28+
T* delegation = new T[length+1];
29+
for(int pos = 0; pos < length; pos++){
30+
delegation[pos] = T();
31+
delegation[pos] = ptr[pos];
32+
}
33+
delegation[position++] = object;
34+
this->length = length + 1;
35+
}
36+
return object;
37+
}
38+
template <class T>
39+
T DynamicArray<T>::add(int position, T object){
40+
if(position < length){
41+
ptr[position] = object;
42+
}else{
43+
T* delegation = new T[length+1];
44+
for(int pos = 0; pos < length; pos++){
45+
delegation[pos] = ptr[pos];
46+
}
47+
delegation[position] = object;
48+
this->length = length + 1;
49+
}
50+
return object;
51+
}
52+
template <class T>
53+
DynamicArray<T>& DynamicArray<T>::operator+(T object){
54+
add(object);
55+
return *this;
56+
}
57+
template <class T>
58+
DynamicArray<T>& DynamicArray<T>::operator-(int position){
59+
ptr[position] = T();
60+
return *this;
61+
}
62+
template <class T>
63+
T DynamicArray<T>::get(int position){
64+
return ptr[position];
65+
}
66+
template <class T>
67+
T* DynamicArray<T>::remove(T* &object){
68+
object = nullptr;
69+
return object;
70+
}
71+
template <class T>
72+
void DynamicArray<T>::removeAt(int position){
73+
ptr[position] = T();
74+
}
75+
template <class T>
76+
void DynamicArray<T>::forEach(){
77+
for(int position=0; position < length; position++){
78+
Scrappers::DynamicArray<T>::forEachUpdateDispatcher(ptr[position]);
79+
}
80+
}
81+
template <class T>
82+
void DynamicArray<T>::destroyMemory(){
83+
delete ptr;
84+
ptr=NULL;
85+
delete &length;
86+
}
87+
template <class T>
88+
void DynamicArray<T>::reConstruct(){
89+
std::cout << "Reconstruct the instance back then" << std::endl;
90+
}
91+
template <class T>
92+
DynamicArray<T> DynamicArray<T>::shallowCopy(DynamicArray &instance){
93+
std::cout << "Shallow copy of data" << std::endl;
94+
return 0;
95+
}
96+
template <class T>
97+
DynamicArray<T> DynamicArray<T>::deepCopy(DynamicArray &instance){
98+
std::cout << "DeepCopy of Data" << std::endl;
99+
return 0;
100+
}
101+
template <class T>
102+
void DynamicArray<T>::print(){
103+
std::cout << "print" << std::endl;
104+
}
105+
#else
106+
#endif
107+

build/.build/app.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <stdlib.h>
4+
#include "DynamicArray.cpp"
5+
#include <app.h>
6+
7+
8+
/**
9+
* Redefining the Templated/Generic classes in the form of concrete dataTypes using typedef & enclosing them in a utils struct(short-* hand-class-utils).
10+
* <br>
11+
* Using length pointer variable to keep track of the length of the DynamicArray as an example of pointers
12+
* <br>
13+
* utils : is a de-referenced pointer instance of the Class Utils, so can be used to access non-static members : *length value
14+
*/
15+
struct Utils{
16+
public:
17+
typedef Scrappers::DynamicArray<int> DynamicIntArray;
18+
typedef Scrappers::DynamicArray<std::string> DynamicStringArray;
19+
typedef App::Greeter Greeter;
20+
int* length = new int();
21+
} utils;
22+
/**
23+
* Overriding the member function greeting() of class Greeter, & commands a greeting message.
24+
*/
25+
std::string App::Greeter::greeting() {
26+
return std::string("Simple Language Coverage : \n")
27+
.append("\n")
28+
.append("1)Classes, Abstraction(virtual), Reservedkeywords, OOP\n")
29+
.append("2)Basic Pointers, value References, address of memmory blocks\n")
30+
.append("3)Dynamic Arrays\n")
31+
.append("4)Static Arrays\n")
32+
.append("5)Base memory address of arrays\n")
33+
.append("6)Jumping among arrays members using pointers & subscript notations\n")
34+
.append("7)Functions, Pointer, Value & Reference paramters\n")
35+
.append("8)Usuage of reference parameters\n")
36+
.append("9)Shallow & deep copying of variables through the assignment operator\n")
37+
.append("\n");
38+
}
39+
/**
40+
* Overrding the dispatcher & using it as interfaces in java
41+
* @param T is an object of type T, as the template provides
42+
*/
43+
template <class T>
44+
void Scrappers::DynamicArray<T>::forEachUpdateDispatcher(T object){
45+
std::cout << object << std::endl;
46+
}
47+
/**
48+
* Commands & run a forEach call to a dynamic array instance
49+
* @param Scrappers::DynamicArray<T>* pointer instance of that dynamic array
50+
*/
51+
template <class T>
52+
void command(Scrappers::DynamicArray<T>* dynmArray){
53+
std::cout <<"For Each Dispatcher started>>>>>>>>" << std::endl;
54+
dynmArray->forEach();
55+
56+
}
57+
/**
58+
* My Code execution unit.
59+
*/
60+
int main () {
61+
Utils::Greeter* greeter = new Utils::Greeter();
62+
std::cout << (*greeter).greeting() << std::endl;
63+
Utils::DynamicStringArray* stringArr = new Utils::DynamicStringArray(3);
64+
stringArr->add("Welcome to Dynamic Arrays Example Usuage.....>>>>");
65+
stringArr->add("Cpp codingBootcamp by Scrappers Team >>>>>>");
66+
stringArr->add("\n");
67+
command(stringArr);
68+
69+
std::cin >> *(utils.length);
70+
71+
Utils::DynamicIntArray* dynmArray = new Utils::DynamicIntArray(*(utils.length));
72+
std::cout <<"Length : "<< dynmArray->getLength() << std::endl;
73+
dynmArray->add(220);
74+
(*dynmArray) + 500 + 300 + 200;
75+
command(dynmArray);
76+
77+
(*dynmArray) + 100 - 1;
78+
command(dynmArray);
79+
//delete the pointer after program execution, assign the memory address to zero
80+
delete utils.length;
81+
utils.length = nullptr;
82+
//print out concatenated pointers for memory reference
83+
std::cout << utils.length << "\n" << &utils << "\n" << std::endl;
84+
return 0;
85+
}

build/compile.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#**
2+
#* Ccoffee Build tool, manual build, alpha-v1.
3+
#* Custom Includsions for GTKmm cpp wrapper
4+
#* dependencies '-I"/usr/include/glibmm-2.9.1/glib" -I"/usr/include/sigc++-2.0/sigc++" -I"/usr/include/giomm-2.4" -I"/usr/include/gtkmm-4.2.0/gtk"'
5+
#*
6+
#* @author pavl_g.
7+
#*#
8+
echo "Compiling the project"
9+
10+
#1) define work directory
11+
workDir='/home/twisted/GradleProjects/CppCodingBootcamp'
12+
13+
#2) delete old build
14+
oldbuild=(${workDir}'/build/.build/*')
15+
for ((idx=0; idx < ${#oldbuild[@]}; idx++)); do
16+
rm ${oldbuild[$idx]}
17+
done
18+
19+
##remove the dir
20+
rmdir ${workDir}'/build/.build/'
21+
22+
#3) recreate a clean one to gather cpp files for compiling & linking
23+
mkdir ${workDir}'/build/.build'
24+
25+
##attrs : dir to compile & sharedLib name
26+
libs=('/home/twisted/GradleProjects/CppCodingBootcamp/src/libs/*')
27+
main=('/home/twisted/GradleProjects/CppCodingBootcamp/src/main/*')
28+
29+
merge[0]=${libs}
30+
merge[1]=${main}
31+
32+
#4) copy cpp files to a gather directory
33+
for ((idx=0; idx < ${#merge[@]}; idx++)); do
34+
##act on ${merge[$idx]}
35+
cp ${merge[$idx]} ${workDir}'/build/.build'
36+
done
37+
38+
# 5) get the final String
39+
final=${workDir}'/build/.build/*'
40+
41+
# 6) execute using g++ compiler against cpp code
42+
##prepare exec names
43+
sharedlib='shared'
44+
executable='CppCodeCamp.exec'
45+
# 7) compile files with inclusions
46+
g++ -x c++ -I'/home/twisted/GradleProjects/CppCodingBootcamp/src/includes' -o ${sharedlib} ${final}
47+
clang++ -x c++ -I'/home/twisted/GradleProjects/CppCodingBootcamp/src/includes' -o ${executable} ${final}
48+
# 7) move files to output dir
49+
##prepare dirs
50+
builddir=${workDir}'/build/'
51+
outputdir=${workDir}'/output/'
52+
##moving files
53+
mv ${builddir}${sharedlib} ${outputdir}${sharedlib}
54+
mv ${builddir}${executable} ${outputdir}${executable}
55+
56+
echo "Successfully Compiled"

output/CppCodeCamp.exec

24.9 KB
Binary file not shown.

output/shared

29.3 KB
Binary file not shown.

src/includes/DynamicArray.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*a guard macro preprocessor command, to check in this header file only once before compile-time*/
2+
#ifndef DYNAMIC_CLASS_HEADER
3+
#define DYNAMIC_CLASS_HEADER
4+
/*namespace is a code enclosure*/
5+
namespace Scrappers{
6+
/*using templates or generics for any class displacement*/
7+
template <class T>
8+
class DynamicArray{
9+
private:
10+
T* ptr;
11+
int length;
12+
int position;
13+
virtual void destroyMemory();
14+
public:
15+
DynamicArray(int length = 0);
16+
~DynamicArray();
17+
T add(T object);
18+
T add(int position, T object);
19+
T get(int position);
20+
T* remove(T* &object);
21+
void removeAt(int position);
22+
virtual void forEach();
23+
void forEachUpdateDispatcher(T currentObj);
24+
virtual void reConstruct();
25+
DynamicArray shallowCopy(DynamicArray &instance);
26+
DynamicArray deepCopy(DynamicArray &instance);
27+
DynamicArray& operator+(T object);
28+
DynamicArray& operator-(int position);
29+
const int getLength();
30+
void print();
31+
};
32+
};
33+
34+
35+
#endif

src/includes/app.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This C++ source file was generated by the Gradle 'init' task.
3+
*/
4+
#ifndef APP_H
5+
#define APP_H
6+
7+
#include <string>
8+
9+
namespace App {
10+
class Greeter {
11+
public:
12+
std::string greeting();
13+
};
14+
}
15+
16+
#endif

0 commit comments

Comments
 (0)