I am learning C++, but I have very much dificult with type char.
I write the simple program to learn, and I have use the type string because I not know how to use char[256].
Can anyone help me? It is necessary rewrite the program using char instead of string.
// Friends - Demonstrate the use of friend class.
// Functions also can be friend
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
class People
{
friendclass Age;
public:
void setName(string name)
{
sName = name;
}
void setAge(int age)
{
nAge = age;
}
string getName(void)
{
return sName;
}
int getAge(void)
{
return nAge;
}
private:
string sName;
int nAge;
};
class Age
{
public:
void changeAge(People* pS)
{
int newAge;
cout << "Type the new age: ";
cin >> newAge;
pS->nAge = newAge;
}
};
int main(int NumberofArgs, char* pszArgs[])
{
cout << "Program to demonstrate the use\n"
<< "of the friend classes.\n\n";
string name;
cout << "Name = ";
cin >> name;
int age;
cout << "Age = ";
cin >> age;
cout << "Call the class People with the values typed.\n";
People p;
p.setName(name);
p.setAge(age);
cout << "Verify the values in the object People.\n"
<< "Name - " << p.getName() << "\n"
<< "Age - " << p.getAge() << "\n";
cout << "Use a friend class Age to modify the value.\n";
Age a;
a.changeAge(&p);
cout << "Verify again he values in the object People.\n"
<< "Name - " << p.getName() << "\n"
<< "Age - " << p.getAge() << "\n";
// wait until user is ready before terminating program
// to alow the user to see the program results
system("PAUSE");
return 0;
}
Replace string in main() with char name[256]. Replace strings in People class with char*. That should work. It would probably be better to declare sName as char[256] and use strcpy in setName() though. That way any changes made to name won't affect sName.
I aplly the changes recommended for you. It works, but still have errors.
The code is list below, and my doubt is, why the program only accept a single argument in Name field? If I type "Yuri" it works fine, but instead off, I type "Yuri Rodrigues", it no longer works!
// Friends - Demonstrate the use of friend class.
// Functions also can be friend
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
usingnamespace std;
class People
{
friendclass Age;
public:
void setName(char* name)
{
strcpy(sName,name);
}
void setAge(int age)
{
nAge = age;
}
string getName(void)
{
return sName;
}
int getAge(void)
{
return nAge;
}
private:
char sName[256];
int nAge;
};
class Age
{
public:
void changeAge(People* pS)
{
int newAge;
cout << "Type the new age: ";
cin >> newAge;
pS->nAge = newAge;
}
};
int main(int NumberofArgs, char* pszArgs[])
{
cout << "Program to demonstrate the use\n"
<< "of the friend classes.\n\n";
char name[256];
cout << "Name = ";
cin >> name;
int age;
cout << "Age = ";
cin >> age;
cout << "Call the class People with the values typed.\n";
People p;
p.setName(name);
p.setAge(age);
cout << "Verify the values in the object People.\n"
<< "Name - " << p.getName() << "\n"
<< "Age - " << p.getAge() << "\n";
cout << "Use a friend class Age to modify the value.\n";
Age a;
a.changeAge(&p);
cout << "Verify again he values in the object People.\n"
<< "Name - " << p.getName() << "\n"
<< "Age - " << p.getAge() << "\n";
// wait until user is ready before terminating program
// to alow the user to see the program results
system("PAUSE");
return 0;
}
This command, getline, should not be used with type string?
No. If you had looked at the function definitions properly you would have seen that there is no overload available that takes a std::istream& and a maximum amount of characters to extract:
1 2
istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );
// Friends - Demonstrate the use of friend class.
// Functions also can be friend
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
usingnamespace std;
class People
{
friendclass Age;
public:
void setName(char* name)
{
strcpy(sName,name);
}
void setAge(int age)
{
nAge = age;
}
string getName(void)
{
return sName;
}
int getAge(void)
{
return nAge;
}
private:
char sName[256];
int nAge;
};
class Age
{
public:
void changeAge(People* pS)
{
int newAge;
cout << "Type the new age: ";
cin >> newAge;
pS->nAge = newAge;
}
};
int main(int NumberofArgs, char* pszArgs[])
{
cout << "Program to demonstrate the use\n"
<< "of the friend classes.\n\n";
char name[256];
cout << "Name = ";
cin.getline(name, 256);
int age;
cout << "Age = ";
cin >> age;
cout << "Call the class People with the values typed.\n";
People p;
p.setName(name);
p.setAge(age);
cout << "Verify the values in the object People.\n"
<< "Name - " << p.getName() << "\n"
<< "Age - " << p.getAge() << "\n";
cout << "Use a friend class Age to modify the value.\n";
Age a;
a.changeAge(&p);
cout << "Verify again he values in the object People.\n"
<< "Name - " << p.getName() << "\n"
<< "Age - " << p.getAge() << "\n";
// wait until user is ready before terminating program
// to alow the user to see the program results
system("PAUSE");
return 0;
}