0% found this document useful (0 votes)
4 views

Assignment 4 - Mayuri

The document contains a C++ assignment that includes a program for converting basic types to class types, an explanation of virtual functions and their rules, and a discussion on ambiguity resolution in inheritance. It also provides short notes on abstract classes, containership, and types of inheritance such as multilevel and hierarchical inheritance. The assignment is submitted by Mayuri Agrawal to Professor Sanjeev Sharma.

Uploaded by

Ashutosh Kewat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 4 - Mayuri

The document contains a C++ assignment that includes a program for converting basic types to class types, an explanation of virtual functions and their rules, and a discussion on ambiguity resolution in inheritance. It also provides short notes on abstract classes, containership, and types of inheritance such as multilevel and hierarchical inheritance. The assignment is submitted by Mayuri Agrawal to Professor Sanjeev Sharma.

Uploaded by

Ashutosh Kewat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[Department of Information Technology]

(230202)
[Assignment 4]
Submitted to: - Proff. Sanjeev Sharma sir

Submitted by: - Mayuri Agrawal


(0901AI201033)
Q.1 Write a C++ Program to convert basic type to class
type
#include <iostream>
using namespace std;
class pet {
string PetName;

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;

cout << "Enter Name of Pet : ";


cin >> name;

dog = name;
dog.display();

name = "Bill";

dog.operator=(name);
dog.display();
}
Output Screen

Q.2 Describe need of virtual function using suitable


example? What are the rules for virtual function?
Needs of Virtual Function:
• Virtual Function ensure that the correct function is called for an object,
regardless of the type of reference (or pointer) used for function call.
• They are mainly used to achieve Runtime Polymorphism.
• Functions are declared with the virtual keyboard in base class.
• The resolving of function call is done at Run-time

#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.

Q.3 Explain Ambiguity Resolution in Inheritance


using suitable example

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();
}

Q.4 write short notes on following:


1. Abstract Class:
It is a kind of class that has at least one pure virtual function is known as
abstract class. In other words, if we only declare the function in the base
class and doesn’t write its implementation in the base class and we use
derived classes for writing an implementation of the declared function,
then that class is known as abstract class.
2. Container Ship:
We can create an object of one class into another and that object will be a
member of the class. This type of relationship between classes is known
as containership or has a relationship as one class contain the object of
another class. And the class which contains the object and members of
another class in this kind of relationship is called a container class.
3. Type of Inheritance:
Multilevel Inheritance:
In C++ programming, not only you can derive a class from the base class
but you can also derive a class from the derived class. This form of
inheritance is known as Multilevel inheritance.
Hierarchical Inheritance:
If more than one class is inherited from the base class, it’s known as
Hierarchical inheritance. In hierarchical inheritance, all features that are
common in child classes are included in the base class.

You might also like