How to pass an object of a class with a string data member to another class in C++

Phantomatrixx
#include <iostream>
using namespace std;
class fruit{
    private:
    string name;
    int amount;
    float price;
public:
    void getData(){
        cout <<"Enter name of fruit: ";
        cin.ignore();
        getline(cin, name);
        cout <<"Enter amount: ";
        cin >>amount;
        cout <<"Enter price: $";
        cin >>price;
        cout <<endl;
    }
    void displayData(){
        cout <<"Fruit: " <<name <<endl;
        cout <<"In stock: " <<amount <<endl;
        cout <<"Price: $" <<price <<endl;
    }
};
class seller{
public:
    void display(fruit& obj){
        cout <<"You are a seller!" <<endl <<endl;
        obj.getData();
    }
};
class buyer{
public:
    void display(fruit& obj){
        cout <<"You are a buyer!" <<endl <<endl;
        obj.displayData();
    }
};
int main(){
int userChoice;
cout <<"Welcome to shop simulator!" <<endl;
    while(true){
        cout <<"Please select anything from the following:" <<endl;
        cout <<"[0] Exit the program" <<endl;
        cout <<"[1] Seller" <<endl;
        cout <<"[2] Buyer" <<endl;
        cin >>userChoice;
        fruit obj1;
        seller obj2;
        buyer obj3;
        switch(userChoice){
            case 0:
                _Exit(0);
                break;
            case 1:
                obj2.display(obj1);
                break;
            case 2:
                obj3.display(obj1);
                break;
        }
    }
}

Whenever I tried to enter a fruit and its properties in the buyer option and then tried to access it to the seller option, only the "amount" and "price" are being displayed. (I have tried using array of characters and it worked, but when I use string it doesn't work).

Waqar

These three variables:

        fruit obj1;
        seller obj2;
        buyer obj3;

are created from scratch everytime you loop. Hence the values you enter in iteration 1 are not preserved in the second iteration. The fact that you are getting price and amount correctly is just a coincidence.

To fix this quickly, just move the variables above outside the loop:

        fruit obj1;
        seller obj2;
        buyer obj3;
    cout <<"Welcome to shop simulator!" <<endl;
    while(true){

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

분류에서Dev

Creating a class instance as a member of another class

분류에서Dev

accessing the member of a class of pointer array of another class

분류에서Dev

How to create an object of a class which is inside a module and inherited another class?

분류에서Dev

How do I reference a string in another class?

분류에서Dev

Pass Class as parameter and return same class object

분류에서Dev

How can I pass the stringbuilder and display out in another class

분류에서Dev

calling method in template class in template member function of another class

분류에서Dev

How can I access a class data member from a method within the same class?

분류에서Dev

get value of an object in another class

분류에서Dev

Converting object of a class to of another one

분류에서Dev

Class has no member "Class"

분류에서Dev

Forcing initialization of static data member of template class

분류에서Dev

Constructing a class using an another class C++

분류에서Dev

C# Class Instance Holding String Reference But Not Object Reference

분류에서Dev

How can I do an array of object of my class (with inheritance of another custom class)?

분류에서Dev

How to access variable from another class directly(i.e. without using class name or object)?

분류에서Dev

How do I pass data from a widget config class to a widget provider class via intents?

분류에서Dev

How to pass an optional value to objective c class from swift

분류에서Dev

C++ class member check if not a template

분류에서Dev

Does Python data type are all object based on C Class?

분류에서Dev

how to call class AsyncTask to another class?

분류에서Dev

How to change the class of an element based on the class of another

분류에서Dev

How to keep track of instances of another class in a class?

분류에서Dev

How to reference a class in another project?

분류에서Dev

Ruby Beginner - Accessing Another Class Object

분류에서Dev

Access an Object in Another Class from an Interface?

분류에서Dev

How do I pass a single Firestore document ID to another class based on a selection using Flutter/Dart?

분류에서Dev

How Would I Delete This Class Object? (C++)

Related 관련 기사

  1. 1

    How is a constructor for a derived class supposed to be like in c++ when derived class has added data member

  2. 2

    Creating a class instance as a member of another class

  3. 3

    accessing the member of a class of pointer array of another class

  4. 4

    How to create an object of a class which is inside a module and inherited another class?

  5. 5

    How do I reference a string in another class?

  6. 6

    Pass Class as parameter and return same class object

  7. 7

    How can I pass the stringbuilder and display out in another class

  8. 8

    calling method in template class in template member function of another class

  9. 9

    How can I access a class data member from a method within the same class?

  10. 10

    get value of an object in another class

  11. 11

    Converting object of a class to of another one

  12. 12

    Class has no member "Class"

  13. 13

    Forcing initialization of static data member of template class

  14. 14

    Constructing a class using an another class C++

  15. 15

    C# Class Instance Holding String Reference But Not Object Reference

  16. 16

    How can I do an array of object of my class (with inheritance of another custom class)?

  17. 17

    How to access variable from another class directly(i.e. without using class name or object)?

  18. 18

    How do I pass data from a widget config class to a widget provider class via intents?

  19. 19

    How to pass an optional value to objective c class from swift

  20. 20

    C++ class member check if not a template

  21. 21

    Does Python data type are all object based on C Class?

  22. 22

    how to call class AsyncTask to another class?

  23. 23

    How to change the class of an element based on the class of another

  24. 24

    How to keep track of instances of another class in a class?

  25. 25

    How to reference a class in another project?

  26. 26

    Ruby Beginner - Accessing Another Class Object

  27. 27

    Access an Object in Another Class from an Interface?

  28. 28

    How do I pass a single Firestore document ID to another class based on a selection using Flutter/Dart?

  29. 29

    How Would I Delete This Class Object? (C++)

뜨겁다태그

보관