SlideShare a Scribd company logo
1
Shape
Triangl
e
Rectangle Square
Virtual Functions
 High Level Languages
 Easy to develop and update
 Virtual means existing in appearance but not in reality.
 When virtual functions are used, a program that
 appears to be calling a function of one class may in
 reality be calling a function of a different class.
 Why are virtual functions needed?
 Suppose you have a number of objects of different classes but you want to
put them all in an array and perform a particular operation on them using
the same function call.
 For example, suppose a graphics program includes several different shapes: a
triangle, a ball, a square, and so on. Each of these classes has a member
function draw() that causes the object to be drawn on the screen.
Virtual Functions
 Now suppose you plan to make a picture by grouping a number of these
elements together, and you want to draw the picture in a convenient way.
 One approach is to create an array that holds pointers to all the different
objects in the picture.
Shape Pointers Array
Index Number [0] [1] [2] [3] [4]
Shape
Pointer
Square
Object
Rectangle
Object
Triangle
Object
Circle
Object
Triangle
Object
Virtual Functions
 The array might be defined like this:
 shape* ptrarr[100]; // array of 100 pointers to shapes
 If you insert pointers to all the shapes into this array, you can then draw an
entire picture using a simple loop:
for(int j=0 ; j<N ; j++) {
ptrarr[j]->draw(); // (*ptrarr[j] ).draw()
}
 This is an amazing capability: Completely different functions are executed by
the same function call.
 If the pointer in ptrarr points to a ball object, the function that draws a ball is
called; if it points to a triangle object, the triangle-drawing function is called.
Virtual Functions
 This is called polymorphism, which means different forms. The functions have
the same appearance, the draw() expression, but different actual functions are
called, depending on the contents of ptrarr[j].
 Polymorphism is one of the key features of object-oriented programming,
after classes and inheritance.
 For the polymorphic approach to work, several conditions must be met.
 First, all the different classes of shapes, such as balls and triangles, must be
descended from a single base class (called shape).
 Second, the draw() function must be declared to be virtual in the base class.
// Normal functions accessed from pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Parent { // Parent class
public:
void Show() {
cout << "I am Parent n";
}
};
// Derived class Child1
class Child1 : public Parent {
public:
void Show() {
cout << "I am Child 1 n";
}
};
// Derived class Child2
class Child2 : public Parent {
6
Normal Member Functions Accessed with Pointer
public:
void Show() {
cout << "I am Child 2 n";
}
};
int main() {
Child1 C1, C2;
Parent* P; // pointer to Parent class
P = &C1; // put the address of object C1 in
// pointer P. The rule is that pointer to the
// object of a Child class is type compatible
// with the pointer to the objects of the
// Parent class.
P->Show(); // execute P->Show() OR (*P).Show();
// the function in Parent Class is executed.
P = &C2; // put address of object C1 in P.
P->Show(); // execute P->Show()
// the function in Parent Class is executed.
system("PAUSE"); return 0;
}
1 2
7
Normal Member Functions Accessed with Pointer
&Child1 Parent
Show()
Child1
Show()
Child2
Show()
ptr
Ptr->Show()
&Child2
ptr
Ptr->Show()
Figure: Non Virtual Pointer Access
// Normal functions accessed from pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Parent { // Parent class
public:
virtual void Show() {
cout << "I am Parent n";
}
};
// Derived class Child1
class Child1 : public Parent {
public:
void Show() {
cout << "I am Child 1 n";
}
};
// Derived class Child2
class Child2 : public Parent {
8
Virtual Member Functions Accessed with Pointer
public:
void Show() { cout << "I am Child 2 n"; }
};
int main() {
Child1 C1,
Child2 C2;
Parent* P; // pointer to Parent class
P = &C1; // put the address of object C1 in
// pointer P. The rule is that pointer to the
// object of a Child class is type compatible
// with the pointer to the objects of the
// Parent class.
P->Show(); // execute P->Show() OR (*P).Show();
// the function in Child1 Class is executed.
P = &C2; // put address of object C1 in P.
P->Show(); // execute P->Show()
// the function in Child2 Class is executed.
system("PAUSE"); return 0;
}
1 2
9
Virtual Member Functions Accessed with Pointer
Figure: Virtual Pointer Access
&Child2
Parent
Show()
Child1
Show()
Child2
Show()
ptr
Ptr->Show()
&Child1
ptr
Ptr->Show()
Late Binding
 Which version of draw() does the compiler call?
 In fact the compiler doesn’t know what to do, so it arranges for the decision to
be deferred until the program is running.
 At runtime, when it is known what class is pointed to by ptr, the appropriate
version of draw will be called. This is called late binding or dynamic binding.
 Choosing functions in the normal way, during compilation, is called early
binding or static binding.
 Late binding requires some overhead but provides increased power and
flexibility.
 We’ll put these ideas to use in a moment, but first let’s consider a refinement
to the idea of virtual functions.
Abstract Classes and Pure Virtual Functions
 Think of the shape class. We’ll never make an object
of the shape class; we’ll only make specific shapes
such as circles and triangles.
 When we will never want to instantiate objects of a
base class, we call it an abstract class. Such a class exists only to act as a
parent of derived classes that will be used to instantiate objects.
 If we don’t want anyone to instantiate objects of the base class, we’ll write
our classes so that such instantiation is impossible.
 How can we can do that? By placing at least one pure virtual function in the
base class.
 A pure virtual function is one with the expression =0 added to the declaration.
This is shown in the next example.
Shape
Triangle
Rectangle Square
#include <iostream>
using namespace std;
class Base { // base class
public: virtual void show() = 0;
// pure virtual function
// Using =0 syntax we tell the compiler
// that a virtual function will be pure.
// this is only a declaration, you never
// need to write a definition of the base
// class show(), although you can if you
// need to.
};
class Derv1 : public Base {
public:
void show() {
cout << "Derv1 n";
}
}; 12
Virtual Member Functions Accessed with Pointer
class Derv2 : public Base {
public:
void show() {
cout << "Derv 2n";
}
};
int main() {
// you can’t make object from abstract class
// Base bad;
Base* arr[2]; // array of base class pointers
Derv1 dv1; // object of derived class 1
Derv2 dv2; // object of derived class 2
arr[0] = &dv1; // put address of dv1 in array
arr[1] = &dv2; // put address of dv2 in array
arr[0]->show(); // execute show() in objects
arr[1]->show();
return 0;
} 2
1
Abstract Classes and Pure Virtual Functions
 Once you’ve placed a pure virtual function in the base class, you must override
it in all the derived classes from which you want to instantiate objects.
 If a class doesn’t override the pure virtual function, it becomes an abstract
class itself, and you can’t instantiate objects from it (although you might from
classes derived from it).
 For consistency, you may want to make all the virtual functions in the base
class pure.
#include <iostream>
using namespace std;
class person // person class
{
protected:
char name[40];
public:
void getName() {
cout << " Enter name: ";
cin >> name;
}
void putName() {
cout << "Name is: " << name << endl;
}
// two pure virtual functions
virtual void getData() = 0;
virtual bool isOutstanding() = 0;
};
14
Virtual Functions and the Person Class
class student : public person
{
private:
float gpa; // grade point average
public:
void getData() // get student data from user
{
person::getName();
cout << " Enter student's GPA: ";
cin >> gpa;
}
bool isOutstanding()
{
return (gpa > 3.5);
}
};
1 2
class professor : public person {
// number of papers published
private: int numPubs;
public:
void getData() { //get professor data
person::getName();
cout << "Enter number of professor's"
"publications: ";
cin >> numPubs;
}
bool isOutstanding() {
return (numPubs > 100);
}
};
int main() {
person* Ptr[100]; // array of pointers
int n = 0;
char choice;
15
Virtual Functions and the Person Class
do {
cout << "Enter student or professor (s/p): ";
cin >> choice;
if(choice=='s') Ptr[n] = new student;
else Ptr[n] = new professor;
Ptr[n++]->getData(); //get person data
cout << " Enter another (y/n)? ";
cin >> choice;
fflush(stdin);
} while( choice=='y' ); //cycle until not ‘y’
for(int j=0; j<n; j++){ //print names of all
Ptr[j]->putName(); // say if outstanding
if( Ptr[j]->isOutstanding() )
cout << " This person is outstandingn";
}
system("PAUSE"); return 0;
} //end main()
3 4
16
Virtual Functions UML Diagram
Sensor
virtual init()
virtual read()
virtual write()
init()
read()
write()
Robot
Temperature
init()
read()
write()
Humidity
init()
read()
write()
RTC
17
Virtual Functions UML Diagram
Customer
Name
Bank
Name
Account
balance
virtual Credit()
virtual Debit()
CurrentAccount
Credit()
Debit()
SavingAccount
interestRate
Credit()
Debit()
AddInterest()
Virtual Destructors
 For consistency, you may want to make all the virtual functions in the base
