Vous êtes sur la page 1sur 4

OBJECT: TO

BECOME FAMILIAR WITH

INHERITANCE.

Inheritance When ever we deal with classes they will lead us to the idea of inheritance. To understand the inheritance lets take an example of our physical world; in this world we commonly group certain things in the classes and subclasses, like vehicle is the class and the buses, cars, trucks etc are the subclasses; computer is the class and the supercomputer, mainframe, minicomputer and desktop PCs are the subclasses; Educational institutions is the class and the School, College, University and Madarsa are the subclasses. In all above subclasses we will notice a thing that each subclass shares some common features from the class form which it is derived. Like School and Madarsa are the two subclasses of the Educational Institution School and Madarsa have the same function of making the students learn means they share a feature but is possible that School or college may have their own extra features also. In object oriented programming there is the same concept that once you have created a class you can also create many subclasses of it then all the subclasses will inherit some features of the main class from which they are derived and they may have some extra features also. The main class ids called the Base Class and the subclass is called the Derived Class.

Educational Institution

Education Behavior

Education Behavior General Knowledge

Education Behavior Religious Knowledge

School

Madarsa

Base Class and Derived Class The first main class is called the Base class. The base class contains the capabilities which are shared between the derived classes and the subclass which is derived from the base class is called the Derived class. The derived class inherits all the features that are present in the base class but the derived class can have extra capabilities which are not present in the base class.

Program (inherit1.cpp)
#include <constream.h> class Counter { protected: int count; public: Counter(){count=0;} Counter(int c){count=c;} int get_count(){return count;} int operator ++(){return (++count);} }; class countDN:public Counter { public: int operator -(){return (--count);} }; void main() { countDN C1; cout<<C1.get_count(); ++C1; ++C1; ++C1; cout<<C1.get_count(); --C1; --C1; cout<<C1.get_count(); getch(); }

Program (inherit2.cpp)
#include <constream.h> #include <string.h> class TUI_OS { protected: char name[15]; int version; char file_system[10]; public: void setdata(char nm[], int ver, char fs[]) { strcpy(name,nm); strcpy(file_system,fs); version=ver; } void showdata() { cout<<"Operating System = "<<name<<endl; cout<<"Version = "<<version<<endl; cout<<"File System = "<<file_system<<endl<<endl; } }; class GUI_OS:public TUI_OS { private: char edition[20]; public: void setdata2(char nm[], int ver, char fs[],char edt[]) { strcpy(name,nm); strcpy(file_system,fs); version=ver; strcpy(edition,edt); } void { showdata2() cout<<"Operating System = "<<name<<endl;

cout<<"Version = "<<version<<endl; cout<<"File System = "<<file_system<<endl; cout<<"Edition = "<<edition<<endl<<endl; } }; void main() { clrscr(); TUI_OS PC_DOS, MS_DOS; GUI_OS Win_98, Win_2000,Win_XP; PC_DOS.setdata("PC-DOS",1970,"FAT"); MS_DOS.setdata("MS-DOS",1972,"FAT"); Win_98.setdata2("Windows 98",1998,"FAT32", "Professional Edition"); Win_2000.setdata2("Windows 2000",2000,"NTFS", "Professional Edition"); Win_XP.setdata2("Windows XP",2006,"NTFS", "Home Edition"); PC_DOS.showdata(); MS_DOS.showdata(); Win_98.showdata2(); Win_2000.showdata2(); Win_XP.showdata2(); getch(); }
The above program contain the base class TUI_OS in which there are three features, name, version and file system and the derived class GUI_OS contains these three features as well as an extra feature the Edition which is not present in the TUI_OS. It is general that the Text User Interface Operating Systems have no Edition but Version and the Graphical User Interface Operating Systems as Windows XP has certain Editions, as Home, Professional and Server etc.

Vous aimerez peut-être aussi