Object-oriented basics: function overloading, constructor, and overload Constructor

Source: Internet
Author: User

Object-oriented basics: function overloading, constructor, and overload Constructor

Function Overloading

Definition:

1. Two or more functions are in the same class;

2. The function name is the same;

3. The parameter list is different;

Example:

// Class file Aclass A {// The following three are member function void funA () {System. out. println ("funA function without Parameters");} void funA (int I) {// return value with void being null. funA is the function name and the parameter System is enclosed in brackets. out. println ("funA function with Integer Parameters");} void funA (int I, double d) {System. out. println ("funA function with two parameters ");}}

  

// TestAclass testA {public static void main (String args []) {A = new A (); // create a class A Object A. funA (); // call function a of object. funA (10);. funA (1, 2.5 );}}

 

Constructor: Class Name ()

The constructor does not belong to a member function, but does not return a value. The constructor name must be the same as the class name.

When the compiler compiles the source file, it checks whether there is a constructor. If no constructor exists, the compiler automatically adds a constructor with no parameters and the function body is empty.

// Class file Aclass A {// The constructor has no return value. The constructor name must be the same as the class name A () {System. out. println ("I Am a constructor ");}}
// TestAclass testA {public static void main (String args []) {A = new ();}}

The call of member functions uses objects..Function Name

New + constructor is used to call constructor.

Role of constructor:

1. When we call the constructor, this class object will be generated.

2. You can manually define the constructor to assign initial values to member variables to facilitate the input of object parameters. Example:

Class Person {Person () {} String name; int age;
Person (String n, int a) {// The overloaded name of the constructor = n; age = ;}}

 

Class testPerson {public static void main (String args []) {// Person p1 = new Person (); // p1.name = "zhangsan"; // p1.age = 10; simplified to the following code: Person p1 = new Person ("zhangsan", 10); System. out. println ("p1 name is" + p1.name + ", age is" + p1.age );}}

When the class file already has a custom constructor, during compilation, the compiler will not add a constructor without parameters, and then pass new () an error is reported when a new object is generated.

 


What is the syntax of the overloaded constructor?

Class
{
Public A (): this ("aaa ")
{
}
Public A (string name)
{
}
}
Call of two constructor methods in a class...
Class B:
{
Public B (): base ("B ")
{
}
}
Subclass calls the constructor of the parent class

What is a constructor? Can constructors be overloaded? Why?

A member function with the same name as the class name is a constructor. The constructor cannot return values or even return statements. It indicates that a constructor with a returned value is incorrect, and the address of the constructor is also incorrect.
If a class has constructor, the constructor initializes each class-type object in the program before it is used (for more information about initialization, see "initialize with special member functions" later in this chapter ").

Constructors are called at the object creation point. The created object can be:

* Global Object (File range or external link ).

* Local variables in a function or small closed block.

* Dynamic objects created using the new operator. The new operation assigns an object in the program's heap or free storage area.

* Temporary objects created by explicitly calling Constructors (see "temporary objects" after this chapter ").

* Temporary objects created because the compiler implicitly calls the constructor (For details, refer to "temporary objects" after this chapter ").

* Data members of other classes. When creating an object of the class type, if this type is composed of other class type variables, it will cause the creation of each object in the class.

* A base-class sub-object of a class. When you create an object of the derived class type, the base class component is created.

Role of Constructors

A constructor executes various tasks, but for programmers, these tasks are invisible and you don't even have to write any code for the constructor. These tasks are related to creating a complete and correct class object instance.

In ms c ++ (also in many other C ++), a constructor:

* Initialize the virtual base pointer (vbptr) of the object ). If the class is derived from the virtual base class, this step is required.

* The base class and member constructor are called in the order described.

* Initialize the virtual function pointer (vfptr) of the object ). If this class has or inherits virtual functions, You need to execute this step. The virtual function pointer points to the virtual function table (v-table) of the class ), and make the call of the virtual function correct binding with the code ).

* Execute the optional code in the constructor body.

After the constructor ends, the allocated memory is an object of the given class type. Because the constructor executes these steps, the "late binding" form of the virtual function can be solved at the call point of the virtual function, constructors also need to construct base classes and composite objects (objects used as data members). Late binding is a mechanism for C ++ to implement the multi-state Behavior of objects.

Description of the constructor rules

The constructor has the same name. As long as you follow the rules of the overload function (for details, see chapter 12th "overload"), you can describe multiple constructors.

Syntax

Class Name (parameter description table opt) cv-modifier table opt

C ++ defines two types of constructor, default and copy constructor. As shown in Table 11.2.

Table 11.2 default and copy constructors

Objective of type parameters of Constructor
The default constructor can call a non-parametric call to construct a default object of the class type.
The copy constructor accepts the reference of the same type as the object of the unique parameter copy class type.

The default constructor can be called without parameters. However, you can specify a default constructor with parameters as long as all parameters have default values. Similarly, the copy constructor must accept the reference of the same class type as the unique parameter. But more parameters can be provided, as long as subsequent parameters have default values.

If you do not provide any constructor, the compiler will try to generate a default constructor. Similarly, if you do not provide a copy constructor, the compiler will try to generate one. The constructor generated by the compiler is regarded as a public member function. If you describe a copy constructor, and its first parameter is an object rather than a reference, an error will occur.

The default constructor generated by the compiler creates objects (initialize vftables and vbtables, as described earlier) and calls the default constructor of the base class and members, but does not take any other action. The constructor of the base class and member can be accessed as long as it exists and is unambiguous.

The copy constructor generated by the compiler creates an object and uses a member COPY method to copy the content of the object to be copied. If the remaining full text of the base class or member...>

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.