Using C++ Write a Twitter class that sets the Twitter user first (who is to be f
ID: 3878111 • Letter: U
Question
Using C++
Write a Twitter class that sets the Twitter user first (who is to be followed) and lets the client add up to 5 followers, which will be stored in an array with a capacity of 5. These users/followers could be in the form of string or Profile. (Note that the followers are not stored as users, but as strings or Profiles).
At any time, your main program should be able to remove any user, x from following another user, y (i.e., x is no longer in the array of followers of y).
You are free to design your program as you wish as long as you fulfill the following requirements:
Twitter class should be declared as a template class (the template parameter defines how the followers are represented)
Twitter class includes a constructor that initializes the name of the user that is passed. The number of followers starts off at 0.
AddFollower function in Twitter class should be defined correctly. It takes as parameter an object of the template parameter.
RemoveFollower function in Twitter class should be defined correctly. It takes as parameter an object of the template parameter.
PrintFollowers function in Twitter class should be defined correctly. It displays the name of all the followers. (Remember: each follower is an object of the template parameter. As you will declare Profile struct which can be a template parameter, for the Profile struct, we must overload stream insertion << operator. Now, we can use cout << follower to display the follower).
Submitting a main program that tests the Twitter class as follows:
Twitter<string> object
Twitter<Profile> object
Whenever any change is made to a Twitter object (like a follower is added, a follower is removed), call PrintFollowers to display the followers of the Twitter object.
Tips:
Your Profile struct definition along with overloaded << operator could look like the following. Note that you define the Profile struct in your main program (not in the header). You may also need to overload the == operator for the RemoveFollower function.
struct Profile {
string userName;
int age;
string state;
ostream& operator << (ostream & output, Profile p) {
output << p.userName;
return output;
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
Twitter.h
========
#ifndef Twitter_h
#define Twitter_h
#include <iostream>
using std::string;
using std::cout;
using std::endl;
template <typename T>
class Twitter
{
private:
string name;
T followers[5];
int numFollowers;
public:
Twitter(string n);
bool AddFollower(T follower);
bool RemoveFollower(T follower);
void PrintFollowers();
};
template <typename T>
Twitter<T>::Twitter(string n)
{
name = n;
numFollowers = 0;
}
template <typename T>
bool Twitter<T>::AddFollower(T follower)
{
if(numFollowers < 5)
{
followers[numFollowers] = follower;
numFollowers++;
return true;
}
else
return false;
}
template <typename T>
bool Twitter<T>::RemoveFollower(T follower)
{
int index;
bool found = false;
for(index = 0; index < numFollowers; index++)
{
if(followers[index] == follower)
{
found = true;
break;
}
}
if(found)
{
//shift all other followers after the one to be removed to one position left
for(++index; index < numFollowers; ++index)
{
followers[index-1] = followers[index];
}
numFollowers--;
return true;
}
else
return false;
}
template <typename T>
void Twitter<T>::PrintFollowers()
{
cout << "Followers for " << name << endl;
for(int i = 0; i < numFollowers; i++)
cout << followers[i] << endl;
cout << "------" << endl << endl;
}
#endif /* Twitter_h */
main.cpp
=========
#include "Twitter.h"
#include <iostream>
using namespace std;
struct Profile {
string userName;
int age;
string state;
};
ostream& operator << (ostream & output, Profile p) {
output << p.userName;
return output;
}
bool operator == (const Profile &p1, const Profile p2)
{
return p1.userName == p2.userName;
}
int main()
{
Twitter<string> t1("John");
Twitter<Profile> t2("Bob");
cout << "Adding followers to John" << endl;
t1.AddFollower("Bill");
t1.AddFollower("Jack");
t1.PrintFollowers();
Profile p1 = {"Alice", 20, "Alaska"};
Profile p2 = {"Janet", 22, "California"};
Profile p3 = {"Jim", 20, "Texas"};
cout << "Adding follower profiles to Bob" << endl;
t2.AddFollower(p1);
t2.AddFollower(p2);
t2.AddFollower(p3);
t2.PrintFollowers();
cout << "Removing Bill from John" << endl;
t1.RemoveFollower("Bill");
cout << "Removing Janet's profile from Bob" << endl << endl;
t2.RemoveFollower(p2);
t1.PrintFollowers();
t2.PrintFollowers();
}
output
-======
Adding followers to John
Followers for John
Bill
Jack
------
Adding follower profiles to Bob
Followers for Bob
Alice
Janet
Jim
------
Removing Bill from John
Removing Janet's profile from Bob
Followers for John
Jack
------
Followers for Bob
Alice
Jim
------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.