Thread: The programmers c++ have a question about the class

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    9

    The programmers c++ have a question about the class

    Write a class called Person, which has three private data members: first_name, last_name and year_born. The class should contain the following:
    • A default constructor.
    • An overloaded constructor that sets all variables to the values of the arguments passed to the parameters.
    • One get function for each variable that returns the current value.
    • One set function for each variable that sets a new value.
    • An overloaded >> operator that inputs a person’s information.
    • An overloaded << operator that outputs a person’s information.
    • An overloaded == operator that compares the first and last names of two persons.
    • A friend function add_person that takes an array of persons, the number of persons in the array, and another person as input parameters. The function should add the other person to the array if there is nobody in the array with that first and last name, and modify the size accordingly.

    Note: Embed your class in a test program.

    Code:
    #include <string>
    using namespace std;
    
    class Person
    {
     public:
    	Person();
    	Person(string fn, string ln, int y);
    	string get_first_name();
        string get_last_name();
    	int    get_year_born();
    	void set_first_name(string fn);
    	void set_last_name(string ln);
    	void set_year_born(int y);
    	friend ostream& operator <<(ostream& outs, const Person& P);
    	friend istream& operator >>(istream& ins, Person& P);
    	friend void operator ==(Person& P1, const Person& P2);
    	friend int sum_age(Person person[], int number, int current_year);
    
     private:
    	string first_name, last_name;
    	int year_born;
    };
    
    int main()
    {
    	Person People[4];
    	
    	People[0] = Person("Salma", "Foad", 1990);
    	People[1] = Person("Sophie", "Smith", 1986);
    	People[2] = Person("Neda'a", "Hesham", 1984);
    	People[3] = Person("Bushra", "Sultan", 1980);
    
    
    
    	cout << "The sum of all ages is: " << sum_age(People, 4, 2006) << " years." << endl;
    	return 0;
    }
    
    Person::Person() : year_born(0)
    { 
    }
    
    Person::Person(string fn, string ln, int y) : first_name(fn), last_name(ln), year_born(y)
    { 
    }
    
    string Person::get_first_name() 
    {
    	return first_name;
    }
    
    
    string Person::get_last_name() 
    {
    	return last_name;
    }
    
    string Person::get_year_born() 
    {
    	return year_born;
    }
    
    void Person::set_first_name(string fn)
    {	
    	first_name = fn;
    }
    
    void Person::set_last_name(string ln)
    {	
    	last_name = ln;
    }
    void Person::set_year_born(int y)
    {	
    	year_born = y;
    }
    
    ostream& operator <<(ostream& outs, const Person& P)
    {
    	outs << P.first_name << ":" << P.last_name<<":"<<p.year_born;
    	return outs;
    }
    
    
    istream& operator >>(istream& ins, Person& P)
    {
    	cout << "Enter first_name: ";
    	ins >> P.first_name;
    	
    	cout << "Enter last_name: ";
    	ins >> P.last_name;
    	
    	cout << "Enter year_born: ";
    	ins >> P.year_born;
    	
    	return ins;
    }
    
    
    void operator ==(Person& P1, const Person& P2)
    {
    	int extra = 0;
    
    	P1.last_name == P2.last_name;
    	P1.first_name == p2.first_name=extra ;
    }
    
    
    
    int sum_age(Person person[], int number, int current_year)
    {
    	if (number < 0)
    		return 0;
    	
    	int sum=0;
    	for (int i=0; i<number; i++)
    		sum = sum + (current_year - person[i].year_born);
    
    	return sum;
    }
    this question and this answer please help me about correct answer
    Last edited by Salem; 11-17-2007 at 04:35 PM. Reason: detag the prose

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to see that you have made an attempt. Now, how does it not work? Or are you just looking for ways to improve code that works?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A friend function add_person that takes an array of persons, the number of persons in the array, and another person as input parameters. The function should add the other person to the array if there is nobody in the array with that first and last name, and modify the size accordingly.
    I think instead of this requirement you have implemented a function to sum up the ages?

    Operator == should return a bool: whether both the first and last names are equal.
    Then you can use this operator to check if the array contains a Person object that is equal to what you are planning to add.

    P.S I don't see a reason why this function should be a friend because the public interface has everything to implement this function.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    thanks everybody I want correct answer

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Our policy is to not write out whole solutions for homework questions. http://cboard.cprogramming.com/annou...t.php?f=3&a=39

    If you have a specific question, great. If you have some code that you want comments on, fine. But we're not going to do your homework for you. As MacGyver's signature says: http://cboard.cprogramming.com/image...ine=1175208402

    How is this answer that you have provided not correct? Did you write it?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    this answer me I want correct answer

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    this answer me I want correct answer
    How disappointing, I even congratulated you on making an attempt

    Since you insist on going against forum policy:
    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM