declaring class for implementation HELP!

#include <iostream>


using namespace std;
class Transaction

{
private:
int TransactionId, CustomerID,Cost;
public:
Transaction(int TransactionId =0, int CustomerID =0,int Cost=0 ) :TransactionId (TransactionId ),CustomerID(CustomerID),Cost(Cost)
{
}
int getTransactionId() {return TransactionId;}
int getCustomerID(){return CustomerID; }
int getCost(){ return Cost;}
void putTransactionId(int TransactionId){this->TransactionId;}
void settCustomerID(int CustomerID) { this ->CustomerID;}
void setCost (int Cost){ this -> Cost;}
void setTransactionIdCustomerIDCost(int TransactionId, int CustomerID, int Cost )
{this ->TransactionId= TransactionId; this-> CustomerID= CustomerID; this-> Cost=Cost;}
void print(){
cout <<"TransactionId # ("<<TransactionId<< ","<<CustomerID<<","<<Cost<<")";
}


};
I see your code. What is your question?

I see several problems with this code. For example:

 
void settCustomerID(int CustomerID) { this ->CustomerID;}

"this->CustomerID;" just evaluates the customerID and then throws the value away. You want to assign it to the parameter value:
 
void settCustomerID(int CustomerID) {this ->CustomerID = CustomerID}

This will work but it's bad practice to have the function parameter name be the same name as a class member. If you use a different name then you also don't need to superfluous "this->":
 
void settCustomerID(int val) {CustomerID = val}


Personally I think accessor functions (getXXX() and setXXX()) are overrated. If you can get and set the values then just make them public. That way you can also create pointers and references to them, which you can't do with get/set functions like the ones you have.

The print() function should probably be a << operator instead. At the very least, change it to take the stream as an argument so it's more flexible.
Topic archived. No new replies allowed.