class pure.
 Base class destructors should always be virtual.
 Suppose you use delete with a base class pointer to a derived class object to
destroy the derived-class object.
 If the base-class destructor is not virtual then delete, like a normal member
function, calls the destructor for the base class, not the destructor for the
derived class.
 This will cause only the base part of the object to be destroyed.
 The next program shows how this looks.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Base {
public:
// ~Base() // non-virtual destructor
virtual ~Base() // virtual destructor
{
cout << "Base destroyedn";
}
};
class Derv : public Base {
public:
~Derv() {
cout << "Derv destroyedn";
}
};
19
Virtual Functions and the Person Class
int main() {
Base* pBase = new Derv;
delete pBase;
system("PAUSE");
return 0;
}
1 2
Using Non-virtual Destructor
Using Virtual Destructor
Virtual Base Classes
 Before leaving the subject of virtual programming elements, we should
mention virtual base classes as they relate to multiple inheritance.
 Consider the situation shown in Fig. with a base class, Parent; two derived
classes, Child1 and Child2; and a fourth class, Grandchild, derived from both
Child1 and Child2.
 In this arrangement a problem can arise
if a member function in the Grandchild class
wants to access data or functions in the
Parent class.
 The next program shows what happens.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Parent {
protected:
int basedata;
};
// virtual public
class Child1 : public Parent { };
// virtual public
class Child2 : public Parent { };
21
Virtual Base Classes
class Grandchild : public Child1, public Child2 {
public:
void getdata() {
cin >> basedata; // ERROR: ambiguous
}
void putdata() {
cout << basedata << endl; // Error
}
};
int main() {
Grandchild c1;
c1.getdata();
c1.putdata();
system("PAUSE");
return 0;
}
1 2
The use of the keyword virtual in these two
classes causes them to share a single common
subobject of their base class Parent. Since
there is only one copy of basedata, there is no
ambiguity when it is referred to in Grandchild.
Friend Functions
 The concepts of encapsulation and data hiding dictate that nonmember
functions should not be able to access an object’s private or protected data
 The policy is, if you’re not a member, you can’t get in.
 However, there are situations where such rigid discrimination leads to
considerable inconvenience.
 Imagine that you want a function to operate on objects of two different
classes.
 Perhaps the function will take objects of the two classes as arguments, and
operate on their private data.
 In this situation there’s nothing like a friend function.
 The next example shows how that friend functions can act as a bridge
between two classes:
#include <iostream>
#include <stdlib.h>
using namespace std;
class beta;// for frifunc declaration
class alpha {
private:
int data;
public:
alpha() : data(3) { }
friend int frifunc(alpha, beta);
};
class beta {
private:
int data;
public:
beta() : data(7) { }
friend int frifunc(alpha, beta);
};
23
Friends as Bridges
int frifunc(alpha a, beta b)
{
return( a.data + b.data );
}
int main()
{
alpha aa; beta bb;
// call the function
cout << frifunc(aa, bb) << endl;
system("PAUSE");
return 0;
}
1 2
#include <iostream>
#include <stdlib.h>
using namespace std;
class Distance {// English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0){}
Distance(float fltfeet){ //convert float
//feet is integer part
feet = static_cast<int>(fltfeet);
inches = 12*(fltfeet-feet);
}
Distance(int ft, float in)
{ feet = ft; inches = in; }
void showdist(){
cout<<feet<<"'-" <<inches<<'"';
}
Distance operator + (Distance);
};
24
English Distance Example
Distance Distance::operator + (Distance d2) {
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) // if total exceeds 12.0,
{ i -= 12.0; f++; }
// return new Distance with sum
return Distance(f,i);
}
int main() {
Distance d1 = 2.5, d2 = 1.25, d3;
cout << "nd1 = "; d1.showdist();
cout << "nd2 = "; d2.showdist();
d3 = d1 + 10.3F;
cout << "nd3 = "; d3.showdist();
// d3 = 10.0 + d1; //float + Distance: ERROR
cout << endl;
system("PAUSE");
return 0;
}
1 2
English Distance Example
 In this program, the + operator is overloaded to add two objects of type
Distance.
 Also, there is a one-argument constructor that converts a value of type float,
representing feet and decimal fractions of feet, into a Distance value. (it
converts 10.25' into 10'–3''.)
 When such a constructor exists, you can make statements like this in main():
d3 = d1 + 10.0;
 The overloaded + is looking for objects of type Distance both on its left and on
its right, but if the argument on the right is type float, the compiler will use
the one-argument constructor to convert this float to a Distance value, and
then carry out the addition.
 The statement: d3 = 10.0 + d1; does not work, because the object of which
the overloaded + operator is a member must
English Distance Example
be the variable to the left of the operator.
 When we place a variable of a different type there, or a constant, then
the compiler uses the + operator that adds that type (float in this case),
not the one that adds Distance objects.
 Unfortunately, this operator does not know how to convert
float to Distance, so it can’t handle this situation.
 How can we write natural-looking statements that have nonmember
data types to the left of the operator?
 As you may have guessed, a friend can help you out of this dilemma.
The FRENGL program shows how.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Distance {// English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0){}
Distance(float fltfeet){ //convert float
//feet is integer part
feet = static_cast<int>(fltfeet);
inches = 12*(fltfeet-feet);
}
Distance(int ft, float in)
{ feet = ft; inches = in; }
void showdist(){
cout<<feet<<"'-" <<inches<<'"';
}
friend Distance operator + (Distance,
Distance);
};
27
English Distance Example Friend Operator
Distance operator + (Distance d1, Distance d2) {
int f = d1.feet + d2.feet; //add the feet
float i = d1.inches + d2.inches; //add inches
if(i >= 12.0) //if inches exceeds 12.0,
{ i -= 12.0; f++; }
//return new Distance with sum
return Distance(f,i);
}
int main(){
Distance d1 = 2.5, d2 = 1.25, d3;
cout << "nd1 = "; d1.showdist();
cout << "nd2 = "; d2.showdist();
d3 = d1 + 10.0; //distance + float: OK
cout << "nd3 = "; d3.showdist();
d3 = 10.3F + d1; //float + Distance: OK
cout << "nd3 = "; d3.showdist(); cout << endl;
system("PAUSE");
return 0;
}
1 2
English Distance Example
 The overloaded + operator is made into a friend:
 friend Distance operator + (Distance, Distance);
 Notice that, while the overloaded + operator took one argument as a
member function, it takes two as a friend function.
 In a member function, one of the objects on which the + operates is the
object of which it was a member, and the second is an argument.
 In a friend, both objects must be arguments. The only change to the
body of the overloaded + function is that the variables feet and inches,
used in NOFRI for direct access to the object’s data, have been
replaced in FRENGL by d1.feet and d1.inches, since this object is
supplied as an argument.
English Distance Example
 Sometimes a friend allows a more obvious syntax for calling a function
