0

I wrote a project and cannot solve these 2 errors

Error (active) E0286 base class "CDevice" is not unambiguous
Error C2594 initialization: ambiguous "CSmartphone *" to "CDevice *" conversions The task of the code Design and implement a hierarchy of classes that describe the subject area according to the embodiment, which is implemented by class 1. Class 1, in turn, is is formed by multiple inheritance of classes 2 and 3, each of which in turn in turn inherits class 4.

Smartphone-Subject area CSmartphone-Class 1 CComputer-Class 2 CPhone-Class 3 CDevice-Class 4 Additional requirements:

  1. The base class contains at least one virtual method, one non-virtual method and one dynamically created property.
  2. Provide mechanisms for the correct operation of constructors and destructors.
  3. Overload the assignment operator in order to ensure its correct operation.
  4. Each of the classes must contain at least one property and 4 methods.
  5. Write a main() function where you create an object of class 1 and demonstrate the difference between static and dynamic polymorphism.
// File 1: CDevice.h
#pragma once

class CDevice {
public:
    CDevice();
    virtual ~CDevice();

    virtual void turnOn() = 0;
    virtual void turnOff() = 0;
    virtual void charge() = 0;

    int getBatteryLevel() const;

protected:
    int m_batteryLevel;
};

// File 2: CComputer.h
#pragma once


#include "CDevice.h"

class CComputer : public CDevice {
public:
    CComputer();
    ~CComputer();

    void turnOn();
    void turnOff();
    void charge();

    void openApp(const char* appName);
    void closeApp(const char* appName);
    void installApp(const char* appName);
    void uninstallApp(const char* appName);

private:
    }; bool m_isAppOpen;
};


// File 3: CPhone.h
#pragma once


#include "CDevice.h"

class CPhone : public CDevice {
public:
    CPhone();
    ~CPhone();

    void turnOn();
    void turnOff();
    void charge();

    void makeCall(const char* phoneNumber);
    void receiveCall(const char* phoneNumber);
    void sendSMS(const char* phoneNumber, const char* message);
    void receiveSMS(const char* phoneNumber, const char* message);

private:
    }; bool m_isCallActive;
};

// File 4: CSmartphone.h
#ifndef CSMARTPHONE_H
#define CSMARTPHONE_H

#include "CComputer.h"
#include "CPhone.h"

class CSmartphone : public CComputer, public CPhone {
public:
    CSmartphone();
    ~CSmartphone();

    void turnOn();
    void turnOff();
    void charge();

    void useSmartAssistant();
    void takePhoto();
    void playMusic();
    void browseInternet();

private:
    bool m_isSmartAssistantActive;
};

#endif

// File 5: CDevice.cpp
#include "CDevice.h"

CDevice::CDevice() : m_batteryLevel(100) {}

CDevice::~CDevice() {}

int CDevice::getBatteryLevel() const {
    return m_batteryLevel;
}

// File 6: CComputer.cpp
#include "CComputer.h"

CComputer::CComputer() : m_isAppOpen(false) {}

CComputer::~CComputer() {}

void CComputer::turnOn() {
    // Switching on the computer
}

void CComputer::turnOff() {
    // Switching off the computer
}

void CComputer::charge() {
    // Charging the battery
}

void CComputer::openApp(const char* appName) {
    // Opening the application
    m_isAppOpen = true;
}

void CComputer::closeApp(const char* appName) {
    // Closing the application
    m_isAppOpen = false;
}

void CComputer::installApp(const char* appName) {
    // Installing the application
}

void CComputer::uninstallApp(const char* appName) {
    // Uninstalling the application
}

// File 7: CPhone.cpp
#include "CPhone.h"

CPhone::CPhone() : m_isCallActive(false) {}

CPhone::~CPhone() {}

void CPhone::turnOn() {
m_batteryLevel = 100;
// additional code to switch on the phone
}

void CPhone::turnOff() {
m_isCallActive = false;
// additional code for switching off the phone
}

void CPhone::charge() {
m_batteryLevel = 100;
// additional code for charging the phone battery
}

void CPhone::makeCall(const char* phoneNumber) {
// code for making a call
m_isCallActive = true;
}

void CPhone::receiveCall(const char* phoneNumber) {
// code for receiving a call
m_isCallActive = true;
}

void CPhone::sendSMS(const char* phoneNumber, const char* message) {
// code for sending an SMS message
}

void CPhone::receiveSMS(const char* phoneNumber, const char* message) {
// code for receiving an SMS message
}
// File 8: CSmartphone.cpp
#include "CSmartphone.h"

CSmartphone::CSmartphone() : CComputer(), CPhone(), m_isSmartAssistantActive(false) {}

CSmartphone::~CSmartphone() {}

void CSmartphone::turnOn() {
CComputer::turnOn();
CPhone::turnOn();
}

void CSmartphone::turnOff() {
CComputer::turnOff();
CPhone::turnOff();
}

void CSmartphone::charge() {
CPhone::charge();
}

void CSmartphone::useSmartAssistant() {
m_isSmartAssistantActive = true;
// Implementation of the smart assistant functionality
}

void CSmartphone::takePhoto() {
// Implementation of the camera functionality
}

void CSmartphone::playMusic() {
// Implementation of the music functionality
}

void CSmartphone::browseInternet() {
// Implementation of the browser functionality
}

// File 9: main.cpp
#include "CSmartphone.h"
#include <iostream>

int main()
{
    // Creating an object of class CSmartphone
    CSmartphone myPhone;

    // Demonstration of static polymorphism
    std::cout << "Current battery level: " << myPhone.getBatteryLevel() << std::endl;
    myPhone.turnOn();
    myPhone.useSmartAssistant();
    myPhone.makeCall("123-456-7890");

    // Demonstration of dynamic polymorphism
    CDevice* myDevice = &myPhone;
    myDevice->charge();
    myDevice->turnOff();

    return 0;
}

I tried specifying which ancestor class the called method or variable belongs to, but it didn't work

Heise Fun
  • 11
  • 1
  • I recommend reading [mre] (MRE) and patterning the code example after what you learn from the reading. But the real reason you want to read the link is it describes a very effective divide and conquer-based debugging technique. It's rare that you can finish making one without discovering and fixing the bug yourself. Currently the volume of code provided suggests that the debugging effort performed to date was misguided as there are no signs of attempting to isolate the problem. – user4581301 Apr 26 '23 at 21:28
  • 1
    Side note: there are a few bit of code looking like `}; bool m_isAppOpen;` that are most likely transcription errors that only exist in the question. I recommend fixing these up as well because they will distract readers from the problem you are truly facing – user4581301 Apr 26 '23 at 21:32
  • 1
    Is this an assignment where you are supposed to lean about dealing with diamonds and `virtual` classes? – user4581301 Apr 26 '23 at 21:34
  • 1
    This does look like a place where you want a virtual base class. Related: [https://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class](https://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class) – drescherjm Apr 26 '23 at 21:35

0 Answers0