Home > Back-end >  For help! O great god answers to these two function..
For help! O great god answers to these two function..

Time:03-10



The class People
{
Protected:
int age;
Char * name;
Public:
People ();
People (int the age, const char * name);
~ People ();
Const int GetAge ();
Int SetAge (int the age);
Const char * GetName () const;
Int elegantly-named SetName (const char * name);
Int CopyPeople (const People& AnotherPeople);
}
People: People ()
{
Age=0;
Name=new char [strlen (" NO name ") + 1);
Strcpy (name, "NO name");
}

People: People (int the age, const char * name)
{
This - & gt; Age=age;
This - & gt; Name=new char [strlen (name) + 1);
Strcpy (this - & gt; The name, name);
}
People: : ~ People ()
{
If (the name!=NULL)
The delete [] name;
}

Const int People: : GetAge ()
{
return age;
}


Int People: : SetAge (int age)
{
This - & gt; Age=age;
return 0;
}

Const const char * People: : GetName ()
{
Return this - & gt; name;
}

Int People: : elegantly-named SetName (const char * name)
{
If (this - & gt; The name!=NULL)
The delete [] this - & gt; name;
This - & gt; Name=new char [strlen (name) + 1);
Strcpy (this - & gt; The name, name);
return 0;
}

Int People: : CopyPeople (const People& AnotherPeople)
{
//* * * * * * * * * * * * * * please candidates to complete the function * * * * * * * * * * * * * *

}
Int main ()
{
//* * * * * * * * * * * * * * please candidates to complete the main function * * * * * * * * * * * * * *

return 0;

CodePudding user response:

Reference:
 # include 
# include

The class People
{
Protected:
int age;
Char * name;
Public:
People ();
People (int the age, const char * name);
~ People ();
Const int GetAge ();
Int SetAge (int the age);
Const char * GetName () const;
Int elegantly-named SetName (const char * name);
Int CopyPeople (const People& AnotherPeople);
};

People: People ()
{
Age=0;
Name=new char [strlen (" NO name ") + 1);
Strcpy (name, "NO name");
}

People: People (int the age, const char * name)
{
This - & gt; Age=age;
This - & gt; Name=new char [strlen (name) + 1);
Strcpy (this - & gt; The name, name);
}

People: : ~ People ()
{
If (the name!=NULL)
The delete [] name;
}

Const int People: : GetAge ()
{
return age;
}


Int People: : SetAge (int age)
{
This - & gt; Age=age;
return 0;
}

Const const char * People: : GetName ()
{
Return this - & gt; name;
}

Int People: : elegantly-named SetName (const char * name)
{
If (this - & gt; The name!=NULL)
The delete [] this - & gt; name;
This - & gt; Name=new char [strlen (name) + 1);
Strcpy (this - & gt; The name, name);
return 0;
}

Int People: : CopyPeople (const People& AnotherPeople)
{
//* * * * * * * * * * * * * * please candidates to complete the function * * * * * * * * * * * * * *
This - & gt; Age=anotherPeople. Age;
Strcpy (this - & gt; The name, anotherPeople. Name);
return 0;
}
Int main ()
{
//* * * * * * * * * * * * * * please candidates to complete the main function * * * * * * * * * * * * * *

People people1 (20, "Tom");//(a) create People object people1, initialized to the name and his age 20 and Tom
Printf (" % d, % s \ n ", people1. The GetName (), people1. GetAge ());//(b) output people1 object name and age
People1. Elegantly-named SetName (" Jerry ");//(c) set the name of a people1 object for Jerry

People people2.//people2 (d) declare a new object, use the default constructors to initialize the
Printf (" % d, % s \ n ", people2. The GetName (), people2. GetAge ());//(e) output people2 object name and age

People2. CopyPeople (people1);//call CopyPeople function (f), the values in the people1 copy to people2 in
Printf (" % d, % s \ n ", people2. The GetName (), people2. GetAge ());//(g) output people2 object name and age

return 0;
}
  • Related