Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a functional a model and then write a C++ program as explained below. Yo

ID: 3813192 • Letter: D

Question

Develop a functional a model and then write a C++ program as explained below. You will be using classes, objects strings, appropriate data members and member functions, constructors, destructor, etc., inheritance and abstract classes to handle the tasks outlined below The class Car will be declared with at least one virtual function or one pure virtual function and will contain the following data members (in the private access region) Manufacturer Car Manufacturer State State where the car was purchased You will determine the type of data to be used for each data member The class MyCar is derived from the class Car and adds the following data members in the private access region. Model Car Model Car Price Price You will determine the type of data to be used for each data member Appropriate member functions, constructors and destructors for both classes are provided in the classes. After the declaration of the both classes, declare an array of two MyCar objects of the class called mc[0] and mc[1] Both objects will be initialized using the default constructors defined by you. You will prompt the user to provide the necessary data for the first object and the second object. A member function displayO will display the information about each car in the format shown below. You will need to define and implement all of the needed accessor and mutator member functions. A user-defined function named displayMyCars0 will retrieve the information about each car and displays them in the format shown below This is a second display of the information). At the beginning of each display, display a label indicting the output is displayed via the member function displayO and the output is displayed via the user-defined function displayMyCars0

Explanation / Answer

Pure Virtual Functions and Abstract Classes in C++
Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw(). Similarly an Animal class doesn’t have implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.
// An abstract class
class Test
{   
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;

/* Other members */
};

A complete example:
A pure virtual function is implemented by classes which are derived from a Abstract class. Following is a simple example to demonstrate the same.
#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class ingerits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};

int main(void)
{
Derived d;
d.fun();
return 0;
}

Output:
fun() called


Some Interesting Facts:

1) A class is abstract if it has at least one pure virtual function.
In the following example, Test is an abstract class because it has a pure virtual function show().
// pure virtual functions make a class abstract

#include<iostream>
using namespace std;
class Test
{
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};

int main(void)
{
Test t;
return 0;
}

Output:
Compiler Error: cannot declare variable 't' to be of abstract
type 'Test' because the following virtual functions are pure
within 'Test': note:    virtual void Test::show()