Abstract Class in C++

Hi welcome it this is Kishore and today we are going to discuss about the Abstract base class and pure virtual function ok already in previous sessions we have discussed what is a virtual function what is a virtual class and what is a virtual destructor also ok now we are going to discuss about pure virtual function and abstract base classes now what is called pure virtual function ok pure virtual function is a function which is declared in base class but there is no definition related to the base class for itself generally when a function is declared in a class letter we are going to define it with the base class contains but in pure virtual function in pure virtual function function is declared function is declared in base class without any code without any code that means it is not having any code related to the base class and once again it is redefined in derived classes once again it is redefined in derived classes that’s why the function a function which is declared in base class but it doesn’t contain any content related to the base class and it is redefined once again in derived class with the content ok now there only for few abstract means are there only for inheritance purpose and one more thing there also called do nothing functions ok pure virtual functions are also called do nothing functions ok and how they are going to be executed OK I will will show you how they are going to be executed before going to discuss about pure virtual function we have to discuss one more topic called abstract base class abstract base class now what is called abstract base class it is a class it is a base class which contains the pure virtual function class which contains pure virtual function is called abstract base class ok next the most important thing is abstract classes never allows to define the object ok means we are not able to create the object of abstract base class that purely their Fury for inheritance concept only the desired especially for inheritance concept only ok next there used to there used to create base class pointer base class pointer at runtime to get the runtime polymorphism remember this abstract base class are just used to create base class pointer one time to get the dynamic polymorphism runtime polymorphism how to implement this concept first what is a pure virtual function it is a function which is declared in a class but not defined in the class or it is not having any content related to the base class now watch this for example hash include iostream what has it is a common header letter hashinclude new road has no I am going to declare class like this class for example shape now there is a ship ok come this here I am going to declared protected here there is a protected visibility because of I want to derive summer classes from the sky and here I want to use this data in other classes whenever you want to use one class data members in another class means in derived classes you should have declared protected the Sahir protected NTR float what it maybe float D1 comedy d1 D2 or what dimension One dimension to float B1, B2 even mention one thing that is the dimension to ok next public area it is the public June and here one function wide get information or get data or get dimensions ok we can declare any Name no problem at all get dimensions ok here just I am going to use only this funsi in D1 D2 OK what is here get dimension function is defined inside the shape and I am going to use only seen because of letter and going to use this out for this because of I want to use this function in two different manners for two different objects are shapes that’s why I here I am going to write only seeing I am not going to write the SP of ok fine now how to declare the pure virtual function first of all you should have to start the virtual keyword OK it is a Keyword ok every pure virtual function on every virtual function should be started it was ok friend letter the return data type function is having return type now suppose float OK what I want to find out area Ok actually or program is what shape and every shape is generally having area ok already D1 D2 or what floating data types D1 D2 are the floating data types that’s why I hear area also what fruit now virtual fruit area and here it is the most important thing equal to zero that’s all now this function is called pure virtual function when a function OK when a function is declared and initialised with zero when a function is declared with virtual keyword and initialised with zero then it is called pure virtual function now this function is declared where in shape class that’s why this class is called as tract base class the Abstract base class means what take class it is a class which contains pure virtual function now it is a pure virtual function next I said one more thing about this one when it is a pure virtual function it is only declared in base class OK it is declared in base class without any content related to the base class that’s why simply I am going to close the class now watch it is it is a pure virtual function initialised with zero and one more important. Always initialise with zero no more means other values are not allowed remember this pure virtual function always initialising 30 only ok other values are not accepted it gives error the next now see this here it is declared but it is defined no it i going tos not having any definition because of fever virtual functions are defined in derived classes the how to we divide defined in the file by abstract base class created and pure virtual function also different letters in this class there is a triangle examples triangle shaped now it is the derived class now driving from public Sachit public shape actually shape is the Abstract base class because of Vedas having pure virtual function from that shaped class I am going to create triangle letters here public area now I want to find out triangle area I want to find out triangle area already here a function declared now I want to implement virtual function i’m going to find out the function. Ok now here why do it now in void mean what we have to do we have to call the derived class member functions first of all suppose it is a triangle I am going to create triangle and one more most important thing actually here shape classes what abstract base class because of this pure virtual function try to create an object from basic shape yes actually she is the best classmate and ac is the what name now this program gives error this program gives error because of because of here shape is the Abstract base class and abstract base classes are not able to derive are declared as objects ok for abstract base class we are not able to declare the objects that only for inheritance purpose only the road only used in inheritance concept ok that’s why here don’t use this statement that gives yeah that’s why I am not going to use this one ok next how to call suppose here this time triangle is there a rectangular shape of a triangle first of all triangle triangle know that is what object of triangle letter simply do like this T dot actually here function called get diamonds can now get dimensions but this get dimension belongs to right now triangle that’s why I am going to use like this out 10th triangle ok and the triangle base and height I want to enter base and height enter triangle base fight scene Ok actually scene is not required why because already here input is completed that’s why I directly used T dot get what did not get d i get that is going to read the what ok triangle objects that is going to re enter triangle base and height is 6 letter simply do like this out rectangle area printed now get fresh fresh ok now it is how to use of abstract base class and derived classes and pure virtual function now first of all we are going to find out the triangle area later we are going to find out the rectangle area it is how to work with abstract base class with virtual functions with pure virtual function and abstract base class never used to create the objects it will give you the and one more thing I have discussed here your virtual functions and abstract base classes and here when the pure virtual function is declared you should be started with what virtual and return datatype function name and it should be initialised with what next year the most important thing is here already you have used the virtual virtual functions are defined in which class we are going to define ok once again don’t use virtual keyword

Speak Your Mind

*