than does a member function.
 For example, suppose we want a function that will square (multiply by
itself) an object of the English Distance class and return the result in
square feet, as a type float.
 The MISQ example shows how this might be done with a member
function.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Distance { // English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0) { }
Distance(int ft, float in) : feet(ft),
inches(in) { }
void showdist() { // display distance
cout<<feet<<"'-"<<inches<<'"';
}
float square(); // member function
};
float Distance::square(){
float fltfeet = feet + inches/12;
float feetsqrd = fltfeet * fltfeet;
return feetsqrd; //return square feet
}
30
Friend for Functional Notation
int main() {
Distance dist(3, 6.0);
float sqft;
sqft = dist.square(); // return square of dist
// sqft = square(dist);
// But this is more natural way We can achieve
// this effect by making square() a friend of
// the Distance class
// display distance and square
cout << "nDistance = "; dist.showdist();
cout << "nSquare = " <<sqft<<" square feetn";
system("PAUSE");
return 0;
}
1 2
#include <iostream>
#include <stdlib.h>
using namespace std;
class Distance { // English Distance class
private: int feet; float inches;
public:
Distance() : feet(0), inches(0.0) { }
Distance(int ft, float in) : feet(ft),
inches(in) { }
void showdist() { // display distance
cout<<feet<<"'-"<<inches<<'"';
}
friend float square(Distance);
};
float square(Distance d1){
float fltfeet = d1.feet + d1.inches/12;
float feetsqrd = fltfeet * fltfeet;
return feetsqrd; //return square feet
}
31
Friend for Functional Notation
int main()
{
Distance dist(3, 6.0);
float sqft;
sqft = square(dist);
// display distance and square
cout << "nDistance = "; dist.showdist();
cout << "nSquare = " <<sqft<<" square feetn";
system("PAUSE");
return 0;
}
1 2
#include <iostream>
#include <stdlib.h>
using namespace std;
class alpha{
private: int data1;
public:
alpha() : data1(99) { } // constructor
friend class beta;
// (friend beta) is a friend class
};
32
Friend Class
class beta{
// all member functions can
public: // access private alpha data
void func1(alpha a) {
cout << "ndata1=" << a.data1;
}
void func2(alpha a) {
cout << "ndata1=" << a.data1;
}
};
int main(){
alpha a; beta b;
b.func1(a);b.func2(a);cout << endl;
system("PAUSE");
return 0;
}
1 2
Static Function
 A static data member is not duplicated for each object; rather a single data
item is shared by all objects of a class.
 The STATIC example showed a class that kept track of how many objects of
itself there were.
 Let’s extend this concept by showing how functions as well as data may be
static.
 Next program models a class that provides an ID number for each of its
objects.
 So, you to query an object to find out which object it is.
 The program also casts some light on the operation of destructors.
 Here’s the listing for STATFUNC:
[33]
#include <iostream>
#include <stdlib.h>
using namespace std;
class gamma{
private: static int total;
// total objects of this class
// (declaration only)
int id; // ID number of this object
public:
gamma() { total++; id = total; }
~gamma() { total--;
cout << "Destroying ID " <<id<<endl; }
static void showtotal()
{ cout << "Total is " << total << endl; }
void showid() //non-static function
{ cout << "ID number is " <<id<< endl; }
};
Static Function
int gamma::total = 0; // definition of total
int main(){
gamma g1;
gamma::showtotal();
// To access showtotal() using only
// the class name, we must declare it to be a
// static member function
// there may be no such objects at all
gamma g2, g3;
gamma::showtotal();
g1.showid();
g2.showid();
g3.showid();
gamma * g4 = new gamma;
g4->showid();
delete g4;
system("PAUSE");
return 0;
}
1 2
[34]
#include <iostream>
using namespace std;
class alpha{
private: int data;
public:
alpha() { }
alpha(int d) { data = d; }
void display(){ cout << data; }
alpha operator = (alpha& a) {
// not done automatically
data = a.data;
cout << "Assignment operator invoked";
// return copy of this alpha
return alpha(data);
}
};
Overloading the Assignment = Operator
int main() {
alpha a1(37);
alpha a2;
a2 = a1; // invoke overloaded =
cout << "na2="; a2.display(); // display a2
alpha a3 = a2; // is not an assignment but an
// initialization, with the same effect
alpha a3(a2); // it does NOT invoke =
// we initialize the object a3 to the value a2
cout << "na3="; a3.display(); // display a3
cout << endl;
return 0;
}
1 2
[35]
Overloading the Assignment Operator
 The assignment operator = is unique among operators in that it is not inherited.
 If you overload the assignment operator in a base class, you can’t use this same function in
any derived classes.
 you can define and at the same time initialize an object to the value of another object with
two kinds of statements:
 Both styles of definition invoke a copy constructor: A constructor that creates a new
object and copies its argument into it.
 The default copy constructor, which is provided automatically by the compiler for every
object, performs a member-by-member copy. This is similar to what the assignment
operator does; the difference is that the default copy constructor also creates a new object.
 Like the assignment operator, the copy constructor can be overloaded by the user.
[36]
alpha a3(a2); // copy initialization
alpha a3 = a2; // copy initialization, alternate syntax
#include <iostream>
using namespace std;
class alpha{
private: int data;
public: alpha() { }
alpha(int d) { data = d; }
alpha(alpha& a){ // copy constructor
data = a.data;
cout << "nCopy constructor invoked";
}
void display() { cout << data; }
// overloaded = operator
void operator = (alpha& a){
data = a.data;
cout << "Assignment operator"
<< "invoked";
}
};
Overloading the Copy Constructor
int main() {
alpha a1(37);
alpha a2;
a2 = a1; // invoke overloaded =
cout << "na2="; a2.display(); //display a2
alpha a3(a1); // invoke copy constructor
// alpha a3 = a1; // equivalent of a3(a1)
cout << "na3="; a3.display(); //display a3
cout << endl;
return 0;
}
1 2
[37]
Overloading the Copy Constructor
 The copy constructor is invoked when an object is passed by value to a function. It creates
the copy that the function operates on.
 Thus if the function void func(alpha); were declared in the program and this function were
called by the statement func(a1); then the copy constructor would be invoked to create a
copy of the a1 object for use by func().
 Of course, the copy constructor is not invoked if the argument is passed by reference or if
a pointer to it is passed. In these cases no copy is created; the function operates on the
original variable.
 The copy constructor also creates a temporary object when a value is returned from a
function. Suppose there was a function like this alpha func(); and this function was called by
the statement a2 = func();
 The copy constructor would be invoked to create a copy of the value returned by func(), and
this value would be assigned (invoking the assignment operator) to a2.
 When an argument is passed by value, a copy of it is constructed. What makes the copy?
The copy constructor. But this is the copy constructor, so it calls itself.
[38]
How to Prohibit Copying
 To avoid copying, overload the assignment operator and the copy constructor as private
members.
 As soon as you attempt a copying operation, such as
 The compiler will tell you that the function is not accessible. You don’t need to define the
functions, since they will never be called.
[39]
class alpha{
private:
alpha& operator = (alpha&); // private assign. Operator
alpha(alpha&); // private copy constructor
};
alpha a1, a2;
a1 = a2; // assignment
alpha a3(a1); // copy constructor
#include <iostream>
using namespace std;
class array{
private:
// occupies 10 bytes
char charray[10];
public:
void reveal() {
cout << "nMy object’s address is "
<< this;
}
};
int main(){
array w1, w2, w3; // make three objects
w1.reveal(); // see where they are
w2.reveal();
w3.reveal();
cout << endl;
return 0;
}
The this Pointer
1 2
[40]
 The main() program in this example creates three
objects of type where.
 It then asks each object to print its address, using the
reveal() member function.
 This function prints out the value of the this pointer.
