Moonlit said:
Either use string (preffered way):
string name( "jamesbond" );
Since 'name' is a member, you cannot initialise it that way.
It has to be done in the constructor member initialisation
list. Nonetheless, using string is A VERY GOOD IDEA(tm).
Or
strncpy( name, "jamesbond", sizeof name );
Why are you copying 'sizeof name' bytes? What if there are
more elements in 'name' than 10? You will try accessing memory
beyond the end of the string literal, which causes undefined
behaviour. If you need to copy, (a) make sure you have enough
elements in 'name' and (b) use 'strcpy', which only copies as
many chars as there are in the second argument, plus the
terminating null character.
strcpy(name, "jamesbond");
Of course, if there is not enough elements in 'name', you end
up overrunning memory, which is also undefined behaviour. So,
all things considered, strings are much better alternative,
aren't they?
or
char *name="jamesbond";
It's much better to NEVER declare a pointer to _non-const_ char
and initialise it with a literal. This nonsense is dragging
into C++ from the old C days, and should be avoided. Use
const char * name = "jamesbond";
And, again, if it's a member, it cannot be initialised that way,
blah blah blah
Just my $0.04...
Regards, Ron AF Greve
]
Developwebsites said:
#ifndef PERSON_H
#define PERSON_H
#include<iostream>
#include<iomanip>
#include<string>
class Person {
protected:
char name[12];
name = jamesbond;
public:
Person();
void Print();
};//close Person
#endif
or this:
#include "person.h"
Person:
erson()
{
name = "jamesbond";
cout<<"The student's name is: "<<name;
}