gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:07:30 27 4
gpt4 key购买 nike

好的,这是程序

#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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com