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 7908015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:43:49+00:00 2026-06-03T11:43:49+00:00

My base class: class Item { protected: int count; string model_name; int item_number; public:

  • 0

My base class:

class Item
{
protected:  
    int count;
    string model_name;
    int item_number;

public:
    Item();
    void input();
}

My derived Class:

class Bed : public Item
{
private:
    string frame;
    string frameColour;
    string mattress;

public:
    Bed();
    void input();
}

for now all my input function is trying to do is output which method is being used:

void Item::input()
{ 
    cout<<"Item input"<<endl;
}

void Bed::input()
{
    cout<<" Bed Input"<<endl;
}

when I call the function in main I’d like the derived class input to be used but at present the item input is.

Main:

vector<Item> v;
Item* item;
item= new Bed;
v.push_back(*item);
v[count].input();
count++;

I have followed the method laid out in a book I have but I think i may be confused about how to create new objects stored in the vector.

Any help would be great,
Thanks
Hx

  • 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-06-03T11:43:55+00:00Added an answer on June 3, 2026 at 11:43 am

    You haven’t marked your method as virtual.

    Also, because you have a vector of objects, not pointers, you’ll run into object slicing. Although it will compile, it isn’t correct.

    The proper way is having a vector of pointers or smart pointers.

    class Item
    {
       //....
       virtual void input(); //mark method virtual in base class
    };
    
    class Bed : public Item
    {
       //....
       virtual void input();
    };
    
    
    vector<Item*> v;
    Item* item = new Bed;
    v.push_back(item);
    //...
    //remember to free the memory
    for ( int i = 0 ; i < v.size() ; i++ ) 
        delete v[i];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// Base Class class Base { public: void RegisterWithServer(); // some more functions protected:
Given the following public class MyControl : CompositeControl { private DropDownList myList; protected override
I have a generic collection: public Items : Collection<Object> { protected override void InsertItem(int
public abstract class MyControllerBase : Controller { protected override void OnActionExecuting(ActionExecutingContext context) { //
This is my code : private string[] MesiSelezionati; protected void Page_Load(object sender, EventArgs e)
In base class constructors I always see a parameterless constructor, like so: public abstract
I have a base class, defined as below (I'm also using DevExpress components): public
I have a base class A and a derived class B: class A {
#include stdafx.h #include <iostream> #include <vector> #include <string> class Helper { public: Helper() {
Ok, I'm trying to do the following: protected bool ValidAdvert(Base item) { throw ThisIsAnAbstractClassException();

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.