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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:58:33+00:00 2026-05-31T16:58:33+00:00

I have the class Furniture with: Furniture.h: #include <iostream> #include <string> using namespace std;

  • 0

I have the class Furniture with:

Furniture.h:

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

class Furniture {
public:
    Furniture();
    ~Furniture();
    void setname(string name);
    void setprice(double price);
    double getprice();
    string getname();
    virtual void printSpecs();
private:
    string name;
    double price;
protected:
    static int NumberOfItems;
    int Id;

};

furniture.cpp:

#include "furniture.h"

Furniture::Furniture() {
}
Furniture::~Furniture() {
}
void Furniture::setname(string name) {
    this->name = name;
}
string Furniture::getname()
{
    return this->name;
}
void Furniture::setprice(double price) {
    this->price = price;
}
double Furniture::getprice() {
    return this->price;
}
void Furniture::printSpecs() {
    cout<<"Price: "<<this->price<<endl;
    cout<<"Name: "<<this->name<<endl;
}

int main() {
    Furniture *model = new Furniture();
    model->setname("FinalDestiny");
    model->setprice(149.99);
    model->printSpecs();
    delete model;
}

Everything works fine but I want to add multiple furniture items with the same class and just update the NumberOfItems. Is there any way to do that?

Also, is my code ok? I mean, how can I improve it? I’m quite new to OOP and I’d like to learn some good practices.

Thanks.

  • 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-31T16:58:34+00:00Added an answer on May 31, 2026 at 4:58 pm

    The idea is conceptually broken. You cannot do that; you really need different objects.

    Alternatively, if you really want to have multiple identical items, you can create one item and create multiple pointers to it, and maintain a separate count for the number of active items. A shared_ptr does that for instance.

    That said, your code shouldn’t use pointers at all, this is a common anti-pattern in C++ code. Furthermore, your code probably shouldn’t have setters, provide a proper constructor instead:

    int main() {
        Furniture model("FinalDestiny", 149.99);
        model.printSpecs();
    }
    

    Much shorter, simpler, and no possiblity of leaking memory.

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

Sidebar

Related Questions

I have this in furniture.h: #include <iostream> #include <string> using namespace std; class Furniture
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class: static class AnotherClass { int number; String isPrime; public int getNumber()
I have class with member functions: typedef std::function<bool (const std::string &)> InsertFunction; bool insertSourceFile(
I have class with a forwarding method foo : void foo( Concrete c, String
I have class: static class GetRole { public static string OfUser(string username) { string
I have class public class Class2 { public string myName { get; set; }
I have class A: public class ClassA<T> Class B derives from A: public class
I have class Cab(models.Model): name = models.CharField( max_length=20 ) descr = models.CharField( max_length=2000 )
I have class A: public B { ...} vector<A*> v; I want to 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.