113k views
0 votes
Create a class Student and another called StudentClub. StudentClub should have the fields President, Vice-President, Secretary, and Treasurer, all of which should be pointers to a Student object. In this way, the same student could hold different offices. In addition, StudentClub should have a club members field, which should be a vector of pointers to Student objects. Your class definition should include (but not limited to) the following constructor and member functions.

StudentClub(Student* p, Student* v, Student* s, Student* t, vector m); Student* get_president() const; Student* get_vice_president() const; Student* get_secretary() const; Student* get_treasurer() const; vector get_members() const; void add_member(Student* s); int number_members()

1 Answer

1 vote

Answer:

See explaination

Step-by-step explanation:

StudentClub.h

#ifndef STUDENTCLUB_H_INCLUDED

#define STUDENTCLUB_H_INCLUDED

//#include <vector>

//class student

class Student

{

public:

std::string name;

Student(){}

Student(std::string name)

{

this->name=name;

}

};

//studentclub class

class StudentClub

{

Student* President=new Student();

Student* VicePresident=new Student();

Student* Secretary=new Student();

Student* Treasurer=new Student();

std::vector<Student> clubMember;

public:

StudentClub(Student* p, Student* v, Student* s, Student* t, std::vector<Student> m);

Student* get_president() const;

Student* get_vice_president() const;

Student* get_secretary() const;

Student* get_treasurer() const;

std::vector<Student> get_members() const;

void add_member(Student* s);

int number_members();

};

#endif // STUDENTCLUB_H_INCLUDED

StudentClub.cpp

#include <iostream>

#include <vector>

#include "StudentClub.h"

using namespace std;

//constructor with parameter

StudentClub::StudentClub(Student* p, Student* v, Student* s, Student* t, vector<Student> m)

{

President =p;

VicePresident=v;

this->Secretary =s;

this->Treasurer =t;

this->clubMember=m;

}

//return president

Student* StudentClub::get_president() const

{

return this->President;

}

//return vice president

Student* StudentClub::get_vice_president() const

{

return this->VicePresident;

}

//return secretary

Student* StudentClub::get_secretary() const

{

return this->Secretary;

}

//return treasurer

Student* StudentClub::get_treasurer() const

{

return this->Treasurer;

}

//return memeber

vector<Student> StudentClub::get_members() const

{

return clubMember;

}

//add member

void StudentClub::add_member(Student* s)

{

for(int i = 0; i < clubMember.size(); i++)

{

if(clubMember[i].name == s->name)

return ;

}

this->clubMember.push_back(*s);

}

//return memeber count

int StudentClub::number_members()

{

return clubMember.size();

}

main.cpp

#include <iostream>

#include <vector>

#include "StudentClub.h"

using namespace std;

int main()

{

//declarations

Student p,s,v,t,m;

//get names

cout<<"President: ";

cin>>p.name;

cout<<"VicePresident: ";

cin>>v.name;

cout<<"Secretary: ";

cin>>s.name;

cout<<"Treasurer: ";

cin>>t.name;

vector<Student> cl;

//get members

do{

cout<<"\\New member (Q to quit): ";

cin>>m.name;

if(m.name!="Q")

cl.push_back(m);

}while(m.name!="Q");

//add to club

StudentClub club(&p,&v,&s,&t,cl);

club.add_member(&p);

club.add_member(&v);

club.add_member(&s);

club.add_member(&t);

//print details

cout<<"\\\\President: "<<club.get_president()->name;

cout<<"\\VicePresident: "<<club.get_vice_president()->name;

cout<<"\\Secretary: "<<club.get_secretary()->name;

cout<<"\\Treasurer: "<<club.get_treasurer()->name;

cout<<"\\Total Members: "<<club.number_members();

return 0;

}

User Rahul Pareta
by
6.5k points