9 classes and inheritance

Upload: yashbanwani2014

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 9 Classes and Inheritance

    1/6

    Classes and Inheritance 1

    Classes and Inheritance

    Creating and Editing Classes

    A Description of ClassesC++ 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.

    How to Make a Dog Class

    classDog

    {

    public:

    charname[25];

    intgender;

    intage;

    intsize;

    boolhealthy;

    };

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

    #include

    classDog

    {

    public:

    charname[25];

    intgender;

    intage;

    intsize;

    boolhealthy;

    http://en.wikiversity.org/w/index.php?title=Object_Oriented_Software_Design
  • 8/11/2019 9 Classes and Inheritance

    2/6

    Classes and Inheritance 2

    };

    intmain(intargc, char*argv)

    {

    Dog newDog;

    newDog.gender =1;

    newDog.age =3;

    return0;

    }

    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.

    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 its variables, and functions to change them.

    #include

    classDog

    {

    private:

    charname[25];

    intgender;

    intage;

    intsize;

    boolhealthy;

    public:

    Dog(intgetAge=20, boolgetHealthy=true)

    {

    gender =1;

    age =getAge;

    healthy =getHealthy;

    size =20;

    setName("Doggy");

    }

    //This function sets the dog's name.

    voidsetName(chargetName[25])

  • 8/11/2019 9 Classes and Inheritance

    3/6

    Classes and Inheritance 3

    {

    for(intx =0; x

  • 8/11/2019 9 Classes and Inheritance

    4/6

    Classes and Inheritance 4

    intgender;

    intage;

    intsize;

    boolhealthy;

    public:

    Dog(intgetAge=20, boolgetHealthy=true);

    //This function sets the dog's name.

    voidsetName(chargetName[25]);

    //This function checks to see that the dog's age is possible,

    and returns falseifit is not.

    boolsetAge(intgetAge);

    //Inline function set the size inlinevoidsetSize(ints);

    };

    As you can see, there is no code but the class declaration in there.

    dog.cpp :

    #include

    #include

    Dog::Dog(intgetAge=20, boolgetHealthy=true)

    {

    gender =1;

    age =getAge;

    healthy =getHealthy;

    size =20;

    setName("Doggy");

    }

    //This function sets the dog's name.

    voidDog::setName(chargetName[25])

    {

    for(intx =0; x

  • 8/11/2019 9 Classes and Inheritance

    5/6

    Classes and Inheritance 5

    boolDog::setAge(intgetAge)

    {

    if(getAge >=0&&getAge

  • 8/11/2019 9 Classes and Inheritance

    6/6

    Article Sources and Contributors 6

    Article Sources and ContributorsClasses and Inheritance Source: http://en.wikiversity.org/w/index.php?oldid=796010 Contributors: DBChristensen, Dominis, Gbaor, Hampshire2004, MichaelFrey, Remi0o, Steaxauce,

    Xentalion, 8 anonymous edits

    LicenseCreative Commons Attribution-Share Alike 3.0 Unported

    //creativecommons.org/licenses/by-sa/3.0/