Book Data Structures and Other Objects using C 4th edition D

Book: Data Structures and Other Objects using C++ 4th edition
Doing problem number 4
Needs also have a separate header file (.h) and implementation file
Must have getters and setters
Suit should be: string
And rank should be: int
Specify, design, and implement a class for a card in a deck of playing cards. The object should contain methods for setting and retrieving the suit and rank of a card.

Solution

Below is the class declaration (.h) and implementation(.cpp) You just need to write the client (main) function to use this class. Hope this helps!

//header file with class definitions- card.h

#ifndef CARD_H
#define   CARD_H

#include<string>
using namespace std;

class card{
private:
   int rank;
   string suit;
public:
   int setRank(int); //returns a 0 if rank could not be set
   int setSuit(string);//returns a 0 if suit could not be set
   int getRank();
   string getSuit();
};

#endif   /* CARD_H */

_______________________________________________________________________________________________

//implementation file- card.cpp
//this class implementation assumes a deck of 52 cards: 4 suits, each having 13 cards
#include<iostream>
#include<string>
#include<locale> //for using the tolower() function, one of its arguments is a locale object
#include\"card.h\" //the custom header file shown above

using namespace std;

int card::setRank(int r){
   if(r<1||r>13){ //if rank is outside the range of Ace,2,3..upto Queen and King
       cout<<\"Invalid Rank\ \";
       return 0;
   }
   else{
       rank=r;
       return 1;
   }
}

int card::setSuit(string str){
  
   locale loc;
  
   /*convert str to lower case for accurate comparison in the next step so that this function will work
   regardless of the case of the input string, e.g- \"Hearts\", \"Hearts\" or \"hearts\"*/
   for(int i=0;i<str.length();i++)
       str[i]=tolower(str[i],loc); //tolower() function converts any string in a string object one character at a time
  
   //check if the string entered is a valid suit
   if(str!=\"hearts\"&&str!=\"diamonds\"&&str!=\"clubs\"&&str!=\"spades\"){
       cout<<\"Invalid Suit\";
       return 0;
   }
   else{
       suit=str;
       return 1;
   }
}

Book: Data Structures and Other Objects using C++ 4th edition Doing problem number 4 Needs also have a separate header file (.h) and implementation file Must ha
Book: Data Structures and Other Objects using C++ 4th edition Doing problem number 4 Needs also have a separate header file (.h) and implementation file Must ha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site