How to Create a Deck of Cards in OOP Without Dynamic Memory Allocation?

  • Thread starter Saladsamurai
  • Start date
  • Tags
    Oop
In summary: My suggestion isn't based on a rational vision of what the requirements for the class Deck are. It's just based on the...In summary, Stephen is trying to create a class to represent a deck of cards, but is struggling to understand how to do it in a OOP way. He is considering two possible solutions, either having a member function to initialise the deck, or initialising the deck in the constructor of the class. He is unsure of which is better.
  • #1
Saladsamurai
3,020
7
I decided I would try to create a relatively simple class in order to gain some more experience with thinking in an OOP mindset. But some things are hanging me up. I want to create a class of objects that represent a deck of cards. The number of cards should be able to change depending on what the user wants to do. A simple function that I will implement will be used to shuffle the deck. I figured to do this, I will use a vector object as the deck and then add some additional functionality to it. I am a little confused as to what the best way to do this is. I always hear that dynamic memory allocation is not good (when it can be avoided). This is presenting me with difficulty since I want to be able to initialize the Deck size.

How would you do this? Would you have a member function that takes in the number of cards as a parameter and then initializes the deck with it? Or is there a better way?
 
Technology news on Phys.org
  • #3
Saladsamurai said:
How would you do this? Would you have a member function that takes in the number of cards as a parameter and then initializes the deck with it? Or is there a better way?

If you are doing this as an exercise in Object Oriented Programming, I suggest you put the initialization process in the constructor of the class rather than equipping the class with a function that must be called to initialize the deck. Generally speaking, an object should be ready to use after it is instantiated.
 
  • #4
Stephen Tashi said:
If you are doing this as an exercise in Object Oriented Programming, I suggest you put the initialization process in the constructor of the class rather than equipping the class with a function that must be called to initialize the deck. Generally speaking, an object should be ready to use after it is instantiated.

Hi Stephen :smile: Yes, this is what was bothering me ... the whole ready for use thing. The problem I am facing though is that the deck size is up to the user. So either way, I need to pass a value to the object in order to set its size. I am a little new to the lingo as well. When you say:

<snip> I suggest you put the initialization process in the constructor of the class rather than equipping the class with a function that must be called to initialize the deck

Do you mean that I should create an object class such that when I go to my main() fuction (c++) I can pass it an initialization parameter like this?

Code:
#include<Deck.h>

int main()

    int n(500);
    Deck myDeck(n);
    // code ...

return 0;
 
  • #5
I suspect you are trying to figure out how to implement something (your "deck of cards"), before you have really got your head around what the "something" is. (Don't worry about it - that's a very common beginner's OOP problem!).

Also you can't really design one class in isolation from everything else. I would start by designing a "card" class that represents one card. It probably doesn't need to do much, for example
- know which unique card it is
- be able to print itself
- if you make a graphical interface, be able to draw itself.
- know how to compare the "value" of two cards, following the rules of your game.

Then, your "deck" will be some sort of collection of "cards". Whether the best sort of collection is a vector, or a list, or something else, will depend what you want to do with the "deck".

In a typical card game, there will probably be "players" with "hards" of cards, and possibly other "interesting" sets of cards as well. It might turn out that a "deck" is actually a special case of something else...
 
  • #6
Saladsamurai said:
Do you mean that I should create an object class such that when I go to my main() fuction (c++) I can pass it an initialization parameter like this?

Code:
#include<Deck.h>

int main()

    int n(500);
    Deck myDeck(n);
    // code ...

return 0;

Yes, that's what I mean. You can also study how to prevent someone from using the default constructor (which has no argument n) for Deck. I haven't written a C++ program in several years and I've forgotten the good way to do that, but it's in many books.

I agree with AlephZero that you can't make realistic design decisons about a class unless you understand how the class will be used. My suggestion isn't based on a rational vision of what the requirements for the class Deck are. It's just based on the statistically most common way to write classes, which is probably the first thing you should practice.

The architect Christopher Alexander is credited with inspiring many ideas about computer programming. I'm unsure whether ever wrote anything about computer programming per se. His Phd. Thesis is interesting. It's probably available on the web. The main point I took away from reading it is that it is important to identify goals in the design process that tend to be contradictory. For example, designing a simple object like a tea kettle might have the goals of "Be sturdy" and "Be lightweight". Given the nature of most materials these are somewhat contradictory aims. Alexander says that outstanding designs are often perfect compromises between somewhat contradictory goals or surprising configurations where two contradictory aspects are accomplished in some harmonious way. Hence, it is important to identify the goals of a design that tend to be in harmony or conflict. With designing classes, you've already encountered the conflict between efficiency and convenience.

If you want to study design (of computer software or anything else), you'll have to do it in a realistic situation where there are definite goals - I don't mean a laundry list of specifications, I mean general goals that you have freedom to meet in various ways.

If you want to study the syntax of programming and the nuts and bolts of how classes work, don't fret too much about design problems. Just start coding. For example, you could write a class Deck and then study how to extend classes by inheritance.

An important practical skill in all the OOP is how to cut-and-paste effectively. There is so much boilerplate-like stuff to write!
 
  • #7
There's nothing wrong with a method that sets the deck size.
Just try not to do dynamic allocation yourself, but try to let the library do the work for you.
So use for instance:

Code:
class Deck
{
public:
   void setDeckSize(int n) { m_cards.resize(n); }
private:
   std::vector<Card> m_cards;
};
 

Related to How to Create a Deck of Cards in OOP Without Dynamic Memory Allocation?

1. What is OOP and why is it important?

OOP stands for Object-Oriented Programming and it is a programming paradigm that focuses on creating objects that have properties and behaviors. It is important because it allows for more efficient and organized code, as well as easier maintenance and scalability.

2. What are the main principles of OOP?

The main principles of OOP are encapsulation, inheritance, and polymorphism. Encapsulation refers to the bundling of data and behaviors within an object. Inheritance allows for the creation of new classes based on existing classes, inheriting their properties and behaviors. Polymorphism allows for the use of the same method on different types of objects.

3. How is OOP different from other programming paradigms?

OOP differs from other programming paradigms such as procedural programming in that it focuses on creating objects and their interactions, rather than just a series of steps to complete a task. It also allows for code reuse and easier maintenance, compared to procedural programming which can often lead to repetitive code.

4. What are some common challenges when learning OOP?

Some common challenges when learning OOP include understanding the different concepts and terminology, as well as grasping the concept of objects and their interactions. It can also be challenging to transition from other programming paradigms, such as procedural programming, to OOP.

5. How can I improve my understanding of OOP?

To improve your understanding of OOP, it is important to practice writing code and creating objects. You can also read documentation and tutorials, as well as study and analyze code written by experienced programmers. Additionally, taking courses or attending workshops can also help improve your understanding of OOP.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
4
Views
431
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
10
Views
6K
Replies
7
Views
7K
  • Programming and Computer Science
Replies
4
Views
3K
  • Sci-Fi Writing and World Building
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
305
  • Computing and Technology
Replies
3
Views
1K
  • Programming and Computer Science
Replies
21
Views
3K
Back
Top