Here’s the output:
My object’s address is 0x8f4effec
My object’s address is 0x8f4effe2
My object’s address is 0x8f4effd8
 Since the data in each object consists of an array of 10
bytes, the objects are spaced 10 bytes apart in memory.
 Some compilers may place extra bytes in objects, making
them slightly larger than 10 bytes.
// dothis.cpp the this pointer referring
to data
#include <iostream>
using namespace std;
class what{
private: int alpha;
public:
void tester(){
// same as alpha = 11;
this->alpha = 11;
//same as cout << alpha;
cout << this->alpha;
}
};
int main(){
what w; w.tester();
cout << endl; return 0;
}
Accessing Member Data with this
1 2
[41]
 This program simply prints out the value 11.
 Notice that the tester() member function accesses the
variable alpha as this->alpha
 This is exactly the same as referring to alpha directly.
 This syntax works, but there is no reason for it except to
show that this does indeed point to the object.
Using this for Returning Values
 A more practical use for this is in returning values from member functions and overloaded
operators.
 Recall that in the program where we could not return an object by reference, because the
object was local to the function returning it and thus was destroyed when the function
returned.
 We need a more permanent object if we’re going to return it by reference. The object of
which a function is a member is more permanent than its individual member functions.
 An object’s member functions are created and destroyed every time they’re called, but the
object itself endures until it is destroyed by some outside agency (for example, when it is
deleted).
 Thus returning by reference the object of which a function is a member is a better bet than
returning a temporary object created in a member function. The this pointer makes this
easy.
[42]
//assign2.cpp returns contents of the this pointer
#include <iostream>
using namespace std;
class alpha{
private: int data;
public:
alpha() { } // no-arg constructor
alpha(int d) { data = d; } // one-arg constructor
void display() { cout << data; } // display data
alpha& operator = (alpha& a){ // overloaded = operator
data = a.data; // not done automatically
cout << "nAssignment operator invoked";
return *this; // return copy of this alpha
}
};
int main(){
alpha a1(37); alpha a2, a3;
a3 = a2 = a1; //invoke overloaded =, twice
cout << "na2="; a2.display(); cout << "na3="; a3.display(); //display a3
cout << endl; return 0; }
Using this for Returning Values
[43]
Using this for Returning Values
 In this program we can use the declaration alpha& operator = (alpha& a) which returns by
reference, instead of alpha operator = (alpha& a) which returns by value.
 The last statement in this function is return *this;
 Since this is a pointer to the object of which the function is a member, *this is that object
itself, and the statement returns it by reference. Here’s the output of ASSIGN2:
 Assignment operator invoked
 Assignment operator invoked
 a2=37
 a3=37
 Each time the equal sign is encountered in a3 = a2 = a1; the overloaded operator=()
function is called, which prints the messages. The three objects all end up with the same
value.
 You usually want to return by reference from overloaded assignment operators, using *this,
to avoid the creation of extra objects. [44]
Beware of Self-Assignment
 A corollary of Murphy’s Law states that whatever is possible, someone will eventually do.
 This is certainly true in programming, so you can expect that if you have overloaded the =
operator, someone will use it to set an object equal to itself: alpha = alpha;
 Your overloaded assignment operator should be prepared to handle such self-assignment.
Otherwise, bad things may happen
[45]
Checking the Type of a Class with dynamic_cast
 Suppose some other program sends your program an object (as the operating
system might do with a callback function). It’s supposed to be a certain type of
object, but you want to check it to be sure.
 How can you tell if an object is a certain type?
 The dynamic_cast operator provides a way, assuming that the classes whose
