Classes and Inheritance

From Wikiversity

Jump to: navigation, search

Contents

[edit] Creating and Editing Classes

[edit] A Description of Classes

C++ uses classes to implement the object-oriented methodology. In short, object-oriented design is a method for design which models data objects on the idea of a real object, using variables to create traits for the object. For instance, you could have a class that defines a dog:

Dog:

----

char name
int gender
int age
int size
bool healthy

This class has several variables: name, the gender of the dog, the age of the dog, it's size, and whether or not it is healthy.

[edit] How to Make a Dog Class

class Dog
{
 
    public:
        char name[25];
        int gender;
        int age;
        int size;
        bool healthy;
 
};


This class may now be accessed from inside the program. Here is an example.

#include <iostream>
 
class Dog
{
 
    public:
        char name[25];
        int gender;
        int age;
        int size;
        bool healthy;
 
};
 
 
int main(int argc, char *argv)
{
   Dog newDog;
   newDog.gender = 1;
   newDog.age = 3;
   return 0;
}

As you can see, the program creates a dog named newDog, and sets its gender to 1, and its age to 3. However, this method is not advised that let the traits of object be public member, because it allows any class or function to change the variables.

[edit] Creating a Class Containing Methods

To increase the safety of the variables, make them private. This will hide them from everything except the class that they are in. To change these variables, we create methods inside of the class.

C++ comes with two default methods for every class you write. They are both identical to the name of the class, so if your class is called "Dog", the default functions will be Dog() and ~Dog() (notice the tilde at the front). The first function, Dog() is called every time the class is created. The second one, ~Dog() is called when the class is destroyed, or deleted. These methods do not have return values.

Here is our Dog class, with better protection for it's variables, and functions to change them.

#include <iostream>
 
class Dog
{
    private:
        char name[25];
        int gender;
        int age;
        int size;
        bool healthy;
 
    public:
        Dog(int getAge=20, bool getHealthy=true)
        {
           gender = 1;
           age = getAge;
           healthy = getHealthy;
           size = 20;
           setName("Doggy");
        }
 
        //This function sets the dog's name.
        void setName(char getName[25])
        {
           for (int x = 0; x < 25; x++)
           {
              name[x] = getName[x];
           }
           name[25] = '\0';
        }
 
        //This function checks to see that the dog's age is possible, and returns false if it is not.
        bool setAge(int getAge)
        {
           if (getAge >= 0 && getAge < 50)
           {
              age = getAge;
              return true;
           }
           else
           {
              return false;
           }
        }
 
        //Inline function set the size
        inline void setSize(int s) { size = s; }
 
 
};
 
 
int main(int argc, char *argv)
{
   Dog newDog();                // or: Dog newDog(10,false);
   newDog.setName("Puddles");
   newDog.setAge(20);
   return 0;
}

[edit] Where To Go Next

Topics in C++
Beginners Data Structures Advanced
Part of the School of Computer Science