C + + classes and inheritance

Source: Internet
Author: User


First talk about the class

If you're writing an online game, there's a lot of monsters in this game, right? The monster that the novice wants to fight is chicken, dog, pig, fish.
Online games are to be updated maybe you just come out of the game to play chicken and players are practicing to the full level you need to continue to attract players to hit the door on the introduction of new monster dog AH pig ah fish ah ...

The obvious object-oriented approach is to write a class for each monster (class chicken, Class dog, Classpig, Class fish ) to create a corresponding object delete object when the monster is refreshed.

Then you may have to ask, why use the class ah? What is the class used for? Let's explain the following:

First of all, there are three chickens , called big chickens, chicken chicks.

We found chicken chicks in big chickens have the same attributes, like height, weight, age .

Big chicken Chicken chick still have the same action ah eat Sleep

We pull these same attributes and the same action out to form a class

Class Chicken ()

{

int height;

int weight;

int age;

void Eat ()
{
What did the chicken do when it was written here?
}
void Sleep ()
{
What did the chickens do when they slept here?
}

}
We generate objects from the class and then the objects are differentiated because of their different attributes (because big chickens are bigger than chickens)

So, a class is a collection of properties and actions.

Objects use classes as templates to generate and then assign their own properties to the object

Talk about inheritance.

I said before hitting the door. Have these classes Class chicken, Class dog, Class pig, Class fish

We've found these classes to have something in common. They're all animals, and they all have an age, height, weight , sleep .

So we can be lazy and just write a class of animals ?

The answer is no, man. chickens can lay eggs, dogs don't have chickens . Although there are properties and movements of animals, there are different properties and actions.

But programmers will be lazy (should say that programmers follow the "reusable Principle ") try not to write repetitive code in the chicken to write the action of eating in the dog to write again? No, it's too much trouble.

So they created the inheritance

Now there's an animal class

Class Animal ()
{
int height;
int weight;
int age;
void Eat ()
{
To write about what to do here.
}
void Sleep ()
{
Here, write about what to do in bed.
}
}
  

And then we write a class chicken in the way we inherited it.

Class Chicken: Animal
{
void Eggs ()
{
Here, write about what to do with your eggs.
}

}
 

Class Chicken: Animal means chicken inherits from animal
Then the chickens automatically have the properties and movements of the animals. There is also an access modifier to restrict what you can inherit from chickens (if you don't want the chickens to sleep, set the sleeping action to private) here, I'm not going to go into specifics. The access modifier is what restricts access and inheritance. Look at the information.
That's the inheritance.

C + + classes and inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.