I must make a program which has a class (Person) which has phone and name attributes, I must create an arrangement to be able to call 5 different people, which allows requesting said required information and after it has it, show it completely .
#include <iostream>
#include <string.h>
using namespace std;
class Persona{
int telefono;
string nombre;
public:
void getPersona();
};
void Persona::getPersona(){
cout<<"Telefono: "<<telefono<<endl;
cout<<"Nombre: "<<nombre<<endl;
}
int main (){
Persona personas[5];
for (int i=1; i<6; i++){
cout<<personas[i].getPersona()<<endl;
}
return 0;
}
The new problem that comes out is this, I would like to understand it if possible
My observations are:
cout
expects a value . The functiongetPersona()
returns none.Just write
personas[i].getPersona();
and problem solved.The array has 5 elements and the for goes through the numbers from 1 to 5 inclusive.
This means that in the last iteration it will try to access the element at position 5 (the sixth).
The only possible positions for the arrays are between 0 and N-1 where N is the number of elements. For this they would be 0, 1, 2, 3 and 4.
std::endl
inserts the newline character into the stream and "cleans" it.It is not an error but it is important to know, since its repeated use can cause a significant decrease in the performance of your program.
More information in this question and on this recommended site .
string.h
contains functions that work with C-style strings (char *
).You may have wanted to include the header
string
.Well, look, as a result of the fact that I perceive that you are just beginning in this world of programming, I cannot make very complex observations; however, there are certain changes that I would make to your Person class for a better implementation.
Changed Points:
*It is not necessary to use a string object, since the type std::string or any object that pretends to fulfill the role of representing a string of alphanumeric characters, was made to implement a principle of software architecture called: covariance decorators. Your case is not so complex as to implement such sophisticated entities. Therefore, I recommend giving an alias to a constant pointer of type char to store your string natively and dynamically (this will make the string persistent and its characters will not be lost even when you pass data from one function to another, also applies with objects).
* For good programming practice and, taking into account that you are working with C++ and not pure C, I recommend that you implement an explicit constructor in your class. Explicit constructors are those that are declared through the new operator and usually receive a series of data whose purpose is to initialize the private attributes of the object in question.
*For example:
entity_t *entity = new entity_t(); //Constructor explicito.
* After this, you must structure your respective getters, which are access methods to be able to verify the values of your encapsulated attributes (standard practice but not necessary).
* Finally, a more elegant way to print the data inside your attributes in a string format is through the << operator overload. The friend modifier means that this overload will not be attached as a member function to the object itself; therefore, you'll notice that you must pass your object as an immutable reference. * Last point, the error in your vector is because you have initialized the for loop in the wrong way and you are trying to access a memory location that does not exist.
for(std::size_t i = 0; i < 6; i++) {...}
Your file would look like this: