is this a good example to demonstrate the different types of inheritance?
kindly give feed back and suggestions..

ps:using the g++ compiler

#include<iostream>
using namespace std;
class life{
    public:
        virtual void eats()=0;
    protected:
        char* predator; char* prey;
};
class producers : public life{
    virtual void eats()=0;
};
class animal : public life{
    virtual void eats()=0;
};
class plants : public producers{
    public:
        void eats();
        plants();
};
class carni : public animal{
    public:
        void eats();
        carni();
};
class herbi : public animal{
    public:
        void eats();
        herbi();
};
class omni : virtual public carni, virtual public herbi{
    public:
        void eats();
        omni();
};
class decomps :virtual public omni, virtual public plants{
    public:
        void eats();
        decomps();
};
carni :: carni(){
    predator="tiger";
    prey="deer";
}
void carni :: eats(){
    cout<<predator<<" eat "<<prey<<endl;
}
herbi :: herbi(){
    predator="deer";
    prey="Plants";
}
void herbi :: eats(){
    cout<<predator<<" eat "<<prey<<endl;
}
plants :: plants(){
    predator="plants";
    prey="sun light";
}
void plants :: eats(){
    cout<<predator<<" uses "<<prey<<endl;
}
omni :: omni(){
    herbi::predator="Man";
    carni::predator="Man";
}
void omni :: eats(){
    herbi::eats();
    cout<<"and ";
    carni::eats();
}
decomps :: decomps(){
    plants::predator="Algae & Fungi";
    plants::prey="dead plants";
    omni::carni::predator="Algae & Fungi";
    omni::carni::prey="Dead Animals";
}
void decomps :: eats(){
    cout<<plants::predator<<" decomposes "<<plants::prey<<endl;
    cout<<"and ";
    cout<<omni::carni::predator<<" decomposes "<<omni::carni::prey<<endl;
}
int main(){
    life* c;
    carni c1;
    c=&c1;
    c->eats();
    herbi h1;
    c=&h1;
    c->eats();
    plants p;
    c=&p;
    c->eats();
    omni o;
    o.eats();
    decomps d;
    d.eats();
    system("pause");
    return 0;
}

Recommended Answers

All 8 Replies

doesnt seem good to me ... why is the base class named 'life'? and why does 'life' have a member function called 'eats' ? and then you have 'producers' and 'animals' deriving from life.. you might have some explanation for all the names and member functions. but you should always try to see that what would a complete stranger make out of the code by just reading it. for me, i dont understand the first inheritance step by just reading it and hence i didnt go any furthur.

done some modifications to the previous code. here it goes.

#include<iostream>
using namespace std;
class life{
    public:
        virtual void eats()=0;
    protected:
        char* predator; char* prey;
};
class producers : public life{
    public:
        void eats();
};
class animal : public life{
    public:
         void eats();
};
class plants : public producers{
    public:
        plants();
};
class carni : public animal{
    public:
        carni();
};
class herbi : public animal{
    public:
        herbi();
};
class omni : public carni, public herbi{
    public:
        omni();
};
class decomps :virtual public omni, virtual public plants{
    public:
        decomps();
        void eats();
};
void animal :: eats(){
    cout<<predator<<" eat "<<prey<<endl;
}
void producers :: eats(){
    cout<<predator<<" uses "<<prey<<endl;
}
carni :: carni(){
    predator="tiger";
    prey="deer";
}
herbi :: herbi(){
    predator="deer";
    prey="Plants";
}
plants :: plants(){
    predator="plants";
    prey="sun light";
}
omni :: omni(){
    herbi::predator="Man";
    carni::predator="Man";
}
decomps :: decomps(){
    plants::predator="Algae & Fungi";
    plants::prey="dead plants";
    omni::carni::predator="Algae & Fungi";
    omni::carni::prey="Dead Animals";
}
void decomps:: eats(){
    cout<<plants::predator<<" consumes "<<plants::prey<<endl;
    cout<<" and "<<omni::carni::predator<< " consumes "<<omni::carni::prey<<endl;
}
int main(){
    plants p2;
    p2.eats();

    animal* a;

    herbi h;
    a=&h;
    a->eats();

    carni c;
    a=&c;
    a->eats();
    
    omni o;
    
    herbi* h2;
    h2=&o;
    h2->eats();
    
    carni* c2;
    c2=&o;
    c2->eats();
    
    decomps d;
    d.eats();
    
    system("pause");
    return 0;
}

