Question

C++, entry level no pointers or vectors. Write a class called Person that has two data...

C++, entry level no pointers or vectors.

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 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.cpp. and main method for testing

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer Source code: Person.h: # include<iostream> # include<string> using namespace std; #lfndef PERSON INCLUDE #define PERSOPerson.cpp: # include«iostream> # include<string> #include person.h using namespace std: Person: : Person (string x,doublestdDev.cpp # include< iostream> # include<string> #include Persoh.h using namespace std double stdDev (Person g[2],int sz)Sample Output rumal documents visual studio DebuglProject.exe use roie ro Enter number of person 5 Enter person name Sameera

Program code:

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<<"\nName: "<<name;

     cout<<"\nAge: "<<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<<"\nPerson 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<<"\nEnter number of person: ";

     cin>>n;

     for(int i=1;i<=n;i++)

     {

          cout<<"\nEnter person name:";

          cin>>x;

          cout<"\nEnter Person age: ";

          cin>>y;

         g[i]=Person(x,y);

     }

     cout<<"\nStandard deviation: "<<stdDev(g,n)<<endl;

     system("pause");

     return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++, entry level no pointers or vectors. Write a class called Person that has two data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Taxicab that has three int fields (data members) to store its current...

    Write a class called Taxicab that has three int fields (data members) to store its current x- and y-coordinates and the total distance it has driven so far (the actual distance driven by the Taxicab, not the Euclidean distance from it's starting point). The class should have a constructor that takes two parameters and uses them to initialize the coordinates, and also initializes the distance traveled to zero. The class should have a default constructor that sets all three fields...

  • Write a class called Player that has four data members: a string for the player's name,...

    Write a class called Player that has four data members: a string for the player's name, and an int for each of these stats: points, rebounds and assists. The class should have a default constructor that initializes the name to the empty string ("") and initializes each of the stats to -1. It should also have a constructor that takes four parameters and uses them to initialize the data members. It should have get methods for each data member. It...

  • Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has...

    Need helps on C++ attach. Thanks Pointers with classes a) A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • Write a class called Shelf that contains instance data that represents the length, breadth, and capacity...

    Write a class called Shelf that contains instance data that represents the length, breadth, and capacity of the shelf. Also include a boolean variable called occupied as instance data that represents whether the shelf is occupied or not. Define the Shelf constructor to accept and initialize the height, width, and capacity of the shelf. Each newly created Shelf is vacant (the constructor should initialize occupied to false). Include getter and setter methods for all instance data. Include a toString method...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • 2- Write a program that has a Person class, a Student class, that is derived from...

    2- Write a program that has a Person class, a Student class, that is derived from Person, and a Faculty class, derived from Person as well, with the following specifications: The Person class has the following members: a. An integer private member variable named SSN. b. Two string protected member variables named firstName and lastName. C. An integer protected member variable named age. d. A constructor that takes SSN, first name, last name, and age as parameters. e. A constructor...

  • Write a class named Taxicab that has three private data members: one that holds the current...

    Write a class named Taxicab that has three private data members: one that holds the current x-coordinate, one that holds the current y-coordinate, and one that holds the odometer reading (the actual odometer distance driven by the Taxicab, not the Euclidean distance from its starting point). The class should have an init method that takes two parameters and uses them to initialize the coordinates, and also initializes the odometer to zero. The class should have get members for each data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT