I'm working on a multilevel inheritance project involving a deck of cards. RedCard and BlackCard have to inherit from Card. Heart and diamond from redcard. Spade and Club from blackcard. Right now im hung up on the blackcard and redcard, with my current code in the blackcard.cpp I can get the value(0 for unknown, 2-10 for numbered cards, 11-14 for face/aces) to output however I cannot get the color(red or black) to display after the value.
blackcard.cpp
#include "blackcard.h"
// parameterized constructor to create a black card of value v
BlackCard::BlackCard(int v):Card(v) // calls card class constructor
{
SetColor("black"); // sets the color of the card to "black"
}
string BlackCard::Description() const
{
// Card::Description() returns the description from base class and appends the color information to it
return Card::Description() + ", Color =" + Card::GetColor();
}
// end of blackcard.cpp
I will also attach the card.h file and card.cpp file
card.cpp
//
// card.cpp -- CPE 212-01, Fall 2010 -- Project02 -- Classes
//
// Add the missing statements to complete each method below
// and SUBMIT this file for grading !!!
//
#include <iostream>
#include <cstdlib>
#include "card.h"
Card::Card()
// Default Constructor: Initializes value to 0, color to "unknown", and suit to 'U'
{
value = 0;
color = "unknown";
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
}
card.h
//
// card.h -- CPE 212-01, Fall 2010 -- Project02 -- Classes
//
// Add the missing statements to complete the class declaration below
// and SUBMIT this file for grading !!!
//
#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 v, 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
I'm not really sure what to try
Aucun commentaire:
Enregistrer un commentaire