c++ exercise question (strcpy and new commands)

I'm working with an exercise and it tells me i need to use a new command and strcpy, i don't really understand how strcpy works i've been reading up on it, but for some reason just can't figure out what exactly needs to be done. i'm gonna list out the code, this is a homework assignment for a class so i'm not looking for a complete solution to this, just a little guidance.

thanks in advance.

person.h (provided by teacher)
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
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <cstring>
using namespace::std;

class person
{
   public:
       person(void);                      // new needed, strcpy needed
       person(const char * n, int a);     // new needed, strcpy needed
       person(const person & other);      // new needed, strcpy needed

       ~person(void);                     // delete needed

       int getAgeV(void) const;
       const int * getAgeP(void) const;
       const int & getAgeA(void) const;

	   const person & operator=(const person & right);     // = operator

       const char * getName(void) const; 

       void changeAge( int newAge);
       void changeName( const char * newName);  //  delete, new and strcpy needed

       void print(ostream &) const;

   private:
       int age;
       char * pName;
 };



#endif 


person.cpp (the one i need to build)
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
#include "person.h"
#include <string>

person::person(void)
{
}
person::person(const char * n, int a)
{
	string str;
	str.copy(n,);
}
person::person(const person & other)
{
}
person::~person(void)
{
}
int person::getAgeV(void) const
{
	return 0;
}
const int * person::getAgeP(void) const
{
	return 0;
}
const int & person::getAgeA(void) const
{
	return 0;
}
const person & person::operator=(const person & right)
{

}
const char * person::getName(void) const
{
}
void person::changeAge( int newAge)
{
}
void person::changeName( const char * newName)
{
}
void person::print(ostream &) const
{
}


driver.cpp (main application that i need to test the functions once they are built)(provided by teacher and I need to work with it a little)
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
//   exercise driver

#include "person.h"
#include <iostream>
using namespace::std;


void main()
{
	person boss("Tom", 42);
	// sending the return value to cout, test the 3 ways of getting the age




	person * p;
	p = new person("Susan", 31);
	// sending the return value to cout, test the 3 ways of getting the age




	//  test your member functions







    return;
}
strcpy takes 2 pointers. It copies the string data from one pointer into the other pointer.

For example:

1
2
3
4
5
6
char a[] = "This is a cstring buffer";
char b[100];  // this is another buffer

strcpy(b,a); // this copies the string contained in 'a' to 'b'

cout << b;  // this prints "This is a cstring buffer" 



Now since your person class only has a char pointer, you need to dynamically allocate the buffer to hold the person's name. The buffer must be large enough to hold the full name + 1 (for the null termination character).

Presumably, you learned out to get the length of strings and how to use new[] and delete[] already.
Topic archived. No new replies allowed.