Learning this pointer which represents an object that invokes or call a member function. Concept explanation with programming example.
The video contains virtual function and pure virtual function concept with programming example in C++. Introduction abstract class concept in c++ with real and C++ example.
A virtual function is a member function that is declared within a base class and redefined by a derived class.It is a runtime polymorphism.
Base class and derived class have same function name and a base class pointer is assigned an address of derived class object then also pointer will execute a base class function.
To execute a function of a derived class, we have to declare a function of a base class as virtual.
To declare virtual function just uses keyword virtual preceding its normal function declaration.
After making a virtual function, the compiler will determine which function to execute at runtime on the basis of assigned address to a pointer of a base class.
1. The virtual functions must be a member of any class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it may not be used.
6. If two functions with the same name have different prototypes, C++ considers them as overloaded functions, and the virtual function mechanism is ignored.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. The derived class pointer cannot point to the object of a base class.
9. When a base pointer points to a derived class, then also it is incremented or decremented only relative to its base type. Therefore we should not use this method to move the pointer to the next object.
10. If a virtual function is defined in a base class, it need not be necessarily redefined in the derived class. In such cases, call will invoke the base class.
A pure virtual function means ‘do nothing’ function.
We can say empty function. A pure virtual function has no definition relative to the base class.
Programmers have to redefine the pure virtual function in a derived class because it has no definition in a base class.
A class containing pure virtual function cannot be used to create any direct objects of its own.
This type of class is also called an abstract class.
GTU - Computer Engineering (CE) - Semester 4 - 2140703 - Object Oriented Programming with C++ - Concepts of C++ - Introduction to Object Oriented Programming
Object-Oriented Programming with C++ PPTs are available here: http://www.darshan.ac.in/DIET/CE/GTU-...
This video is recorded by Prof. Rupesh Vaishnav ([email protected], +91-9428037452) at Computer Engineering Department of Darshan Institute of Engineering & Technology, Rajkot as per GTU Syllabus.
Darshan Institute of Engineering & Technology, Rajkot is a leading institute offering undergraduate, graduate and postgraduate programs in engineering. The Institute is affiliated to the Gujarat Technological University (GTU) and approved by the AICTE, New Delhi.
Visit us: http://www.darshan.ac.in
Write us: [email protected]
Facebook: / darshaninstitute.official
Twitter: / darshan_inst
Instagram: / darshan_inst