Hi everybody :-) this is my first post... I'm hoping to learn C++ so that I can change my career and get out of the crappy Logistics industry!! I'm a total programming noob (C++ is my first language I'm learning) and I'm a little stuck.
Just as an exercise I'm trying to manipulate individual elements of a private char array (using the member function setChar here). This is my code:
insertThat which I've highlighted in bold, the setChar function, is what is causing this basic program to crash.Code:#include <iostream> #include <string> using namespace std; class Cat { public: Cat() {itsAge = new int; *itsAge=3; itsName = new char[10];} Cat(int age) {itsAge = new int; *itsAge = age; new char[10];} ~Cat () {delete itsAge; itsAge = 0; delete itsName; itsName = 0;} int getAge () const {return *itsAge;} char* getName () {return itsName;} char getChar (unsigned short offset) {return itsName[offset];} void setChar (int x) {itsName[x] = 'J';} void setName (char* NewName) {itsName = NewName;} private: int *itsAge; char *itsName; }; int main () { Cat Furball; Furball.setName("Furball"); cout << Furball.getName() << "\n"; cout << Furball.getChar(0) << "\n"; Furball.setChar(0); cout << Furball.getChar(0) << "\n"; return 0; }
Can anyone help before windows lives up to its name and get's thrown out of my living room one?!!
Cheers :-)