161k views
0 votes
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. The user can be of type string or Profile; each follower can also be of type string or Profile.

User Seer
by
5.0k points

1 Answer

6 votes

Answer:

C++ code explained below with its sample outputs

Step-by-step explanation:

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

------

User Dan Bennett
by
5.1k points