Вы находитесь на странице: 1из 6

Classes and Inheritance

1
Classes and Inheritance
Creating and Editing Classes
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.
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;
Classes and Inheritance
2
};
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.
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 <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])
Classes and Inheritance
3
{
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;
}
Creating a class using header files
You can also create a class using header files, lets use the dog class from above as an example.
dog.h :
#include <iostream>
class Dog
{
private:
char name[25];
Classes and Inheritance
4
int gender;
int age;
int size;
bool healthy;
public:
Dog(int getAge=20, bool getHealthy=true);
//This function sets the dog's name.
void setName(char getName[25]);
//This function checks to see that the dog's age is possible,
and returns false if it is not.
bool setAge(int getAge);
//Inline function set the size
inline void setSize(int s);
};
As you can see, there is no code but the class declaration in there.
dog.cpp :
#include <dog.h>
#include <iostream>
Dog::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 Dog::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.
Classes and Inheritance
5
bool Dog::setAge(int getAge)
{
if (getAge >= 0 && getAge < 50)
{
age = getAge;
return true;
}
else
{
return false;
}
}
//Inline function set the size
inline void Dog::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;
}
As you can see, there is no class declaration but all of the function definitions for the class are preceeded by Dog::
and there is code
Where To Go Next
Topics in C++
Beginners Data Structures Advanced
Lesson 1: Introduction to C++ Lesson 8: Pointers Lesson 12: The STL
Lesson 2: Variables and User Input Lesson 9: Classes and Inheritance Lesson 13: STL Algorithms
Lesson 3: Simple Math Lesson 10: Templates 1
Lesson 4: Conditional Statements Lesson 11: Templates 2
Lesson 5: Loops
Lesson 6: Functions and Recursion
Lesson 7: More Functions
Part of the School of Computer Science
Article Sources and Contributors
6
Article Sources and Contributors
Classes and Inheritance Source: http://en.wikiversity.org/w/index.php?oldid=796010 Contributors: DBChristensen, Dominis, Gbaor, Hampshire2004, MichaelFrey, Remi0o, Steaxauce,
Xentalion, 8 anonymous edits
License
Creative Commons Attribution-Share Alike 3.0 Unported
//creativecommons.org/licenses/by-sa/3.0/

Вам также может понравиться