doesnt seem good to me ... why is the base class named 'life'? and why does 'life' have a member function called 'eats' ? and then you have 'producers' and 'animals' deriving from life.. you might have some explanation for all the names and member functions. but you should always try to see that what would a complete stranger make out of the code by just reading it. for me, i dont understand the first inheritance step by just reading it and hence i didnt go any furthur.

its named after life because the assignment which was given to me told me to do so.
what i gather is.
life=world
world has the animal kingdom and plant kingdom
animal kingdom has the subclasses called the herbivore, carnivore, and omnivore.

every animal or plant has the common property called eats, which i'm trying to inherit from the base class life.

hope i am clear.

any suggestions welcome.
am i on the right track with regards to the inheritance concepts?

again like i said you can explain anything but what if you write a code and someone has to understand it on his own? he might not be able to interpolate that life=world etc. anyways since i'm not able to suggest anything better i'll have to accept it.

to focus more on inheritance, i dont think that the 'life' should have a pure virtual method called 'eats', because plants dont eat as such and it would not be right to make every plant class implement the 'eats' method even though it doesnt use it. i think you can have one more layer of abstraction in between. from life you can inherit 'animalKingdom' and 'plantKingdom' classes and the put the 'eats' method in animalKingdom. for 'plantkingdom' you can have something like 'produces'. then derive all animals from 'animalkingdom' and plants from 'plantkingdom' and keep only the common functionality in the 'life' class.

also having predator and 'prey' in life class n then in plants you are setting 'prey' as 'sun light' doesnt look too nice. you can have separate members for plant classes like 'energysource' or something, unless your assingment specifically asks you to use this.

also having predator and 'prey' in life class n then in plants you are setting 'prey' as 'sun light' doesnt look too nice. you can have separate members for plant classes like 'energysource' or something, unless your assingment specifically asks you to use this.

okie :)

gosh :( getting thses many errors in the borland compiler
:'(

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
main.cpp:
Error E2090 main.cpp 63: Qualifier 'carni' is not a class or namespace name in function decomps::decomps()
Error E2379 main.cpp 63: Statement missing ; in function decomps::decomps()
Error E2090 main.cpp 64: Qualifier 'carni' is not a class or namespace name in function decomps::decomps()
Error E2379 main.cpp 64: Statement missing ; in function decomps::decomps()
Error E2090 main.cpp 68: Qualifier 'carni' is not a class or namespace name in function decomps::eats()
Error E2379 main.cpp 68: Statement missing ; in function decomps::eats()
*** 6 errors in Compile ***


//edit:
well cleared off the errors.. changed the scope to

carni::predator="Algae & Fungi";
carni::prey="Dead Animals";

lines 63, 64 - 68

these are what my friends have done..

suggestions are welcome :)

//INHERITANCE

#include<iostream.h>
#include<conio.h>

class LIFE
 {
	public:
		virtual void display()
		 {
			cout<<"SUN\n";
		 }
 };

class PLANTS:public LIFE
 {
	public:
		void display()
		 {
			LIFE::display();
			cout<<"PLANTS DERIVE ENERGY FROM SUN\n";
		 }
 };

class HERBIVORE:public PLANTS
 {
	public:
		void display()
		 {
			PLANTS::display();
			cout<<"HERBIVORES EAT PLANTS\n";
		 }
 };

class CARNIVORE:public HERBIVORE
 {
	public:
		void display()
		 {
			HERBIVORE::display();
			cout<<"CARNIVORES EAT HERBIVORES\n";
		 }
 };

class OMNIVORE:public CARNIVORE
 {
	public:
		void display()
		 {
			CARNIVORE::display();
			cout<<"OMNIVORES EAT BOTH HERIVORES AND CARNIVORES\n";
		 }
 };

class DECOMPOSERS:public OMNIVORE
 {
	public:
		void display()
		 {
			LIFE::display();
			OMNIVORE::display();
			cout<<"DECOMPOSERS DERIVE FROM SUN AND OMNIVORES\n";
		 }
 };

