Hi, I was helping problems with the following question:
Write the definition of the class below. The insertion operator is overloaded to
display the name and year of birth of a Person object (e.g., “Galileo Galilei was born in 1564”). Member function getName returns the name of the Person capitalizing only the beginning of every word in the name; for example if name is “lEonArDo DA VINCi”, getName should return “Leonardo Da Vinci”. Submit only the definition of the function getName and the function that overloads the insertion operator; nothing else.
The problem is that the code should displayed the converted name and then "was born in year something". For example, if I put in (rAVniK JAgPaL", 1997) in the main function, I believe it should display, "Ravnik Jagpal was born in 1997".
Instead what happens is that it displays "Ravnik JagpalRavnik Jagpal".
Also could someone please explain the bolded lines of code, i really dont understand them, a friend told me to write them but I cant understand his explanation of it.
#include <string>
usingnamespace std;
class Person
{
public:
Person(string n, int year) : name(n), yearOfBirth(year){}
string getName()
{
int size = name.length(); // length of name is assigned to name
if (name[0] != ' ') //checks to see if first element of array is a space
{
name[0] = putchar(toupper(name[0])); //puts first element to upper case
}
for (int i = 1; i < size; i++)
{
if (name[i] == ' ') //checks if an element is equal to a space
{
name[i] = putchar(toupper(name[i])); //space is set to upper case
name[i + 1] = putchar(toupper(name[i + 1])); //the element next to space is set to upper case
i++; //i is incremented
}
else
{
name[i] = putchar(tolower(name[i]));
}
}
return name;
}
friend ostream& operator <<(ostream& i, const Person& per);
private:
string name; int yearOfBirth;
};
ostream& operator <<(ostream& i, const Person& per)
{
cout << per.name << " was born in " << per.yearOfBirth;
return i;
}
int main()
{
Person Name("rAVniK JAgPaL", 1997);
cout << Name.getName() << endl;
system("Pause");
return 0;
}
yea thats the problem, i dont completely understand how to show it, could u maybe help on how i can show that, cause im new to overloading and not sure on how to show it. Also, if u can could u explain the bold lines i mentioned. Thanks
Yeah, you make a function for year the same way you did for name...
The lines have explanations on them It doesn't get much simpler... not sure what I can add to them.
string getName()
{
int size = name.length(); // length of name is assigned to name
if (name[0] != ' ') //checks to see if first element of array is a space
{
name[0] = putchar(toupper(name[0])); //puts first element to upper case
}
for (int i = 1; i < size; i++)
{
if (name[i] == ' ') //checks if an element is equal to a space
{
name[i] = putchar(toupper(name[i])); //space is set to upper case
name[i + 1] = putchar(toupper(name[i + 1])); //the element next to space is set to upper case
i++; //i is incremented
}
else
{
name[i] = putchar(tolower(name[i]));
}
}
return name;
}
#include <iostream>
#include <string>
usingnamespace std;
class Person
{
public:
Person(string n, int year) : name(n), yearOfBirth(year){}
int getYear(){ return yearOfBirth;}
string getName()
{
int size = name.length(); // length of name is assigned to name
if (name[0] != ' ') //checks to see if first element of array is a space
{
name[0] = putchar(toupper(name[0])); //puts first element to upper case
}
for (int i = 1; i < size; i++)
{
if (name[i] == ' ') //checks if an element is equal to a space
{
name[i] = putchar(toupper(name[i])); //space is set to upper case
name[i + 1] = putchar(toupper(name[i + 1])); //the element next to space is set to upper case
i++; //i is incremented
}
else
{
name[i] = putchar(tolower(name[i]));
}
}
return name;
}
private:
string name; int yearOfBirth;
};
int main()
{
Person Name("rAVniK JAgPaL", 1997);
std :: cout << Name.getName();
std :: cout << " " << Name.getYear() <<std :: endl;
system("Pause");
return 0;
}
Thanks, but what I dont understand by the bold lines is that how could spaces be uppercase or lower case, a space is a blank space isnt it?Whats meant by putchar? Also, why does the same line that shows up in the if statement come again in the else part?
And again for the code u replied with shows the year but doesnt have the overloaded operator anymore, the problem is that i need that operator part of the code, and it needs to display, ex. "Ravnik Jagpal was born in 1997". Instead it displays, "Ravnik JagpalRavnik Jagpal 1997". Thanks.
#include <iostream>
#include <string>
class Person
{
public:
Person(std::string n, int year) : name(n), yearOfBirth(year) {}
std::string getName()
{
int size = name.length(); // length of name is assigned to name
if (name[0] != ' ') //checks to see if first element of array is a space
{
name[0] = toupper(name[0]); //puts first element to upper case
}
for (int i = 1; i < size; i++)
{
if (name[i] == ' ') //checks if an element is equal to a space
{
name[i] = toupper(name[i]); //space is set to upper case
name[i + 1] = toupper(name[i + 1]); //the element next to space is set to upper case
i++; //i is incremented
}
else
{
name[i] = tolower(name[i]);
}
}
return name;
}
friend std::ostream& operator <<(std::ostream& i, Person& per);
private:
std::string name;
int yearOfBirth;
};
std::ostream& operator <<(std::ostream& i, Person& per)
{
std::cout << per.getName() << " was born in " << per.yearOfBirth;
return i;
}
int main()
{
Person Name("rAVniK JAgPaL", 1997);
std::cout << Name << std::endl;
return 0;
}
I still recommend you look into the string find operators.
Something to note, your getName() permanently changes the name data member. Is that something you want? If you don't, then copy the name string to a temporary string and modify and return that string.
Thanks so much, it actually works as it should now. I appreciate it. But, if u dont mind, could u explain the following section from the code:
I dont understand how a space can be upper case or lower case? Isnt it just a blank space? i just need an explanation on how it works. Thanks.
1 2 3 4 5 6 7 8 9 10
if (name[i] == ' ') //checks if an element is equal to a space
{
name[i] = toupper(name[i]); //space is set to upper case
name[i + 1] = toupper(name[i + 1]); //the element next to space is set to upper case
i++; //i is incremented
}
else
{
name[i] = tolower(name[i]);
}
Also, so for bothering again but how does the following overloaded function work? I read the chapter in c++ for overloading but still dont get it. Also, what did u really change to make the year appear on the screen, how did removing getName change it? I no this might be alot to ask for but im really new to this stuff and im having a hard time understanding it even after reading through the book. Thanks.
1 2 3 4 5
std::ostream& operator <<(std::ostream& i, Person& per)
{
std::cout << per.getName() << " was born in " << per.yearOfBirth;
return i;
}
im having a hard time understanding it even after reading through the book.
Welcome to my world! ;)
+=========+
cout is written to deal with the built-in data types, ints, floats, strings, etc.
Your class created a custom data type, and the operator<< function overload is used so cout is able to work with your class as seamlessly as it does the other data types.
So what u mean is that the << operator in the main is pretty much calling the overloaded ostream& operator which gets the converted name from the getName() and the year and then displays them?