Write a class called Person that has two data members - a string variable called
ID: 3869352 • Letter: W
Question
Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods.
Write a separate function (not part of the Person class) called stdDev that takes two parameters - an array of Persons and the size of the array - and returns the standard deviation of all the ages (the population standard deviation that uses a denominator of N, not the sample standard deviation, which uses a different denominator).
The files must be named Person.hpp, Person.cpp and stdDev.cpp.
(You cannot use pointer)
Explanation / Answer
Person.h:
#include<iostream>
#include<string>
using namespace std;
#ifndef PERSON_INCLUDE
#define PERSON_INCLUDE
class Person
{
public:
string name;
double age;
public:
Person()
{
age=0;
}
Person(string a,double b );
void getdetails();
};
#endif
Person.cpp:
#include<iostream>
#include<string>
#include "Person.h"
using namespace std;
Person::Person(string x,double y)
{
name=x;
age=y;
}
void Person::getdetails()
{
cout<<" Name: "<<name;
cout<<" Age: "<<age;
}
stdDev.cpp:
#include<iostream>
#include<string>
#include "Person.h"
using namespace std;
double stdDev(Person g[2],int sz)
{
int sum=0;
double mean, standardDeviation = 0.0;
cout<<" Person details:";
for(int i=1;i<=sz;i++)
{
g[i].getdetails();
sum=sum+g[i].age;
}
mean=sum/sz;
for(int k = 1; k <= sz; ++k)
standardDeviation += pow(g[k].age - mean, 2);
return sqrt(standardDeviation / sz);
}
int main()
{
int n;
string x;
double y;
Person g[5];
cout<<" Enter number of person: ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<" Enter person name:";
cin>>x;
cout<" Enter Person age: ";
cin>>y;
g[i]=Person(x,y);
}
cout<<" Standard deviation: "<<stdDev(g,n)<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.