int main()
 {
	PLANTS p;
	HERBIVORE h;
	CARNIVORE c;
	OMNIVORE o;
	DECOMPOSERS d;
	clrscr();
	cout<<"PLANTS\n";
	p.display();
	cout<<"\n\nHERBIVORE\n";
	h.display();
	cout<<"\n\nCARNIVORE\n";
	c.display();
	cout<<"\n\nOMNIVORE\n";
	o.display();
	cout<<"\n\nDECOMPOSERS\n";
	d.display();
	getch();
	return 0;
 }
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class life
{
	virtual void put()=0;

};
class tiger:virtual public life
{
	public:

		void put()
		{
			cout<<"tiger eats deer"<<endl;
		}
};
class lion:virtual public life
{
	public:
		void put()
		{
			cout<<"Lion eats deer"<<endl;
		}
};
class deer:public tiger,public lion
{
	public:
		void put()
		{
			tiger::put();
			lion::put();
			cout<<"deer eats plants"<<endl;
		}
};
class plant:public deer
{
	public:
		void put()
		{
			deer::put();
			cout<<"Plants consumes water"<<endl;
		}
};
void main()
{
	int ch;
	cout<<"Enter your choice 1.lion 2.tiger 3.deer 4.plant 5.exit"<<endl;
	cin>>ch;
	while(ch!=5)
	{
		switch(ch)
		{
			case 1:
			{
				lion obj1;
				obj1.put();
				break;
			}
			case 2:
			{
				tiger obj1;
				obj1.put();
				break;
			}
			case 3:
			{
				deer obj1;
				obj1.put();
				break;
			}
			case 4:
			{
				plant obj1;
				obj1.put();
				break;
			}
			case 5:
				exit(0);
		}
		cout<<"Enter your choice 1.lion 2.tiger 3.deer 4.plant 5.exit"<<endl;
		cin>>ch;

	}


}
#include<iostream.h>
#include<conio.h>

class life
{
	public:
		virtual void dis()=0;
		virtual void get()=0;
};
class producers:virtual public life
{
	public:
		producers(){name="producers";}
		void get()
		{
			cout<<"Enter "<<name<<" name:";cin>>name;
		}
		void dis()
		{
			cout<<name;
		}
	protected:
		char *name;
};

class herbivorous:virtual public life,public producers
{
	public:
		herbivorous(){name="herbivorous";}
		void get()
		{
			cout<<"Enter "<<name<<" name:";cin>>name;
		}
		void dis()
		{
			cout<<endl<<name<<" eats "<<producers::name;
		}
	protected:
		char *name;
};
class omnivorous:public herbivorous,virtual public life
{
	public:
		omnivorous(){name="omnivorous";}
		void get()
		{
			cout<<"Enter "<<name<<" name:";cin>>name;
		}
		void dis()
		{
			cout<<endl<<name<<" eats "<<producers::name<<" and "<<herbivorous::name;
		}

	protected:
		char *name;
};

class carnivorous:virtual public life,public omnivorous
{
	public:
		carnivorous(){name="carnivorous";}
		void get()
			{
			cout<<"Enter "<<name<<" name:";cin>>name;
		}
		void dis()
		{
			cout<<endl<<name<<" eats "<<herbivorous::name<<" and "<<omnivorous::name;
			herbivorous::dis();
		}
	protected:
		char *name;
};
class decomposer:public carnivorous,virtual public life
{
	public:
		decomposer()
		{
			name="decomposer";
		}
		void get()
		{
			cout<<"Enter "<<name<<" name:";cin>>name;
		}
		void dis()
		{
			cout<<"\n\n"<<name<<" degrades "<<carnivorous::name<<",";
			cout<<herbivorous::name<<","<<omnivorous::name<<" and "<<producers::name;
		}
		protected:
			char *name;
};
void main()
{
	clrscr();
	life *l;
	producers p;
	herbivorous h;
	carnivorous c;
	omnivorous o;
	decomposer d;
	p.get();
	h.get();
	c.get();
	o.get();
	d.get();
	l=&c;
	l->dis();
	l=&o;
	l->dis();
	l=&d;
	l->dis();
	getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.