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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:54:11+00:00 2026-06-05T01:54:11+00:00

I have a Base class, and a Derived class; on the other hand I

  • 0

I have a Base class, and a Derived class; on the other hand I made a List class that uses the STL . Base class has a virtual function called PrintData(), that prints an integer that belongs to the Base class. In the Derived class; the same function printData() prints an integer that belong to the Derived and the other one from the Base class.

The thing is that, in the class List, I’m just getting the data from Base, no matter if I inserted a Derived instance on the list.

I need to print Derived data, that is supposed to have Base data as well.
Here is the code:

 #pragma once;
#include <iostream>
#include <sstream>
using namespace std;

class Base{
protected:
    int x;

public:
    Base(){
        x=3;
    }
    void setX(int a){
        x=a;
    }
    int getX(){
        return x;
    }
    virtual string printData(){
        stringstream f;
        f<<getX()<<endl;
        return f.str();
    }
};

class Derived : public Base{

    int a;

public:
    Derived(){
        this->Base::Base();
        a=4;
    }
    void setA(int x){
        a=x;
    }
    int getA(){
        return a;
    }
    string printData(){
        stringstream a;
        a<<getA()<<getX()<<endl;
        return a.str();
    }


};

And here is the List class:

 #pragma once;
#include "Prueba.cpp"
#include <list>

    class Lista{
        list<Base*> lp;
    public:
        Lista(){

        }

        void pushFront(Base* c){
            lp.push_front(c);
        }

        void pushBack(Base* c){
            lp.push_back(c);
        }

        void printList(){

            list<Base*>::const_iterator itr;
                for(itr=lp.begin(); itr!=lp.end(); itr++){ 
                    cout<<(*itr)->printData(); 

                }
        }

         ~Lista(){

        }
    };

    int main(){
        Derived* d=new Derived();
        Lista* l=new Lista();
        l->pushFront(d);
        l->printList();
        system("Pause");
        return 0;
    }

I’m just getting the Base class data, that is an integer with the value of 3.
But I’m not getting the integer from Derived that has the value of 4.

  • 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-05T01:54:12+00:00Added an answer on June 5, 2026 at 1:54 am

    Replace

        a<<getA()<<getX()<<endl;
    

    with

        a<<getA()<<" "<<getX()<<endl;
    

    and run it again. I suspect that your code does print both values, only it disguises the fact. The new line strips the disguise away, so to speak.

    By the way, @MarkRansom is right. It is not necessary to call the Base constructor explicitly. (In fact, when I tried your code, my compiler wouldn’t even allow it.)

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

Sidebar

Related Questions

i have a base class, 2 derived classes and an entitymanager class that has
I have a base and derived class called Node which form elements of a
I have a base class Element and derived two other classes from it, Solid
I have a derived class that's a very thin wrapper around a base class.
I have a base class and several subclasses derived from that base class. I
A base class have readonly field of type List<SomeEnum> which the derived classes will
I have a base class with an optional virtual function class Base { virtual
Scenario I have a single base class and 2 (possibly 3) other classes that
Lets say I have an abstract base class with a pure virtual that returns
I have two classes, Base and Derived . I provide a function that applies

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.