Project 7.b Write a class called Person that has two data members - a string var
ID: 3866510 • Letter: P
Question
Project 7.b 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.
Explanation / Answer
/*
******Person.hpp*******
*/
#include<iostream>
#include<string>
using namespace std;
class Person{
string name;
double age;
// Constructor with two parameters
Person(string n, double a){
name = n;
age = a;
};
// Getter functions
string getName(){
return name;
};
double getAge(){
return age;
};
};
/*
*******stDev.cpp******
*/
#include<iostream>
#include<string>
#include<person.hpp>
#include<math>
using namespace std;
double stdDev(Person p_arr[], int N){
double mean=0, variance=0, std;
for(int i=0;i<N;i++){
mean += p_arr[i].getAge();
}
mean = mean/N;
for(int i=0;i<N;i++){
variance = pow((p_arr[i].getAge()-mean),2);
}
variance = variance/N;
std = sqrt(variance);
return std;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.