Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
using namespace std;
class Person
{
        private:
		char name[20], work[30];
        int age;
        public:
		void input();
        void display();
                        
                
};
void Person::input()
{
        cout<<"Enter Name:";
        cin>>name;
        cout<<"Enter Age:";
        cin>>age;
        cout<<"Enter Work:";
        cin>>work;
}
void Person::display()
{
        cout<<"\nName:"<<name;
        cout<<"\nAge:"<<age;
        cout<<"\nWork:"<<work;
        
}
int main()
{
        Person p;
        p.input();
        p.display();
}


What I have tried:

#include<iostream>
using namespace std;
class Person 
{        
  private:          
    string name;  
    string work; 
    int age;      
    Person(string n, string w, int a); 
};


Person::Person(string n, string w, int a) 
{
	    cout<<"Enter Name:";
        cin>>n;
        cout<<"Enter Age:";
        cin>>a;
        cout<<"Enter Work:";
        cin>>w;
  		name=n;
  		work=w;
  		age=a;
}

int main() 
{
  
  Person p
  

  cout << p.name << " " << p.work << " " << p.age << "\n";
  
  return 0;
}
Posted
Updated 4-Jun-21 7:58am

Don't do it like that: the Person class should know nothing about the environment in which it is running, so it should never assume that the console will be available. Hence, it should never try to get information directly from the user, but get the name, age, and job title passed to it as constructor parameters when the new instance is created.

The main function can prompt the user and get the responses, store them, and pass them to the Person class when it builds it.

That way, you can reuse the same class to hold a collection of people read from a file or a database for example, or from textboxes in a windows or web based app without any changes to it.
 
Share this answer
 
Comments
[no name] 2-Jun-21 15:29pm    
that is, what should I do??
OriginalGriff 2-Jun-21 16:00pm    
Read what I said, read your course notes, and then start coding?
[no name] 2-Jun-21 16:23pm    
my english is not good and google translate does not help much
OriginalGriff 2-Jun-21 16:25pm    
Which bits are giving you problems?
[no name] 2-Jun-21 16:26pm    
"The main function can prompt the user and get the responses, store them, and pass them to the Person class when it builds it."
You look a bit confused.
As already suggested, you should avoid I/O calls in your class, on the contrary, the main function should collect input from the user, call the object constructor and produce the output based on the object properties (publicly exposed as methods).
Try, for instance
C++
#include<iostream>
using namespace std;


class Person
{
  string name;
  string job;
  int age;
public:
  Person(string  name, string job, int age) : name{name}, job{job}, age{age}
  {
  }
  string get_name() { return name; }
  string get_job() { return job; }
  int get_age() { return age; }
};


int main()
{
  string name, job;
  int age;

  cout << "plaese enter name, job and age: \n";

  cin >> name >> job >> age;

  Person p{ name, job, age };

  cout << "name = " << p.get_name() << ", job = " << p.get_job() << ", age = " << p.get_age() << endl;
}
 
Share this answer
 
Comments
[no name] 4-Jun-21 6:41am    
in my notes i dont hove something like
name{name}, job{job}, age{age}
{ return name; }
maybe for that i dont understand
thanks a lot but my teacher is not the best....
CPallini 4-Jun-21 7:12am    
You may replace that with
name(name), job(job), age(age)
or even rewrite the constructor this way
Person(string name, string job, int age)
{
this->name = name;
this->job = job;
this->age = age;
}
Since you are just beginning to learn, I will try to point you only to the steps you need to do first:

1. You asked for a solution that uses a constructor. So write a constructor. In the comments you already offered one version with a working constructor: it had three function arguments and assigned them to the internal data members. That's right, keep this. CPallini also offered one. It uses a different syntax but does essentially the same thing. If you want to learn about this syntax, google for "c++ member initializer list"

