Hey all,

I am continuing with my self study and now I have come across classes. I have read about this
and read about this but it seems very alien to me.The problem I have selected to do is as follows:

Here is a rather simple class definition:

class Person
{
private:
	static const LIMIT = 25;
	string lname; 						// Person's last name
	char fname[LIMIT]; 					//Person's first name
public:
	Person() {lname=""; fname[0]='\0';} 			//#1
	Person(const string & ln, const char * fn = "Hey you"); 	// #2
// the following methods display lname and fname
	void Show() const; // firstname lastname format
	void FormalShow() const; //lastname firstname format
};

Write a program that completes the implementation by providing code for the undefined
methods. The program in which you use the class should also use the three possible
constructor calls (no arguments, one argument and two arguments) and the two display
methods. Here's an example that uses the constructors and methods:

Person one; //use default constructor
Person two("Smythecraft"); //use #2 with one default argument
Person three("Dimwiddy", "Sam"); //use #2, no defaults
one.Show();
cout<< endl;
one.FormalShow(); //etc. for two and three

It seems as if there are 2 files left to write, since I have written the header file to be:

// person.h
#ifndef PERSON_H_
#define PERSON_H_

class Person
{
private:
	static const LIMIT = 25;
	string lname; 						// Person's last name
	char fname[LIMIT]; 					//Person's first name
public:
	Person() {lname=""; fname[0]='\0';} 			//#1
	Person(const string & ln, const char * fn = "Hey you"); 	// #2
// the following methods display lname and fname
	void Show() const; // firstname lastname format
	void FormalShow() const; //lastname firstname format
};
#endif

The second file is where I don't seem to know what to do.
It seems as if I should define the functions from public.
Is there anyone out there that could sort of guide me in that direction?
I have tried reading other forums but the questions seem a bit more advanced.
All I need now is pointing in the right direction and later I can get more complicated.
I am using Dev-C++ with Windows XP.

Thanks as always!

The other file you need to write is the *.cpp file that implements the functions in that class, and main() which all standard C and C++ programs have to have. When you created that Dev-C++ project it already created the main.cpp and main() function shell. So now all you have to do is add the implementation code for the Person class, specifically the functions that appear on lines 13, 15 and 16. For example, so start you out, put this in main.cpp

Person::Person(const string & ln, const char * fn)
{
   // put the implementation code here
}

I guess the best way to start is to have a clear concept of what you are trying to do.

I'm not sure from reading the above, but I think the ultimate goal is to make a "person" array that enters and displays personal data in different formats????

If so, you have allot of work to do on that code!
Why are you setting a default value of "hey you"? I'm very confused by this

The default constructor just nulls everything.
You define a character array for fname but define lname as a string?

The other file you need to write is the *.cpp file that implements the functions in that class, and main() which all standard C and C++ programs have to have. When you created that Dev-C++ project it already created the main.cpp and main() function shell. So now all you have to do is add the implementation code for the Person class, specifically the functions that appear on lines 13, 15 and 16. For example, so start you out, put this in main.cpp

Person::Person(const string & ln, const char * fn)
{
   // put the implementation code here
}

Shouldn't this go in person.cpp rather than main.cpp?

You need to add these two lines to the top of your person.h file if you have not already:

#include <string>
using namespace std;

I agree with JRM. I'd change "Hey you" to "Bob" or something that makes more sense to you and others. As far as one name being a character array and the other a string, it's a little odd, but possibly intentional due to the fact that it is a learning exercise and they are giving you an example that uses many things. It's perfectly legal as far as the language is concerned, and it is implementable. With the above two lines added, I think your person.h file is ready.

For the person.cpp file, here's a model:

// person.cpp
#include <iostream>
#include <string>
#include "person.h"
using namespace std;


Person::Person (const string & ln, const char * fn)
{
      // code assigns fname to be fn and lname to be ln
      // these should be "deep copies", not "shallow copies"
}


void Person::Show() const
{
    // implementation code
}


void Person::FormalShow() const
{
    // implementation code
}

Please not that Person () is NOT implemented in person.cpp. This is intentional. It has already been implemented in person.h.

Make sure you put this line:

#include "person.h"

in your main program.

eesti44 it appears we're working from the same book, C++ Primer Plus, 5th Edition, and at the same chapter doing the programming examples. I've just completed this particular exercise and it gave me no end of grief. As another poster has mentioned the header file from the book is missing the include <string> and using::string and as it turns out I believe that was probably the point of the exercise (or one of the points).

Also in the header file I kept getting a compiler error regarding the line static const LIMIT = 25; the error reported as

ISO C++ forbids declaration of `LIMIT' with no type

I merely added an int to the line and that solved it.

I used three files, person.h, person.cpp and main.cpp. Get the header fixed up and merely use the previous example from the book as a guide and you should bowl this one over pretty quick.

Hope this helps.

ps. I've just commented out #include <string> from the header file and yet the program still compiled, no error. Maybe because I've included it in the other files.

when you include one file in more than one places it might cause a redeclaration problem. to avoid that put ur includes in guards with ifndef and endif preprocessor directives.

eg:

person.h

#ifndef PERSON_H
#define PERSON_H

class person
{
\\\
};

#endif

and then wherever you include this file, do it like this

#ifndef PERSON_H
#include <person.h>
#endif

this will ensure the file does not get included more than once

and then wherever you include this file, do it like this


#ifndef PERSON_H
#include <person.h>
#endif
this will ensure the file does not get included more than once

Yes it will, but its unnecessary when person.h includes the code guards. We never do that with standard include files such as <string> so why do it with your own header files.

you are right, but there is some case in which it gives error if i dont include it with the #ifndef directive.. cant remember right now, but there was some error, may be at link time, will check and get back..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.