Class learning in C ++

Source: Internet
Author: User

Let's first understand what is a class?
Class is the encapsulation of logically related functions and data.
Below is my summary of the class:

Knowing a class starts with its members:
The class has
1. Data Member-attribute
2. member functions-methods

Class member functions are mainly used to operate data members in the class.

Let's take a look at the default constructor with parameters:
There are three ways to assign an initial value using the constructor:

1. Use constructor to initialize in class
For example:
Clock (int h = 0, int m = 0, int s = 0)
{
......
}
 
For example, when a constructor is called without parameters, the default value is used.
Clock C4

2. Pass parameters to the constructor outside the class (pass real parameters ).
For example, when:
Clockmyclock (9, 30, 45)
Myclock. showtime ();
At this time, clock (9,30, 45) is called when an object is created)

3. Use the constructor to initialize the newly created object.
Clock myclock = clock (9, 30, 45)
Clock (9, 30, 45) is a constant of the clock type.

The class introduction is almost the same. What is the relationship between the class and the class? -- Inherit (Child class inherits parent class)


So what is the role of the defined class? Class is the instantiation of objects. Like the relationship between the mold and the casting, let's take a look at the object below:

After an object is created, the same message is received by different types of objects, resulting in different behaviors-polymorphism is the relationship between classes and objects.

Understanding the entire process of object-oriented makes me deeply understand this sentence. Instead of making the same wheel, the process of building a class is to build a model to avoid duplication, let us make full use of our work efficiency! The summary of learning (the process of abstract classes) is the same. The trunk has a leaf, and the rest is the addition of leaves. This process is inheritance. polymorphism depends on our knowledge, then it is used flexibly.

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.