Define a class called user_details and the class will have at least five member
ID: 3553470 • Letter: D
Question
Define a class called user_details and the class will have at least five member
variables to store first name, last name, date of birth, age, and phone number.
Include at least six member functions: a constructor to set first name, last name, date
of birth, age, and phone number. An overloaded constructor to set first name, last
name, date of birth, and phone number. The age is computed from the date of birth
when the age is not given. Second overloaded constructor to set first name, last
name, date of birth, and age. The phone number is set to all zeroes when the phone
number is not given. A function to compute the age from date of birth. A function to
validate date of birth and age. A function to validate the number of digits in a phone
number. Write a C++ program that uses this class to input the user details, validate
the details, and prints the formatted details. A format value can be used to input
different values. Format 1 can be used to input all details, 2 for all details without age,
and 3 for all details without phone number. Use time() library to get the current date.
Input/Output:
1 Mike Tyson 06301966 47 3343456453
Mike Tyson 06/30/1966 47 334-345-6453
1 Victor Oladipo 05041992 22 4554355353
Wrong age for Victor Oladipo 05/04/1992 22 455-435-5353
2 Charlie Hunnam 04101980 322333222
Wrong phone number for Charlie Hunnam 04/10/1980 33 32-233-3222
3 Tom Cruise 07031962 51
Tom Cruise 07/03/1962 51 000-000-0000
Explanation / Answer
#include <iostream>
#include <cstring>
#include <ctime> /* time_t, struct tm, difftime, time, mktime */
using namespace std;
class user_details{
public:
string first_name,last_name;
int age,phn,dob;
time_t timer;
struct tm y2k;
double seconds;
int result;
user_details(string first_name_,string last_name_,int dob_,int age_,int phn_)
{
first_name=first_name_;
last_name=last_name_;
dob=dob_;
age=age_;
phn=phn_;
//age checking
time(&timer); /* get current time; same as: timer = time(NULL) */
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = dob_%10000;
dob_=dob_/10000;
y2k.tm_mon = dob_%100;
dob_=dob_/100;
y2k.tm_mday = dob_%100;
seconds = difftime(timer,mktime(&y2k));
if(age!=(int)seconds/86400)
cout<<" Wrong age for ";
//date of birth checking
if(F_validDate(y2k.tm_mday,y2k.tm_mon,y2k.tm_year)==0)
cout<<" Wrong date of birth for ";
//phn number checking
if(F_validphn(phn)==0)
cout<<" Wrong phone number for ";
}
user_details(string first_name_,string last_name_,int dob_,int phn_)
{
first_name=first_name_;
last_name=last_name_;
dob=dob_;
phn=phn_;
time(&timer);
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = dob_%10000;
dob_=dob_/10000;
y2k.tm_mon = dob_%100;
dob_=dob_/100;
y2k.tm_mday = dob_%100;
seconds = difftime(timer,mktime(&y2k));
age=(int)seconds/86400;
//date of birth checking
if(F_validDate(y2k.tm_mday,y2k.tm_mon,y2k.tm_year)==0)
cout<<" Wrong date of birth for ";
//phn number checking
if(F_validphn(phn)==0)
cout<<" Wrong phone number for ";
}
int F_validphn(int phn)
{
int count=0;
while(phn>0)
{
phn/=10;
count++;
}
if(count==10)
return 1;
else
return 0;
}
int F_validDate(int day,int month,int year)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(day>=1&&day<=31)
return 1;
else
return 0;
case 4:
case 6:
case 9:
case 11:
if(day>=1&&day<=30)
return 1;
else
return 0;
case 2:
if(day>=1&&(year%4!=0&&day<=28)||(year%4==0&&day<=29))
return 1;
else
return 0;
} return 0;
}
};
int main()
{
string data,first_name,last_name;
int age=0,phn=0,dob=0;
user_details obj;
char *c;
getline(cin,data,' ');
c=&data+2;
while(*c!=' ')
{
first_name+=*c;
c++;
}
c++;
while(*c!=' ')
{
last_name+=*c;
c++;
}
c++;
while(*c!=' ')
{
dob=dob*10+((int)(*c)-48);
c++;
}
if(data[0]=='1')
{
c++;
while(*c!=' ')
{
age=age*10+((int)(*c)-48);
c++;
}
c++;
while(*c!='')
{
phn=phn*10+((int)(*c)-48);
c++;
}
obj=new user_details(first_name,last_name,dob,age,phn);
}else if(data[0]=='2')
{
c++;
while(*c!='')
{
phn=phn*10+((int)(*c)-48);
c++;
}
obj=new user_details(first_name,last_name,dob,phn);
}else if(data[0]=='3')
{
c++;
while(*c!=' ')
{
age=age*10+((int)(*c)-48);
c++;
}
obj=new user_details(first_name,last_name,dob,age,0000000000);
}
//printing
cout<<obj.first_name<<" "<<obj.last_name<<" ";
cout<<dob%1000000<<"/";
dob-(dob/1000000)*1000000;
cout<<dob%10000<<"/";
dob-(dob/10000)*10000;
cout<<dob<<" ";
cout<<obj.age<<" ";
phn=obj.phn;
cout<<phn%10000000<<"-";
phn-(phn/10000000)*10000000;
cout<<phn%10000<<"-";
phn-(phn/10000)*10000;
cout<<phn<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.