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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:24:57+00:00 2026-06-10T17:24:57+00:00

I am working on OOP C++ program and I’m bit struggling. I’m trying to

  • 0

I am working on OOP C++ program and I’m bit struggling. I’m trying to create a program that demonstrates use of default and non-default constructors and pointers. I’m trying to do default constructor first.

So I am able to store and retrieve only the local variables inside of one method. But now I must pass values to other class (I think I must) and then retrieve the information again but little modified.

I could initialize object to one class as I did but then when I try to retrieve the object it basically doesn’t retrieve anything but empty space. How do I correctly pass an object to method in another class and then retrieve it back?

Any pointers?

Book.cpp

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

#include "Book.h"

Book::Book()
{
}

void Book::setTitle(string  title)
{
    this->title = title;
}

void Book::setAuthorName(string first, string last)
{
    Author author;

    author.setFirstName(first);
    author.setLastName(last);
}


void Book::setPrice(double price)
{
    this->price = price;
}

string Book::convertDoubleToString(double number)
{
    return static_cast<ostringstream*>( &(ostringstream() << number) ) -> str();
}

string Book::getBookInfo()
{
    stringstream ss;
    Author author;

    ss << title << endl << author.getFullName() << endl << "$" << convertDoubleToString(price) << endl;

    return ss.str();
}
  • 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-10T17:24:59+00:00Added an answer on June 10, 2026 at 5:24 pm

    This part is not going to work

    void Book::setAuthorName(string first, string last)
    {
        Author author;
    
        author.setFirstName(first);
        author.setLastName(last);
    }
    

    because inside this function you create a local object, set its values and then its destroyed when the function exits. You need to create a member variable of class Author inside your Book class if you want to retain this author information.

    Inside your Book class declaration, you need something like this

    class Book {
        Author m_Author;  // This is your member variable that you can store author data in
    
    };
    

    then inside your setAuthorName function, set the values of m_Author rather than creating a local variable. This will retain the values inside the member variable m_Author

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

Sidebar

Related Questions

So I'm working on an OOP program that is meant to create 50 unique
I'm new to Python OOP and trying to create a OOP program to manage
I have created a functional OOP program that uses a bunch of classes, and
I'm very new to OOP, and in the program I'm working on, I have
I am working on a drawing program and am trying to figure out the
I have been working with QT and I noticed that they took OOP to
I'm new to OOP and could use some help. My class is not working:
I am working on a wordpress plugin and i decided to use OOP instead
I'm trying to grasp OOP and I decided to build a site that accesses
I prefer to use OOP in large scale projects like the one I'm working

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.