user5173712
user5173712

Reputation:

How do I return the value from the private class back to the main

Ok so here is the program

#include<iostream>
#include<iomanip>

using namespace std;

class My_Date{
private:
    int year;
    int month;
    int day;
public:
    My_Date(){}
    ~My_Date(){}
    void setDate(int recieve_year,int recieve_month,int recieve_day)
    {
        year = recieve_year;
        month = recieve_month;
        day = recieve_day;
    }
    int getYear()
    {
        return year;
    }
    int getMonth()
    {
        return month;
    }
    int getday()
    {
        return day;
    }
};

class ROrder{
private:
    unsigned int order_ID;
    My_Date Order_Date;
    double amount;
    double tip;
    double totalamount();


public:
    void setorder_ID(unsigned int recieve_order_ID)
    {
        order_ID = recieve_order_ID;
    }
    void setamount(double recieve_amount)
    {
        amount = recieve_amount;
    }
    void settip(double recieve_tip)
    {
        tip = recieve_tip;
    }

    double getorder_ID()
    {
        return order_ID;
    }
    double getamount()
    {
        return amount;
    }
    double gettip()
    {
        return tip;
    }
    void setDate(int y, int m, int d)
    {
        Order_Date.setDate(y,m,d);
    }

};

int main()
{
    int ID,send_Year,send_Month,send_Day,send_amount;
    class ROrder Rice;
    cout << "Enter Order ID: ";
    cin >>  ID;
    cout << "Enter Date (YYYY/MM/DD): ";
    cin >> send_Year >> send_Month >> send_Day;
    Rice.setorder_ID(ID);
    Rice.setDate(send_Year,send_Month,send_Day);
    cout << "Your Order ID is: " << Rice.getorder_ID()<<endl;

    return 0;
}

From the program I think I have been able to access the My_Date Class to put the value in the year month and day variables Now the only thing I do not know is how to return the values to back to the main class since the my_date class is a private class of the ROrder class. Thought the code is incomplete I would only require help with the returning valuesfrom the my_date class

Upvotes: 0

Views: 70

Answers (3)

A Osman
A Osman

Reputation: 161

If I understand correctly you would like to return a value that's within the inner class. If that's the case, you need to create a getter method in the outer class,which would require you to instantiate the object in the outer class.

Upvotes: 0

Magical Gordon
Magical Gordon

Reputation: 404

Sounds to me like you want to prove a wrapper getter function for an inner class.

Here's what that looks like:

outer class wrapper a getter for an inner class

Upvotes: 0

huoyao
huoyao

Reputation: 121

In class ROrder, add:

public:
  int getYear(){
     return Order_Date.getYear();
  }

In main function, add:

cout<<Rice.getYear()<<endl;

I am not sure whether it has solved your problem.

Upvotes: 0

Related Questions