Hej alle sammen,
Jeg sidder lige og prover paa at arbejde med en private member fra en klasse...
Det jeg gerne vil er at give den variabel som er private en vaerdi (0.0416)
Jeg har derfor lavet en class som ser saadan her ud:
#ifndef PERSON_H
#define PERSON_H
#include<string>
using namespace std;
class Person {
public:
string firstName, lastName, homeTown, homeCountry;
double annualSalary, nextYearSalary, pubRaiseRate;
int month, day, year;
void person(); // the Constructor
void setRaiseRate(double pubRaiseRate);
double getRaiseRate();
void calcNextYear();
void printPerson();
private:
double raiseRate;
};
#endif
En anden fil som ser saadan her ud:
#include<iostream>
#include<iomanip>
#include"person.h"
void Person::person() // The Person Constructor
{
firstName = "";
lastName = "";
homeTown = "";
homeCountry = "";
annualSalary = 0.0;
nextYearSalary = 0.0;
month = 0;
day = 0;
year = 0;
raiseRate = 0.0;
pubRaiseRate = 0.0;
} // End Person Constructor
void Person::setRaiseRate(double pubRaiseRate)
{
raiseRate = pubRaiseRate;
}
double Person::getRaiseRate()
{
return raiseRate;
}
void Person::calcNextYear()
{
nextYearSalary = (raiseRate * annualSalary);
}
void Person::printPerson()
{
cout << "-----------------------------------------" << endl;
cout << lastName << ", " << firstName << endl;
cout << homeTown << ", " << homeCountry << endl;
switch (month) {
case 1: {cout << "January ";
break;}
case 2: {cout << "February ";
break;}
case 3: {cout << "March ";
break;}
case 4: {cout << "April ";
break;}
case 5: {cout << "May ";
break;}
case 6: {cout << "June ";
break;}
case 7: {cout << "July ";
break;}
case 8: {cout << "August ";
break;}
case 9: {cout << "September ";
break;}
case 10:{cout << "October ";
break;}
case 11:{cout << "November ";
break;}
case 12:{cout << "December ";
break;}
};
cout << day << ", " << year << endl;
cout << "This year's salary: $" << setw(8) << annualSalary << endl;
cout << "Next year's salary: $" << setw(8) << nextYearSalary << endl;
cout << "-----------------------------------------" << endl;
}
og en main function som der saadan her ud:
#include<iostream>
#include"person.h"
using namespace std;
const double RAISE_RATE = 0.0416;
main()
{
Person newPerson;
// simply collect the data from the user!
cout << "Enter first and last name and press <Enter>: ";
cin >> newPerson.firstName >> newPerson.lastName;
cout << "Enter city where you were born and press <Enter>: ";
cin >> newPerson.homeTown;
cout << "Enter the initials of the country where you were born and press <Enter>: ";
cin >> newPerson.homeCountry;
cout << "Enter your annual salary and press <Enter>: ";
cin >> newPerson.annualSalary;
while ((newPerson.month >= 13) ||
(newPerson.month == 0) ||
(newPerson.day == 0) ||
(newPerson.day >= 32) ||
(newPerson.year < 1800) ||
(newPerson.year > 2100))
{
cout << "Enter your Birthdate (month date year) and press <Enter>: ";
cin >> newPerson.month >> newPerson.day >> newPerson.year;
}
// cout << newPerson.raiseRate << endl;
newPerson.setRaiseRate(RAISE_RATE);
cout << newPerson.pubRaiseRate << endl;
newPerson.printPerson();
return 0;
}
Hvad gor jeg forkert?...
Undskyld at jeg har vist alt denne kode, men jeg er ikke lige istand til at forklare det uden af vise koden.
men altsaa, jeg vil gerne have vaerdien 0.0416 til raiseRate som er private i klassen Person. og saa skal nextYearSalary blive udregnet vedhjaelp af raiseRate..
Haaber at der er nogle som har tid til at hjaelpe mig ..
tak
Smox