Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-07-2007, 12:54 PM   PM User | #1
gak
New Coder

 
Join Date: Dec 2006
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
gak is an unknown quantity at this point
Question Does C++ support User-Defined Types, Like Java?

Hi

I'm new to C++ but have some experience with Java. To practice using C++, I've been trying to translate some old Java programs I've written to see how the languages compare.

I've created two basic C++ classes - 'Worker' and 'Job' and what I'm trying to do is to make them hold references to each other i.e. each worker does a job and holds a reference to it. When I did this in Java I simply implemented a User-Defined Type but I have not been able to achieve this in C++:

Code:
public class Worker {
  private String name;
  private String address;
  private String telephone;   // Java Attributes
  private Job job; 
   .........etc.
I've managed to get the name, address and telephone attributes to work in C++, with getters/setters and constructor etc. but no luck with job.

I've used #include "job.hpp" which compiles ok, but then will not recognise '"Job' as a type.

Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include "job.hpp"



using namespace std;

//worker.cpp

class Worker {
      public:
             //constructor
             Worker (string name, string address, string telephone, Job job);
             //destructor
             ~Worker();
             //setters
             void setName (string name);
             void setAddress (string address);
             void setTelephone (string telephone);
             void setJob (Job job);
             //getters
             string getName();
             string getAddress();
             string getTelephone();
             Job getJob();
      
      private:
              string name;
              string address;
              string telephone;
              Job job
              };
              
              //constructor definitions
              Worker::Worker(string name, string address, string telephone, Job job)
              { 
              this -> name = name;
              this -> address = address;
              this -> telephone = telephone;
              this -> job = job;
              }  etc...
If anyone could offer some advice or point me in the direction of a good tutorial It'd be much appreciated!

Many thanks

Gak
gak is offline   Reply With Quote
Old 09-07-2007, 04:13 PM   PM User | #2
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
Well what is in your job.hpp file?
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 09-07-2007, 05:53 PM   PM User | #3
gak
New Coder

 
Join Date: Dec 2006
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
gak is an unknown quantity at this point
At the moment it's just a bare-bones class with one attribute, although I intend to add a vector list of Workers objects:

Code:
//job.cpp

#include <cstdlib>
#include <iostream>
#include <string>


using namespace std;

class Job {
      public:
             //constuctor
             Job(string title);
             
             //destructor
             ~Job();

             //setters
             void setTitle (string title);

             //getters
             string getTitle() const;
                          
             private:
                     string title;
                     };
                     
                     //constructor definition
                     Job::Job (string title)
                   { this -> title = title;
                      }
                     
                     //destructor definition
                     Job::~Job() { }
                     
                     //setter definitions 
                     inline void Job::setTitle (string title) { this -> title = title; }
                                         
                     //getter definitions
                     inline string Job::getTitle() const {return title;}
                                  
             

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
Thanks!
gak is offline   Reply With Quote
Old 09-07-2007, 07:37 PM   PM User | #4
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
The class definition should go into a hpp file not a cpp file.

In addition, to prevent multiple definitions from including the header file in more than one place you need to use preprocessor definitions like:

Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include "job.hpp"

#ifndef WORKER_HPP
#define WORKER_HPP
class Worker {
      public:
             //constructor
             Worker (string name, string address, string telephone, Job job);
             //destructor
             ~Worker();
             //setters
             void setName (string name);
             void setAddress (string address);
             void setTelephone (string telephone);
             void setJob (Job job);
             //getters
             string getName();
             string getAddress();
             string getTelephone();
             Job getJob();
      
      private:
              string name;
              string address;
              string telephone;
              Job job
              };
              
              //constructor definitions
              Worker::Worker(string name, string address, string telephone, Job job)
              { 
              this -> name = name;
              this -> address = address;
              this -> telephone = telephone;
              this -> job = job;
              };
#endif
And if your job.hpp file contains the class definition, you shouldn't have any problems using it in the worker class. However it seems you posted the .cpp file, so what is in the .hpp?

You can declare the class and it's members in the hpp and then you should put the implementation for the different member functions in a cpp file. Do you understand what I am saying?

int main goes in a cpp file not a header file (hpp file) too.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Users who have thanked oracleguy for this post:
gak (09-08-2007)
Old 09-08-2007, 12:27 PM   PM User | #5
gak
New Coder

 
Join Date: Dec 2006
Posts: 20
Thanks: 3
Thanked 0 Times in 0 Posts
gak is an unknown quantity at this point
Many thanks for your help - much appreciated!

I'd actually been using one of those quick start-type books from the library that didn't explain things very well. The book explains how to create a .cpp file and then says you can use one class within another using the include ".hpp" command... but then doesn't say anything about creating .hpp files until 5 chapters later at the end of the book!!

So anyway, I'd made 2 .cpp files (but no .hpp files) and was trying to use one in the other with include "job.hpp" - D'oh!

As said before, thanks for helping me - I wish the book could have explained it like that
gak is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.