Вы находитесь на странице: 1из 42

Object Oriented Programming

Inheritance [Overriding & Explicit


Constructor Call]
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 1
Review

• Learned the inheritance behavior


• w.r.t kind (public, private, protected)
• w.r.t access modifier (public, private, protected)

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 2


Objective

• Some more about Inheritance


• Overriding
• Explicit Call to Base Class Constructor

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 3


Inheritance Review

• Inheritance is an “is-a” relationship


• For instance,“every employee is a person”
• Inheritance lets us create new classes from existing classes
• New classes are called the derived classes
• Existing classes are called the base classes
• Derived classes inherit the properties of the base classes

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 4


Inheritance Review (cont.)

• Single inheritance: derived class has a single base class


• Multiple inheritance: derived class has more than one
base class

• Can be viewed as a tree (hierarchy) where a base class is


shown with its derived classes

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 5


Inheritance Review (cont.)
• Inheritance can be viewed as a tree-like, or hierarchical,
structure wherein a base class is shown with its derived
classes.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 6


Inheritance & Overriding
• The derived class can redefine the public member
functions of the base class.

• That is, in the derived class, you can have a member


function with the same name, number, and types of
parameters as a function in the base class.

• Redefining a base class function in derived class is


known as Overriding.

• However, this redefinition applies only to the objects of


the derived class, not to the objects of the base class.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 7


Overriding a Member Functions
of the Base Class

• To redefine a public member function of a base


class

• Corresponding function in the derived class must have


the same name, number, and types of parameters

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 8


Overriding a Member Functions of
the Base Class (cont.)

• If derived class overrides a public member function of


the base class, then to call the base class function,
specify:
• Name of the base class
• Scope resolution operator (::)
• Function name with the appropriate parameter list

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 9


Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 10
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 11
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 12
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 13
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 14
Box derived from Rectangle

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 15


Box derived from Rectangle
(cont.)
• From the definition of the class boxType, it is clear that
the class boxType is derived from the class
rectangleType, and it is a public inheritance.
• Therefore, all public members of the class
rectangleType are public members of the class
boxType.
• The class boxType also overrides (redefines) the
functions print and area.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 16


Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 17
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 18
Defining a derive class
In general, while writing the definitions of the member
functions of a derived class to specify a call to a public
member function of the base class, we do the following:

• If the derived class overrides a public member function


of the base class, then to specify a call to that public
member function of the base class you use the name of
the base class, followed by the scope resolution
operator, ::, followed by the function name with the
appropriate parameter list, i.e print()

print(){
rectangleType::print();
//some new definitions
}
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 19
Defining a derive class (cont.)

• If the derived class does not override a public member


function of the base class, you may specify a call to that
public member function of base class by using the
appropriate name and parameter list of the function.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 20


Defining a derive class (cont.)
• The class boxType has three member variables:
•length, width, and height.

• The member function print of the class boxType prints the


values of these member variables.

• To write the definition of the function print of the class


boxType, keep in mind the following:

1. The member variables length and width are private


members of the class rectangleType, and so cannot be
directly accessed in the class boxType. Therefore, when
writing the definition of the function print of the class
boxType, we cannot access length and width directly.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 21


Defining a derive class (cont.)
2. The member variables length and width of the class
rectangleType are accessible in the class boxType through the
public member functions of the class rectangleType.

Therefore, when writing the definition of the member function print


of the class boxType, we first call the member function print of
the class rectangleType to print the values of length and
width.

After printing the values of length and width, we output the


values of height.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 22


Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 23
//Box Area formula: 2lw + 2lh + 2wh

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 24


Constructors Explicit Call from
Derived to Base Class
• Derived class constructor cannot directly access
private members of the base class
• Derived class can directly initialize only public
member variables of the derived class
• When a derived object is declared
• It must execute one of the base class constructors
• Call to the base class constructor is specified in the
heading of derived class constructor definition like:
boxType():rectangleType(){ // explicit call
}

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 25


• The class rectangleType has two constructors and two
member variables.

• The class boxType has three member variables: length,


width, and height.

• The member variables length and width are inherited from


the class rectangleType.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 26


• First, let us write the definition of the default constructor of the
class boxType.

• Recall that, if a class contains the default constructor and no


values are specified when the object is declared, the default
constructor executes and initializes the object.

• Because the class rectangleType contains the default


constructor, when writing the definition of the default constructor
of the class boxType, we do not specify any constructor of
the base class.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 27


Definitions of constructors with parameters.
• To trigger the execution of a constructor (with
parameters) of the base class, you specify the
name of a constructor of the base class with the
parameters in the heading of the definition of the
constructor of the derived class.

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 28


Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 29
Output:

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 30


const (Constant) Objects and
const Member Functions

• Principle of least privilege


