Subscribe to Invariance_Under_Translation        RSS Feed
-----

Inheritence Pt. 2

Icon Leave Comment
In my last post I began introducing Inheritence. You will notice that in my last entry that all derived classes inherited as follows;

class Derived : public Base



Notice the
public
portion of this code. What this is saying is that all of the member variables inherited from Base that are not protected will be made public in Derived. Full Example below;

#include<iostream>

using namespace std;


class Base
{
protected:

	int val;	//val is protected, meaning Base member 
			//funtions or Derived classes have access to it. 
public:

	Base(){};
	Base(int j) : val(j)
	{
		cout<<val<<" in Base"<<endl;
	};
	~Base(){cout<<"Destructing Base"<<endl;};

};


class Derived : public Base
{

public:

	Derived(){};
	Derived(int b) : Base(b)
	{
		cout<<b<<" in Derived"<<endl;
	};

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

	int getVal()
	{
		return val;	//Since val is only protected in Base, our 
	}			//Derived class can output val to main()
};

int main()
{

	Base obj;
	Derived obj2(5);			//We only initialize a Derived object, 
	cout<<obj2.getVal()<<" from getVal";	//but notice that Base gets constructed first
	cout<<endl;				//in the output.



	cin.get();

	return 0;

}



As you can see, Derived inherits from Base and does so publically. So what if added an additional variable to Base that was public, but inherited protectedly to Derived? Example below;

#include<iostream>

using namespace std;


class Base
{
protected:

	int val;	

public:

	int val2;

	Base(){};
	Base(int j, int d) : val(j), val2(d)
	{
		cout<<val<<" in Base"<<endl;
		cout<<val2<<" in Base"<<endl;
	};
	~Base(){cout<<"Destructing Base"<<endl;};

};


class Derived : protected Base  //inherits Base member variables as protected
{

public:

	Derived(){};
	Derived(int b, int f) : Base(b, f)
	{
		cout<<b<<" in Derived"<<endl;
		cout<<f<<" int Derived"<<endl;
	};

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

	int getVal2()
	{
		return val2;
	}

};


int main()
{

	Base obj;
	Derived obj2(5, 6);	

	cout<<endl;
	cout<<obj2.getVal2()<<" from getVal2()"<<endl;

	obj2.val2 = 4;			//obj2 is as object of Derived, and since Derived inherits from Base
					//as protected we cannot change val2 to 4.

	cout<<obj2.getVal2()<<" after changing value"<<endl;

	cin.get();

	return 0;

}



This will give us compilation errors. Derived inherits member variables from Base as protected, So even though we could directly access val2 using a Base object we are unable to access val2 using an object of Derived. The same applies to using
class Derived : private Base
.

Base member variables are inherited as private, thus they cannot be accessed through Derived. I will continue with the topic of Inheritence in my next entry, for now enjoy and happy coding!

-alias

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

September 2014

S M T W T F S
  1 23456
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)