Currently need help answering this question, please follow the steps shown Creat
ID: 3843384 • Letter: C
Question
Currently need help answering this question, please follow the steps shown
Create class called person. This class has 3 data members as follows: first Name: this data member holds the person's first name. lastName: this data member holds the person's last name. birthYear: this data member holds the year the person was born. You need to validate the birth year. Set the birth year to 1900 if a value of less than 1900 is provided and set it to 2017 if a value of more than 2017 is provided. All data members of the class must be private. Make sure you create the appropriate functions to access these data (get and set functions). You need to create a public member function of the class called getAge() which returns the age of the person. The age is calculated as the difference between the current year (2017) and the birth year. Make sure you separate the class interface from the class implementation. Do not forget to use the preprocessor wrappers (#ifndef, #define, #endif). Create a program to test this class. To complete the assignment, you need to submit a header file (class interface), .cpp (class implementation), and your program (.cpp file). Also, you need to submit your application (.exe file).Explanation / Answer
//Header files
#include <iostream>
using namespace std;
//Class defination
class person
{string firstname;
string lastname;
int birthyear;
public:
string getfirstname()
{return firstname;}
string getlastname()
{return lastname;}
void setfirstname(string f)
{firstname=f;}
void setlastname(string l)
{lastname=l;}
void setbirthyear(int b) //For setting the birthyear
{if (b<1990) //test condition
birthyear=1990;
else
birthyear=b;
}
int getage() //For calculating the age of the person
{int age;
age=2017-birthyear;
return age;
}
};
//implementation of the class
int main()
{person p; //object for accessing the member functions
p.setbirthyear(1997);
p.setfirstname("Sheldon");
p.setlastname("cooper");
cout<<"FirstName="<<p.getfirstname()<<endl; //Display the First Name
cout<<"LastNmae="<<p.getlastname()<<endl; //Display the last Name
cout<<"Age="<<p.getage(); //Display the age
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.