Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    gak
    gak is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Well what is in your job.hpp file?
    OracleGuy

  • #3
    gak
    gak is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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

  • Users who have thanked oracleguy for this post:

    gak (09-08-2007)

  • #5
    gak
    gak is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •