...

View Full Version : Does C++ support User-Defined Types, Like Java?



gak
09-07-2007, 01:54 PM
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++:


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.


#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

oracleguy
09-07-2007, 05:13 PM
Well what is in your job.hpp file?

gak
09-07-2007, 06:53 PM
At the moment it's just a bare-bones class with one attribute, although I intend to add a vector list of Workers objects:


//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!

oracleguy
09-07-2007, 08:37 PM
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:



#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.

gak
09-08-2007, 01:27 PM
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 :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum