dynamic or static binding

This is a discussion on dynamic or static binding within the C++ Programming forums, part of the General Programming Boards category; This would be an example of dynamic binding right, the use(aPager) and use(aPager)?? Code: class Device { public: virtual void ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    dynamic or static binding

    This would be an example of dynamic binding right, the use(aPager) and use(aPager)??

    Code:
    class Device
    {
    public:
    	virtual void operate();
    	const char* getID() const;
    	static int getDeviceCount();
    	virtual ~Device();
    };
    
    class Phone : public Device
    {
    public:
    	virtual void operate();
    	static int getDeviceCount();
    	virtual ~Phone();
    };
    
    class Pager : public Device
    {
    public:
    	virtual void operate();
    	virtual void erase();
    	virtual ~Pager();
    };
    
    void use(Device& d)
    {
    	d.operate();		
    	d.getDeviceCount();	
    }
    
    int main()
    {
    Phone aPhone;
    Pager aPager;
                    use(aPhone);		
    	use(aPager);		
    }
    Last edited by noob2c; 08-06-2003 at 02:24 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    int main()
    {
    use(aPhone);
    use(aPager);
    }

    Do you expect anyone to take that main() function seriously? Where is the declaration of aPhone and aPager? What types are those variables: ints? base class types? derived class types?

    The idea is to:

    1)create an instance of the derived class
    2)create a base class pointer
    3)assign the address of the derived class to the base class pointer
    4)call functions using the base class pointer and see what happens

    If you use the keyword virtual for a function, then you will get dynamic binding, which means the compiler will look at the type pointed to by the base class pointer at runtime, and use that type's version of the function. Without the virtual keyword you get static binding at compile time, which means the compiler uses the base class pointer's version of the function.

    Here is an example:
    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:	
    	
    	Base(int n)
    	{
    		num1 = n;		
    	}
    
    	virtual void print(void)
    	{
    		cout<<"Base Class: "<<num1<<endl;
    	}
    
    	void identify(void)
    	{
    		cout<<"This is the Base class function."<<endl;
    	}
    
    private:
    	int num1;
    };
    
    
    class Derived: public Base
    {
    
    public:
    
    	Derived(int n, double d):Base(n)
    	{
    		num2 = d;
    	}
    
    	void print(void)
    	{
    		cout<<"Derived class: "<<num2<<endl;
    	}
    	
    	void identify(void)
    	{
    		cout<<"This is the Derived class function."<<endl;
    	}
    
    private:
    
    	double num2;
    };
    
    int main()
    {
    	Derived d(1, 20.0);	
    	Base* pbase = &d;
    
    	pbase->print();//dyanmic binding
    	pbase->identify();//static binding
    
    	return 0;
    }
    Last edited by 7stud; 08-06-2003 at 03:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 07:51 PM

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