Subscribe to Invariance_Under_Translation        RSS Feed
-----

Inheritence Pt. 1

Icon 1 Comments
Inheritence allows our data structures to inherit certain member variables from a Base/Parent structure. In this entry I will only use public accessors, we will keep the topic of protected and private accessors for a later entry. When you build and run this program, you will notice the order in which output occurs. When we initialize our constructor in our Derived class, the Base class is initialized first. Derived hands control from main() to it's Base class, which then hands control back over to Derived. This is why the output,

Quote

Inside Base
Inside Base
Inside Derived


occurs. Enjoy and Happy Coding!

//Inheritence
#include<iostream>

using namespace std;


class Base		//Base Class
{

	int val;	

public:

	Base(){};
	
	Base(int i)
	{
		val = i;
		cout<<"Base..."<<endl;
		cout<<endl;
	}

	~Base()
	{
		cout<<"Destructing Base"<<endl;
		cout<<endl;
	}

	 void f()
	{
		cout<<"Inside Base"<<endl;
		cout<<endl;
		
	}


};


class Derived : public Base		//Derived inherits Base's member variables
{					//as public. It does not inherit val from Base
					//because val is private and not public or protected.
	int val2;

public:

	Derived(){};

	Derived(int j) : val2(j)
	{
		cout<<"Derived..."<<endl;
		cout<<endl;
	}

	~Derived()
	{
		cout<<"Destructing Derived"<<endl;
		cout<<endl;
	}

	void f()
	{
		Base::f();			//When f() is called in Derived, 
		cout<<"Inside Derived"<<endl;	//f() from Base will also be called
						//by using the scope operator and making
	}					//a function call to f() in Base.

};




int main()

{

	Base obj(8);		//Initialize constructors
	Derived obj2(6);

	obj.f();		//Call Base f()
	obj2.f();		//Notice output when Derived::f() is called
	
	cout<<endl;

	cin.get();

	return 0;

}

1 Comments On This Entry

Page 1 of 1

alias120 Icon

14 May 2010 - 07:07 PM
Here is another simple example I just wrote. Notice the class Orc. It inherits from Monster, but also has access to NPC since Monster inherits from NPC. Enjoy!

//Inheritence
#include<iostream>
#include<string>

using namespace std;


class NPC		//Base class that other classes will derive from
{
protected:
	string npcName;		
	int npcLvl;

public:
	NPC()
	{
	}

	~NPC(){}
	
	 void setName(string nameIn){};	//
	
	 void setLvl(int lvlIn){};

	 string getName()
	{
		return npcName;
	}

	 int getLvl()
	{
		return npcLvl;
	}

};

class Monster: public NPC
{

public:

	Monster()
	{
	npcLvl = 0;
	cout<<"Garrr!! So Hungry!"<<endl;
	cout<<endl;
	}

	~Monster(){}
	
	void setName(string nameIn)
	{
		npcName = nameIn;
	};

	void setLvl(int lvlIn)
	{
		npcLvl = lvlIn;
	};

	void yell()
	{
		cout<<"Evil shall prevail!";
	}

};

class Knight : public NPC
{
public:

	Knight()
	{
		npcLvl = 0;
		cout<<"I Shall Smight Thee!"<<endl;
		cout<<endl;
	}

	~Knight(){}

	void setName(string nameIn)
	{
		npcName = nameIn;
	};

	void setLvl(int lvlIn)
	{
		npcLvl = lvlIn;
	};
};

class Orc : public Monster
{
public:

	Orc()
	{
		npcLvl = 0;
		cout<<"Geradf Fargye Cro!!"<<endl;
		cout<<endl;
	}

	~Orc(){}

	void setName(string nameIn)
	{
		npcName = nameIn;
	}

	void setLvl(int lvlIn)
	{
		npcLvl = lvlIn;
	}


};


		


int main()
{
	NPC obj;

	Monster obj2;

	Knight obj3;

	Orc obj4;

	string defMonName("Golem");
	string defKniName("Bregstrom");

	int defMonLvl(35);
	int defKniLvl(62);

	obj2.setName(defMonName);
	obj2.setLvl(defMonLvl);

	obj3.setName(defKniName);
	obj3.setLvl(defKniLvl);

	cout<<obj2.getName()<<endl;
	cout<<endl;
	cout<<obj2.getLvl()<<endl;
	cout<<endl;
	obj4.yell();

	cout<<endl;


	cin.get();

	return 0;

}

0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

September 2014

S M T W T F S
 1 2 3456
78910111213
14151617181920
21222324252627
282930    

Tags

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)