Programming Fundamentals
Assignment no 4
Q1. Examine the “card game example” from your book chapter 4, compile it using visual studio and
attach screenshots of code with output with this assignment. Write down your comments about this
program at the end?
Solution:
// cardobj.cpp
// cards as objects
#include <iostream>
using namespace std;
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11; //from 2 to 10 are
const int queen = 12; //integers without names
const int king = 13;
const int ace = 14;
////////////////////////////////////////////////////////////////
class card
private:
int number; //2 to 10, jack, queen, king, ace
Suit suit; //clubs, diamonds, hearts, spades
public:
card() //constructor (no args)
{}
//constructor (two args)
card(int n, Suit s) : number(n), suit(s)
{}
void display(); //display card
bool isEqual(card); //same as another card?
};
//--------------------------------------------------------------
void card::display() //display the card
if (number >= 2 && number <= 10)
cout << number << " of";
else
switch (number)
case jack: cout << "jack of "; break;
case queen: cout << "queen of "; break;
case king: cout << "king of "; break;
case ace: cout << "ace of "; break;
switch (suit)
case clubs: cout << " clubs"; break;
case diamonds: cout << " diamonds"; break;
case hearts: cout << " hearts"; break;
case spades: cout << " spades"; break;
//--------------------------------------------------------------
bool card::isEqual(card c2) //return true if cards equal
return (number == c2.number && suit == c2.suit) ? true : false;
}
////////////////////////////////////////////////////////////////
int main()
card temp, chosen, prize; //define various cards
int position;
card card1( 7 , clubs ); //define & initialize card1
cout << "\nCard 1 is the ";
card1.display(); //display card1
card card2(jack, hearts); //define & initialize card2
cout << "\nCard 2 is the ";
card2.display(); //display card2
card card3(ace, spades); //define & initialize card3
cout << "\nCard 3 is the ";
card3.display(); //display card3
prize = card3; //prize is the card to guess
cout << "\nI'm swapping card 1 and card 3";
temp = card3; card3 = card1; card1 = temp;
cout << "\nI'm swapping card 2 and card 3";
temp = card3; card3 = card2; card2 = temp;
cout << "\nI'm swapping card 1 and card 2";
temp = card2; card2 = card1; card1 = temp;
cout << "\nNow where(1, 2, or 3) is the ";
prize.display(); //display prize card
cout << " ? ";
cin >> position; //get user’s guess of position
switch (position)
{ //set chosen to user’s choice
case 1: chosen = card1; break;
case 2: chosen = card2; break;
case 3: chosen = card3; break;
if (chosen.isEqual(prize)) //is chosen card the prize?
cout << "That's right! You win!";
else
cout << "Sorry! You lose.";
cout << " You choose the ";
chosen.display(); //display chosen card
cout << endl;
return 0;
Output and Explanation:
There are two constructors in class card. The first, which takes no arguments, is used in main() to create
the cards temp, chosen, and prize, which are not initialized. The second constructor, which takes two
arguments, is used to create card1, card2, and card3 and to initialize them to specific values. Besides the
constructors, card has two other member functions, both defined outside the class. The display()
function takes no arguments; it simply displays the card object of which it is a member, using the
number and suit data items in the card. The statement in main() chosen.display(); displays the card
chosen by the user. The isEqual() function checks whether the card is equal to a card supplied as an
argument. It uses the conditional operator to compare the card of which it is a member with a card
supplied as an argument. This function could also have been written with an if...else statement
if( number==c2.number && suit==c2.suit )
return true;
else
return false;
but the conditional operator is more compact.
In isEqual() the argument is called c2 as a reminder that there are two cards in the comparison:
The first card is the object of which isEqual() is a member. The expression if( chosen.isEqual(prize) )
in main() compares the card chosen with the card prize.
Here’s the output when the user guesses an incorrect card: Card 1 is the 7 of clubs
Card 2 is the jack of hearts
Card 3 is the ace of spades
I’m swapping card 1 and card 3
I’m swapping card 2 and card 3
I’m swapping card 1 and card 2
Now, where (1, 2, or 3) is the ace of spades? 1
Sorry, you lose. You chose the 7 of clubs
(b).
Examine “Enumerations” topic from book chapter 4 and explain it in your own words?
Answer:
An enumeration is a user-defined data type that consists of integral constants. To define an
enumeration, keyword enum is used. An enumeration is a user-defined data type that consists of
integral constants. To define an enumeration, keyword enum is used.
enum season { spring, summer, autumn, winter };
Here, the name of the enumeration is season.
And, spring, summer and winter are values of type season.
By default, spring is 0, summer is 1 and so on. You can change the default value of an enum element
during declaration (if necessary).
enum season
{ spring = 0,
summer = 4,
autumn = 8,
winter = 12
};