OOPs, I did it again.... lol....

This is a discussion on OOPs, I did it again.... lol.... within the C++ Programming forums, part of the General Programming Boards category; I think I understand, polymorphism is when the inherited class changes the member functions... Like when mallard speaks, it changes ...

  1. #16
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I think I understand, polymorphism is when the inherited class changes the member functions... Like when mallard speaks, it changes speak from "I'm a duck," to "quack, quack," or "squeek, squeek."

    I can already see how useful it is!

    *imagines all the fun programming jokes you can make with polymorphism*
    Last edited by Shamino; 11-17-2005 at 03:06 AM.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Also, with this bit of code, are we just connecting Mallard to the parent class Duck?
    Code:
    class Mallard : public Duck
    That's called inheritance. That means Mallard inherits the members and functions from the Duck class. Here is an example:
    Code:
    #include<iostream>
    using namespace std;
    
    class Duck
    {
    public:
    	void speak()
    	{
    		cout<<"I'm a Duck."<<endl;
    	}
    };
    
    class Mallard : public Duck
    {
    public:
    	void greeting()
    	{
    		cout<<"Hi, I'm a Mallard."<<endl;
    	}
    };
    
    int main()
    {
    	Mallard M;
    	M.greeting();
    
    	M.speak();
    
    	return 0;
    }
    Notice how a Mallard could call the speak() function even though there is no speak() function declared in Mallard.

  3. #18
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I understand now...

    *feels enlightened*

    Now, when you Mallard inherits Ducks speak ability(function), mallard does it a bit differently, so it uses polymorphism to change exactly what the speak ability(function) is.

    So the two generally come hand in hand.... But of course, not always...


    What happens when we have two base classes, and one child inherits both? (real life issue!)
    Do we create a super child with ALL of the traits of the parents? or does it meld together? Or do you decide how they mix in your code?
    Last edited by Shamino; 11-17-2005 at 03:45 AM.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There are three possibilities with respect to functions: overloading, overriding, and hiding.

    Overloading occurs when two functions declared in the same scope(that eliminates inherited functions since they are declared in a different scope) have the same name but they have different parameters. The correct function will be called based on what parameters you use.

    If a Derived class function has the same name as a Base class function--it doesn't even have to have the same parameters, just the same name--then it hides the Base class function.

    If a Derived class function has the same name and parameters as a Base class function, and the Base class function is declared virtual, then the Derived class function is said to override the Base class function, and the version in the Derived class will be called if you use a Base class pointer to call the function and the Base class pointer points to a Derived class object.
    Last edited by 7stud; 11-17-2005 at 03:52 AM.

  5. #20
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I see, so the generally complex subject can be melded down into how it works in the real world, with people, animals, humans...

    We've created a monster.

    I'm definately claiming that quote in this context

    Overloading occurs when you inherit blue AND green eyes, I guess blue-green makes turqoise eyes!

    Overriding occurs when your kid dyes their hair blue even though they got natural red hair (those silly emo kids!), still have hair, but it has polymorphisized!

    hiding occurs when you inherit big feet instead of little feet, you don't have little feet at all any more, you got those big ones...
    Last edited by Shamino; 11-17-2005 at 03:51 AM.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What happens when we have two base classes, and one child inherits both? (real life issue!)
    Do we create a super child with ALL of the traits of the parents? or does it meld together? Or do you decide how they mix in your code?
    Thats called multiple inheritance and the Derived class inherits all the member variables and functions from each of the Base classes. Here is an example:
    Code:
    #include<iostream>
    using namespace std;
    
    class Duck
    {
    public:
    	void speak()
    	{
    		cout<<"I'm a Duck."<<endl;
    	}
    };
    
    class Biped
    {
    public:
    	void walk()
    	{
    		cout<<"Waddle, waddle."<<endl;
    	}
    };
    
    class Mallard : public Duck, public Biped
    {
    	//empty
    };
    
    int main()
    {
    	Mallard M;
    	M.speak();
    	M.walk();
    
    	return 0;
    }

  7. #22
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    If we ever decided to turn the human genome into written code... I believe it would be in C++

  8. #23
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Overloading occurs when you inherit blue AND green eyes, I guess blue-green makes turqoise eyes!
    Nope. Overloading doesn't involve inherited functions--read my post again.

    hiding occurs when you inherit big feet instead of little feet
    Hiding keeps you from calling the inherited function, similar to the way the outer x is hidden by the inner x:
    Code:
    int main()
    {
         int x = 10;
         if(true)
         {
              int x = 30;
              cout<<x<<endl;
         }
         return 0;
    }
    There is a way to get around a Derived class hiding an inherited function.
    Last edited by 7stud; 11-17-2005 at 04:03 AM.

  9. #24
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Ahhh, overloading is like learning to speak two new languages then! You can learn spanish and french, theyre both a Language function, but its like

    Language(Spanish), Language(French)


    So hiding is like, instead of inheriting feet at all, you like, freakin mutate and grow fins! Fins are still ways to move, just, not feet

    Please forgive my metaphors, they help me understand the subject fully.
    Last edited by Shamino; 11-17-2005 at 04:17 AM.

  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I updated the Duck example to get rid of the awkwardness of using the operateDuck() function:
    Code:
    #include<iostream>
    using namespace std;
    
    class Sound; 
    
    class Duck
    {
    public:
    	Duck(Sound* p); 
    	Duck();
    	
    	virtual void species() const = 0;
    	void operateDuck();  //see definition after main()
    	
    private:
    	Sound* pSound;
    };
    
    ////////////////////////////
    class Sound
    {
    public:
    	virtual void speak() = 0;
    };
    
    class Quack: public Sound
    {
    private:
    	void speak()
    	{
    		cout<<"Quack, quack."<<endl;
    	}
    };
    
    class Squeak: public Sound
    {
    private:
    	void speak()
    	{
    		cout<<"Squeak, squeak."<<endl;
    	}
    };
    //////////////////////////////
    
    class Mallard : public Duck
    {
    public:
    	Mallard(Sound* p) : Duck(p)
    	{}
    	Mallard()
    	{}
    
    	void species() const
    	{
    		cout<<"Mallard"<<endl;
    	}
    
    };
    
    class RubberDuck : public Duck
    {
    public:
    	RubberDuck(Sound* p) : Duck(p)
    	{}
    	RubberDuck()
    	{}
    	
    	void species() const
    	{
    		cout<<"Rubber"<<endl;
    	}
    };
    
    int main()
    {
    	Quack Q;
    	Squeak S;
    
    	Mallard myDuck(&Q);
    	RubberDuck yourDuck(&S);
    
    	myDuck.operateDuck();
    	cout<<endl;
    	yourDuck.operateDuck();
    
    
    	return 0;
    }
    
    ////////////////////////
    Duck::Duck(Sound* p)
    {
    	pSound = p;
    }
    Duck::Duck()
    {}
    
    void Duck::operateDuck()
    {
    	this->species();
    
    	pSound->speak();  
    }
    ///////////////////////////
    Last edited by 7stud; 11-18-2005 at 12:53 AM.

  11. #26
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    It's so complex yet so simple at the same time, concepts are simple, syntax is so confusing though...

    I understand the whole thing except for the Constructors and Destructors, never really payed attention to those in school, i'll read over them then... Also, the parameters that operateDuck takes, I kindof don't understand them. I know that they take Ducks* and a myDuck.getSound() function.. but what do these data types mean?

    Code:
    void operateDuck(Duck* const pDuck, Sound* const pSound)
    I know that Duck* means we need a pointer to duck, but what is the const pDuck part? I know Sound* means we need a pointer to sound, but what does const pSound mean?

  12. #27
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Shamino
    It's so complex yet so simple at the same time, concepts are simple, syntax is so confusing though...

    I understand the whole thing except for the Constructors and Destructors, never really payed attention to those in school, i'll read over them then...
    Constructors are required to create objects of the class. Destructors are used to do some tidying up before an object is destroyed, which happens when an object goes out of scope. For instance, if you use 'new' in a constructor to dynamically allocate some memory for one of the member variables of an object, then you have to be sure to 'delete' it.

    [quote]Also, the parameters that operateDuck takes, I kindof don't understand them. I know that they take Ducks* and a myDuck.getSound() function.. but what do these data types mean?

    Code:
    void operateDuck(Duck* const pDuck, Sound* const pSound)


    I know that Duck* means we need a pointer to duck, but what is the const pDuck part? I know Sound* means we need a pointer to sound, but what does const pSound mean?
    I rewrote that function to make it easier to use, but to answer your question, making a function parameter const prohibits the function from changing the parameter. If you know that your function shouldn't change a parameter, then you should declare the parameter const. That way if you make a mistake and accidentally try to change the parameter inside the function, the compiler will flag it as an error and then you can correct the error. On the other hand, if you need to change the parameter inside the function, then don't declare the parameter const.

    The more times you look at the syntax and try to figure out what it does, the easier it becomes.
    Last edited by 7stud; 11-17-2005 at 11:35 PM.

  13. #28
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Here is my attempt at an object oriented animal Hierarchy, I'm stuck from here, don't look at the content (what they say) it might be goofy and wrong!...

    My questions, How do I go about creating an operate Animal function, I'll end up throwing these into vectors and such, what would my next step from here be?

    Code:
    #include <iostream.h>
    
    class Animals
    {
    public:
    	Animals();
    	~Animals();
    
    	virtual void Movement()
    	{
    	}
    	virtual void Eat()
    	{
    	}
    };
    
    class Mammals : public Animals
    {
    public:
    	Mammals();
    	~Mammals();
    		void Movement()
    	{
    		cout << "I use my legs!" << endl;
    	}
    		void Eat()
    	{
    		cout << "I eat anything I can find!" << endl;
    	}
    };
    
    class Reptiles : public Animals
    {
    public:
    	Reptiles();
    	~Reptiles();
    		 void Movement()
    	{
    		cout << "I use my legs and slither!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    };
    
    class Birds : public Animals
    {
    public:
    	Birds();
    	~Birds();
    		void Movement()
    	{
    		cout << "I fly for the most part, some of the oddballs walk!" << endl;
    	}
    	    void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    
    };
    
    class Amphibians : public Animals
    {
    public:
    	Amphibians();
    	~Amphibians();
    		void Movement()
    	{
    		cout << "I generally crawl with my legs, and slither, AND swim!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    };
    
    class Fish : public Animals
    {
    public:
    	Fish();
    	~Fish();
    		void Movement()
    	{
    		cout << "You guessed it, I swim! (some of us get beached though, stupid fat whales!)" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I eat anything I can find in the water!" << endl;
    	}
    };
    
    int main()
    {
    	return 0;
    }
    Last edited by Shamino; 11-18-2005 at 12:19 AM.

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Are you asking about operator overloading?

  15. #30
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)#include <iostream.h> --> <iostream>

    2) Since it looks like you are defining all your functions inside the clase, these also have to be defined:
    Code:
    Animals();
    ~Animals();
    Those are just prototypes(the function headers). In addition, the compiler supplies invisible default constructors and destructors that do nothing for you. If you define other constructors, i.e. one's that have parameters, then the compiler won't supply a default constructor, so if you need one you have to include it in the class yourself.

    3)
    My questions, How do I go about creating an operate Animal function,
    The goal is:

    a) to write a single function that will accept any derived class object and
    b) to have the function call the correct version of the function in the derived class.

    To accomplish a) you need to assign your objects to a base class pointer. A base class pointer can point to any derived object. Then you can pass the base class pointers to the function.

    To accomplish b) requires "polymorphic" behavior, which also requires a base class pointer that points to a derived object. In addition, it requires that you declare the base class function as virtual. Then, you use the base class pointer to call the virtual function, and the version of the function that is called will correspond to the type of the object that the base class pointer points to.
    Last edited by 7stud; 11-18-2005 at 12:29 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why C++ is not OOPS
    By noobcpp in forum C++ Programming
    Replies: 13
    Last Post: 10-20-2008, 01:20 PM
  2. lol need some help :D
    By lolol in forum C Programming
    Replies: 28
    Last Post: 03-28-2008, 07:46 AM
  3. Oops, she did it again...
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-19-2004, 02:34 PM
  4. lol, funniest acting program EVER -- and i need help with it :(
    By Leeman_s in forum Windows Programming
    Replies: 1
    Last Post: 01-09-2003, 06:27 PM
  5. Lol!
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2002, 11:29 AM

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