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; Code: #include <iostream> #include <vector> class Animal { public: virtual void Movement() { } virtual void Eat() { } }; ...

  1. #31
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Code:
    #include <iostream>
    #include <vector>
    
    class Animal
    {
    public:
    
    	virtual void Movement()
    	{
    	}
    	virtual void Eat()
    	{
    	}
    
    };
    
    class Mammals : public Animal
    {
    public:
    		void Movement()
    	{
    		cout << "I use my legs!" << endl;
    	}
    		void Eat()
    	{
    		cout << "I eat anything I can find!" << endl;
    	}
    };
    
    class Reptiles : public Animal
    {
    public:
    		 void Movement()
    	{
    		cout << "I use my legs and slither!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    };
    
    class Birds : public Animal
    {
    public:
    		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 Animal
    {
    public:
    		void Movement()
    	{
    		cout << "I generally crawl with my legs, and slither, AND swim!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    };
    
    class Fishes : public Animal
    {
    public:
    		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;
    	}
    };
    
    void operateAnimals(vector<Animal*> Animals)
    {
    	for(int i=0; i<Animals.size(); i++)
    	{
    		//This is where the interesting stuff happens:
    		Animals[i]->Movement(); 
    		Animals[i]->Eat();
    	}
    }
    
    
    int main()
    {
    	Animal allAnimals;
    	Mammals Mammal;
    	Reptiles Reptile;
    	Birds Bird;
    	Amphibians Amphibian;
    	Fishes Fish;
    
    	vector<Animal*> Animals;
    	Animals.push_back(&Mammals);
    	Animals.push_back(&Reptile);
    	Animals.push_back(&Bird);
    	Animals.push_back(&Bird);
    	Animals.push_back(&Amphibian);
    	Animals.push_back(&Fish);
    
    	operateAnimals(Animals);
    
    	return 0;
    }

    Lotsa errors, here they are, when i make it include <iostream> couts and stuff are undeclared, and it seems that it isnt taking vector either?

    Code:
    --------------------Configuration: Practice with OOP - Win32 Debug--------------------
    Compiling...
    Practice.cpp
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(22) : error C2065: 'cout' : undeclared identifier
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(22) : error C2297: '<<' : illegal, right operand has type 'char [15]'
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(22) : error C2065: 'endl' : undeclared identifier
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(26) : error C2297: '<<' : illegal, right operand has type 'char [27]'
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(83) : error C2065: 'vector' : undeclared identifier
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(83) : error C2059: syntax error : '>'
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(84) : error C2143: syntax error : missing ';' before '{'
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(84) : error C2447: missing function header (old-style formal list?)
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(103) : error C2059: syntax error : '>'
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(104) : error C2065: 'Animals' : undeclared identifier
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(104) : error C2228: left of '.push_back' must have class/struct/union type
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(104) : error C2275: 'Mammals' : illegal use of this type as an expression
            c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(17) : see declaration of 'Mammals'
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(105) : error C2228: left of '.push_back' must have class/struct/union type
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(106) : error C2228: left of '.push_back' must have class/struct/union type
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(107) : error C2228: left of '.push_back' must have class/struct/union type
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(108) : error C2228: left of '.push_back' must have class/struct/union type
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(109) : error C2228: left of '.push_back' must have class/struct/union type
    c:\documents and settings\jonathan\my documents\c++ programming\opengl with nehe\nehe tutorials\practice with oop\practice.cpp(111) : error C2065: 'operateAnimals' : undeclared identifier
    Error executing cl.exe.
    
    Practice with OOP.exe - 18 error(s), 0 warning(s)

  2. #32
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Fixed it!

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace::std;
    
    class Animal
    {
    public:
    
    	virtual void Movement()
    	{
    	}
    	virtual void Eat()
    	{
    	}
    
    };
    
    class Mammals : public Animal
    {
    public:
    		void Movement()
    	{
    		cout << "I use my legs!" << endl;
    	}
    		void Eat()
    	{
    		cout << "I eat anything I can find!" << endl;
    	}
    };
    
    class Reptiles : public Animal
    {
    public:
    		 void Movement()
    	{
    		cout << "I use my legs and slither!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    };
    
    class Birds : public Animal
    {
    public:
    		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 Animal
    {
    public:
    		void Movement()
    	{
    		cout << "I generally crawl with my legs, and slither, AND swim!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    };
    
    class Fishes : public Animal
    {
    public:
    		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;
    	}
    };
    
    void operateAnimals(vector<Animal*> Animals)
    {
    	for(int i=0; i<Animals.size(); i++)
    	{
    		//This is where the interesting stuff happens:
    		Animals[i]->Movement(); 
    		Animals[i]->Eat();
    	}
    }
    
    
    int main()
    {
    	Animal allAnimals;
    	Mammals Mammal;
    	Reptiles Reptile;
    	Birds Bird;
    	Amphibians Amphibian;
    	Fishes Fish;
    
    	vector<Animal*> Animals;
    	Animals.push_back(&Mammal);
    	Animals.push_back(&Reptile);
    	Animals.push_back(&Bird);
    	Animals.push_back(&Amphibian);
    	Animals.push_back(&Fish);
    
    	operateAnimals(Animals);
    
    	return 0;
    }
    It works!

  3. #33
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    OMG this is amazing! I can keep narrowing the species and races down, the coding is SO easy now!

    I added a Dog class, that inherits mammals, which inherits Animal...

    I can now go from dog, to Terrier, hound, poodle

    down to Jack Russel Terrier, Coon Hound, Toy poodle!!!

    So now I can still use the animal class to access DOG! Freakin amazing!

    OOps, I've done it...

    End thread.

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace::std;
    /////////////////////////////////////
    class Animal
    {
    public:
    	virtual void Speak()
    	{
    	}
    	virtual void Movement()
    	{
    	}
    	virtual void Eat()
    	{
    	}
    	virtual void Species()
    	{
    	}
    
    };
    /////////////////////////////////////
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Mammals : public Animal
    {
    public:	
    	
    		void Movement()
    	{
    		cout << "I use my legs!" << endl;
    	}
    		void Eat()
    	{
    		cout << "I eat anything I can find!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm a Mammal!" << endl;
    	}
    
    
    };
    
    class Reptiles : public Animal
    {
    public:
    
    		 void Movement()
    	{
    		cout << "I use my legs and slither!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm a Reptile!" << endl;
    	}
    };
    
    class Birds : public Animal
    {
    public:
    
    		void Movement()
    	{
    		cout << "I fly for the most part, some of the oddballs walk!" << endl;
    	}
    	    void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm a Bird!" << endl;
    	}
    
    };
    
    class Amphibians : public Animal
    {
    public:
    
    		void Movement()
    	{
    		cout << "I generally crawl with my legs, and slither, AND swim!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm an Amphibian!" << endl;
    	}
    };
    
    class Fishes : public Animal
    {
    public:
    		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;
    	}
    		void Species()
    	{
    		cout << "I'm a Fish!" << endl;
    	}
    };
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    class Dogs :  public Mammals
    {
    public:
    	void Speak()
    	{
    		cout << "Bark! Bark!" << endl;
    	}
    };
    
    ////////////////////////////////////////////////////////////
    void operateAnimals(vector<Animal*> Animals)
    {
    	for(int i=0; i<Animals.size(); i++)
    	{
    		//This is where the interesting stuff happens:
    		Animals[i]->Species();
    		Animals[i]->Movement(); 
    		Animals[i]->Eat();
    		Animals[i]->Speak();
    		cout << endl;
    
    	}
    }
    ////////////////////////////////////////////////////////////
    
    int main()
    {
    	Animal allAnimals;
    	Mammals Mammal;
    	Reptiles Reptile;
    	Birds Bird;
    	Amphibians Amphibian;
    	Fishes Fish;
    	Dogs Dog;
    
    	vector<Animal*> Animals;
    	Animals.push_back(&Mammal);
    	Animals.push_back(&Reptile);
    	Animals.push_back(&Bird);
    	Animals.push_back(&Amphibian);
    	Animals.push_back(&Fish);
    	Animals.push_back(&Dog);
    
    	operateAnimals(Animals);
    	return 0;
    }

  4. #34
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Example of a Simple Class Hierarchy!

    I think this is as object oriented as it gets, lets start from the bottom: We have Poodle, Poodle is a Dog, Dog is a Mammal, Mammal is an Animal.

    All animals have skin, can make noise, can move, can eat, and have a species, thus declared in the Animal class. We use like, ultra inheritence and polymorphism to define the poodle class, it inherits alot, and changes only a few things, very cool!

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace::std;
    /////////////////////////////////////
    class Animal
    {
    public:
    	virtual void Skin()
    	{
    	}
    	virtual void Speak()
    	{
    	}
    	virtual void Movement()
    	{
    	}
    	virtual void Eat()
    	{
    	}
    	virtual void Species()
    	{
    	}
    
    };
    /////////////////////////////////////
    
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Mammals : public Animal
    {
    public:	
    	
    		void Movement()
    	{
    		cout << "I use my legs!" << endl;
    	}
    		void Eat()
    	{
    		cout << "I eat anything I can find!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm a Mammal!" << endl;
    	}
    
    
    };
    
    class Reptiles : public Animal
    {
    public:
    
    		 void Movement()
    	{
    		cout << "I use my legs and slither!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm a Reptile!" << endl;
    	}
    };
    
    class Birds : public Animal
    {
    public:
    
    		void Movement()
    	{
    		cout << "I fly for the most part, some of the oddballs walk!" << endl;
    	}
    	    void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm a Bird!" << endl;
    	}
    
    };
    
    class Amphibians : public Animal
    {
    public:
    
    		void Movement()
    	{
    		cout << "I generally crawl with my legs, and slither, AND swim!" << endl;
    	}
    		 void Eat()
    	{
    		cout << "I usually eat meat!" << endl;
    	}
    		void Species()
    	{
    		cout << "I'm an Amphibian!" << endl;
    	}
    };
    
    class Fishes : public Animal
    {
    public:
    		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;
    	}
    		void Species()
    	{
    		cout << "I'm a Fish!" << endl;
    	}
    };
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////
    class Dogs :  public Mammals
    {
    public:
    	virtual void Skin()
    	{
    		cout << "I'm covered in fur!" << endl;
    	}
    	void Speak()
    	{
    		cout << "Bark! Bark!" << endl;
    	}
    };
    
    ////////////////////////////////////////////////////////////
    
    ///////////////////////////////////////////////////////////
    class Poodles : public Dogs
    {
    public:
    	void Skin()
    	{
    		cout << "My fur is long and curly!" << endl;
    	}
    };
    
    ////////////////////////////////////////////////////////////
    void operateAnimals(vector<Animal*> Animals)
    {
    	for(int i=0; i<Animals.size(); i++)
    	{
    		//This is where the interesting stuff happens:
    		Animals[i]->Species();
    		Animals[i]->Movement(); 
    		Animals[i]->Eat();
    		Animals[i]->Speak();
    		Animals[i]->Skin();
    		cout << endl;
    
    	}
    }
    ////////////////////////////////////////////////////////////
    
    int main()
    {
    	Animal allAnimals;
    	Mammals Mammal;
    	Reptiles Reptile;
    	Birds Bird;
    	Amphibians Amphibian;
    	Fishes Fish;
    	Dogs Dog;
    	Poodles Poodle;
    
    	vector<Animal*> Animals;
    	Animals.push_back(&Mammal);
    	Animals.push_back(&Reptile);
    	Animals.push_back(&Bird);
    	Animals.push_back(&Amphibian);
    	Animals.push_back(&Fish);
    	Animals.push_back(&Dog);
    	Animals.push_back(&Poodle);
    
    	operateAnimals(Animals);
    	return 0;
    }
    Sorry if I'm overposting, I feel like these examples can help anyone else with the question, Maybe we can get this moved onto a faq board? How woudl I go about doing that? I found this thread INCREDIBLY useful.


    I think I'm going to make a dynamic database for Taxonomy and actually allow the user to add in the family/phylum/genus/species/etcetcetc at will, to constantly increase the database and make it larger and larger and larger.

    For a practice project, ykno :d
    Last edited by Shamino; 11-18-2005 at 02:24 AM.

Page 3 of 3 FirstFirst 123
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