Assignment 4 - Mayuri
Assignment 4 - Mayuri
(230202)
[Assignment 4]
Submitted to: - Proff. Sanjeev Sharma sir
public:
void display() { cout << "Pet Name : " << PetName << endl; }
// overloading function
void operator=(string name) {
cout << "\nBasic Type to Class Type Conversion\n";
PetName = name;
}
};
int main() {
pet dog;
string name;
dog = name;
dog.display();
name = "Bill";
dog.operator=(name);
dog.display();
}
Output Screen
#include <iostream>
class Base {
public:
virtual void display() = 0;
};
class Derived : public Base {
public:
void display() { std::cout << "Derived" << std::endl; }
};
int main() {
class Derived a;
a.display();
}
Rules for Virtual Function:
• It cannot be static.
• It should be accessed using pointer or reference of base class type to achieve
run time polymorphism.
• The prototype of virtual function should be the same in the base as well as
derived class.
• A class may have virtual destructor but it cannot have a virtual constructor.
• 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.
When a derived class has two base class, base classes have function with the
same name. If we want to call same name function of base class by child class
then compiler get confused which function it should call. It is called ambiguity
resolution in inheritance. This problem is resolved using scope resolution operator
to specify the class in which function lies
#include <iostream>
class One {
public:
static void Display() { std::cout << "Class One" << std::endl
; }
};
class Two {
public:
static void Display() { std::cout << "Class Two" << std::endl
; }
};
int main() {
One::Display();
Two::Display();
}