Download presentation
Presentation is loading. Please wait.
1
Reusing Code Private or Protected inheritance
2
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 q_values; // an array of int valarray weights; ……………………………………
3
double gpa[5]={3.1,3.5,3.8, 2.9, 3.3}; valarray v1; valarray v2(8); // array of 8 int valarray v3(10,8); // array of 8 int each set to 10; valarray v4(gpa,4); // 4 elements, 4 first values of gpa.
4
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.
5
The public method of the base class becomes private method of derived class. The derived class does not inherit the base- class interface.
6
class Student : private string, private valarray { 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.
7
How to initialize base-class component Student( const char *str, const double *pd, int n) : string(str), ArrayDb(pd, n) { }
8
Accessing Base-Class methods double Student :: Average( ) { if( ArrayDb ::size() > 0) return ArrayDb::sum() / ArrayDb::size(); else return 0; }
9
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.
10
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.
11
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.
12
Protected Inheritance A variation of private Inheritance class Student : protected string, protected valarray { ….}; The interface for the base class is available to the derived class but not to the outside world.
13
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).
14
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.
15
Name Hiding If you redefine a member function in the derived class, all other overloaded functions in the base class are inaccessible.
16
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
17
How inheritance affects access Suppose class B is derived from A. Then Base class member access specifier
18
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 Class Client Derived class
19
Overriding Overriding redefines the body of a virtual function class Base { public: virtual void func(); } class Derived : public Base { public: virtual void func(); //overrides Base::func() }
20
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!
21
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); }
22
int & Array::operator[ ]( int i) { if( i =n) { cout<<“error in array lim “<<“ out of rang “<<endl; exit(1); } return arr[i]; }
23
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; }
24
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; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.