So just to lay it all out, I'm hard core practicing classes right now, I'm going to dev the shittiest shit that was ever shit devved, all night xD.
And to practice some classes I was going to make a simple program that asked the name of your pet, and what animal it was. Every animal would be a class...but would it work to do this:
Um, assuming Dog is a class name not a class instance, no.
If you want more than one copy of something and you don't know in advance how many you'll have, you'll probably want a vector. Or a map.
EDIT If you want a fixed preset limit, use an array.
With a vector, you'd do something like (simple but inefficient example follows...)
#include <vector>
#include <iostream>
...
using namespace std;
...
vector<Dog> dogs;
string input;
cout << "Name?";
cin >> input;
while (input != "q") {
Dog dog;
dog.name = input;
cout << "Name?";
cin >> input;
dogs.push_back(dog);
}
cout << "outputting all dogs" << endl;
//this just says i want an iterator (something that steps through elements) that can iterate through a vector containing Dog[s]. go from beginning to end. (end returns one past the last element)
for (vector<Dog>::iterator i = dogs.begin(); i != dogs.end(); ++i)
cout << i->name << " other stuff: " << i->MyClassOutputFn() << endl;
No, your definition is wrong. SetName wouldn't know what dog to change EDIT: because "dog" wouldn't be in scope (unless dog was global but 'globals are bad')
then it should be
class Dog {
...
setName(input) { name = input }
}
not
{ dog.name = input }
Otherwise you need
class Dog {
setName(input);
};
Dog::setName(input) { name = input }
You can't call dog.setName unless setName is at least declared (prototyped) in Dog's class definition, because setName would then not be part of Dog. If it is part of Dog, then you need to remove the "dog." part.
If dog is not defined in the class at all, you need
SetName(Dog& dog, ... input) { dog.name = input }
because otherwise dog is not in SetName's SCOPE.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Ok, I see its not in its scope, but then why would it be in scope for other accessor functions? (sorry if my noobish questions piss you off)
I have three accessor functions, and I define and declare them the same way. I use Pet.GetName/Age/Weight...Just like the examples in my book...it just doesn't work for GetName...So why is it only for GetName and not the others?
EDIT: nevermind, it doesn't work at all...
The reason I keep trying this is because my book has coding examples that have compiled fine such as:
I was just to about to put that before I scrolled down to your reply
I guess you could use this.member if you were using VC#, this.close() makes your form close, but in C++ i'm sure only this->member would only work.
but who the hell cares about C# :P
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
--------------------------------------------------------------------------------------- And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I got it to work, and I did it how I thought I could do it...I still have no idea what the hell Marauder was saying...I think there was some miscommunication somewhere...either that or I'm just a dumb ass, which is highly possible.