Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7512571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:54:13+00:00 2026-05-29T23:54:13+00:00

Lets say i have a television class and BigTelevision class class television { protected:

  • 0

Lets say i have a television class and BigTelevision class

class television
{
  protected:
       int a; 
  ...
}
class BigTelevision:public television
{
   private:
   int b;
...
}

I want to contain a collection of a mixed of television and BigTelevision, what options do i have.
I know one way is to use array but the problem with this is that if i declare an array of television type to store them the additional attributes(eg int b) of BigTelevision would be lost.

How can i work around this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:54:16+00:00Added an answer on May 29, 2026 at 11:54 pm

    You have to store base class pointers or base class smart pointers ore use a pointer collection like boost:ptr_vector.

    std::vector<television*> tv;
    tv.push_back(new television);
    tv.push_back(new BigTelevision);
    // don't forget to delete 
    
    
    // better:
    std::vector<std::unique_ptr<television>> tv;
    tv.push_back(std::unique_ptr<television>(new television));
    tv.push_back(std::unique_ptr<television>(new BigTelevision));
    

    You can now use different object through a common interface (Polymorphism).

    class television
    {
     public:
        // The interface for all television objects.
        // Each television can calculate its price.
        virtual int Price() const { return price_; }
     private:
        int price_;
    };
    
    class BigTelevision
    {
     public:
        virtual int Price() const { return television::Price() * discount_; }
     private:
        double discount_;
    };
    
    int main()
    {
        std::vector<std::unique_ptr<television>> shoppingCard;
        // add a basic television and a BigTelevision to my shopping card
        shoppingCard.push_back(std::unique_ptr<television>(new television));
        shoppingCard.push_back(std::unique_ptr<television>(new BigTelevision));
    
        // whats the price for alle the tvs?
        int price = 0;
        for(auto tv = begin(shoppingCard), last = end(shoppingCard); 
            tv != last; ++tv)
            price += (*tv)->Price();
    
        // or:
        int price = std::accumulate(begin(shoppingCard), end(shoppingCard), 0,
                    [](int sum, const std::unique_ptr<television>& tv)
                    { return sum + tv->Price()});
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say have this immutable record type: public class Record { public Record(int x,
lets say I have the following public class A { private string _someField; public
Lets say I have this amputated Person class: class Person { public int Age
Lets say I have the following: public class MyObject { [Bindable] public var foo:int
Lets say i have the following controller: // // GET: /Courses/Edit/5 public ActionResult Edit(int
Lets say I have a class that is doing something like: public class Foo
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have a single object of type Car which I want to
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.