FRESH Beginer, Learning to use a '.h' '.cpp'

#include <iostream>
#include <vector>

using namespace std;

class Vector{
private:
vector<int> * myvect = NULL; // declaring private pointer;

void create_vector(){
if(myvect == NULL)
myvect = new vector<int>; // creating your vector in a private method
}


public:
Vector(){
create_vector();
}

~Vector(){
delete myvect;
}

vector<int>* get_vector(){

return (vector<int>*)myvect; //returning pointer to your vector
}

};

int main(){
Vector v;
v.get_vector()->push_back(2); // insert 2
cout << v.get_vector()->back(); // prints 2

}




I have an okay code rite now however I have been told that I have bad programming habits from some not so friendly patrons. I'm just trying to learn. How do I separate this code into more files. For example, like a .cpp and a .h file that would make three separate codes (or files or whatever the correct definition may be I really don't know yet but I will). All together this is so new and abstract to me. To me it see I must separate the variables and my function declarations and my function definitions. But how do I they know how will reference each other? Is there a name for this process or something? Will I have to #include<iostream> or "using namespace std;' more than once? Will the class still work for my private and public member functions or even though its in multiple files?

I'm sure I can figure out most the variables but vectors are already a nightmare for me. I might be asking for a lot but I'm also open to any honest polite and constructive criticism to this program. (I'm not going to lie this program does not run every time and when it does run it runs correctly occasionally.)
> vector<int> * myvect
http://www.cplusplus.com/forum/general/138037/#msg731921

> But how do I they know how will reference each other?
headers are #include in the source files.
The sources are each one compiled in its own object file. Then you link the object files together.
$ g++ -c foo.cpp
$ g++ -c bar.cpp
$ g++ foo.o bar.o -o program.bin


Read http://www.cplusplus.com/faq/beginners/multiple-sources/


¿why is this in `unices'?
your .h file would look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
#include <vector> //all your includes here
 
using namespace std;

class Vector
{
         public:
                   //here you have to declare your public functions/variables/constuctors whatever
                   //but dont define for example         
                Vector();
              ~Vector();
        private:
                //here the same but for private  
                void create_vector();
               void delete_vector();
}
#endif


header files (.h) basically are used to describe the content of the class

your .cpp file would look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <Vector.h> //you have to include your header(.h) file 

void Vector::create_vector() //see the scope operator(this ::) dont forget this 
{
 
//here you write your code for the function

}

Vector::Vector()
{

//here you write your code for the constructor

}



and the main function in separate file (.cpp)

1
2
3
4
5
6
7
8
#include <Vector.h> //include  your header file 
 
int main()
{
// now everything is normal 

return 0;
}


then you compile like in the post before or

$ g++ Main.cpp Vector.h Vector.cpp -o Vectors

Last edited on
Topic archived. No new replies allowed.