Question

C++ There is a class called Person which has name and age as private variables. Another...

C++

There is a class called Person which has name and age as private variables.

Another class called Student, which is derived from person with gpa and id as variables.

name is a string, age and id are integers and gpa is a double... All of them are private variables.

age, gpa and id should be generated randomly when the object is created with the following ranges:

age 20 to 32

gpa 0.0 to 4.0 // this one is tricky to get as random doesnt generate decimal numbers.

id 1000 to 9999

In the start of the program, you will ask for how many students there are. dynamically create an array of students of that size. (we are not creating an object of type person for this test).

In a loop, you will ask for the names of each of these people (all the other data was already created randomly through the constructors).

Once you are done, you are to display everyone in the array neatly.

Sort the students by gpa (from highest to lowest, make sure you sort the students not just swapping of gpa) (do not use the bubbleish sort used in the review for test. look up the exact way to code Bubblesort in the book.

and display the sorted array neatly. (show the Students, not just the GPA's).

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

Answer:

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Person{
private:
string name;
int age;
public:
Person(){
name = "";
age = 20 + rand() % 13;
}
Person(string name){
age = 20 + rand() % 13;
}
string getName(){return name;}
int getAge() { return age;}
void setName(string n){ name = n;}
};

class Student: public Person{
private:
int id;
double gpa;
public:
Student(){
id = 1000 + rand() % 9000;
int x = rand() % 101; //generate a number from 0 - 100
gpa = x/100.0 * 4.0;
}
Student(string name):Person(name){
id = 1000 + rand() % 9000;
int x = rand() % 101; //generate a number from 0 - 100
gpa = x/100.0 * 4.0;
}
int getId() {return id;}
double getGPA() { return gpa;}
};

void displayStudents(Student *s, int n);
void sortStudents(Student *s, int n);

int main(){
int n;
Student *s;
string name;
cout << "How many students? ";
cin >> n;
s = new Student[n];
for(int i = 0; i < n; i++){
cout << "Enter name for student " << (i+1) << ": ";
cin >> name;
s[i].setName(name);
}
cout << "Before sorting the student list is " << endl;
displayStudents(s, n);
sortStudents(s, n);
cout << "After sorting the student list is " << endl;
displayStudents(s, n);
delete []s;
}
void displayStudents(Student *s, int n){
cout << setw(15) << "Name" << setw(10) << "ID" << setw(10) << "Age" << setw(10) << "GPA" << endl;
cout << fixed << setprecision(1);
for(int i = 0; i < n; i++)
cout << setw(15) << s[i].getName() << setw(10) << s[i].getId() << setw(10) << s[i].getAge()
<< setw(10) << s[i].getGPA() << endl;
cout << endl;
}
void sortStudents(Student *s, int n){
int i, j;
Student key;
//insertion sort descending order of gpa
for(i = 1; i < n; i++ ){
j = i - 1;
key = s[i];
while(j >= 0 && key.getGPA() < s[j].getGPA()){
s[j+1] = s[j];
j--;
}
s[j+1] = key;
}
}

Add a comment
Know the answer?
Add Answer to:
C++ There is a class called Person which has name and age as private variables. Another...
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
  • Visual C# Homework 2 You are to write a program which will create a class called...

    Visual C# Homework 2 You are to write a program which will create a class called Student Each student has a name age height and weight. These variables should be declared as private. Create the correct constructors and functions. In the main, you will create 5 students. Input the data for the 5 students from a file which already has information in it. Name the file “Information.txt”. After setting up all the data, it is time to sort based on...

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

  • A. Create a class called StudentRecord that has the following private variables: year, GPA Implem...

    In Java a. Create a class called StudentRecord that has the following private variables: year, GPA Implement 2 constructors: default constructor (doesn't take any parameters, has b. an empty body) and a constructor, that initializes all variables. according to the following template: Implement Comparable interface, so that StudentRecord class has ĢompareTo c. Implement a toString() method that returns text representation of an object First name: Benoit, last name: Mandelbrot, year: 3, GPA: 3.68 d. method, that performs comparison as the...

  • PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a...

    PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a method that adds all persons Validate all input, not left empty for First Name, Last Name, Gender and numeric for age and throw an exception if it does not validate within the class. Create a Student Class that: Inherits the Person Class Accepts GPA Validate GPA (>= 0 and <= 4.0) and throw exception if it does not validate within the class. Has methods...

  • Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean)...

    Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean) variables. You have to create constructors and properties for the class. Create a “MatchingDemo” class. In the main function, the program reads the number of people in the database from a “PersonInfo.txt” file and creates a dynamic array of the object. It also reads the peoples information from “PersonInfo.txt” file and saves them into the array. In C# Give the user the ability to...

  • You are to create a class called Item which contains the following private variables:  string name;  // Will...

    You are to create a class called Item which contains the following private variables:  string name;  // Will be received from files. int weight;  //random number from 1-10 The class Weapon is derived from Item.  The variable for that class is int atk;  //random number from 1-10 The class Armor is derived from Item.  The variable for that class is int def;  //random number from 1-10 You will have 2 files.  One called "Weapons.txt" which has the following: Sword Dagger Bow Lance Mace The "Armors.txt" file...

  • You are to create a class called Item which contains the following private variables:  string name;  // Will...

    You are to create a class called Item which contains the following private variables:  string name;  // Will be received from files. int weight;  //random number from 1-10 The class Weapon is derived from Item.  The variable for that class is int atk;  //random number from 1-10 The class Armor is derived from Item.  The variable for that class is int def;  //random number from 1-10 You will have 2 files.  One called "Weapons.txt" which has the following: Sword Dagger Bow Lance Mace The "Armors.txt" file...

  • Programming: Create a class called City with two public variables: a string to store the name...

    Programming: Create a class called City with two public variables: a string to store the name of the city and a float to store the average temperature in Fahrenheit. Create one object of the class that you create and ask the user to enter the city name and the average temperature in Fahrenheit. Store these in the object of the class that you create. Then display the contents of the class. Once that is working, make the variables private and...

  • Using C++ create a class called person. A person can have a name, address, age. For...

    Using C++ create a class called person. A person can have a name, address, age. For this class create a default constructor and parameterized constructor. Show the main as int main() { } ----------------------------------------------------- Show how you will define the class and call it in the main. Only create two instances/objects in main using the two created constructors.

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

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