Skip to content

Commit b5982df

Browse files
author
Dan Huantes
committed
Initial Commit
0 parents  commit b5982df

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
4+
project(CountObjects)
5+
6+
7+
set(CO_SOURCES
8+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
9+
${CMAKE_CURRENT_SOURCE_DIR}/src/JoshHat.cpp
10+
${CMAKE_CURRENT_SOURCE_DIR}/src/JoshHat.h
11+
)
12+
13+
add_executable(CountObjects ${CO_SOURCES})
14+
15+

src/JoshHat.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "JoshHat.h"
2+
3+
int JoshHat::s_numJoshHatObjects = 0;

src/JoshHat.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
class JoshHat
3+
{
4+
public:
5+
JoshHat()
6+
{
7+
s_numJoshHatObjects++;
8+
}
9+
~JoshHat()
10+
{
11+
s_numJoshHatObjects--;
12+
}
13+
static int s_numJoshHatObjects;
14+
15+
};
16+

src/main.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include "JoshHat.h"
4+
5+
using namespace std;
6+
7+
8+
int main(int argc,char* argv[])
9+
{
10+
JoshHat m_hat;
11+
cout << "First Object on Stack...." << endl;
12+
cout << "Current Count: " << JoshHat::s_numJoshHatObjects << endl;
13+
vector<JoshHat*> l_vecHats;
14+
cout << "Next 5 Objects on Heap...." << endl;
15+
for(int i=0;i<5; i++)
16+
{
17+
JoshHat* anotherOne = new JoshHat();
18+
cout << "Current Count: " << JoshHat::s_numJoshHatObjects << endl;
19+
l_vecHats.push_back(anotherOne);
20+
}
21+
22+
cout << "Current Count: " << JoshHat::s_numJoshHatObjects << endl;
23+
24+
cout << "Another Object on Heap...." << endl;
25+
JoshHat* l_hat = new JoshHat();
26+
cout << "Current Count: " << JoshHat::s_numJoshHatObjects << endl;
27+
28+
cout << "Delete Object on Heap...." << endl;
29+
delete l_hat;
30+
31+
cout << "Current Count: " << JoshHat::s_numJoshHatObjects << endl;
32+
33+
cout << "Delete Remaining Objects on Heap...." << endl;
34+
for (auto itr = l_vecHats.begin();itr != l_vecHats.end();itr++)
35+
{
36+
delete *itr;
37+
cout << "Current Count: " << JoshHat::s_numJoshHatObjects << endl;
38+
}
39+
return 0;
40+
41+
};

0 commit comments

Comments
 (0)