header file problem

hey guys, im currently trying to tackle this problem:

Class Definition
Write the class definition for a class named Person as follows:
1. The class definition should be stored in a header file named person.h.
2. There should be documentation for all class member functions.
3. The class will have two private data fields: name and age.
4. The class will have two constructors: One default constructor and one constructor with parameters.
5. The class will have mutator (set) functions for the name and age data fields.
6. The class will have accessor (get) functions for the name and age data fields.

Class Member Function Implementation and Creating a Driver Program
Write the implementation of the class member functions and a driver program as follows
1. The class member function implementations should be stored in the .cpp implementation file.
2. There should be documentation for all function implementations.
3. The driver program (main function) should be in its own .cpp file.

5. The driver program should create two person objects as follows:
a. Create a Person object using the default constructor.
b. Read a name and age.
c. Use the mutator set functions to set the name and age.
d. Create a Person object using the constructor with parameters. Make up a name and age for this one in the code.
e. Display the data fields for both Person objects using the accessor get functions.

and this is what I came up with:
For the header....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef NAME_H
#define NAME_H
#include <string>

using namespace std;
//     Write the class definition for a class named Person as follows:
//1. The class definition should be stored in a header file named person.h.
//2. There should be documentation for all class member functions.

class Person
{
	public:								
//4. The class will have two constructors: One default constructor and one 
//   constructor with parameters.
	Person();							//constructor with 0 input
	Person(string nameOne, int ageOne);		//constructor with 2 input;
//5. The class will have mutator (set) functions for the name and age data 
//   fields.
     void set_name() const;
     void set_name() const;
//6. The class will have accessor (get) functions for the name and age data 
//   fields.
	string get_name() const;
	int get_age() const;
	
//3. The class will have two private data fields: name and age.
	private:
	string name;
	int age;
};
// end of the compiler directives
#endif 


Implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <string>
#include "person.h"

using namespace std;

//  Class member function implementations **************************************
/*Write the implementation of the class member functions 
and a driver program as follows
1. The class member function implementations should be stored in the .cpp 
   implementation file.
2. There should be documentation for all function implementations.
3. The driver program (main function) should be in its own .cpp file.
4. In the spirit of compiling as you go, it is a good idea to alternate writing
   a member function and testing it in your driver program. Then you would 
   compile what you have so far, and, once you have no syntax errors, you can 
   run your program. Again you will find it much easier to diagnose your errors
   when testing small pieces at a time.*/
   
Person::Person(std::string nameOne,int ageOne)
{
	name = nameOne;
	age = ageOne;
}

void set_name() const;
{
     name = nameInput;
}

void set_age() const;
{
     age = ageInput;
}

string Person::get_name() const
{
	return name;
}

int Person::get_age() const
{
	return age;
}


and for my main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;


// main function ***********************************************************

int main ()
{
//a. Create a Person object using the default constructor.
Person personOne;
string nameInput;
int ageInput;

//b. Read a name and age.

cout << "Enter your name " << endl;
cin >> nameInput;
cout << "Enter your age " << endl;
cin >> ageInput;

//c. Use the mutator set functions to set the name and age.
personOne.set_name(nameInput);
personOne.set_age(ageInput);

//d. Create a Person object using the constructor with parameters. Make up a 
//   name and age for this one in the code.

name = " John";
age = 23;
Person personTwo (name, age);

//e. Display the data fields for both Person objects using the accessor get
//   functions.

cout << "Your name is: " << personOne.get_name() << endl
     << "You are " << personOne.get_age() << " old." << endl
     << "Other person's name is: " << personTwo.get_name() << endl
     << "He is " << personTwo.get_age() " old." << endl;


	// End of program statements
	cout << "Please press enter once or twice to continue...";
	cin.ignore().get();    			// hold console window open
	return EXIT_SUCCESS;           	// successful termination
    
}


am I doing it right?
thanks
Last edited on
void set_name() const
- You want to give it a parameter in which the new name is passed, as it is now it won't do anything;
- You qualify the function as 'const', but you actually change a member variable in it, so it isn't const. You should remove that 'const'.
( this also applies to set_age() )

In your main.cpp you use Person, but the compiler will not know what that is: you should #include "person.h"
Last edited on
Topic archived. No new replies allowed.