C++ programming language part- 61 Virtual Function and polymorphism
Virtual Function and polymorphism
Object-oriented
programming refers to use of derived classes and virtual function. Suppose
you have three televisions with three different VCRs. Those are requiring
different and complex operation to use them. Then one day you have got a simple
remote control that can operate all kinds of VCRs. That marvelous device
represents the essence of object-oriented
programming . That is using the same function from base class called
virtual function.
Consider a class named furniture
can be the base class from which the chair
class is derived. So consider the
statement: chair X; X.data_in(); .
An object of the class chair named X is created and a member function
called data_in(). In this case there
is no ambiguity. consider the next statement again furniture *x; X->data_in().
Here is no ambiguity also. This process is called binding. In some situation may the type of object or pointer may
not known at the time of compilation that are depended on the request of the
user. since the object are dynamically created. This process is called dynamic
binding, also known as late binding. consider the following statement.
furniture *f;
f=new chair;
f->data_in();
Virtual function with static polymorphism
Program 45_1
//virtual
class.demo
class
furniture //class defined
{private:
char
color[10];
int h,w;
public:
void data_in()
{ cout<<"Type color,width &
height \t";
cin>>color>>w>>h; }
void data_out()
{ cout<
void massage()
{ cout<<"This result from base
class"<
};
class
chair:public furniture
{ private:
int leg_no;
public:
void data_in() //member function
{furniture::data_in(); // Scope Resolution Operator
cout<<"Type number of
legs\t";
cin>>leg_no; }
void data_out() //member function
{furniture::data_out(); // Scope Resolution Operator
cout<<" Number of legs are
"<
void massage()
{ cout<<" This result from derived
class "<
};
void main()
{furniture X;
furniture *f;
chair Y;
f=&X;
f->data_in();
f->data_out();
f->massage();
f=&Y;
f->data_in();
f->data_out();
f->massage(); }
Note: The both function will generate from
base class. We can modify the code from base class furniture.
virtual void data_in(); virtual void data_out();virtual
void massage();
Now it will generate from both base
and derrived.
Part- 86 assign the content of twostring
জ্ঞানকোষ প্রকাশনী
৩৮/২-ক, বাংলাবাজার (২য় তলা), ঢাকা।
ফোনঃ ৭১১৮৪৪৩, ৮১১২৪৪১, ৮৬২৩২৫১.
কলকাতায় পরিবেশক/প্রাপ্তিস্থান
রিতা ইন্টারন্যাশনাল
৩৬, পি.এন. ব্যানার্জি রোড, কলকাতা
ফোনঃ ২৫১৩৮৩৫৯, ৯৮৩০৪৩৯৬৭৯, +৯১৯৮৩০৪৩৯৬৭৯