Thread: Sharing Class functions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Sharing Class functions

    Hello there, it's been a while since I posted so it's nice to see you guys again!

    Straight to the point... I have a class which has a certain function, but I need that function to be used by another class. I made some research and the best solution might be using a "friend function". Is it the best way to do so? Can anyone shed some light on this topic for a better understanding? Thanks in advance.

    Code:
    class Playlist
    {
    public:
        Playlist();
    
    
        // "set" methods
        void setMusicList(const std::string& filename);
    
    
        // "get" methods
        std::vector<uint> getMusicList() const {return musicList;};
    
    
        // other
        void addSongToPlaylist(uint songID);
        void removeSongFromPlaylist(uint songID);
        void showPlaylist() const;  -> Function which needs to be used by a RadioStation (class) function
        void orderPlaylist();
        void writePlaylistToFile(const std::string& filename);
    
    
    private:
        std::vector<uint> musicList;
    
    
        // these methods can be private, the function orderPlaylist calls them
        void swapElementsInPlaylist();
        bool isOrdered() const;
    };
    
    
    class RadioStation
    {
    public:
        RadioStation(); // creates a radio station
    
    
        // "set" methods
        void setName(const std::string& n) {name = n;};
        void setTracksToBroadcast();
        void setTopTracks();
        void setUsers();
    
    
        // "get" methods
        std::string getName() const {return name;};
        Playlist getTTBroadcast() const {return tracksToBroadcast;};
        Playlist getTopTracks() const {return topTracks;};
        std::vector<User> getUsers() const {return radioUsers;};
    
    
        // other
        void addUser(User u); // adds a new listener to a previously created r station
        void createTopTenFile();
        void showTopTracks() const; //  show top ten tracks
        void createNewUser();
    
    
        // radio station menus
        void firstLaunch();
        void userAccount(const std::string& lineWithUserInfo);
        void userOptions(); -> Function which requires the "showPlaylist" function
        void login();
        void signup();
        void mainMenu();
    
    
    
    
    private:
        std::string name;
        Playlist tracksToBroadcast;
        Playlist topTracks;
        std::vector<User> radioUsers;
    
    
    };
    Last edited by Khabz; 05-25-2013 at 11:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions in class and class variables.
    By Karakus in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2006, 03:29 AM
  2. Class and functions
    By Rune Hunter in forum C++ Programming
    Replies: 15
    Last Post: 08-05-2005, 09:17 PM
  3. Class sharing
    By Heavens in forum C++ Programming
    Replies: 9
    Last Post: 06-28-2005, 10:24 AM
  4. Problem with class functions.
    By Jaken Veina in forum C++ Programming
    Replies: 11
    Last Post: 06-07-2005, 11:48 AM
  5. sharing same functions
    By devil@work in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2003, 12:34 AM