objects you want to check are all descended from a common ancestor. The
DYNCAST1 program shows how this looks.
[46]
// RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for dynamic_cast
using namespace std;
class Base{
virtual void vertFunc() //needed for dynamic cast
{ }
};
class Derv1 : public Base { };
class Derv2 : public Base { };
//checks if pUnknown points to a Derv1
bool isDerv1(Base* pUnknown){ //unknown subclass of Base
Derv1* pDerv1;
if( pDerv1 = dynamic_cast<Derv1*>(pUnknown) )
return true;
else
return false;
}
//--------------------------------------------------------------
int main(){
Derv1* d1 = new Derv1;
Checking the Type of a Class with dynamic_cast
[47]
Checking the Type of a Class with dynamic_cast
 Here we have a base class Base and two derived classes Derv1 and Derv2.
 There’s also a function, isDerv1(), which returns true if the pointer it received as an
argument points to an object of class Derv1.
 This argument is of class Base, so the object passed can be either Derv1 or Derv2.
 The dynamic_cast operator attempts to convert this unknown pointer pUnknown to
type Derv1.
 If the result is not zero, pUnknown did point to a Derv1 object.
 If the result is zero, it pointed to something else.
 The dynamic_cast operator allows you to cast upward and downward in the
inheritance tree. However, it allows such casting only in limited ways.
 The DYNCAST2 program shows examples of such casts.
[48]
// RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for dynamic_cast
using namespace std;
////////////////////////////////////////////////////////////////
class Base {
protected:
int ba;
public:
Base() : ba(0)
{ }
Base(int b) : ba(b)
{ }
virtual void vertFunc() //needed for dynamic_cast
{ }
void show()
{ cout << "Base: ba=" << ba << endl; }
};
////////////////////////////////////////////////////////////////
class Derv : public Base
{
Changing Pointer Types with dynamic_cast
[49]
The typeid Operator
 Sometimes you want more information about an object than simple verification that
it’s of a certain class.
 You can obtain information about the type of an unknown object, such as its class
name, using the typeid operator.
 The TYPEID program demonstrates how it works.
[50]
// typeid.cpp
// demonstrates typeid() function
// RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for typeid()
using namespace std;
////////////////////////////////////////////////////////////////
class Base
{
virtual void virtFunc() //needed for typeid
{ }
};
class Derv1 : public Base
{ };
class Derv2 : public Base
{ };
////////////////////////////////////////////////////////////////
void displayName(Base* pB)
{
cout << "pointer to an object of "; //display name of class
cout << typeid(*pB).name() << endl; //pointed to by pB
The typeid Operator
[51]
The typeid Operator
 In this example the displayName() function displays the name of the class of the
object passed to it. To do this, it uses the name member of the type_info class,
along with the typeid operator.
 In main() we pass this function two objects of class Derv1 and Derv2 respectively,
and the program’s output is
pointer to an object of class Derv1
pointer to an object of class Derv2
 Besides its name, other information about a class is available using typeid. For
example, you can check for equality of classes using an overloaded == operator.
 We’ll show an example of this in the EMPL_IO program in Chapter 12, “Streams
and Files.” Although the examples in this section have used pointers, dynamic_cast
and typeid work equally well with references.
[52]

More Related Content

Similar to Object Oriented Programming using C++: Ch11 Virtual Functions.pptx

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Boro
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
Stefano Fago
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
farhan amjad
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Venugopalavarma Raja
 
OO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual FunctionOO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual Function
Yu-Sheng (Yosen) Chen
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
chetan_p211
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
Julie Iskander
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
Aadil Ansari
 
C questions
C questionsC questions
C questions
parm112
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
RashidFaridChishti
 
Oops concept
Oops conceptOops concept
oop objects_classes
oop objects_classesoop objects_classes
oop objects_classes
sidra tauseef
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
ShriKant Vashishtha
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
Mark Whitaker
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
Muhammad Fayyaz
 

Similar to Object Oriented Programming using C++: Ch11 Virtual Functions.pptx (20)

Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Uncommon Design Patterns
Uncommon Design PatternsUncommon Design Patterns
Uncommon Design Patterns
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
OO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual FunctionOO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual Function
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
C questions
C questionsC questions
C questions
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
 
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptxObject Oriented Programming using C++: Ch09 Inheritance.pptx
Object Oriented Programming using C++: Ch09 Inheritance.pptx
 
Oops concept
Oops conceptOops concept
Oops concept
 
oop objects_classes
oop objects_classesoop objects_classes
oop objects_classes
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 

More from RashidFaridChishti

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
RashidFaridChishti
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
RashidFaridChishti
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
RashidFaridChishti
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
RashidFaridChishti
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
RashidFaridChishti
 

More from RashidFaridChishti (20)

Lab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docxLab Manual Arduino UNO Microcontrollar.docx
Lab Manual Arduino UNO Microcontrollar.docx
 
Object Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docxObject Oriented Programming OOP Lab Manual.docx
Object Oriented Programming OOP Lab Manual.docx
 
Lab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docxLab Manual Data Structure and Algorithm.docx
Lab Manual Data Structure and Algorithm.docx
 
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptxData Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Data Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptxData Structures and Agorithm: DS 20 Merge Sort.pptx
Data Structures and Agorithm: DS 20 Merge Sort.pptx
 
Data Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptxData Structures and Agorithm: DS 18 Heap.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
 
Data Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptxData Structures and Agorithm: DS 17 AVL Tree.pptx
Data Structures and Agorithm: DS 17 AVL Tree.pptx
 
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptxData Structures and Agorithm: DS 16 Huffman Coding.pptx
Data Structures and Agorithm: DS 16 Huffman Coding.pptx
 
Data Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptxData Structures and Agorithm: DS 15 Priority Queue.pptx
Data Structures and Agorithm: DS 15 Priority Queue.pptx
 
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptxData Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
 
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptxData Structures and Agorithm: DS 10 Binary Search Tree.pptx
Data Structures and Agorithm: DS 10 Binary Search Tree.pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptxData Structures and Agorithm: DS 08 Infix to Postfix.pptx
Data Structures and Agorithm: DS 08 Infix to Postfix.pptx
 
Data Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptxData Structures and Agorithm: DS 06 Stack.pptx
Data Structures and Agorithm: DS 06 Stack.pptx
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
 
Data Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptxData Structures and Agorithm: DS 02 Array List.pptx
Data Structures and Agorithm: DS 02 Array List.pptx
 
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptxObject Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
 

Recently uploaded

哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 

Recently uploaded (20)

哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 

Object Oriented Programming using C++: Ch11 Virtual Functions.pptx

  • 2. Virtual Functions  High Level Languages  Easy to develop and update  Virtual means existing in appearance but not in reality.  When virtual functions are used, a program that  appears to be calling a function of one class may in  reality be calling a function of a different class.  Why are virtual functions needed?  Suppose you have a number of objects of different classes but you want to put them all in an array and perform a particular operation on them using the same function call.  For example, suppose a graphics program includes several different shapes: a triangle, a ball, a square, and so on. Each of these classes has a member function draw() that causes the object to be drawn on the screen.
  • 3. Virtual Functions  Now suppose you plan to make a picture by grouping a number of these elements together, and you want to draw the picture in a convenient way.  One approach is to create an array that holds pointers to all the different objects in the picture. Shape Pointers Array Index Number [0] [1] [2] [3] [4] Shape Pointer Square Object Rectangle Object Triangle Object Circle Object Triangle Object
  • 4. Virtual Functions  The array might be defined like this:  shape* ptrarr[100]; // array of 100 pointers to shapes  If you insert pointers to all the shapes into this array, you can then draw an entire picture using a simple loop: for(int j=0 ; j<N ; j++) { ptrarr[j]->draw(); // (*ptrarr[j] ).draw() }  This is an amazing capability: Completely different functions are executed by the same function call.  If the pointer in ptrarr points to a ball object, the function that draws a ball is called; if it points to a triangle object, the triangle-drawing function is called.
  • 5. Virtual Functions  This is called polymorphism, which means different forms. The functions have the same appearance, the draw() expression, but different actual functions are called, depending on the contents of ptrarr[j].  Polymorphism is one of the key features of object-oriented programming, after classes and inheritance.  For the polymorphic approach to work, several conditions must be met.  First, all the different classes of shapes, such as balls and triangles, must be descended from a single base class (called shape).  Second, the draw() function must be declared to be virtual in the base class.
  • 6. // Normal functions accessed from pointer #include <iostream> #include <stdlib.h> using namespace std; class Parent { // Parent class public: void Show() { cout << "I am Parent n"; } }; // Derived class Child1 class Child1 : public Parent { public: void Show() { cout << "I am Child 1 n"; } }; // Derived class Child2 class Child2 : public Parent { 6 Normal Member Functions Accessed with Pointer public: void Show() { cout << "I am Child 2 n"; } }; int main() { Child1 C1, C2; Parent* P; // pointer to Parent class P = &C1; // put the address of object C1 in // pointer P. The rule is that pointer to the // object of a Child class is type compatible // with the pointer to the objects of the // Parent class. P->Show(); // execute P->Show() OR (*P).Show(); // the function in Parent Class is executed. P = &C2; // put address of object C1 in P. P->Show(); // execute P->Show() // the function in Parent Class is executed. system("PAUSE"); return 0; } 1 2
  • 7. 7 Normal Member Functions Accessed with Pointer &Child1 Parent Show() Child1 Show() Child2 Show() ptr Ptr->Show() &Child2 ptr Ptr->Show() Figure: Non Virtual Pointer Access
  • 8. // Normal functions accessed from pointer #include <iostream> #include <stdlib.h> using namespace std; class Parent { // Parent class public: virtual void Show() { cout << "I am Parent n"; } }; // Derived class Child1 class Child1 : public Parent { public: void Show() { cout << "I am Child 1 n"; } }; // Derived class Child2 class Child2 : public Parent { 8 Virtual Member Functions Accessed with Pointer public: void Show() { cout << "I am Child 2 n"; } }; int main() { Child1 C1, Child2 C2; Parent* P; // pointer to Parent class P = &C1; // put the address of object C1 in // pointer P. The rule is that pointer to the // object of a Child class is type compatible // with the pointer to the objects of the // Parent class. P->Show(); // execute P->Show() OR (*P).Show(); // the function in Child1 Class is executed. P = &C2; // put address of object C1 in P. P->Show(); // execute P->Show() // the function in Child2 Class is executed. system("PAUSE"); return 0; } 1 2
  • 9. 9 Virtual Member Functions Accessed with Pointer Figure: Virtual Pointer Access &Child2 Parent Show() Child1 Show() Child2 Show() ptr Ptr->Show() &Child1 ptr Ptr->Show()
  • 10. Late Binding  Which version of draw() does the compiler call?  In fact the compiler doesn’t know what to do, so it arranges for the decision to be deferred until the program is running.  At runtime, when it is known what class is pointed to by ptr, the appropriate version of draw will be called. This is called late binding or dynamic binding.  Choosing functions in the normal way, during compilation, is called early binding or static binding.  Late binding requires some overhead but provides increased power and flexibility.  We’ll put these ideas to use in a moment, but first let’s consider a refinement to the idea of virtual functions.
  • 11. Abstract Classes and Pure Virtual Functions  Think of the shape class. We’ll never make an object of the shape class; we’ll only make specific shapes such as circles and triangles.  When we will never want to instantiate objects of a base class, we call it an abstract class. Such a class exists only to act as a parent of derived classes that will be used to instantiate objects.  If we don’t want anyone to instantiate objects of the base class, we’ll write our classes so that such instantiation is impossible.  How can we can do that? By placing at least one pure virtual function in the base class.  A pure virtual function is one with the expression =0 added to the declaration. This is shown in the next example. Shape Triangle Rectangle Square
  • 12. #include <iostream> using namespace std; class Base { // base class public: virtual void show() = 0; // pure virtual function // Using =0 syntax we tell the compiler // that a virtual function will be pure. // this is only a declaration, you never // need to write a definition of the base // class show(), although you can if you // need to. }; class Derv1 : public Base { public: void show() { cout << "Derv1 n"; } }; 12 Virtual Member Functions Accessed with Pointer class Derv2 : public Base { public: void show() { cout << "Derv 2n"; } }; int main() { // you can’t make object from abstract class // Base bad; Base* arr[2]; // array of base class pointers Derv1 dv1; // object of derived class 1 Derv2 dv2; // object of derived class 2 arr[0] = &dv1; // put address of dv1 in array arr[1] = &dv2; // put address of dv2 in array arr[0]->show(); // execute show() in objects arr[1]->show(); return 0; } 2 1
  • 13. Abstract Classes and Pure Virtual Functions  Once you’ve placed a pure virtual function in the base class, you must override it in all the derived classes from which you want to instantiate objects.  If a class doesn’t override the pure virtual function, it becomes an abstract class itself, and you can’t instantiate objects from it (although you might from classes derived from it).  For consistency, you may want to make all the virtual functions in the base class pure.
  • 14. #include <iostream> using namespace std; class person // person class { protected: char name[40]; public: void getName() { cout << " Enter name: "; cin >> name; } void putName() { cout << "Name is: " << name << endl; } // two pure virtual functions virtual void getData() = 0; virtual bool isOutstanding() = 0; }; 14 Virtual Functions and the Person Class class student : public person { private: float gpa; // grade point average public: void getData() // get student data from user { person::getName(); cout << " Enter student's GPA: "; cin >> gpa; } bool isOutstanding() { return (gpa > 3.5); } }; 1 2
  • 15. class professor : public person { // number of papers published private: int numPubs; public: void getData() { //get professor data person::getName(); cout << "Enter number of professor's" "publications: "; cin >> numPubs; } bool isOutstanding() { return (numPubs > 100); } }; int main() { person* Ptr[100]; // array of pointers int n = 0; char choice; 15 Virtual Functions and the Person Class do { cout << "Enter student or professor (s/p): "; cin >> choice; if(choice=='s') Ptr[n] = new student; else Ptr[n] = new professor; Ptr[n++]->getData(); //get person data cout << " Enter another (y/n)? "; cin >> choice; fflush(stdin); } while( choice=='y' ); //cycle until not ‘y’ for(int j=0; j<n; j++){ //print names of all Ptr[j]->putName(); // say if outstanding if( Ptr[j]->isOutstanding() ) cout << " This person is outstandingn"; } system("PAUSE"); return 0; } //end main() 3 4
  • 16. 16 Virtual Functions UML Diagram Sensor virtual init() virtual read() virtual write() init() read() write() Robot Temperature init() read() write() Humidity init() read() write() RTC
  • 17. 17 Virtual Functions UML Diagram Customer Name Bank Name Account balance virtual Credit() virtual Debit() CurrentAccount Credit() Debit() SavingAccount interestRate Credit() Debit() AddInterest()
  • 18. Virtual Destructors  For consistency, you may want to make all the virtual functions in the base class pure.  Base class destructors should always be virtual.  Suppose you use delete with a base class pointer to a derived class object to destroy the derived-class object.  If the base-class destructor is not virtual then delete, like a normal member function, calls the destructor for the base class, not the destructor for the derived class.  This will cause only the base part of the object to be destroyed.  The next program shows how this looks.
  • 19. #include <iostream> #include <stdlib.h> using namespace std; class Base { public: // ~Base() // non-virtual destructor virtual ~Base() // virtual destructor { cout << "Base destroyedn"; } }; class Derv : public Base { public: ~Derv() { cout << "Derv destroyedn"; } }; 19 Virtual Functions and the Person Class int main() { Base* pBase = new Derv; delete pBase; system("PAUSE"); return 0; } 1 2 Using Non-virtual Destructor Using Virtual Destructor
  • 20. Virtual Base Classes  Before leaving the subject of virtual programming elements, we should mention virtual base classes as they relate to multiple inheritance.  Consider the situation shown in Fig. with a base class, Parent; two derived classes, Child1 and Child2; and a fourth class, Grandchild, derived from both Child1 and Child2.  In this arrangement a problem can arise if a member function in the Grandchild class wants to access data or functions in the Parent class.  The next program shows what happens.
  • 21. #include <iostream> #include <stdlib.h> using namespace std; class Parent { protected: int basedata; }; // virtual public class Child1 : public Parent { }; // virtual public class Child2 : public Parent { }; 21 Virtual Base Classes class Grandchild : public Child1, public Child2 { public: void getdata() { cin >> basedata; // ERROR: ambiguous } void putdata() { cout << basedata << endl; // Error } }; int main() { Grandchild c1; c1.getdata(); c1.putdata(); system("PAUSE"); return 0; } 1 2 The use of the keyword virtual in these two classes causes them to share a single common subobject of their base class Parent. Since there is only one copy of basedata, there is no ambiguity when it is referred to in Grandchild.
  • 22. Friend Functions  The concepts of encapsulation and data hiding dictate that nonmember functions should not be able to access an object’s private or protected data  The policy is, if you’re not a member, you can’t get in.  However, there are situations where such rigid discrimination leads to considerable inconvenience.  Imagine that you want a function to operate on objects of two different classes.  Perhaps the function will take objects of the two classes as arguments, and operate on their private data.  In this situation there’s nothing like a friend function.  The next example shows how that friend functions can act as a bridge between two classes:
  • 23. #include <iostream> #include <stdlib.h> using namespace std; class beta;// for frifunc declaration class alpha { private: int data; public: alpha() : data(3) { } friend int frifunc(alpha, beta); }; class beta { private: int data; public: beta() : data(7) { } friend int frifunc(alpha, beta); }; 23 Friends as Bridges int frifunc(alpha a, beta b) { return( a.data + b.data ); } int main() { alpha aa; beta bb; // call the function cout << frifunc(aa, bb) << endl; system("PAUSE"); return 0; } 1 2
  • 24. #include <iostream> #include <stdlib.h> using namespace std; class Distance {// English Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0){} Distance(float fltfeet){ //convert float //feet is integer part feet = static_cast<int>(fltfeet); inches = 12*(fltfeet-feet); } Distance(int ft, float in) { feet = ft; inches = in; } void showdist(){ cout<<feet<<"'-" <<inches<<'"'; } Distance operator + (Distance); }; 24 English Distance Example Distance Distance::operator + (Distance d2) { int f = feet + d2.feet; //add the feet float i = inches + d2.inches; //add the inches if(i >= 12.0) // if total exceeds 12.0, { i -= 12.0; f++; } // return new Distance with sum return Distance(f,i); } int main() { Distance d1 = 2.5, d2 = 1.25, d3; cout << "nd1 = "; d1.showdist(); cout << "nd2 = "; d2.showdist(); d3 = d1 + 10.3F; cout << "nd3 = "; d3.showdist(); // d3 = 10.0 + d1; //float + Distance: ERROR cout << endl; system("PAUSE"); return 0; } 1 2
  • 25. English Distance Example  In this program, the + operator is overloaded to add two objects of type Distance.  Also, there is a one-argument constructor that converts a value of type float, representing feet and decimal fractions of feet, into a Distance value. (it converts 10.25' into 10'–3''.)  When such a constructor exists, you can make statements like this in main(): d3 = d1 + 10.0;  The overloaded + is looking for objects of type Distance both on its left and on its right, but if the argument on the right is type float, the compiler will use the one-argument constructor to convert this float to a Distance value, and then carry out the addition.  The statement: d3 = 10.0 + d1; does not work, because the object of which the overloaded + operator is a member must
  • 26. English Distance Example be the variable to the left of the operator.  When we place a variable of a different type there, or a constant, then the compiler uses the + operator that adds that type (float in this case), not the one that adds Distance objects.  Unfortunately, this operator does not know how to convert float to Distance, so it can’t handle this situation.  How can we write natural-looking statements that have nonmember data types to the left of the operator?  As you may have guessed, a friend can help you out of this dilemma. The FRENGL program shows how.
  • 27. #include <iostream> #include <stdlib.h> using namespace std; class Distance {// English Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0){} Distance(float fltfeet){ //convert float //feet is integer part feet = static_cast<int>(fltfeet); inches = 12*(fltfeet-feet); } Distance(int ft, float in) { feet = ft; inches = in; } void showdist(){ cout<<feet<<"'-" <<inches<<'"'; } friend Distance operator + (Distance, Distance); }; 27 English Distance Example Friend Operator Distance operator + (Distance d1, Distance d2) { int f = d1.feet + d2.feet; //add the feet float i = d1.inches + d2.inches; //add inches if(i >= 12.0) //if inches exceeds 12.0, { i -= 12.0; f++; } //return new Distance with sum return Distance(f,i); } int main(){ Distance d1 = 2.5, d2 = 1.25, d3; cout << "nd1 = "; d1.showdist(); cout << "nd2 = "; d2.showdist(); d3 = d1 + 10.0; //distance + float: OK cout << "nd3 = "; d3.showdist(); d3 = 10.3F + d1; //float + Distance: OK cout << "nd3 = "; d3.showdist(); cout << endl; system("PAUSE"); return 0; } 1 2
  • 28. English Distance Example  The overloaded + operator is made into a friend:  friend Distance operator + (Distance, Distance);  Notice that, while the overloaded + operator took one argument as a member function, it takes two as a friend function.  In a member function, one of the objects on which the + operates is the object of which it was a member, and the second is an argument.  In a friend, both objects must be arguments. The only change to the body of the overloaded + function is that the variables feet and inches, used in NOFRI for direct access to the object’s data, have been replaced in FRENGL by d1.feet and d1.inches, since this object is supplied as an argument.
  • 29. English Distance Example  Sometimes a friend allows a more obvious syntax for calling a function than does a member function.  For example, suppose we want a function that will square (multiply by itself) an object of the English Distance class and return the result in square feet, as a type float.  The MISQ example shows how this might be done with a member function.
  • 30. #include <iostream> #include <stdlib.h> using namespace std; class Distance { // English Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() { // display distance cout<<feet<<"'-"<<inches<<'"'; } float square(); // member function }; float Distance::square(){ float fltfeet = feet + inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd; //return square feet } 30 Friend for Functional Notation int main() { Distance dist(3, 6.0); float sqft; sqft = dist.square(); // return square of dist // sqft = square(dist); // But this is more natural way We can achieve // this effect by making square() a friend of // the Distance class // display distance and square cout << "nDistance = "; dist.showdist(); cout << "nSquare = " <<sqft<<" square feetn"; system("PAUSE"); return 0; } 1 2
  • 31. #include <iostream> #include <stdlib.h> using namespace std; class Distance { // English Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void showdist() { // display distance cout<<feet<<"'-"<<inches<<'"'; } friend float square(Distance); }; float square(Distance d1){ float fltfeet = d1.feet + d1.inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd; //return square feet } 31 Friend for Functional Notation int main() { Distance dist(3, 6.0); float sqft; sqft = square(dist); // display distance and square cout << "nDistance = "; dist.showdist(); cout << "nSquare = " <<sqft<<" square feetn"; system("PAUSE"); return 0; } 1 2
  • 32. #include <iostream> #include <stdlib.h> using namespace std; class alpha{ private: int data1; public: alpha() : data1(99) { } // constructor friend class beta; // (friend beta) is a friend class }; 32 Friend Class class beta{ // all member functions can public: // access private alpha data void func1(alpha a) { cout << "ndata1=" << a.data1; } void func2(alpha a) { cout << "ndata1=" << a.data1; } }; int main(){ alpha a; beta b; b.func1(a);b.func2(a);cout << endl; system("PAUSE"); return 0; } 1 2
  • 33. Static Function  A static data member is not duplicated for each object; rather a single data item is shared by all objects of a class.  The STATIC example showed a class that kept track of how many objects of itself there were.  Let’s extend this concept by showing how functions as well as data may be static.  Next program models a class that provides an ID number for each of its objects.  So, you to query an object to find out which object it is.  The program also casts some light on the operation of destructors.  Here’s the listing for STATFUNC: [33]
  • 34. #include <iostream> #include <stdlib.h> using namespace std; class gamma{ private: static int total; // total objects of this class // (declaration only) int id; // ID number of this object public: gamma() { total++; id = total; } ~gamma() { total--; cout << "Destroying ID " <<id<<endl; } static void showtotal() { cout << "Total is " << total << endl; } void showid() //non-static function { cout << "ID number is " <<id<< endl; } }; Static Function int gamma::total = 0; // definition of total int main(){ gamma g1; gamma::showtotal(); // To access showtotal() using only // the class name, we must declare it to be a // static member function // there may be no such objects at all gamma g2, g3; gamma::showtotal(); g1.showid(); g2.showid(); g3.showid(); gamma * g4 = new gamma; g4->showid(); delete g4; system("PAUSE"); return 0; } 1 2 [34]
  • 35. #include <iostream> using namespace std; class alpha{ private: int data; public: alpha() { } alpha(int d) { data = d; } void display(){ cout << data; } alpha operator = (alpha& a) { // not done automatically data = a.data; cout << "Assignment operator invoked"; // return copy of this alpha return alpha(data); } }; Overloading the Assignment = Operator int main() { alpha a1(37); alpha a2; a2 = a1; // invoke overloaded = cout << "na2="; a2.display(); // display a2 alpha a3 = a2; // is not an assignment but an // initialization, with the same effect alpha a3(a2); // it does NOT invoke = // we initialize the object a3 to the value a2 cout << "na3="; a3.display(); // display a3 cout << endl; return 0; } 1 2 [35]
  • 36. Overloading the Assignment Operator  The assignment operator = is unique among operators in that it is not inherited.  If you overload the assignment operator in a base class, you can’t use this same function in any derived classes.  you can define and at the same time initialize an object to the value of another object with two kinds of statements:  Both styles of definition invoke a copy constructor: A constructor that creates a new object and copies its argument into it.  The default copy constructor, which is provided automatically by the compiler for every object, performs a member-by-member copy. This is similar to what the assignment operator does; the difference is that the default copy constructor also creates a new object.  Like the assignment operator, the copy constructor can be overloaded by the user. [36] alpha a3(a2); // copy initialization alpha a3 = a2; // copy initialization, alternate syntax
  • 37. #include <iostream> using namespace std; class alpha{ private: int data; public: alpha() { } alpha(int d) { data = d; } alpha(alpha& a){ // copy constructor data = a.data; cout << "nCopy constructor invoked"; } void display() { cout << data; } // overloaded = operator void operator = (alpha& a){ data = a.data; cout << "Assignment operator" << "invoked"; } }; Overloading the Copy Constructor int main() { alpha a1(37); alpha a2; a2 = a1; // invoke overloaded = cout << "na2="; a2.display(); //display a2 alpha a3(a1); // invoke copy constructor // alpha a3 = a1; // equivalent of a3(a1) cout << "na3="; a3.display(); //display a3 cout << endl; return 0; } 1 2 [37]
  • 38. Overloading the Copy Constructor  The copy constructor is invoked when an object is passed by value to a function. It creates the copy that the function operates on.  Thus if the function void func(alpha); were declared in the program and this function were called by the statement func(a1); then the copy constructor would be invoked to create a copy of the a1 object for use by func().  Of course, the copy constructor is not invoked if the argument is passed by reference or if a pointer to it is passed. In these cases no copy is created; the function operates on the original variable.  The copy constructor also creates a temporary object when a value is returned from a function. Suppose there was a function like this alpha func(); and this function was called by the statement a2 = func();  The copy constructor would be invoked to create a copy of the value returned by func(), and this value would be assigned (invoking the assignment operator) to a2.  When an argument is passed by value, a copy of it is constructed. What makes the copy? The copy constructor. But this is the copy constructor, so it calls itself. [38]
  • 39. How to Prohibit Copying  To avoid copying, overload the assignment operator and the copy constructor as private members.  As soon as you attempt a copying operation, such as  The compiler will tell you that the function is not accessible. You don’t need to define the functions, since they will never be called. [39] class alpha{ private: alpha& operator = (alpha&); // private assign. Operator alpha(alpha&); // private copy constructor }; alpha a1, a2; a1 = a2; // assignment alpha a3(a1); // copy constructor
  • 40. #include <iostream> using namespace std; class array{ private: // occupies 10 bytes char charray[10]; public: void reveal() { cout << "nMy object’s address is " << this; } }; int main(){ array w1, w2, w3; // make three objects w1.reveal(); // see where they are w2.reveal(); w3.reveal(); cout << endl; return 0; } The this Pointer 1 2 [40]  The main() program in this example creates three objects of type where.  It then asks each object to print its address, using the reveal() member function.  This function prints out the value of the this pointer. Here’s the output: My object’s address is 0x8f4effec My object’s address is 0x8f4effe2 My object’s address is 0x8f4effd8  Since the data in each object consists of an array of 10 bytes, the objects are spaced 10 bytes apart in memory.  Some compilers may place extra bytes in objects, making them slightly larger than 10 bytes.
  • 41. // dothis.cpp the this pointer referring to data #include <iostream> using namespace std; class what{ private: int alpha; public: void tester(){ // same as alpha = 11; this->alpha = 11; //same as cout << alpha; cout << this->alpha; } }; int main(){ what w; w.tester(); cout << endl; return 0; } Accessing Member Data with this 1 2 [41]  This program simply prints out the value 11.  Notice that the tester() member function accesses the variable alpha as this->alpha  This is exactly the same as referring to alpha directly.  This syntax works, but there is no reason for it except to show that this does indeed point to the object.
  • 42. Using this for Returning Values  A more practical use for this is in returning values from member functions and overloaded operators.  Recall that in the program where we could not return an object by reference, because the object was local to the function returning it and thus was destroyed when the function returned.  We need a more permanent object if we’re going to return it by reference. The object of which a function is a member is more permanent than its individual member functions.  An object’s member functions are created and destroyed every time they’re called, but the object itself endures until it is destroyed by some outside agency (for example, when it is deleted).  Thus returning by reference the object of which a function is a member is a better bet than returning a temporary object created in a member function. The this pointer makes this easy. [42]
  • 43. //assign2.cpp returns contents of the this pointer #include <iostream> using namespace std; class alpha{ private: int data; public: alpha() { } // no-arg constructor alpha(int d) { data = d; } // one-arg constructor void display() { cout << data; } // display data alpha& operator = (alpha& a){ // overloaded = operator data = a.data; // not done automatically cout << "nAssignment operator invoked"; return *this; // return copy of this alpha } }; int main(){ alpha a1(37); alpha a2, a3; a3 = a2 = a1; //invoke overloaded =, twice cout << "na2="; a2.display(); cout << "na3="; a3.display(); //display a3 cout << endl; return 0; } Using this for Returning Values [43]
  • 44. Using this for Returning Values  In this program we can use the declaration alpha& operator = (alpha& a) which returns by reference, instead of alpha operator = (alpha& a) which returns by value.  The last statement in this function is return *this;  Since this is a pointer to the object of which the function is a member, *this is that object itself, and the statement returns it by reference. Here’s the output of ASSIGN2:  Assignment operator invoked  Assignment operator invoked  a2=37  a3=37  Each time the equal sign is encountered in a3 = a2 = a1; the overloaded operator=() function is called, which prints the messages. The three objects all end up with the same value.  You usually want to return by reference from overloaded assignment operators, using *this, to avoid the creation of extra objects. [44]
  • 45. Beware of Self-Assignment  A corollary of Murphy’s Law states that whatever is possible, someone will eventually do.  This is certainly true in programming, so you can expect that if you have overloaded the = operator, someone will use it to set an object equal to itself: alpha = alpha;  Your overloaded assignment operator should be prepared to handle such self-assignment. Otherwise, bad things may happen [45]
  • 46. Checking the Type of a Class with dynamic_cast  Suppose some other program sends your program an object (as the operating system might do with a callback function). It’s supposed to be a certain type of object, but you want to check it to be sure.  How can you tell if an object is a certain type?  The dynamic_cast operator provides a way, assuming that the classes whose objects you want to check are all descended from a common ancestor. The DYNCAST1 program shows how this looks. [46]
  • 47. // RTTI must be enabled in compiler #include <iostream> #include <typeinfo> //for dynamic_cast using namespace std; class Base{ virtual void vertFunc() //needed for dynamic cast { } }; class Derv1 : public Base { }; class Derv2 : public Base { }; //checks if pUnknown points to a Derv1 bool isDerv1(Base* pUnknown){ //unknown subclass of Base Derv1* pDerv1; if( pDerv1 = dynamic_cast<Derv1*>(pUnknown) ) return true; else return false; } //-------------------------------------------------------------- int main(){ Derv1* d1 = new Derv1; Checking the Type of a Class with dynamic_cast [47]
  • 48. Checking the Type of a Class with dynamic_cast  Here we have a base class Base and two derived classes Derv1 and Derv2.  There’s also a function, isDerv1(), which returns true if the pointer it received as an argument points to an object of class Derv1.  This argument is of class Base, so the object passed can be either Derv1 or Derv2.  The dynamic_cast operator attempts to convert this unknown pointer pUnknown to type Derv1.  If the result is not zero, pUnknown did point to a Derv1 object.  If the result is zero, it pointed to something else.  The dynamic_cast operator allows you to cast upward and downward in the inheritance tree. However, it allows such casting only in limited ways.  The DYNCAST2 program shows examples of such casts. [48]
  • 49. // RTTI must be enabled in compiler #include <iostream> #include <typeinfo> //for dynamic_cast using namespace std; //////////////////////////////////////////////////////////////// class Base { protected: int ba; public: Base() : ba(0) { } Base(int b) : ba(b) { } virtual void vertFunc() //needed for dynamic_cast { } void show() { cout << "Base: ba=" << ba << endl; } }; //////////////////////////////////////////////////////////////// class Derv : public Base { Changing Pointer Types with dynamic_cast [49]
  • 50. The typeid Operator  Sometimes you want more information about an object than simple verification that it’s of a certain class.  You can obtain information about the type of an unknown object, such as its class name, using the typeid operator.  The TYPEID program demonstrates how it works. [50]
  • 51. // typeid.cpp // demonstrates typeid() function // RTTI must be enabled in compiler #include <iostream> #include <typeinfo> //for typeid() using namespace std; //////////////////////////////////////////////////////////////// class Base { virtual void virtFunc() //needed for typeid { } }; class Derv1 : public Base { }; class Derv2 : public Base { }; //////////////////////////////////////////////////////////////// void displayName(Base* pB) { cout << "pointer to an object of "; //display name of class cout << typeid(*pB).name() << endl; //pointed to by pB The typeid Operator [51]
  • 52. The typeid Operator  In this example the displayName() function displays the name of the class of the object passed to it. To do this, it uses the name member of the type_info class, along with the typeid operator.  In main() we pass this function two objects of class Derv1 and Derv2 respectively, and the program’s output is pointer to an object of class Derv1 pointer to an object of class Derv2  Besides its name, other information about a class is available using typeid. For example, you can check for equality of classes using an overloaded == operator.  We’ll show an example of this in the EMPL_IO program in Chapter 12, “Streams and Files.” Although the examples in this section have used pointers, dynamic_cast and typeid work equally well with references. [52]