嘿伙计们我对OOP和c ++都很新,所以请耐心等待。
我正在尝试定义一个类,它的功能,然后使用它们。我会展示到目前为止我所遇到的以及我遇到错误的地方。
在名为“account.h”的文件中,我有:
#include <iostream>
#include <string>
using namespace std;
class Account{
string fname;
string lname;
string sinnum;
string accttype;
int numtrans;
double balance;
public:
Account(string,string,string,string);
double DepositAmt(double);
double WithdrawAmt(double);
void PrintStatement();
void getFinalBalance();
};
在名为“account.cpp”的文件中,我有:
Account::Account(string firstname, string lastname, string sinnumber, string acc
{
fname = firstname;
lname = lastname;
sinnum = sinnumber;
accttype = accounttype;
numtrans = 0;
balance = 0;
}
double Account::DepositAmt(double deposit)
{
balance = balance + deposit;
return balance;
}
double Account::WithdrawAmt(double withdraw)
{
balance = balance - withdraw;
return balance;
}
void Account::PrintStatement()
{
cout << "First Name: " << fname << endl;
cout << "Last Name: " << lname << endl;
cout << "SIN: " << sinnum << endl;
cout << "Account Type: " << accttype << endl;
cout << "Total Transactions: " << numtrans << endl;
cout << "Final balance: $" << balance << endl;
}
void Account::getFinalBalance()
{
cout << "Your Final balance is: $" << balance << endl;
}
最后在我上一个名为“ass2012.cpp”的文件中,我有:
#include "account.h"
#include "account.cpp"
int main()
{
string fname, lname, sinnum, accttype;
int tempaccttype;
cout << "\nPlease enter your last name: " << endl;
cin >> lname;
cout << "\nPlease enter your SIN number: " << endl;
cin >> sinnum;
cout << "\nPlease choose your account type: "<< endl;
cout << "1: Checking" << endl;
cout << "2: Savings" << endl;
cin >> tempaccttype;
if (tempaccttype == 1)
{
accttype = "Checking";
}
else
{
accttype = "Savings";
}
Account newacc (fname, lname, sinnum, accttype); // HERE IS WHERE I GET THE ERROR
newacc::getFinalStatement();
return 0;
}
你能告诉我我做错了什么!!
编辑:谢谢Naveen !!这总是让我陷入困境的小事。答案 0 :(得分:5)
对于调用对象的方法,你应该使用.
运算符而不是::
运算符,即你应该newacc.getFinalBalance()
(注意方法名称也是错误的,我在这里更正了。)< / p>
答案 1 :(得分:0)
我对C ++和编程知之甚少,我也是新手。但我会更改行newacc :: getFinalStatement();到newacc.getFinalStatement();.如果这不起作用,我不确定。
答案 2 :(得分:0)
Objectname.memberfunction(); //public
Objectname.datzmember //public
classname::staticmembers //public
::是范围分辨率运算符,仅在存在范围歧义时使用。否则就需要使用它。尽管如此,它很适合用作编程实践。