+ All Categories
Home > Documents > Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with...

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

Date post: 20-Dec-2015
Category:
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
24
Reusing Code Private or Protected inheritance
Transcript
Page 1: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

Reusing Code

Private or Protected inheritance

Page 2: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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;

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

Page 3: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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 .

Page 4: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 5: 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 public method of the base class becomes private method of derived class.

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

Page 6: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 7: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

How to initialize base-class component

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

Page 8: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

Accessing Base-Class methods

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

Page 9: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 10: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 11: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 12: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 13: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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).

Page 14: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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.

Page 15: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

Name Hiding

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

Page 16: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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

Page 17: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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

Page 18: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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

Page 19: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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()

}

Page 20: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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!

Page 21: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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);}

Page 22: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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

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

Page 23: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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; }

Page 24: Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.

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;

}


Recommended