2. SYLLABUS
• Pointer
• Polymorphism & Inheritance
• Pointer(Pointer to object ,this pointer, pointer to derive class)
• Introduction to polymorphism(Runtime polymorphism, compiletime polymorphism)
• Operator overloading
• Virtual function
• Inheritance(Single inheritance, multiple inheritance,multilevel inheritance,hierarchical inheritance,hybrid
inheritance)
• Virtual Base class
• Abstract class
3. POINTER
• When a variable is created in C++, a memory address is assigned to the variable. And when we assign a value
to the variable, it is stored in this memory address.
• To access it, use the & operator, and the result will represent where the variable is stored:
• #include <iostream>
• #include <string>
• using namespace std;
• int main() {
• string food = "Pizza";
• cout << &food;
• return 0;
• }
4. POINTER
• A pointer however, is a variable that stores the memory address as its value.
• A pointer variable points to a data type (like int or string) of the same type, and is created with the * operator.
The address of the variable you're working with is assigned to the pointer:
• #include <iostream>
• #include <string>
• using namespace std;
• int main() {
• string food = "Pizza"; // A string variable
• string* ptr = &food; // A pointer variable that stores the address of food
• // Output the value of food
• cout << food << "n";
5. CONT……
• // Output the memory address of food
• cout << &food << "n";
• // Output the memory address of food with the pointer
• cout << ptr << "n";
• return 0;
• }
6. POLYMORPHISM & INHERITANCE
• Polymorphism means "many forms", and it occurs when we have many classes that are related to each
other by inheritance.
• Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from
another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a
single action in different ways.
7. POLYMORPHISM & INHERITANCE
• // Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound n";
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee n";
}
};
8. CONT….
• // Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow n";
}
};
9. POINTER(POINTER TO OBJECT ,THIS POINTER,POINTER
TO DERIVE CLASS)
• A pointer is a variable that stores the memory address of another variable (or object) as its value. A
pointer aims to point to a data type which may be int, character, double, etc.
• Pointers to objects aim to make a pointer that can access the object, not the variables. Pointer to object
in C++ refers to accessing an object.
• There are two approaches by which you can access an object. One is directly and the other is by using a
pointer to an object in C++.
10. // EXAMPLE USING AN OBJECT POINTER.
• #include <iostream>
• using namespace std;
• class My_Class {
• int num;
• public:
• void set_number(int value) {num = value;}
• void show_number();
• };
• void My_Class::show_number()
• {
• cout << num << "n";
• }
11. CONT…..
• int main()
• {
• My_Class object, *p; // an object is declared and a pointer to it
• object.set_number(1); // object is accessed directly
• object.show_number();
12. CONT….
• p = &object; // the address of the object is assigned to p
• p->show_number(); // object is accessed using the pointer
• return 0;
• }
13. THIS POINTER
• In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3
main usage of this keyword in C++.
•
It can be used to pass current object as a parameter to another method.
• It can be used to refer current class instance variable.
• It can be used to declare indexers.
•
15. CONT….
• void display()
• {
• cout<<id<<" "<<name<<" "<<salary<<endl;
• }
• };
• int main(void) {
• Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
• Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee
• e1.display();
• e2.display();
• return 0;
• }
16. POINTER TO DERIVE CLASS
• A pointer is a data type that stores the address of other data types. Pointers can be used for base
objects as well as objects of derived classes. A pointer to the object of the derived class and a pointer to
the object of the base class are type-compatible (may be used in different ways).
17. CONT…..
• A derived class is a class that takes some properties from its base class.
• It is true that a pointer of one class can point to another class, but classes must be a base and derived
class, then it is possible.
• To access the variable of the base class, a base class pointer will be used.
• So, a pointer is a type of base class, and it can access all, public function and variables of the base class
since the pointer is of the base class, this is known as a binding pointer.
• In this pointer base class is owned by the base class but points to the derived class object.
• The same works with derived class pointer, values are changed.
18. INTRODUCTION TO POLYMORPHISM(RUNTIME
POLYMORPHISM, COMPILETIME POLYMORPHISM)
• The word “polymorphism” means having many forms.
• In simple words, we can define polymorphism as the ability of a message to be displayed in more than
one form.
• A real-life example of polymorphism is a person who at the same time can have different
characteristics.
• A man at the same time is a father, a husband, and an employee.
• So the same person exhibits different behavior in different situations. This is called polymorphism.
Polymorphism is considered one of the important features of Object-Oriented Programming.
20. FUNCTION OVERLOADING
• When there are multiple functions with the same name but different parameters, then the functions
are said to be overloaded, hence this is known as Function Overloading.
• Functions can be overloaded by changing the number of arguments or/and changing the type of
arguments.
• In simple terms, it is a feature of object-oriented programming providing many functions that have the
same name but distinct parameters when numerous tasks are listed under one function name.
• There are certain Rules of Function Overloading that should be followed while overloading a function.
21. OPERATOR OVERLOADING
• C++ has the ability to provide the operators with a special meaning for a data type, this ability is known
as operator overloading.
• For example, we can make use of the addition operator (+) for string class to concatenate two strings.
• We know that the task of this operator is to add two operands.
• So a single operator ‘+’, when placed between integer operands, adds them and when placed between
string operands, concatenates them.
22. RUNTIME POLYMORPHISM
• This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism
are other names for runtime polymorphism. The function call is resolved at runtime in runtime
polymorphism. In contrast, with compile time polymorphism, the compiler determines which function
call to bind to the object after deducing it at runtime.
• A. Function Overriding
• Function Overriding occurs when a derived class has a definition for one of the member functions of the
base class. That base function is said to be overridden.
23. VIRTUAL FUNCTION
• A virtual function is a member function that is declared in the base class using the keyword virtual and
is re-defined (Overridden) in the derived class.
• Some Key Points About Virtual Functions:
• Virtual functions are Dynamic in nature.
• They are defined by inserting the keyword “virtual” inside a base class and are always declared with a
base class and overridden in a child class
• A virtual function is called during Runtime
24. VIRTUAL FUNCTION
• A virtual function (also known as virtual methods) is a member function that is declared within a base
class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a
pointer or a reference to the base class, you can call a virtual function for that object and execute the
derived class’s version of the method.
• Virtual functions ensure that the correct function is called for an object, regardless of the type of
reference (or pointer) used for the function call.
• They are mainly used to achieve Runtime polymorphism.
• Functions are declared with a virtual keyword in a base class.
• The resolving of a function call is done at runtime.
25. RULES FOR VIRTUAL FUNCTIONS
• The rules for the virtual functions in C++ are as follows:
• Virtual functions cannot be static.
• A virtual function can be a friend function of another class.
• Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime
polymorphism.
• The prototype of virtual functions should be the same in the base as well as the derived class.
• They are always defined in the base class and overridden in a derived class. It is not mandatory for the
derived class to override (or re-define the virtual function), in that case, the base class version of the
function is used.
• A class may have a virtual destructor but it cannot have a virtual constructor.
26. // C++ PROGRAM TO ILLUSTRATE
// CONCEPT OF VIRTUAL FUNCTIONS
•
• #include <iostream>
• using namespace std;
•
• class base {
• public:
• virtual void print() { cout << "print base classn"; }
•
• void show() { cout << "show base classn"; }
• };
29. LIMITATIONS OF VIRTUAL FUNCTIONS
• Slower: The function call takes slightly longer due to the virtual mechanism and makes it more difficult
for the compiler to optimize because it does not know exactly which function is going to be called at
compile time.
• Difficult to Debug: In a complex system, virtual functions can make it a little more difficult to figure out
where a function is being called from.
30. INHERITANCE(SINGLE INHERITANCE, MULTIPLE
INHERITANCE,MULTILEVEL INHERITANCE,HIERARCHICAL
INHERITANCE,HYBRID INHERITANCE)
• The capability of a class to derive properties and characteristics from another class is called Inheritance.
Inheritance is one of the most important features of Object-Oriented Programming.
• Inheritance is a feature or a process in which, new classes are created from the existing classes. The
new class created is called “derived class” or “child class” and the existing class is known as the “base
class” or “parent class”. The derived class now is said to be inherited from the base class.
• The derived class is the specialized class for the base class.
• Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
• Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
31. VIRTUAL BASE CLASS
• Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a given
class appearing in an inheritance hierarchy when using multiple inheritances.
• Need for Virtual Base Classes: Consider the situation where we have one class A . This class A is
inherited by two other classes B and C. Both these class are inherited into another in a new class D as
shown in figure below.
• As we can see from the figure that data members/function of class A are inherited twice to class D. One
through class B and second through class C. When any data / function member of class A is accessed by
an object of class D, ambiguity arises as to which data/function member would be called? One inherited
through B or the other inherited through C. This confuses compiler and it displays error.
32. VIRTUAL BASE CLASS
• #include <iostream>
• using namespace std;
•
• class A {
• public:
• void show()
• {
• cout << "Hello form A n";
• }
• };
•
33. CONT……
• class B : public A {
• };
•
• class C : public A {
• };
•
• class D : public B, public C {
• };
•
35. HOW TO RESOLVE THIS ISSUE?
• To resolve this ambiguity when class A is inherited in both class B and class C, it is declared as virtual base class
by placing a keyword virtual as :
• Syntax for Virtual Base Classes:
• Syntax 1:
• class B : virtual public A
• {
• };
• Syntax 2:
• class C : public virtual A
• {
• };
36. CONT….
• virtual can be written before or after the public. Now only one copy of data/function member will be
copied to class C and class B and class A becomes the virtual base class.
• Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use
multiple inheritances.
• When a base class is specified as a virtual base, it can act as an indirect base more than once without
duplication of its data members.
• A single copy of its data members is shared by all the base classes that use virtual base.
37. ABSTRACT CLASS
• By definition, a C++ abstract class must include at least one pure virtual function. Alternatively, put a
function without a definition. Because the subclass would otherwise turn into an abstract class in and of
itself, the abstract class's descendants must specify the pure virtual function.
• Broad notions are expressed using abstract classes, which can then be utilized to construct more
specific classes. You cannot make an object of the abstract class type.
• However, pointers and references can be used to abstract class types. When developing an abstract
class, define at least one pure virtual feature. A virtual function is declared using the pure specifier (= 0)
syntax.
38. CONT…..
• C-lass classname //abstract class
• {
• //data members
• public:
• //pure virtual function
• /* Other members */
• };