Thread: Sharing Class functions

  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.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I cannot see the problem. showPlaylist() is a public function of Playlist().
    RadioStation has two Playlist members tracksToBroadcast and topTracks

    so it is ok in RadioStation::userOptions() to call

    Code:
     topTracks.showPlaylist();
    or
    Code:
     tracksToBroadcast.showPlaylist();
    no friend needed

    Kurt

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Friends are for making private or protected data/functions available to specific outside functions/classes/structs. As your function is public, you don't need that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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