My Two C-- Program Using Classes, Objects and Functions From My Local Community
My Two C-- Program Using Classes, Objects and Functions From My Local Community
ID: 23/CS/TEC/130
Department: Computer science
Level: Degree year two(2)
Assignment
Create two C++ programs with classes, objects and functions. The program should be real
life example from something in a local community with Sierra Leone and you should prompt
users for inputs instead of using constants.
First Program
Local Sports Club Management
#include <iostream>
#include <vector>
#include <string>
class Activity {
public:
string name;
Activity(string n) : name(n) {}
};
class Member {
public:
string name;
vector<Activity> activities;
Member(string n) : name(n) {}
void addActivity() {
string activityName;
cout << "Enter the name of the activity: ";
getline(cin, activityName);
activities.push_back(Activity(activityName));
cout << "Activity added successfully! ";
}
void displayActivities() {
cout << " Activities for Member " << name << endl;
for (const auto& activity : activities) {
cout << "Activity: " << activity.name <<endl;
}
}
};
class SportsClub {
private:
vector<Member> members;
public:
void addMember() {
string memberName;
cout << " Enter the name of the member: ";
getline(cin, memberName);
members.push_back(Member(memberName));
cout << " Member added successfully! ";
}
void manageMemberActivities() {
string memberName;
cout << " Enter the name of the member to manage activities: ";
getline(cin, memberName);
void displayAllActivities() {
for (const auto& member : members) {
member.displayActivities();
}
}
};
int main() {
SportsClub club;
int choice;
do {
cout << " Add a Member, Manage Member Activities, Display All Activities, Exit";
cout << " Choose an option: ";
cin >> choice;
cin.ignore(); // clear the input buffer
switch (choice) {
case 1:
club.addMember();
break;
case 2:
club.manageMemberActivities();
break;
case 3:
club.displayAllActivities();
break;
case 4:
cout << " Exiting “;
break;
default:
cout << " Invalid choice. Please try again ” ;
}
} while (choice != 4);
return 0;
}
Program two(2)
Local Cultural Event Organizer
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class EventParticipant {
public:
string name;
string role; // e.g., Performer, Attendee
class CulturalEvent {
public:
string title;
vector<EventParticipant> participants;
CulturalEvent(string t) : title(t) {}
void addParticipant() {
string name, role;
cout << "Enter the name of the participant:";
getline(cin, name);
cout << "Enter the role of the participant: ";
getline(cin, role);
participants.push_back(EventParticipant(name, role));
cout << " Participant added successfully! ";
}
void displayParticipants() {
cout << " Participants in the event '" << title <<endl;
for (const auto& participant : participants) {
cout << " Name: " << participant.name << ", Role: " << participant.role <<endl;
}
}
};
class EventOrganizer {
private:
vector<CulturalEvent> events;
public:
void addEvent() {
string title;
cout << " Enter the title of the cultural event: ";
getline(cin, title);
events.push_back(CulturalEvent(title));
cout << " Event added successfully! ";
}
void manageEventParticipants() {
string title;
cout << "Enter the title of the event to manage participants: ";
getline(cin, title);
void displayAllParticipants() {
for (const auto& event : events) {
event.displayParticipants();
}
}
};
int main() {
EventOrganizer organizer;
int choice;
do {
cout << " Add a Cultural Event . Manage Event Participants . Display All Participants.
Exit ";
cout << "Choose an option: ";
cin >> choice;
cin.ignore(); // clear the input buffer
switch (choice) {
case 1:
organizer.addEvent();
break;
case 2:
organizer.manageEventParticipants();
break;
case 3:
organizer.displayAllParticipants();
break;
case 4:
cout << "Exiting ";
break;
default:
cout << "Invalid choice. Please try again “;
}
} while (choice != 4);
return 0;
}