Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with...

Post on 20-Dec-2015

217 views 0 download

Tags:

transcript

Reusing Code

Private or Protected inheritance

A cool class for array

valarray class deals with numeric values, and it supports operation such as summing the contents and defining largest and smallest value.

valarray < int> q_values; // an array of int

valarray <double> weights;

……………………………………

double gpa[5]={3.1,3.5,3.8, 2.9, 3.3};valarray<double> v1;valarray<int>v2(8); // array of 8 intvalarray<int>v3(10,8); // array of 8 int each set to 10;valarray<double>v4(gpa,4); // 4 elements, 4 first values of gpa .

Private Inheritance

Private inheritance implements has-a relationship.

In private inheritance, public and protected members of the base class become private members of the derived class.

Methods of the base class can be used inside the member function of the derived class.

The public method of the base class becomes private method of derived class.

The derived class does not inherit the base-class interface.

class Student : private string, private valarray<double>

{

public :

….

}

By default inheritance is private. If we don’t use word private the inheritance is private.

If we derive a class from more than one class we have multiple inheritance.

How to initialize base-class component

Student( const char *str, const double *pd, int n) : string(str), ArrayDb(pd, n) { }

Accessing Base-Class methods

double Student :: Average( ) { if( ArrayDb ::size() > 0) return ArrayDb::sum() / ArrayDb::size(); else return 0;}

Accessing Base-Class Objects

const string & Student :: Name()

{

return (string &) *this;

}

// *this is the invoking of type Student

// here we type cast a Student object to a string object.

Accessing Base-Class Friend

ostream & operator << ( ostream &os , const Student & stu)

{

os << “Scores for “<<( string &) stu<<“ :\n”;

….

}

// Instead of *this we use the stu object.

In private inheritance, a reference or pointer to a base class can not assign a reference or pointer to a derived class

with out explicit type cast.

Even if we used public inheritance we would have to use explicit type cast.

os<<stu; //recursive call.

Protected Inheritance

A variation of private Inheritance

class Student : protected string, protected valarray<double>

{ ….};

The interface for the base class is available to the derived class but not to the outside world.

Difference between private and protected inheritance is :

With private inheritance the third generation class doesn’t get the internal use of the base class interface.

With protected inheritance public base class methods become protected in the second generation and are available internally to the next generation (third).

Note on constructors• Base class is always constructed first

• If no explicit arguments are passed to base class– Default constructor will be called

• Destructors are called in exactly the reverse order of the constructors.

Name Hiding

• If you redefine a member function in the derived class, all other overloaded functions in the base class are inaccessible.

Access protection• Members

– Public: visible to all clients– Protected: visible to classes derived from self

(and to friends)– Private: visible only to self and to friends!

• Inheritance– Public: class Derived : public Base ...– Protected: class Derived : protected Base ...– Private: class Derived : private Base ...

• default

How inheritance affects access

InheritanceType (B is)

public protected private

public A public in B protected in B hidden

private A private in B private in B hidden

protected A protected in B protected in B hidden

Suppose class B is derived from A. Then

Base class member access specifier

When is protected not protected?

• Protected is public to all derived classes

• For this reason– make member access functions protected– keep member variables private

Base ClassClient

Derived class

Overriding• Overriding redefines the body of a virtual

functionclass Base {public:

virtual void func();

}class Derived : public Base {public:

virtual void func(); //overrides Base::func()

}

Calls up the chain• You can still call the overridden function:

void

Derived::func() {

cout << "In Derived::func!";

Base::func(); // call to base class

}

• This is a common way to add new functionality• No need to copy the old stuff!

class Array{ private : int len; int *arr; public : Array (int ln=0); Array (const Array &rs); ~Array() {delete [ ] arr; } Array & operator= (const Array & rs); Array & operator+(const Array &rs); int Size( ) { return len;} int & operator[ ]( int i);}

int & Array::operator[ ]( int i){ if( i<0 || i>=n) { cout<<“error in array lim “<<“ out of rang

“<<endl; exit(1); } return arr[i];}

Array & Array::operator+(const Array &rs){ int i,t; if (len > rs.len) t=len; else t=rs.len; Array new_array(t); for(i=0;i<k;i++) new_array[i]=arr[i]+rs.arr[i]; for(i,i<len;i++) new_array[i]=arr[i]; for(i;i<rs.len;i++) new_array[i]=rs.arr[i]; return new_array; }

Array & Array::operator=(const Array &rs)

{

if (this == &rs )

return *this;

delete [ ] arr;

arr=new int[rs.len];

for(int i=0;i<len;i++)

arr[i]=rs.arr[i];

len=rs.len;

return *this;

}