Abstract Class Question (no code)

1.) In my Abstract base class I have an array called seats[25].

2.) I have two types of seats, the floor and the balcony derived classes.

3.)If I create an object of the balcony it will have access to that array. I understand this part.

4.) However,if I create a second or third .. or twenty-five object are they working from the exact same array, or is each object working from its own array of 25 elements?

[My Thoughts ] ::
I want only one array that all of them are working off of. I might have to create the array in my main and assign it a seatType. That way, each object is a single seat - not where each object is a new array.

I hope I explained this question well enough.

Thank You Kindly,
Last edited on
I want only one array that all of them are working off of.
What you want is a static member variable. See this for how to do that:

I'm not sure what the base class is. For this discussion, I'll assume it's "floor". So that means you have
class floor {};
class balcony : public floor {};

In order to have a single array that contains both types, you will probably have to use pointers. You will want something like floor* seats[25];.

Better would be using smart pointers (maybe std::unique_ptr<floor>) or possibly even a std::reference_wrapper<floor>, depending on how the objects are created and used.

If you are just following the assignment you were given, great--ignore the rest of this comment.

But in real life, balcony may not really be a different class from floor. If it is simply the cost of the seat is different, then it makes more sense to simply have a "cost" data member that is set to the appropriate value for each seat in the theater. You would probably only want a new class if there were new data which is only associated with balcony seats and has no meaning for floor seats.
there is a disconnect with these tools and the teaching of them, and that is the root cause of most of the confusion when trying to learn the ideas.
The presenter is tasked with giving a small, easy to follow example to the student, but these small and easy to understand examples almost never make sense to use these big tools. There is absolutely no need for an abstract base class here. That makes the rest of it difficult to follow because its counterintuitive from the beginning to end.

so, floor and balcony both have their own copy of seats unless you use static. But if you use static, theater_1 and theater_2 also share the same seats.
how complex is your task, is that a problem?
This is a Theater (small I guess) that has 25 seats, 13 on the floor and 12 in the balcony.

This is the reason I don't want a separate copy of the array for every object - rather each object should be from the same array. The array is representing the actual physical seats.

I thought if I made it protected in the base Abstract class "Theater" the two derived classes, 'Floor' and 'Balcony' would have access to it.

Then I began to question if each object would have its own array - The thing I don't want.

I thank everybody for their replies to this .... I think using pointers seems to be the only way, and I am going to test out some of the other advice here. If all else fails I will rely on a static array
This seems like a classic XY problem. You have described your solution, but not the actual problem. As a consequence the solution might be overthought. The real solution could be really simple, doug4 answer looks promising to me.

Perhaps you could have a std::vector<Seat> . The Seat class could have Zone and Price members. Simple - no class hierarchy at all, only one container.

What do you actually have to do? What is the expected input / output?

If you have the assignment question, then post it here.
To Do : write a program that focuses on Ticket sales only for a Theater of Two Types of Seats ( Balcony and floor). The balcony cost $5.00 more than floor seating. The program must use Abstract Class.

----------------------------------- Final Design Solution -------------

1.) I am making a Base Seat class (Abstract) that will define one single seat.
( Note : I am using sales() as my pure virtual function in class Seat)

2.) From the class Seat there are two derived classes - Balcony and Floor classes.

3.) I am creating a class called Theater that represents a single Theater.

4. The Theater class will have an array of objects of type Balcony and another one of type Floor.

5.) In my main, I will have an object of the Theater class- one single theater with both arrays.

Thanks again 'Everyone' I really do appreciate all of your replies.
I thought if I made it protected in the base Abstract class "Theater" the two derived classes, 'Floor' and 'Balcony' would have access to it.

No, no, no, no, no.

Inheritance represents an "is-a" relationship. So, class Floor : public Theater {}; would mean that Floor "is a" theater. I believe you are trying to say that Theater contains 25 seats, so Theater "has" Floor seats, and Theater "has" Balcony seats. The "has-a" relationship is better expressed as composition--Theater contains seats of different types.

Since you need an abstract base class for Floor and Balcony seats, why not just create a class call AbstractSeat? There is production code all around the world with names like that. The name makes it quite clear that it is an abstract class representing different types of seats.

Edit: I just read your latest post. I only skimmed it before. I now see that Balcony and Floor are derived from Seat, not Theater as implied by your earlier post. My comments no longer apply, but I thought I'd leave them for others to read.

If you still have problems, now may be a good time for you to post what you have so we can give more specific help. It's hard to comment on "well, this is the base class and these are derived and I want to have 1 array of all of these in this class."
Last edited on
Topic archived. No new replies allowed.