C + + Learning Diary (i) ———— classes and objects

Source: Internet
Author: User

C + + Expedition package (top) Notes

All the content is a lecture note, the classroom is willing to video as follows:

C + + Expedition package (Part One)-MU class network
http://www.imooc.com/learn/382

Classes and objects

1 What is a class and what is an object?

Objects are specific things, and classes are abstracted from objects for different purposes, so the same thing can be abstracted into different classes.

As an example:

A dog, is a specific thing, is an object

When we raise a large group of dogs, it does not mean one, but a group, that corresponds to become a class , will create a file for the dog, and clear certain information about the dog, such as: Age, Variety, mastered skills (called, run) and so on,

By taking this information out alone, you can define a class.

It is important to note that the "attribute" and "method" correspond to the defined information, not all the information of the dog, but only a part, for example, the dog's properties are not only age and category, but also the male and female, wagging the tail and not wagging the tail, right! The dog's method also not only runs and barks, it can also bite, can also sahuan son, rolls to roll to go and so on, therefore, the class abstracts out the correspondence information, depends on the goal, the same class abstract information may be completely different.

Definition of class: data member, member function

2. Access Qualifiers for classes

One more example.

Some of the functions or details of a television set are displayed, some are not described, but they do not mean that they do not exist, but are hidden.

Encapsulate the details of the practice and expose it to the user's concerns, which is the meaning of encapsulation.

How do you expose the information you want to expose and hide the information you want to hide?

This is the role of the Access qualifier in C + +! If you want to be exposed when you define it, use "public" to modify it, and if you want to be hidden, use "prevate" to modify it.

Instantiation of an object

In C + +, a class is a template, and object instantiation is the process by which a computer manufactures multiple objects based on a class.

1. Instantiation of objects in two ways

One is: instantiation from the stack, and one that is instantiated from the heap. Below, respectively,

Instantiate from Stack:

Instantiate from Heap:

Use the following definition: If you want to define an object, then use "TV" with a pointer to the memory (this piece of memory is requested by the "new" operator), this memory is used as the future "TV" object to use; Of course, you can also request an array, the array has 20 elements , if you are requesting an object from the stack (that is, the example above) does not need to be released again, the system will automatically reclaim the memory consumed. But if you are applying from the heap, be sure to release this memory! (Just use "delete function to release")

What was the purpose of the object after it was set up? is to achieve the desired requirements by accessing the various members of the object

So how do you access these object members?

2. Access methods for object Members :

For single-Object members

With different instantiation methods, the resulting object accesses data members and member functions in different ways. The way you access them is also different.

After instantiating an object through a stack, you can use "." If you want to access its object. To assign the corresponding value, you can also use the "." To access its member function, which is the way to access the object after it has been defined with a stack.

If the object is instantiated through a heap, as shown in, define a pointer p,p through such a symbol "--" to access, run out remember to delete.

Log Group Object members (for example, heap)

If we want to access "type", we call its member function, then we need a for loop to make the assignment and call separately. If it's from the heap, remember to release the memory when you're done

C + + Learning Diary (i) ———— classes and objects

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.