2. Next, to use this constructor, you must first determine the values for the constructor arguments. As the others already pointed out, define variables for these arguments within the main function, then read the values like you did in your original input function and store the inputs into your variables.

3. After you have read all the variables, you can call the constructor. Note that you can not define a variable of type person before you have the arguments needed for the constructor! Since you seem to have trouble about constructor syntax, to call a constructor with arguments, you have to list the arguments in brackets after the variable name, e. g.:
Person p(name, job, age);
instead of just:
Person p;

4. Last you can call the display method, but you already did that.

There are a number of things to improve this code, but for now just make it work as described here.

If you like, you can think on OriginalGriffs comment that the class Person should not know about the console - this does not only refer to the input, but also to the display function! So, to take your task one step further: what would it take to move the console I/O entirely out of the class Person?
 
Share this answer
 
a normal pattern for this is a factory function which does your person code with asking the user for input BEFORE creating the object and best practice is that some checking like age over 0 and name not empty. If no valid person can be created it returns NULL which handles the external code.
 
Share this answer
 
#include<iostream>
using namespace std;
/*me thn xrhsh constructor emfanizw to onoma thn douleia kai thn hlikia*/

class Person
{
	private:
  	string name;
  	string work;
  	int age;
	public:
  	Person(string  name, string work, int age) : name(name), work(work), age(age)
 	 {
  		}
  	string get_name() { return name; }
  	string get_work() { return work; }
  	int get_age() { return age; }
};


int main()
{
  	string name, work;
  	int age;
  
  	cout<<"Enter Name:";
	cin >> name;
	
	cout<<"Enter work:";
	cin >> work;
	

	cout<<"Enter Age:";
	cin >> age;

	Person p(name, work, age);

    cout <<"\n"<< "name: "<< p.get_name()<<"\n"<< "work: "<< p.get_work()<<"\n"<< "age: "<< p.get_age()<< endl;
}


thanks all for your help!!!!!
 
Share this answer
 
#include<iostream>
using namespace std;
class Person
{
        private:
		char name[20], work[30];
        int age;
        public:
		Person();
		~Person();
		void getname();
		void getage();
		void getwork();
                        
                
};
Person::Person()
{
	cout<<"Enter Name:";
	gets(name);
	
	cout<<"Enter Work:";
	gets(work);
	
	do{
	cout<<"Enter Age:";
	cin>>age;
	}while(age<1);
	


}
Person::~Person()
{
	
}
void Person::getname()
{
	puts(name);
}
void Person::getwork()
{
	puts(work);
}
void Person::getage()
{
	cout<<age;
}


int main()
{
	
	Person p;
	
	cout<<"\nName:";
	p.getname();
	
	cout<<"\nWork:";
	p.getwork();
	
	cout<<"\nAge:";
	p.getage();
	

	return 0;
}
 
Share this answer
 
Comments
Dave Kreskowiak 4-Jun-21 13:51pm    
Wrong again. DO NOT use cin, cout, gets, or puts in ANY of your Person class code.

The Person class should only be concerned with holding and manipulating the data of a Person. It should NEVER have anything to do with getting that data from the user, nor outputting that data to the console.
[no name] 4-Jun-21 13:56pm    
#include<iostream>
using namespace std;
/*me thn xrhsh constructor emfanizw to onoma thn douleia kai thn hlikia*/

class Person
{
private:
string name;
string work;
int age;
public:
Person(string name, string work, int age) : name(name), work(work), age(age)
{
}
string get_name() { return name; }
string get_work() { return work; }
int get_age() { return age; }
};


int main()
{
string name, work;
int age;

cout<<"Enter Name:";
cin >> name;

cout<<"Enter work:";
cin >> work;


cout<<"Enter Age:";
cin >> age;

Person p(name, work, age);

cout <<"\n"<< "name: "<< p.get_name()<<"\n"<< "work: "<< p.get_work()<<"\n"<< "age: "<< p.get_age()<< endl;
}

now work!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900