can anyone suggest a website/s that teaches how to write a class?
can anyone suggest a website/s that teaches how to write a class?
I can help. I'm only good at defining, though, not usage.
A class is an object very much like struct. However, it is much more useful. Using class, you can make an int function, however, thats pointless since int is already defined.
First, think of what type you want. We'll use Cat.
There, class Cat is made. But before implementing it, you need to build it.Code:class Cat { \\define class Cat private: \\Every thing here is it 'private' area. Meaning no one \\allowed but it. char name[50]; int height; int weight; int lengthwt; \\legnth with tail; int lengthwnt; \\length with no tail :D public: \\The world can see all this anytime, its what the cat \\and if it wants to eat, it does it, meow, same, etc. Cat(); \\Give birth to the cat ~Cat(); \\Kill the cat void eat(); void poop(); void meow(); }; \\note the ';'
Thats the class, built!Code:Cat::Cat() { char name[50]; int height; int weight; int lengthwt; \\legnth with tail; int lengthwnt; \\length with no tail :D } Cat::~Cat() { } void Cat::eat() { statement; } void Cat::poop() { statement, like weight -= 100; } void Cat::meow() { statement; }
To use them, you do something like
Cat Cat1 = ("Joe", 5, 9, 3);
But i can't get it to work.
Hope i helped!
This war, like the next war, is a war to end war.
you can give definitions of class member functions in class itself like
Constructor Cat() function with same name as that of class is called when you create an object of type Cat in your code,Code:class Cat { private: char name[50]; int height; int weight; int lengthwt; \\legnth with tail; int lengthwnt; \\length with no tail public: Cat() //default constructor { strcpy(name,"mycat"); height=30; weight=90; } void eat() //member function { cout<<"cat is eating "<<endl; } };
and function ~Cat()
is called when scope of that object ends,
both these functions are not very necessary to write ,if you omit them compiler will write itself for you but it would not inialize your member variables,like i have done in above code...
Liberty's TYC++ is online in a slightly outdated version. The discussions regarding classes are reasonable, though not to everybody's taste. The cost however is free, and therefore unbeatable. Here is one link to same.
http://www.stud.fim.ntnu.no/~oystesk/CPP/index.htm
how it can work,there is no default constructor which takes "Joe",5,9,3 as parameters,but you can write one urselfOriginally posted by Blizzarddog
To use them, you do something like
Cat Cat1 = ("Joe", 5, 9, 3);
But i can't get it to work.
Hope i helped!
like altering above it would be used as
Now you can construct objects in main likeCode:Cat() //default constructor { strcpy(name,"mycat"); height=30; weight=90; } Cat(char *n,int h=10,int w=50) //constructor with parameters { strcpy(name,n); height=h; weight=w; }
Code:int main() { Cat c1; //c1 is created with default constructor Cat c2("bar"); Cat c3("doop",20,30); //constructor with parameters is called return 0; }
Two men looked out from Prison Bars,One saw the mud,the other saw stars.
oh, okay, i'll try it and give you a ring.
Uhm, do i HAVE to use strcopy()?
This war, like the next war, is a war to end war.
Sorry to use someone else board for help, but can i use, like
if (player.y == enemy.y && player.x == enemy.x)
by any chance with a class?
How would i go about making Cat c4("Plastique", 10, 50); inside a program?
This war, like the next war, is a war to end war.
if x and y are something other than Cstyle strings then you can probably use the == operator on them for comparison. Or, more precisely, if x and y are simple variables like int, float, double, char (not the best idea to use == on float or double, but it's not illegal at least) or if the == operator is overloaded for a user defined type, then you can use == like that.
If the object containing the x and y members isn't a pointer, then the dot operator as you use it is appropriate. If the objects are pointers, then you would need the arrow (->) operator to reference the desired members.
Last edited by elad; 04-10-2003 at 10:47 AM.
I mean, i want to make a text rpg with masters, enemies, magics, runes, people, etc. So if i have class Master, and then make master(daddadadad); with an int x and int y, could i use if (player.y == master.y && player.x == master.x) do this. Is that possible? Or do i have to do a if if (player.y ==bob.y && player.x == bob.x) and do that with all 679 masters?
See, i have a hard time believing that it can decipher which masters which just by the x y coordinates, thats why i was wondering.
Last edited by Blizzarddog; 04-10-2003 at 11:02 AM.
This war, like the next war, is a war to end war.
if player and master are the names of a class, then you can't compare members like that. You can only use objects (aka instances of classes).
Code://declare user types class player { public: int age; }; class master { public: int age; }; //declare instances of user types player One; master Two; //initialze members somehow, NOT like this in your code, probably One.age = 21; Two.age = 23; //use the age members to compare the two objects if(One.age == Two.age) //do something else //do something else
But i still have to user if (one.y == master.y) for every thing. Isn't there some easier way??
This war, like the next war, is a war to end war.
and none of the stuff on this page compiles
This war, like the next war, is a war to end war.
You mean like looping through a container of objects checking all items in one container versus a given value so you don't have to write it out 679 times by hand?
Providing compilable code wasn't the purpose I suspect. Speaking only for myself I was attempting to provide example/explanations, not runable code. If you want a compilable example it can be provided, but thought the above was enough as is.
Yes, thats what i meant.Originally posted by elad
You mean like looping through a container of objects checking all items in one container versus a given value so you don't have to write it out 679 times by hand?
This war, like the next war, is a war to end war.
oh, and, uh, mine doesn't even compile.
This war, like the next war, is a war to end war.