Hi Folks,
I'm still trying to get my head around Classes...
Specifically, I am having problems defining functions within a class. I can only seem to define them outside a class. If i put them in a class I get compiler errors.
Here is my code. How would I change this to put definitions within the person class?
Also, I get errors if I define the destructor just as ~person();
Code:
////////////////////////////////////////////////////
// Demonstrates Class, Member Data, Member Functions
////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <vector>
using namespace std;
///////////////////////////////////////
// Define person class
///////////////////////////////////////
class person
{
public:
/////////////////////////////////
// constructor
/////////////////////////////////
person(int age)
{
itsname="undefined";
itsage=age;
}
/////////////////////////////////
// destructor
/////////////////////////////////
~person()
{
}
/////////////////////////////////
// function prototypes
/////////////////////////////////
void setage(int age);
int getage();
void setname(string name);
string getname();
private:
string itsname;
int itsage;
};
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////
// function definitions
/////////////////////////////////
void person::setage(int age)
{
itsage=age;
}
int person::getage()
{
return itsage;
}
void person::setname(string name)
{
itsname=name;
}
string person::getname()
{
return itsname;
}
/////////////////////////////////////////////////////////////////
///////////////////////////////////////
// Main Program
///////////////////////////////////////
int main()
{
/////////////////////////////////////////
// test class
/////////////////////////////////////////
// create person class called test
person test(9999);
// change member variables and print to test
cout << "initial age = " << test.getage() << "\n";
test.setage(13);
cout << "altered age = " << test.getage() << "\n";
cout << "initial name = " << test.getname() << "\n";
test.setname("bobbins");
cout << "altered name = " << test.getname() << "\n";
/////////////////////////////////////////
// wait for user to end program
/////////////////////////////////////////
cin.get();
return 0;
}