Reusing Code - PowerPoint PPT Presentation

About This Presentation
Title:

Reusing Code

Description:

... and it supports operation such as summing the contents and defining largest and smallest value. ... return ArrayDb::sum() / ArrayDb::size(); else. return 0; ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 25
Provided by: Ara8
Category:
Tags: code | reusing | summing

less

Transcript and Presenter's Notes

Title: Reusing Code


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 lt intgt q_values // an array of int
  • valarray ltdoublegt weights

3
  • double gpa53.1,3.5,3.8, 2.9, 3.3
  • valarrayltdoublegt v1
  • valarrayltintgtv2(8) // array of 8 int
  • valarrayltintgtv3(10,8)
  • // array of 8 int each set to 10
  • valarrayltdoublegtv4(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
    valarrayltdoublegt
  • public
  • .
  • By default inheritance is private. If we dont
    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() gt 0)
  • return ArrayDbsum() / ArrayDbsize()
  • 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 ltlt ( ostream os , const
    Student stu)
  • os ltlt Scores for ltlt( string ) stultlt \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.
  • osltltstu //recursive call.

12
Protected Inheritance
  • A variation of private Inheritance
  • class Student protected string, protected
    valarrayltdoublegt
  • .
  • 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 doesnt 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 Basefunc()

20
Calls up the chain
  • You can still call the overridden function
  • void
  • Derivedfunc()
  • cout ltlt "In Derivedfunc!"
  • Basefunc() // 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 ln0)
  • 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 Arrayoperator ( int i)
  • if( ilt0 igtn)
  • coutltlterror in array lim ltlt out of rang
    ltltendl
  • exit(1)
  • return arri

23
  • Array Arrayoperator(const Array rs)
  • int i,t
  • if (len gt rs.len) tlen
  • else trs.len
  • Array new_array(t)
  • for(i0iltki)
  • new_arrayiarrirs.arri
  • for(i,iltleni) new_arrayiarri
  • for(iiltrs.leni) new_arrayirs.arri
  • return new_array

24
  • Array Arrayoperator(const Array rs)
  • if (this rs )
  • return this
  • delete arr
  • arrnew intrs.len
  • for(int i0iltleni)
  • arrirs.arri
  • lenrs.len
  • return this
Write a Comment
User Comments (0)
About PowerShow.com