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

  • Home
  • SEARCH
  • 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 6803479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:20:59+00:00 2026-05-26T19:20:59+00:00

I have this code, but I don’t see where I went wrong here. It

  • 0

I have this code, but I don’t see where I went wrong here. It seem to compile OK but I cannot access Computer or Appliance functions. Can someone please help me understand how can I make an array that holds different objects on this code example I have here?

#include <iostream>
using namespace std;

class Technics
{
private:
    int price, warranty;
    static int objCount;
    double pvn;
    char *name, *manufacturer;
public:
    Technics()
    {
        this->objCount++;
    };

    Technics(int price)
    {
        this->objCount++;
        this->price = price;
    }

    ~Technics(){
        this->objCount = this->objCount - 2;
    };

    static int getObjCount()
    {
        return objCount;
    }

    void setPrice(int price)
    {
        this->price = price;
    }

    int getPrice()
    {
        return this->price;
    }

    void resetCount()
    {
        this->objCount = 0;
    }
};
int Technics::objCount = 0;

class Computer : public Technics
{
private:
    int cpu, ram, psu, hdd;
public:
    Computer() {}
    Computer(int price)
    {
        this->setPrice(price);
    }

    void setCpu(int cpu)
    {
        this->cpu = cpu;
    }

    int getCpu()
    {
        return this->cpu;
    }
};

class Appliance : public Technics
{
private:
    int height;
    int width;
    char* color;
    char* type;

public:
    Appliance(){}
    Appliance(int height, int width)
    {
        this->height = height;
        this->width = width;
    }

    void setWidth(int width)
    {
        this->width = width;
    }

    int getWidth()
    {
        return this->width;
    }
};

void main()
{
    //Creating array
    Technics *_t[100];

    // Adding some objects
    _t[0] = new Computer();
    _t[1] = new Computer();
    _t[2] = new Appliance();

    // I can access only properties of Technics, not Computer or Appliance
    _t[0]->

    int x;
    cin >> x;
}
  • 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-26T19:20:59+00:00Added an answer on May 26, 2026 at 7:20 pm

    The line:

    _t[0] = new Computer();
    

    Creates a computer object and stores it as a Technics base pointer in the array (i.e. for all intents and purposes while in that array, it is a Technics object).

    You need to cast back to the derived class to access members that are more derived than those in Technics:

    static_cast<Computer*>(_t[0])->Your_Member();
    

    Use dyncamic cast if you don’t know which derived type it is – it will return the casted pointer on success and NULL on fail so it’s kind of a type-check – it has big runtime overhead though, so try to avoid it 🙂

    EDIT in response to your closing comment:

    //Calculate the length of your dynamic array.
    
    //Allocate the dynamic array as a pointer to a pointer to Technics - this is like
    //Making an array of pointers each holding some Technics heirarchy object.
    Technics** baselist = new Technics*[some_length];
    
    //Populate them the same way as before:
    baselist[0] = new Computer();
    baselist[1] = new Appliance();
    

    PS: you could also use std::vector which is dynamically changeable as opposed to just created at run time – it’s the best option if your allowed to use it. It saves you making your own resizable array code. Google it 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code, I know I'm missing something but don't know what. It
I have this code to draw an ellipse in the screen but I don't
I have this code example, but I don't understand why changing the values in
I don't know why this is happening. I have this code here . It
I currently have this code but it leaks because I don't know how to
I have this code which works well but I don't know if it is
Hey I have this code but it doesn't work because it is expecting a
I have this code that works in a unit test but doesn't work when
I have this code so far which perfectly but relies on there being a
I have this code, but I have a problem. When I update but do

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.