Having a little trouble with the set and get functions, can anyone help me?
#include <string> // program uses C++ standard string class
using namespace std;
// Invoice class definition
class Invoice
{
public:
// constructor initializes the four data members
Invoice( string, string, int, int );
// set and get functions for the four data members
void setPartNumber( string ); // part number
string getPartNumber();
void setPartDescription( string ); // part description
string getPartDescription();
void setQuantity( int ); // quantity
int getQuantity();
void setPricePerItem( int ); // price per item
int getPricePerItem();
// calculates invoice amount by multiplying quantity x price per item
int getInvoiceAmount();
private:
string partNumber; // the number of the part being sold
string partDescription; // description of the part being sold
int quantity; // how many of the items are being sold
int pricePerItem; // price per item
}; // end class Invoice
#include <iostream>
#include "Invoice.h"
using namespace std;
// function main begins program execution
int main()
{
// create an Invoice object
Invoice invoice( "12345", "Hammer", 100, 5 );
// display the invoice data members and calculate the amount
cout << "Part number: " << invoice.getPartNumber() << endl;
cout << "Part description: " << invoice.getPartDescription() << endl;
cout << "Quantity: " << invoice.getQuantity() << endl;
cout << "Price per item: $" << invoice.getPricePerItem() << endl;
cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;
// modify the invoice data members
invoice.setPartNumber( "123456" );
invoice.setPartDescription( "Saw" );
invoice.setQuantity( -5 ); // negative quantity, so quantity set to 0
invoice.setPricePerItem( 10 );
cout << "\nInvoice data members modified.\n\n";
// display the modified invoice data members and calculate new amount
cout << "Part number: " << invoice.getPartNumber() << endl;
cout << "Part description: " << invoice.getPartDescription() << endl;
cout << "Quantity: " << invoice.getQuantity() << endl;
cout << "Price per item: $" << invoice.getPricePerItem() << endl;
cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl;
} // end main
#include <iostream>
#include "Invoice.h"
using namespace std;
// Invoice constructor initializes the class's four data members
Invoice::Invoice( string number, string description, int count, int price )
{
setPartNumber( number ); // store partNumber
setPartDescription( description ); // store partDescription
setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store pricePerItem
} // end Invoice constructor
// set part number
void Invoice::setPartNumber( string number )
{
[ need help here ] // no validation needed
} // end function setPartNumber
// get part number
string Invoice::getPartNumber()
{
[ need help here ]
} // end function getPartNumber
// set part description
void Invoice::setPartDescription( string description )
{
[need help here] // no validation needed
} // end function setPartDescription
// get part description
string Invoice::getPartDescription()
{
[ need help here ]
} // end function getPartDescription
// set quantity; if not positive, set to 0
void Invoice::setQuantity( int count )
{
[ need help here ]
} // end function setQuantity
// get quantity
int Invoice::getQuantity()
{
[ need help here ]
} // end function getQuantity
// set price per item; if not positive, set to 0
void Invoice::setPricePerItem( int price )
{
[ need help here ]
} // end function setPricePerItem
// get price per item
int Invoice::getPricePerItem()
{
[ need help here ]
} // end function getPricePerItem
// calulates invoice amount by multiplying quantity x price per item
int Invoice::getInvoiceAmount()
{
[ need help here ]
Disch (6541) Dec 9, 2011 at 6:26pm
Pretty simple. Getters should return the appropriate variable. Setters should assign it to whatever was passed to the function (the parameter).
ya but im having trouble with knowing what i should put in the places like setpartnumber and getpartnumber, i understand what ur saying but the exact code i should place in there is the issue.
// set part number
void Invoice::setPartNumber( string number )
{
partnumber = number;
} // end function setPartNumber
// get part number
string Invoice::getPartNumber()
{
return partnumber;
} // end function getPartNumber
As explained by Disch - this illustrates it for partnumber. The rest follows the same logic.
// set part number
void Invoice::setPartNumber( string number )
{
partNumber = number;
} // end function setPartNumber
// get part number
string Invoice::getPartNumber()
{
return partNumber;
} // end function getPartNumber
class myClass{
public:
myClass( string name, int age );
void setmyName( string name );
string getmyName() const;
void setAge( int age );
int getAge() const;
privates:
string myName;
int age;
}
int main (){
myClass me;
return 0;
}
myClass::myClass( string name, int age ): myName(name), age(age) {}
// it is always better to use initialization list in your cTor. also,
// by saying "age(age)" the compiler won't have problem understanding
// that the first one is the data member & the second one is the parameter
// passed (from right the left). Also when using initialization list, remember
// to list them in the same order that they are defined.
void myClass::setmyName( string name ){
myName = name;
}
// here we have a mutator method that sets the name of myClass.
string myClass::getmyName() const { return myName; }
// here we have an accessor method that returns type string for the
// name of myClass.
void myClass::setAge( int age ){
this->age = age;
}
// same as the first mutator method, but this time we need to point out
// the difference between the two age datas. "this->age" is the data
// member of my class. "age" is the parameter passed.
int myClass::getAge() const { return age; }
// same as the myName accessor, returning type int for the age of myClass.