Calling functions through class

This is a discussion on Calling functions through class within the C++ Programming forums, part of the General Programming Boards category; using very very simple functions because need to use inheritance in multiple classes so let's say I said Code: class ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Calling functions through class

    using very very simple functions because need to use inheritance in multiple classes

    so let's say I said

    Code:
    class a {
    
    public:
    
    void print()
    { cout << "blah blah" << endl;  }
    };
    
    class b {
    
    public:
    void print2()
    { cout << "blah blah2" << endl; }
    };
    
    int main()
    {
    //
    
    }
    return 0;

    what i'm trying to do is simply call either print or print2.. but I'm not passing anything to the function.. can this be done? Every example I have uses int values etc...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by DJPlayer
    what i'm trying to do is simply call either print or print2.. but I'm not passing anything to the function.. can this be done?
    Obviously since you can "simply call either print or print2" once you have an object of the respective class, but then you're probably not saying what you mean.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    I think I have but I'm obviously just missing some very stupid obvious basic syntax..

    in my generic program what syntax would I use to to print "blah blah" via the print function..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by DJPlayer
    I think I have but I'm obviously just missing some very stupid obvious basic syntax..

    in my generic program what syntax would I use to to print "blah blah" via the print function..
    The thing is, your question is so basic that I am not sure if you are asking the right question. My answer would be:
    Code:
    #include <iostream>
    
    class a
    {
    public:
        void print()
        {
            using namespace std;
            cout << "blah blah" << endl;
        }
    };
    
    class b
    {
    public:
        void print2()
        {
            using namespace std;
            cout << "blah blah2" << endl;
        }
    };
    
    int main()
    {
        a a_obj;
        a_obj.print();
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you asking how to pass the string in to the function, so that what can be printed changes?

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    this is my basic code and comments on what will/should go on..

    Code:
    class Phone
    {
    public:
      void dial(int* number)
    	{  cout << "Dialing " << number << "...\n";  }
    
      virtual void mute()
    	{ cout << "Phone::mute\n"; }
    
    };
    
    class Pod
    {
    public:
      
    	void Play(char* song)
    		{ cout << "Pod is Playing" << song << "....\n:  }
    
    	virtual void mute()
    		{ cout << "Pod::mute\n"; }
    };
    
    class PodPhone: public Phone, public Pod
    {
    public:
    
    // class inherits all
    
    	void Shutdown()
    		{ cout << "PodPhone user interaction has shutdown PodPhone" << endl;  }
    };
    
    
    int main()
    	{
    
    
    	int choice;
    
    	cout <<"Press 1 to Dial Phone"<<endl;
        cout <<"Press 2 to Mute/UnMute Phone"<<endl;
        cout <<"Press 3 to Play Pod"<<endl;
    	cout <<"Press 4 to Mute/UnMute Pod"<<endl;
      	cout <<"Press 5 to ShutDown PodPhone"<<endl;
    	
    	cin >> choice;
    
       while (choice <6)
    	   {
    		switch (choice)
    			{
    			case 1:
    			//Phone Dial number displayed
    			break;
    			
    			case 2:
    			//Virtual Phone Mute/Unmute
    			break;
    			
    			case 3:
    			//Play Pod Song Title displayed
    			break;
    			
    			case 4:
    			//Virtual Pod Mute/Unmute
    			break;
    			
    			case 5:
    			//PodPhone Shutdown 
    			break;
    			
    						
    			default:
                cout << "Incorrect entry - Please try again" <<"\n";
    			cout << "Re-enter a # between 1 and 5 ";
                cin >> choice; 
    		
    		}  //end switch
    	   } // end while loop
    	return 0;	
    		}
    I can't use any global variables.. but I'm sure I can get to that later.. if they're public now I can always set them to the correct class via private or protected class. I will have to implement Virtual functions for Mute

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Ah, so you want to use multiple inheritance, but cannot figure out how to call member functions of the same name and signature from different base classes. Incidentally, the fact that Phone and Pod are base classes means that they should have virtual destructors. An example of what you want to do:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Phone
    {
    public:
        virtual ~Phone() {}
    
        void dial(int number)
        {
            cout << "Dialing " << number << "...\n";
        }
    
        virtual void mute()
        {
            cout << "Phone::mute\n";
        }
    };
    
    class Pod
    {
    public:
        virtual ~Pod() {}
    
        void play(const char* song)
        {
            cout << "Pod is Playing " << song << "....\n";
        }
    
        virtual void mute()
        {
            cout << "Pod::mute\n";
        }
    };
    
    class PodPhone : public Phone, public Pod
    {
    public:
        void shutdown()
        {
            cout << "PodPhone user interaction has shutdown PodPhone" << endl;
        }
    };
    
    int main()
    {
        PodPhone pod_phone;
        pod_phone.dial(12345);
        pod_phone.Phone::mute();
        pod_phone.play("Bla Bla Black Sheep");
        pod_phone.Pod::mute();
        pod_phone.shutdown();
    }
    In particular, note how calls to the otherwise ambiguous inherited member functions are differentiated by qualifying them with the base class names.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    thanx.. after some time I did get through it.. had some major syntax issues obviously..

    I used destructors but did you virtual inheritance in PodPhone class that would seperately work the mute feature.

    I then implemented a boolean value in class Pod and Phone to keep track of it's state so that it could mute and unmute itself w/o forgetting it's status.

    thanx though.. just took a little time.. now the real task..

    making a GUI that will work on a Unix machine where I can't install any libraries.. but nobody said I can't copy them into a directory and include them..

    I was told it would be impossible to do in 2 days.. but.. I'm giving it a shot.. anyone familiar w/ GTK+ ? =]

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by DJPlayer
    I used destructors but did you virtual inheritance in PodPhone class that would seperately work the mute feature.
    I am not sure what you mean. Virtual inheritance does not seem necessary here since there is no common base class that Phone and Pod inherit from, and hence no possibility of the diamond inheritance problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    my grammar was somehow horrid on that quote.. but.. I'll explain.

    Pod and Phone both used the function mute. I had to use the PodPhone instead of Pod or Phone to control the muting/unmuting of Pod and phone. i.e. Pod mute() kept track of the state and would either output "pod is muted" or "Pod is unmuted". The same for Phone mute()I can post the code tomorrow.. It's on another computer.. this was an assignment to use:

    3 classes, pod, phone, and podphone. Podphone needed to inherit all and control mute from inherited classes. Pod and Phone had basic functions and also mute.. while keeping track of it's mute() current state. I'll post tomorrow when I'm on the PC my code is stored on..

    My problem is going to be trying to implement a Gui interface that on a unix terminal in which I have no privileges to install libraries.. I went searching for GTK+ libraries but found none anywhere on the terminal. I may have to copy the header files and include them from the implementation file directory. Sort of uncharted territory.. time to see how fast I can brush up on GTK+ GUI interfaces w/ C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read-only class members
    By kidburla in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2006, 12:52 PM
  2. Functions in class and class variables.
    By Karakus in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2006, 02:29 AM
  3. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 05:08 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Calling a Function From A Class
    By Okiesmokie in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21