creating a class with function to get data and display the same. n then creating the array for the same object of the class eg( if my class is person and p is the object) can we use array for p where i can take multiple inputs?
i tried it out but didn't got the desired o/p
#include<iostream>
#include<string>
class person
{
string name[];
int age;
public:
void getdata()
{
cout<<"name";
cin>>name;
cout<<"age";
cin>>age;
}
void display()
{
cout<<name;
cout<<age;
}
}p;
void main()
{
int p[3];
int i;
for(i=0;i<4;i++)
{ p[i]=p.getdata();}
for(i=0;i<4;i++)
{ p[i]=p.display();}
}
If you want to create something like a database, object p would not help! p must be an array and the p in main() must not be an array. it is not even needed. Corrected code with brief explanation:
#include<iostream>
#include<string>
#include<cstdlib> //header files you know them
usingnamespace std; //this namespace is to make cout and cin visible.
class person
{
string name;
int age;
public:
void getdata()
{
cout<<"name: ";
getline(cin,name,'\n'); //this is to accept a line. ie: it waits for the user to hit enter,
//and is good b'cause it allows the user to leave spaces. eg: Micheal Sambo.
// cin, will read up to micheal because of the space but getline will read the whole line!!
cout<<"age: ";
cin>>age;
cin.ignore(); //to flush the buffer in order to avoid running into codes without waiting for user input
}
void display()
{
cout<<name<<endl; //endl - end line just like '\n'
cout<<age<<endl<<flush<<"\n"; //flush - also to flush buffer. note used with cout.
}
}p[5]; //array of a class. an array of type person with five elements
int main()
{
int i;
for(i=0;i<5;i++)
{
p[i].getdata();
}
system("CLS"); //to clear screen defined in cstdlib header file
for(i=0;i<5;i++)
{
p[i].display();
}
cin.sync(); //also to clear buffer
cin.get(); //wait for user to hit enter
}
@ Aceix: Not to be too picky but you should be careful saying things like "always include XXX...". I'm referring to your comment on Line 5 regarding the inclusion of the std namespace. In this particular case the std namespace is really only useful for console programming with the STL. You would rarely use it if you are writing with an API like WinAPI, SDL, SFML or others like it for example. The problem comes up because it defines A LOT more then cout and cin and although memory isn't as scarce of a resource as it once was due to things like the amount of memory in modern systems and the optimization of your code by the linker and compiler, it's still bad practice to include things that you're not using.
Also, although Line 28 is fine, it may not be clear to a new user what you are doing, you may want to mention in your comments that this is the declaration of the array. This is also a global variable, which given to scope of the 'person' array, should be avoided.
Well, "always do X" rules solely exist for beginners. No experienced programmer would pay them any heed. using doesn't include anything, so this is not a matter of memory.
The std namespace has dozens of templates, classes and structs. If you told me that they were all going to be included anyway or that they were optimized out at compile time then I could bring myself to believe it, otherwise all of those things are using up space on the v-table at run time. Maybe I am just being pedantic, but for me it was harder to unlearn habits like this then if I were to not learned them in the first place.