Create class called person. This class has 3 data members as follows: o firstNam
ID: 3811117 • Letter: C
Question
Create class called person. This class has 3 data members as follows: o firstName : this data member holds the person’s first name. o lastName: this data member holds the person’s last name. o 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 File :-
#ifndef PERSON_H
#define PERSON_H
#include "assert.h"
#include <string>
class Person
{
public:
void setAge(int age);
int getAge(int age);
void setName(string firstName,string lastName);
string getFirstName() const {return firstName;}
string getLastName() const {return lastName;}
private:
string firstName;
string lastName;
int age;
};
#endif // PERSON_H
.cpp (class implementation)
#include "person.h"
#include <string>
using namespace::std;
void Person::setAge(int age){
if(age =< 1900)
this->age = 1900;
if(age > 2017)
this->age = 2017;
}
void Person::setName(string firstName,string lastName){
this.firstName=firstName;
this.lastName=lastName;
}
int Person::getAge(int age){
if(age>2017)
return (age - this->age);
if(age < 1990)
return (this-> age - age);
}
Program :-
#include <iostream>
#include "person.h"
int main()
{
Person s;
s.setName("Amarkant","Satya");
s.setAge(1987);
std::cout << s.getFirstName() << ' ';
std::cout << s.getLastName() << ' ';
std::cout << s.getAge() << ' ';
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.