Pi Monkeh Dot Net
main
Pi Monkeh!
c++
page 1    page 2     page 3    page 4    page 5    page 6    page 7    page 8

normal    vulgar

now you've learned the basics, let's move on to some fun stuff (pointers are the devil's minions, we'll try those later).

code:
#include <iostream>

int personcount = 0;

class person
{
public:
    person();
    person(char *);
    ~person();
    void say(char*);
private:
    char *person_name;
};

this is a class. it's your own format for an object.

public:
all the stuff below public is, well, public. anything public can be changed freely outside the class, well you'll understand later.

person();
this is the class constructor. it's the function for when a person object is created. right now, nothing happens.

person(char*);
this is a second constructor, in the case that the user makes a person with a char* argument (this is essentially an array of char), this constructor will be the one to happen.

~person();
this is the destructor, it stands out because of the tilde in front of it. when a person is destroyed (usually at a program's end), the destructor happens.

void
the type void is used generally for functions, ones that don't have a return value. if you're not doing any math in the function or really dealing with numbers, void is a safe pick.

private:
private members can only be changed by members of the same class. it won't make too much sense right now, so skip it.

;
class declarations end with semicolons. good to know.

code:
person::person()
{
    personcount++;
}
person::person(char *name)
{
    personcount++;
    person_name = name;
    std::cout << "new person named " << person_name << " made!\n";
    std::cout << "population: " << personcount;
}
person::~person()
{
    personcount--;
    std::cout << "one less person in the world!\n";
}
void person::say(char *phrase)
{
    std::cout << person_name << " says: " << phrase;
}
int main()
{
    person person1;
    person person2("dave");
    person1.say("hi");
}

that's a ton of new stuff, so let's get going.

person::person()
it's better form to make the bodies of your class functions outside the class, it's even better to do so in another file. but that can wait. to make a function outside the class, give the type (if it's not a constructor or destructor) followed by classname::function(arguments). you should recognize the double colon from the standard namespace, they are used in the same way.

person person1; person person2("dave");
here i demonstrate both constructors for niggers. the first one does nothing but create a person and silently increase the running tab of them, the second one does the same thing and tells us what it's doing and how many persons there are.

person.say("hi");
the new person1 said "hi" before dying in the destructor storm of '86. let's see what's actually happening. now that person has been created, you can access the person members that it inherits with the . symbol, followed by the function or variable. in this code, we get person to use the say() function, and pass "hi" as an argument to it. the program will tell us what person1 said, and then it's finished.

page 1    page 2     page 3    page 4     page 5    page 6    page 7    page 8