c++ - 如何将私有(private)类的值返回到主类

标签 c++ class oop

好的,这是程序

#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;
}

从程序中我认为我已经能够访问 My_Date 类以将值放入年月日变量中 现在我唯一不知道的是如何将值返回到主类,因为 my_date 类是 ROrder 类的私有(private)类。 认为代码不完整我只需要有关 my_date 类的返回值的帮助

最佳答案

ROrder 类中,添加:

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

main函数中,添加:

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

不知道是否解决了你的问题。

关于c++ - 如何将私有(private)类的值返回到主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34689923/

相关文章:

Java 类和构造函数

javascript - 在 oop 语言中使用私有(private)变量/方法的例子是有用的/必要的

c++ - 类接口(interface) : Basic or Complex?

php - 将函数的结果分配给 PHP 类中的变量?面向对象的怪异

c++ std::string name 和 std::string &name 之间的区别

c++ - 嵌入式 Python : Getting func obj from imported module

c++ - 当用户键入定界符时,停止getline()输入

c++ - 向上转换为基类时如何使用继承的类成员的值

c++ - 类的全局随机数生成器

jquery - 为什么使用类选择器的内部样式表会被外部样式表覆盖?