当通过成员函数将文字传递给构造函数时,为什么没有将值分配给class的成员?
When a literal is passed to the constructor through a member function, why is the value not assigned to the member of the class?
我是 C++ 的初学者。我正在研究构造函数。我正在写一个简单的火车票预订程序。这是我的代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Ticket
{
char* name;
long PNR;
Ticket(char *pname, long pnr):name(pname),PNR(pnr)
{
}
public:
Ticket()
{
name = NULL;
PNR=0;
}
void booking()
{
char *n;
n= new char[25];
cout << "Enter Your Name: ";
cin >> n;
Ticket(n,12345);
cout << "Your Ticket is Booked." << endl;
}
void status()
{
long num;
cout << "Enter PNR: ";
cin >> num;
if (num == PNR)
cout << "Ticket is confirmed" << endl;
else
cout << "Ticket is not confirmed" << endl;
}
void print()
{
cout << "Your PNR is " << PNR << endl;
cout << "The PNR is alloted to " << name << endl;
}
};
using namespace std;
int main()
{
int option;
Ticket pass1;
book:
cout << "Select an option: " << endl;
cout << "1. Booking\t2. Status\t3.Print Info" << endl;
cin >> option;
switch(option)
{
case 1:
pass1.booking();
break;
case 2:
pass1.status();
break;
case 3:
pass1.print();
break;
default:
cout << "Invalid Entry. Exiting" << endl;
break;
}
cout << "Do you want to check another ticket?" << endl;
cout <<"1. Yes\t2. No" << endl;
cin >> option;
switch(option)
{
case 1:
goto book;
break;
case 2:
default:
cout << "Exiting" << endl;
exit(0);
}
return 0;
}
在成员函数中,booking()
,我调用了构造函数,并传递了n和12345。然而,传递给构造函数的n被保存到成员变量"name"但12345没有被保存到成员变量"PNR"。我没有在代码中发现任何错误。为什么会这样?
Ticket(n,12345);
不是 re-call 当前元素的构造函数,它创建一个用 (n,12345)
初始化的临时 Ticket
并立即丢弃它。
一般情况下,一个对象只能构造一次。构造完成后,您将无法再次调用该对象的构造函数。
相反,您可以使用成员变量的名称来分配它们:
void booking() {
PNR = 42; // This will change the member PNR of the current instance
// ...
}
Ticket(n,12345);
正在创建临时 Ticket
然后丢弃它。它不会为当前 Ticket
设置成员变量。设置变量 booking()
应该像
void booking()
{
name = new int[25]
cout << "Enter Your Name: ";
cin >> name;
PNR = 12345;
cout << "Your Ticket is Booked." << endl;
}
我建议您使用 std::string 而不是 char 数组,因为您需要记住在析构函数中删除数组,并且需要实现复制构造函数和赋值运算符。如果您使用 std::string
,那么所有默认值都适用。
我是 C++ 的初学者。我正在研究构造函数。我正在写一个简单的火车票预订程序。这是我的代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Ticket
{
char* name;
long PNR;
Ticket(char *pname, long pnr):name(pname),PNR(pnr)
{
}
public:
Ticket()
{
name = NULL;
PNR=0;
}
void booking()
{
char *n;
n= new char[25];
cout << "Enter Your Name: ";
cin >> n;
Ticket(n,12345);
cout << "Your Ticket is Booked." << endl;
}
void status()
{
long num;
cout << "Enter PNR: ";
cin >> num;
if (num == PNR)
cout << "Ticket is confirmed" << endl;
else
cout << "Ticket is not confirmed" << endl;
}
void print()
{
cout << "Your PNR is " << PNR << endl;
cout << "The PNR is alloted to " << name << endl;
}
};
using namespace std;
int main()
{
int option;
Ticket pass1;
book:
cout << "Select an option: " << endl;
cout << "1. Booking\t2. Status\t3.Print Info" << endl;
cin >> option;
switch(option)
{
case 1:
pass1.booking();
break;
case 2:
pass1.status();
break;
case 3:
pass1.print();
break;
default:
cout << "Invalid Entry. Exiting" << endl;
break;
}
cout << "Do you want to check another ticket?" << endl;
cout <<"1. Yes\t2. No" << endl;
cin >> option;
switch(option)
{
case 1:
goto book;
break;
case 2:
default:
cout << "Exiting" << endl;
exit(0);
}
return 0;
}
在成员函数中,booking()
,我调用了构造函数,并传递了n和12345。然而,传递给构造函数的n被保存到成员变量"name"但12345没有被保存到成员变量"PNR"。我没有在代码中发现任何错误。为什么会这样?
Ticket(n,12345);
不是 re-call 当前元素的构造函数,它创建一个用 (n,12345)
初始化的临时 Ticket
并立即丢弃它。
一般情况下,一个对象只能构造一次。构造完成后,您将无法再次调用该对象的构造函数。
相反,您可以使用成员变量的名称来分配它们:
void booking() {
PNR = 42; // This will change the member PNR of the current instance
// ...
}
Ticket(n,12345);
正在创建临时 Ticket
然后丢弃它。它不会为当前 Ticket
设置成员变量。设置变量 booking()
应该像
void booking()
{
name = new int[25]
cout << "Enter Your Name: ";
cin >> name;
PNR = 12345;
cout << "Your Ticket is Booked." << endl;
}
我建议您使用 std::string 而不是 char 数组,因为您需要记住在析构函数中删除数组,并且需要实现复制构造函数和赋值运算符。如果您使用 std::string
,那么所有默认值都适用。