Virtual Base Class, Overloading and Overriding Design Pattern in Object Oriented Programming with C++
Total Page:16
File Type:pdf, Size:1020Kb
Asian Journal of Computer Science And Information Technology 7: 3 July (2017) 68 –71. Contents lists available at www.innovativejournal.in Asian Journal of Computer Science And Information Technology Journal Homepage: http://innovativejournal.in/ajcsit/index.php/ajcsit VIRTUAL BASE CLASS, OVERLOADING AND OVERRIDING DESIGN PATTERN IN OBJECT ORIENTED PROGRAMMING WITH C++ Dr. Ashok Vijaysinh Solanki Assistant Professor, College of Applied Sciences and Professional Studies, Chikhli ARTICLE INFO ABSTRACT Corresponding Author: The objective of this research paper is to discuss prime concept Dr. Ashok Vijaysinh Solanki of inheritance in object oriented programming. Inheritance play major role in Assistant Professor, College of design pattern of modern programming concept. In this research paper I Applied Sciences and Professional explained about inheritance and reusability concepts of oops. Since object Studies, Chikhli oriented concepts has been introduced in modern days technology it will be assist to creation of reusable software. Inheritance and polymorphism play Key Words: Inheritance; vital role for code reusability. OOPs have evolved to improve the quality of the Encapsulation; Polymorphism; programs. I also discuss about the relationship between function overloading, Early binding; Late binding. operator overloading and run-time polymorphism. Key focus of this research paper is to discuss new approach of inheritance mechanism which helps to solve encapsulation issues. Through virtual base class concept we explore the relationship and setup connection between code reusability and inheritance. Paper explained virtual base class, early binding and late binding concepts DOI:http://dx.doi.org/10.15520/ using coding demonstration. ajcsit.v7i3.63 ©2017, AJCSIT, All Right Reserved. I INTRODUCTION Object oriented programming model is modern such as hierarchical and multiple inheritances which is practical approach and it is important programming known as pure hybrid inheritance. methodology that helps to develop modular design and II Literature Survey maximum utilization of software reusability concepts. In literature survey I, covered three major areas that is Object oriented fundamental helps us to reduce overloading, overriding and virtual base classes. With maintenance cost, reliability improvement, reusability of respect to literature review there has been lot research existing code and flexibility approach with the help of data done on oops. encapsulation. In object oriented paradigm encapsulation Michele Yaiser [6]: This paper focused on concepts of and inheritance contribute major role for reusability. inheritance and its applications. Paper also covered Encapsulation is also known as data hiding. Abstraction, encapsulation and overloading. In object oriented terminology, objects are basic run time Bruce L-Horn [8]: This paper presented different entity. More elaborate form it is well organized data mechanism and approaches to compare inheritance. It also structure which associates with set of operations, and emphasis on method combination method. operations recognized as behavior which is visible within Shivam [7]: This paper presented concept of inheritance object. Wrapping of data and function in single unit is and types of inheritance which provides the concept of known as class [1]. This paper is based on important use reusability of existing code. Paper focus on how inheritance and issues of virtual base class compile time plays major role and it is central concept in C++. polymorphism and run-time polymorphism using virtual As per above papers we conclude that work done on the function. Compile time polymorphism or early binding is a inheritance features to maintain more flexible mechanism process of function label which shows entry point of the for reusing code and encourages modular design and function at the time of compilation. This can be done using software reuse. method overloading. Function can be overloaded in two III Virtual Base Classes situations. First one when data types changed for It is also known as virtual inheritance. It is C++ arguments and second one number of parameters is techniques that ensure only one copy of base class changed [2]. members are inherited by grandchild derived class. In Run-time polymorphism can be done using virtual hybrid and specifically pure hybrid inheritance possibility function. It can be empty block or pure virtual function of generation of multiple paths [5]. C++ resolves it as a base defined in abstract class. Run-time polymorphisms can be class intended to be common throughout the hierarchy performed in inheritance only whereas compile time which is denoted as virtual using virtual keyword. It is used polymorphism can be done in same class. Virtual base to prevent from multiple instances of a given class which is class is implemented for avoiding duplication of access appearing in an inheritance hierarchy when using multiple multiple paths due to combination of different inheritance inheritances. Author(s) agree that this article remain permanently open access under the terms of the Creative Commons Attribution License 4.0 International License Page 68 Ashok et.al/Virtual Base Class, Overloading and Overriding Design Pattern in Object Oriented Programming With C++ Figure Figure 1 demonstration about dreaded diamond } which refers to class structure in which a particular class void putmarks() appears more than once in a class inheritance hierarchy. { Such situation is recognized as ambiguity in C++. In said cout<<"\tMarks Obtained\n"; situation compiler get confusion because of multiple path cout<<"\n\tCPP:"<<cpp; generation. Only one access path should be generated, for cout<<"\n\tRDBMS:"<<rdbms; that we have to use virtual keyword at the time of derived } class creation. }; class sports: public virtual student { public: int score; void getscore() { cout<<"Enter Sports Score:"; cin>>score; } void putscore() { cout<<"\n\tSports Score is:"<<score; } }; class result: public test, public sports { int total; public: void display() { total=cpp+rdbms+score; putnumber(); putmarks(); Figure 1: Hybrid Inheritances with multiple paths for putscore(); Class D cout<<"\n\tTotal Score:"<<total; Following code shows demonstration of how to resolve } ambiguity occur in C++ for pure hybrid inheritance. }; #include<iostream.h> #include<conio.h> void main() { class student result o1; { clrscr(); int rno; o1.getnumber(); public: o1.getmarks(); void getnumber() o1.getscore(); { o1.display(); cout<<"Enter Roll No:"; getche(); cin>>rno; } } Code 1: Sample Code for resolve ambiguity in hybrid void putnumber() Inheritance { cout<<"\n\n\tRoll No:"<<rno<<"\n"; } IV Overloading }; Object oriented programming is useful programming methodology for development modern software which class test: virtual public student support concept of software reusability. Overloading is { key concepts of oops for modular design of software. With public: respect to C++ object oriented structure supports compile int cpp, rdbms; time and run time polymorphism. In C++ there are two void getmarks() types of overloading i.e. Function overloading and { Operator overloading. We can do function overloading in cout<<"Enter Marks of C++ and RDBMS both ways. Function overloading means that define same Subjects\n"; function multiple times so that we can call them with cout<<"CPP:"; different parameters [1]. Arguments list also recognized as cin>>cpp; signature. For making proper use of overloading, we have cout<<"RDBMS"; to understand design pattern for writing code. If you not cin>>rdbms; understand concepts of overloading specifically for 69 Ashok et.al/Virtual Base Class, Overloading and Overriding Design Pattern in Object Oriented Programming With C++ operator overloading it leads to wrong result. Object oriented language like Java does not allow operator Code 2: Sample Code for Late Binding using Virtual overloading because operator overloading provides Function multiple meaning for same operator which can make V Overriding learning curve for developer as well as developing applications and thing becomes more confusing and When you create an object of derived class and call the messing. member function which is exists in base and derived Early Binding: In early binding, an object is assigned to a classes, such circumstances member function of derived variable at the time of declaration of a specific object type. get invoked and base class member function is ignored. Early binding objects are strong type objects. In this type This feature of C++ is recognized as function overriding. of binding functions and properties of respective classes Function overriding can be resolved either compile time which are detected and checked during compile time and or run time. Following segment of code shows perform other optimization before an application executes demonstration of function overriding [5]. [2]. The biggest advantage of using early binding is better class base performance and ease of development. { ---------- Late Binding: In this type functions, variables and public: properties are detected and checked only at the run time. void getdata() It means that compiler does not know what types of object { ------------- contain until run time. Advantage of run time binding is } that the objects of this type can hold references to any }; object. It allows software systems to be more easily class derived : public base extended during both development and maintenance. { This Function will not be called Following code demonstration