C++ and can you write the Main function in a separate file? Also have Person.hpp
ID: 3918608 • Letter: C
Question
C++ and can you write the Main function in a separate file? Also have Person.hpp, Person.cpp. and stdDev.cpp in a separate file. No global variables!
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 dai mcm bers. It should he?ve gci: mci hods fr both (lata mcm )crs (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 Person objects 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.cppExplanation / Answer
//Person.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Person
{
string name;
int age;
public:
Person(){}
Person(string str, int n);
string getName() const;
int getAge() const;
};
---------------------------------------------------------------
//Person.cpp
#include"Jul_28_Person.h"
Person::Person(string str, int n)
{
name = str;
age = n;
}
string Person::getName() const
{
return name;
}
int Person::getAge() const
{
return age;
}
-------------------------------------------------------
//stdDev.cpp
#include<cmath>
#include"Jul_28_Person.h"
double stdDev(Person arr[], int n);
//function definition
double stdDev(Person arr[], int n)
{
double sum=0,mean,dev;
//first find mean and then
for (int i = 0; i < n; i++)
{
sum += arr[i].getAge();
}
mean = sum / n;
sum = 0;
for (int i = 0; i < n; i++)
{
sum += pow(arr[i].getAge() - mean,2);
}
dev = sqrt(sum/n);
return dev;
}
---------------------------------------------
//main.cpp
#include"Jul_28_Person.h"
//since function is declared in another file stdDev.cpp ,,we need to declare as extern in main function
extern double stdDev(Person arr[], int n);
int main()
{
//declare a pointer to object Person
Person *persons;
string name;
int n,age;
cout << "Enter how many persons? ";
cin >> n;
//allocate memory for n number of person
persons = new Person[n];
for (int i = 0; i < n; i++)
{
cout << "Enter the name of a person: ";
cin >> name;
cout << "Enter the age of a Person: ";
cin >> age;
//initialize
persons[i] = Person(name, age);
}
//now calculate standard deviation of age
cout<<"Standard deviation is: "<<stdDev(persons, n)<<endl;
}
/*output
Enter how many persons? 5
Enter the name of a person: John
Enter the age of a Person: 24
Enter the name of a person: Smith
Enter the age of a Person: 28
Enter the name of a person: Mary
Enter the age of a Person: 30
Enter the name of a person: Samantha
Enter the age of a Person: 32
Enter the name of a person: Ronald
Enter the age of a Person: 35
Standard deviation is: 3.70945
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.