C++. Do not user pointers or global variables. Write a class called Person that
ID: 3869362 • Letter: C
Question
C++.
Do not user pointers or global variables.
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, stdDev.cpp. and stdDevmain.cpp
Explanation / Answer
If you have any doubts, please give me comment...
Person.hpp
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(){}
Person(string n, double a);
string getName();
double getAge();
private:
string name;
double age;
};
Person.cpp
#include "Person.hpp"
Person::Person(string n, double a)
{
name = n;
age = a;
}
string Person::getName()
{
return name;
}
double Person::getAge()
{
return age;
}
StdDev.cpp
#include <iostream>
#include <string>
#include <cmath>
#include "Person.hpp"
using namespace std;
#define SIZE 5
double stdDev(Person *p, int n);
int main()
{
Person *p = new Person[SIZE];
p[0] = Person("ABC", 20);
p[1] = Person("ABC", 45);
p[2] = Person("ABC", 35);
p[3] = Person("ABC", 28);
p[4] = Person("ABC", 25);
double sd = stdDev(p, SIZE);
cout<<"Standard Deviation: "<<sd<<endl;
return 0;
}
double stdDev(Person *p, int n)
{
double mean = 0.0;
for (int i = 0; i < n; i++)
{
mean += p[i].getAge();
}
mean /= n;
double sd = 0.0;
for (int i = 0; i < n; i++)
{
sd += (p[i].getAge() - mean) * (p[i].getAge() - mean);
}
sd = sqrt(sd / n);
return sd;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.