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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:36:05+00:00 2026-06-15T00:36:05+00:00

Hello I currently face a problem were I want to output data from 2

  • 0

Hello I currently face a problem were I want to output data from 2 separate classes, one is a base class and one is a derived class, I want to overload the << operator to output all the data at once but seem to have trouble doing so, I have something like this:

#include <iostream>
using namespace std;

class A
{
    char* A;
    char* B;

public:
    A() {A = ' '; B = ' ';}
    A(char* pLast, char* pFirst)
    {

        A = new char [strlen(pLast) + 1];
        B = new char [strlen(pFirst) + 1];

        strcpy(A,pLast);
        strcpy(B,pFirst);
    };
}

class C:public A
{
    int X;
    char Y;
    int Z;
public:
    C(char* A, char* B, int X, char Y, int Z)
        :A(A,B)
    {
        //do stuff here
    }
    friend std::ostream& operator<<(std::ostream& out, const C& outPut)
    {
        out << outPut.A << "," << outPut.B << "," <<outPut.X<< "," << outPut.Y << "," << outPut.Z << endl;
        return out;
    }
};

When I try to run this it tells me A and B are out of range which makes sense since those members are private in class A, I don’t know how to get around this. I tried creating getter methods to access A and B but the data comes out as blank. I even tried added an object of class A as a member of class B to try to allow access to the members in class B still the data comes out blank. How do I get around this problem?

  • 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-15T00:36:06+00:00Added an answer on June 15, 2026 at 12:36 am

    There are several ways of dealing with this. One way obviously is to make the members of A protected rather than private. A derived class B can then access them.

    Another way indeed are getter functions. The fact they didn’t work for you is related to problems in your constructors and other problems in your code. But public getters also have the disadvantage of enabling anyone (not just derived classes) to get access to the values of your data members.

    Here is a third approach, which I believe makes sense in your case: Define a separate operator<< in A, and make use of that operator when you define the one for B:

    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    class A
    {
      char* _a;
      char* _b;
    public:
      A()
        : _a(),_b()
      { }
    
      A(const char *pLast, const char *pFirst)
        : _a(new char [std::strlen(pLast)]),
          _b(new char [std::strlen(pFirst)])
      {
        strcpy(_a,pLast);
        strcpy(_b,pFirst);
      }
    
      friend std::ostream& operator<<(std::ostream& out, const A& obj) 
      {
        out << obj._a << "," << obj._b;
        return out;
      }
    };
    
    class B : public A
    {
      int  _x;
      char _y;
      int  _z;
    
    public:
      B(const char *pLast, const char *pFirst, int x, char y, int z)
        : A(pLast,pFirst),
          _x(x),_y(y),_z(z)
      { }
    
      friend std::ostream& operator<<(std::ostream& out, const B& obj)
      {
        out << static_cast<const A&>(obj) << ','
            << obj._x << ','
            << obj._y << ','
            << obj._z;
        return out;
      }
    };
    
    int main()
    {
      B b("hello","world",1,'a',3);
      std::cout << b << std::endl;
      return 0;
    }
    

    I’ve also corrected the other problems I found, so the above actually works.

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

Sidebar

Related Questions

Hello I currently 2 tables of data from different sources, I need to combine
Hello I have a WinForm app that generates a ton of data. Currently I
Hello I am currently working on a django project, in one of my Models
Hello I am currently getting an index out of range error from the following
Hello currently working on my management system. So my problem is my student has
Hello We're currently migrating from SVN to TFS, and when I created the Team
So I currently have this code: (ns contact-form.core (:gen-class)) (def foo Hello World!) (defn
hello guys i wrote 2 classes one for the mapView and another for the
Hello stackoverflow Community, I am currently facing a problem with the jQuery mobile slider.
Hello all i want to control on pushing the screen globally from background application.

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.