mardi 22 février 2022

Does this statement allow inheritance to properly work?

I'm doing a project on multilevel inheritance with a deck of playing cards. While working on the project, i received a compilation error in my program that was specifically for the suit classes (diamond.cpp, heart.cpp, etc.) constructor that stated

error: no matching function for call to 'RedCard::RedCard()'

I realized that this was because the RedCard and BlackCard clases did not have a default constructor. I was able to fix this by putting:

Heart::Heart(int v) : RedCard(v)

for the suit class constructor in order to specify the program to call the parametrized constructor rather than the non-existent default constructor. I know that a class does not need to have a default constructor, and the class that a higher up class Card has a default constructor.

My question is: does what I did allow inheritance to work?

The heirarchy of the programs inheritance structure is here:

link: https://imgur.com/a/aQvub9n

Text

One thing I did notice while trying to finish the program, is that in my RedCard and BlackCard classes, there's a Description method we are to use. The switch statement that is in it uses throws an error saying that the "value" variable from the Card Class is inaccessible, so I presume I'm doing something wrong with the RedCard and BlackCard Classes.

This is the Card.cpp file:

#include <iostream>
#include <cstdlib>
#include "card.h"

Card::Card()  // Default Constructor: Initializes value to 0, color to "unknown", and suit to 'U'
{
  int value = 0;
  string color = "unknown";
  char suit = 'U';

} 

Card::Card(int v)  // Parameterized Constructor: Initializes value to v, color to "unknown", and suit to 'U'
{
  value = v; 
  color = "unknown";
  suit = 'U';
}  

int Card::GetValue() const  // Returns variable value
{
  return value;
}

string Card::GetColor() const  // Returns variable color
{
  return color;
}

char Card::GetSuit() const  // Returns variable suit
{
  return suit;
}

void Card::SetValue(int v)  // Sets value to v
{
  value = v;
}

void Card::SetColor(string c)
// Sets color to c
{
  color = c;
}

void Card::SetSuit(char s)
// Sets suit to s
{
  suit = s;
}


string Card::Description() const   // Outputs card characteristics - value as a string
// DO NOT MODIFY THIS METHOD !!!!
{
  string d = "Value = ";    // temporary variable used to accumulate result

  switch (value)            // Append card value to variable's value
  {
    case 2:   d = d + "2";    break;      // Number cards
    case 3:   d = d + "3";    break;
    case 4:   d = d + "4";    break;
    case 5:   d = d + "5";    break;
    case 6:   d = d + "6";    break;
    case 7:   d = d + "7";    break;
    case 8:   d = d + "8";    break;
    case 9:   d = d + "9";    break;
    case 10:  d = d + "10";   break;
    
    case 11:  d = d + "J";    break;      // Face cards
    case 12:  d = d + "Q";    break;
    case 13:  d = d + "K";    break;
    case 14:  d = d + "A";    break;

    default:  d = d + "?";    break;      // Unknown card
  }

  return d;                 // Return string describing card value
}

This is the Card.h file

#include <string>
using namespace std;

#ifndef CARD_H
#define CARD_H

class Card                      // Class modeling Card ADT
{
 private:
  int value;                    // Card value: 2-10 for number cards, 11-14 for JQKA; 0 for unknown
  string color;                 // Card color: "red", "black", or "unknown"
  char suit;                    // Card suit: 'H' for hearts, 'D' for diamonds, 'C' for clubs, 'S' for spades or 'U' for unknown

 public:
  //******** Add Constructor Prototypes Below  *********//
  Card();      // Default constructor prototype: creates card with value 0, color unknown, and suit U
    
  Card(int v);      // Parameterized constructor prototype: creates card with value v, color unknown, and suit U
    
  //******** Add Transformer Prototypes Below *********//
  void SetValue(int v);      // SetValue prototype: Sets card value equal to v
  void SetColor(string c);      // SetColor prototype: Sets color value equal to c
  void SetSuit(char s);      // SetSuit prototype:  Sets suit value equal to s

  //******** Add Observer Prototypes Below *********//
  int GetValue() const;         // GetValue prototype: Returns current value of value
  string GetColor() const;      // GetColor prototype: Returns current value of color
  char GetSuit() const;     // GetSuit prototype:  Returns current value of suit
  string Description() const;      // Description prototype: Polymorphic Function!!!
                                // Outputs card characteristics - value as a string (see sample output from p01input1.txt)
};
#endif

***The blackcard.cpp file is here and is the same for redcard.cpp: ***

#include <iostream>
#include <cstdlib>

#include "blackcard.h"

BlackCard::BlackCard(int v)
{

}
string BlackCard::Description() const
{
    string d = "Value = ";    // temporary variable used to accumulate result

    switch (value)            // Append card value to variable's value
    {

    case 2:   d = d + "2";    break;      // Number cards
    case 3:   d = d + "3";    break;
    case 4:   d = d + "4";    break;
    case 5:   d = d + "5";    break;
    case 6:   d = d + "6";    break;
    case 7:   d = d + "7";    break;
    case 8:   d = d + "8";    break;
    case 9:   d = d + "9";    break;
    case 10:  d = d + "10";   break;
    
    case 11:  d = d + "J";    break;      // Face cards
    case 12:  d = d + "Q";    break;
    case 13:  d = d + "K";    break;
    case 14:  d = d + "A";    break;

    default:  d = d + "?";    break;      // Unknown card  
    }
  return d;                 // Return string describing card value
}

An example file for what my original issue above is heart.cpp:

#include <iostream>
#include <cstdlib>

#include "heart.h"

Heart::Heart(int v) : RedCard(v)
{

}

string Heart::Description() const
{
    string d = "Value = ";    // temporary variable used to accumulate result
    
    switch (value)            // Append card value to variable's value
    {
        
    case 2:   d = d + "2";    break;      // Number cards
    case 3:   d = d + "3";    break;
    case 4:   d = d + "4";    break;
    case 5:   d = d + "5";    break;
    case 6:   d = d + "6";    break;
    case 7:   d = d + "7";    break;
    case 8:   d = d + "8";    break;
    case 9:   d = d + "9";    break;
    case 10:  d = d + "10";   break;
    
    case 11:  d = d + "J";    break;      // Face cards
    case 12:  d = d + "Q";    break;
    case 13:  d = d + "K";    break;
    case 14:  d = d + "A";    break;

    default:  d = d + "?";    break;      // Unknown card
    
    }

  return d;                 // Return string describing card value
}

I aplogize in advance for the length of the question and if anything is missing. It's a rather large project and the issue is hard to describe with how manny files are involved.

Please let me know if i'm needing to clarify anything. I appreciate any advice and your time.

Aucun commentaire:

Enregistrer un commentaire