C++ programming language part: 52 Inheritance
Inheritance
The way to reuse existing software to create new software is by
means of inheritance also called specialization or derivation.
It is an important feature of object-oriented programming. The most important
of them being the reusability of code. Once a class is defined and debugged, it
can be used to create new subclass. The class from which another class has been
derived is called the base class The class that inherits
the properties of the base class is called the derived class. The derived
class override some or all the properties of the base class. An object defined
outside the class can access only the public member of the class.
Therefore, private members of a class cannot be directly accessed from
outside the class. The protected members of a class can be
accessed by its member function, or within any class derived from it. Protected members behave like public members with
respect to the derived class, and like private members with respect to the rest
of the program.
The common syntax for deriving a class Y from a class
X is: class Y:public X
Program 42_1
//inherit.demo
class
furniture //class defined
{ protected:
char color[10];
int width,height;
};
class
bookshelf:public furniture
{ private:
int self_no; //
member data
public:
void data_in()
{
cout<<"Type color of bookshelf
(red or blue?)\t";
cin>>color;
cout<<"Type width\t";
cin>>width;
cout<<"Type height\t";
cin>>height;
cout<<"Type number of
selves\t";
cin>>self_no; }
void data_out() //member function
{
cout<<" The color is "<
cout<<" The width is
"<
cout<<" The height is
"<
cout<<" Number of selves is
"<
};
class
chair:public furniture
{ private:
int leg_no;
public:
void data_in() //member
function
{cout<<"Type color of chair
(yellow or black?)\t";
cin>>color;
cout<<"Type width\t";
cin>>width;
cout<<"Type height\t";
cin>>height;
cout<<"Type number of
legs\t";
cin>>leg_no; }
void data_out() //member function
{cout<<"
The color is "<
cout<<" The width is
"<
cout<<" The height is
"<
cout<<" Number of legs is
"<
};
void main()
{bookshelf
A; // k is called object
chair B;
A.data_in(); //would
be k->data_in() if pointer
A.data_out();
B.data_in(); //would
be k->data_in() if pointer
B.data_out(); }
Keyword Size Of
Program 42_extra
{
char a1[10],a2[10];//array of size 10 defined
char *a3; //[20];
int
i,j;
a3=new char[20];
cout<<"type the string "<<"\n";
cin>>a1;
cout<<"type
another string "<<"\n";
cin>>a2;
for(i=0;a1[i];i++)
a3[i]=a1[i];
for(j=0;a2[j];j++,i++)
a3[i]=a2[j];
a3[i]='\0';
cout<<"The
marged string is "<
cout<<"
The size of strings= "<
Part- 86 assign the content of twostring
জ্ঞানকোষ প্রকাশনী
৩৮/২-ক, বাংলাবাজার (২য় তলা), ঢাকা।
ফোনঃ ৭১১৮৪৪৩, ৮১১২৪৪১, ৮৬২৩২৫১.
কলকাতায় পরিবেশক/প্রাপ্তিস্থান
রিতা ইন্টারন্যাশনাল
৩৬, পি.এন. ব্যানার্জি রোড, কলকাতা
ফোনঃ ২৫১৩৮৩৫৯, ৯৮৩০৪৩৯৬৭৯, +৯১৯৮৩০৪৩৯৬৭৯