• Only give objects permissions they need, no more
• Keyword const
• Specify that an object is not modifiable
• Any attempt to modify the object is a syntax error
• Example
const Time noon( 12, 0, 0 );
• Declares a const object noon of class Time and initializes it to 12
const (Constant) Objects and const
Member Functions
• const objects require const functions
• Member functions declared const cannot modify their object
• const must be specified in function prototype and definition
• Prototype:
ReturnType FunctionName(param1,param2…) const;
• Definition:
ReturnType FunctionName(param1,param2…) const { …}
• Example:
int A::getValue() const { return
privateDataMember };
• Returns the value of a data member but doesn’t modify anything so is
declared const
• Constructors / Destructors cannot be const
• They need to initialize variables, therefore modifying them
Using the this Pointer

• this pointer
• Allows objects to access their own address
• Not part of the object itself
• Implicitly reference member data and functions
• The type of the this pointer depends upon the type of the object
Using the this Pointer
• Examples using this
• For a member function print data member x, either
this->x
or
( *this ).x

• Cascaded (chainned) member function calls


• Function returns a reference pointer to the same object
{ return *this; }
• Other functions can operate on that pointer
• Functions that do not return references must be called last
Using the this Pointer

• Lets See
1 // Fig. 7.8: time6.h
2 // Cascading member function calls.
3
4
5 #include <iostream>
6 using namespace std;
7
8
9 class Time {
10 public:
11 Time( int = 0, int = 0, int = 0 ); // default constructor
12
13 // set functions
14 Time &setTime( int, int, int ); // set hour, minute, second
15 Time &setHour( int ); // set hour
16 Time &setMinute( int ); // set minute
17 Time &setSecond( int ); // set second Notice the Time & - function
18 returns a reference to a Time
19 // get functions (normally declared const) object. Specify object in
20 int getHour() const; // return hour function definition.
21 int getMinute() const; // return minute
22 int getSecond() const; // return second
23
24 // print functions (normally declared const)
25 void printMilitary() const; // print military time
26 void printStandard() const; // print standard time
27 private:
28 int hour; // 0 - 23
29 int minute; // 0 - 59
30 int second; // 0 - 59
31 };
32
33
34 // Fig. 7.8: time.cpp
35 // Member function definitions for Time class.
36
37
38
39
40
41
42 // Constructor function to initialize private data.
43 // Calls member function setTime to set variables.
44 // Default values are 0 (see class definition).
45 Time::Time( int hr, int min, int sec )
46 { setTime( hr, min, sec ); }
47
48 // Set the values of hour, minute, and second.
49 Time &Time::setTime( int h, int m, int s )
50 { Returning *this enables
51 setHour( h ); cascading function calls
52 setMinute( m );
53 setSecond( s );
54 return *this; // enables cascading
55 }
56
57 // Set the hour value
58 Time &Time::setHour( int h )
59 {
60 hour = ( h >= 0 && h < 24 ) ? h : 0;
61
62 return *this; // enables cascading
63 }
64
65 // Set the minute value
66 Time &Time::setMinute( int m )
67 {
68 minute = ( m >= 0 && m < 60 ) ? m : 0;
69
70 return *this; // enables cascading
71 }
72
73 // Set the second value
Returning *this enables
74 Time &Time::setSecond( int s )
75 { cascading function calls
76 second = ( s >= 0 && s < 60 ) ? s : 0;
77
78 return *this; // enables cascading
79 }
80
81 // Get the hour value
82 int Time::getHour() const { return hour; }
83
84 // Get the minute value
85 int Time::getMinute() const { return minute; }
86
87 // Get the second value
88 int Time::getSecond() const { return second; }
89
90 // Display military format time: HH:MM
91 void Time::printMilitary() const
92 {
93 cout << ( hour < 10 ? "0" : "" ) << hour << ":"
94 << ( minute < 10 ? "0" : "" ) << minute;
95 }
96
97 // Display standard format time: HH:MM:SS AM (or PM)
98 void Time::printStandard() const
99 {
100 cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
101 << ":" << ( minute < 10 ? "0" : "" ) << minute
102 << ":" << ( second < 10 ? "0" : "" ) << second
103 << ( hour < 12 ? " AM" : " PM" );
104 }
105 // Fig. 7.8: fig07_08.cpp
106 // Cascading member function calls together
107 // with the this pointer
108 #include <iostream>
109
110 using std::cout;
111 using std::endl;
112
Notice cascading function calls.
113 #include "time6.h"
114
115 int main()
116 {
117 Time t;
118
119 t.setHour( 18 ).setMinute( 30 ).setSecond( 22 );
120 cout << "Military time: ";
121 t.printMilitary();
122 cout << "\nStandard time: ";
123 t.printStandard();
124
125 cout << "\n\nNew standard time: ";
126 t.setTime( 20, 20, 20 ).printStandard();
127 cout << endl;

128

129 return 0;

130 }

Military time: 18:30


Standard time: 6:30:22 PM

New standard time: 8:20:20 PM


?
Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 41
End for a while

Object Oriented Programming | attiqrafiq@uog.edu.pk | attiqrafiq@gmail.com 42

Вам также может понравиться