Question

Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp.

Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member);

StudentClub should have the following private data members. • name: a string that stores the name of student club. • president: treasurer: pointers to Student objects. • member: a vector of pointers to Student objects. Hint: std::vector member; StudentClub should also have at least the following methods. • StudentClub(std::string n): initializes name with n, pointers as null pointers and member as a vector of size 0. • StudentClub(std::string n, Student* p, Student* t, std::vector m): initializes private members with provided arguments. • get president(), get treasurer(), get member(): return private members accordingly. Hint: don’t forget they are const methods or so-called accessors. • void set president(Student* p) and void set treasurer(Student* t). • void add member(Student* s): appends the student pointer object to member. • print: prints the information of the StudentClub class object. Change the following /* *** */ part with correct syntax to print according information on the console.

void StudentClub::print() const {

std::cout << std::setw(20) << "Club Name: " << name << std::endl; std::cout << std::setw(20) << "President Name: " << /* president name*/ << std::endl; std::cout << std::setw(20) << "Treasurer Name: " << /* treasurer name*/ << std::endl; std::cout << std::setw(20) << "Current members: " << /* number of current members */ << std::endl; }

main.cpp:

#include"StudentClub.h"

int main()
{
StudentClub math("MATHBRUINS");
Student * jess = new Student("Jess",20);
Student * john = new Student("John",50);
Student * kate = new Student("Kate",20);
Student * mary = new Student("Mary",30);
// set president/treasurer
math.set_president(john);
math.set_treasurer(john);
// update members
math.add_member(kate);
math.add_member(mary);
math.add_member(jess);
math.add_member(john);
//print Student club info
math.print();
// find youngest students and print their names
std::vector<std::string> youth;
youth = find_youngest(math.get_member());
std::cout << std::setw(20) << "Youngest members:";
for (int i=0; i<youth.size(); ++i)
std::cout << " " << youth[i] ;
std::cout << std::endl;

// reclaim memory, without doing so, it runs into memory leaks
// even though the memory space will be recalled by the OS after the program terminates, we should manually reclaim the memory after declaring them

delete jess;
delete john;
delete kate;
delete mary;

return 0;
}

Student.h:

#ifndef __STUDENT_H__
#define __STUDENT_H__

#include<string>
#include<vector>
#include<iomanip>
#include<iostream>

class Student
{
private:
std::string name;
int age;
public:
Student(std::string n, int y);
std::string get_name() const;
int get_age() const;
};


#endif

Student.cpp:

#include "Student.h"

Student::Student(std::string n, int y)
{
name = n;
age = y;
}

std::string Student::get_name() const
{
return name;
}

int Student::get_age() const
{
return age;
}

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

hi, here is the code for StudentClub class. Please try it out. If you face any problems let me know, I will help.

StudentClub.h


#ifndef STUDENTCLUB_H_
#define STUDENTCLUB_H_
#include <string>
#include "Student.h"
using namespace std;

class StudentClub
{
       string name;
       Student * president;
       Student * treasurer;
       vector<Student *> member;


   public:
       StudentClub(string n);
       StudentClub(string n, Student* p, Student* t, vector<Student *> m);
       const vector<Student*>& get_member() const;
       const string& get_name() const;
       Student* get_president() const;
       Student* get_treasurer() const;
       void set_president(Student *&president);
       void set_treasurer(Student *&treasurer);
       void add_member(Student * s);
       void print() const;
};
vector<string> find_youngest(vector<Student *> member);
#endif /* STUDENTCLUB_H_ */

StudentClub.cpp

#include "StudentClub.h"

const vector<Student*>& StudentClub::get_member() const
{
   return member;
}

const string& StudentClub::get_name() const
{
   return this->name;
}

Student* StudentClub::get_president() const
{
   return president;
}

Student* StudentClub::get_treasurer() const
{
   return treasurer;
}


void StudentClub::set_president(Student *&president)
{
   this->president = president;
}

void StudentClub::set_treasurer(Student *&treasurer)
{
   this->treasurer = treasurer;
}

StudentClub::StudentClub(string n)
{
   this->name = n;
   // set president
   this->president = NULL;
   // set treasurer
   this->treasurer = NULL;
   // set members
   this->member = {};
}

StudentClub::StudentClub(string n, Student *p, Student *t, vector<Student *> m)
{
   // set name
   this->name = n;
   // set president
   this->president = p;
   // set treasurer
   this->treasurer = t;
   // set members
   this->member = m;
}

void StudentClub::add_member(Student *s)
{
   this->member.push_back(s);
}

void StudentClub::print() const
{
   cout << setw(20) << "Club Name: " << name << endl;
   cout << setw(20) << "President Name: " << this->president->get_name()<< endl;
   cout << setw(20) << "Treasurer Name: " << this->treasurer->get_name() << endl;
   cout << setw(20) << "Current members: " << this->member.size() << endl;
}

vector<string> find_youngest(std::vector<Student *> member)
{
   vector<string> youngestMembers;
   int youngestAge;
   // set youngestAge as the first member's age
   if(member.size()>=1)
   {
       youngestAge = member[0]->get_age();
   }
   for(size_t i = 0; i < member.size();++i)
   {
       // identify the youngest Age among members
       if(youngestAge > member[i]->get_age())
       {
           youngestAge = member[i]->get_age();
       }
   }
   // collect names of members with age matching youngestAge
   for(size_t i = 0; i < member.size();++i)
   {
       if(youngestAge == member[i]->get_age())
       {
           youngestMembers.push_back(member[i]->get_name());
       }
   }
   return youngestMembers;
}

Output
Add a comment
Know the answer?
Add Answer to:
Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...
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
  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the...

    -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the passport photo that belongs in that particular passport. This Photo class holds its location or file name and the pixel data of the image. In this lab the pixel data is only used to show how memory maybe used when a program uses large objects. We will not...

  • // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...

    // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello virtual function (hint this will be done in Person class // 2) Change main to create two Person pointer variables // 3) Set each pointer to one of the two objects already instantiated below // 4) Use the pointer variable to invoke the two sayHello() methods #include <iostream > #include <string > using namespace std ; class Person { protected : string name; int...

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the...

    lab 11 Do not change main.cpp, i need c++ code for queue.h and queue.cpp Given the complete main() function, partial queue class header queue.h, and queue.cpp, you will complete the class declaration and class implementation. The following member functions are required: constructor enqueue() dequeue() You may elect to create the following helper functions: isFull() isEmpty() A description of these ADT operations are available in this Zybook and in the textbook's chapter 17. Example: If the input is: 3 Led Zepplin...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • C++ / Visual Studio Implement the member function below that returns true if the given value...

    C++ / Visual Studio Implement the member function below that returns true if the given value is in the container, and false if not. Your code should use iterators so it works with any type of container and data. template< typename Value, class Container > bool member( const Value& val, const Container& cont ); just implement this function in the code below #include <iostream> #include <array> #include <deque> #include <list> #include <set> #include <string> #include <vector> using namespace std; #define...

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