are three examples of initializing objects with unknown objects.
# Include Iostream > Using Namespace STD;Class Internet{ Public :Internet ( Char * Name, Char * Address){ Cout Strcpy (Internet: Name, name );}Internet (Internet temp){ Cout Strcpy (Internet: name, temp. Name ); CIN . Get ();}~ Internet (){ Cout } Public : Char Name [20]; Char Address [20];}; Void Main (){Internet A = Internet ("China Software Development Lab", "www.cndev-lab.com "); Cout CIN . Get ();}
The running result
I. My question is about initializing C ++ class members. I have seen a lot of such code (including those in your bar ):Csomeclass: csomeclass (){X = 0;Y = 1;}In other places, it is written as follows:Csomeclass: csomeclass (): x (0), y (1){}Some of my programmers say the second method is better, but they don't know why. Can you tell me the differences between the two types of member initialization methods?
AnswerTechnically, your programmers are right
Summary:
First, the constructor is called in the inheritance order. If there is a parameter, the parameter is passed. If there is no parameter, the default constructor is called.
Then, the constructor is called according to the sequence defined by data members (data members of the class type ).
# Include
Using STD: cout;Using STD: Endl;
Class B1{Public:B1 (int
Definition of copy constructor in C + +:
The type of one parameter is the constructor of its class type is a copy constructor.As shown below:
X::X (const x X);
Y::y (const y Y, int =0);
Can be a multiple-parameter form, but its second, successor parameter has a default value
Second, the application of the copy
Reprinted from: http://www.cnblogs.com/BlueTzar/articles/1223313.html
For common objects, copying between them is very simple, for example:Int A = 88;Int B =;Different from common objects, class objects have complicated internal structures and various member variables. The following is a simple example of copying a class object.# Include Using namespace STD;
Class cexample {
PRIVATE:
Int;
Public:
Cexample (int B)
{A = B ;}
Void show ()
{
Cout }
};
Int main ()
{
Cexample A (100 );
Cexample B =
> When compiler automatically generates default constructor
Speaking of the default constructor in C ++, Let's see when the default constructor will be generated.
1. A class member has a member as a class object, and the member's class contains the default constructor.
Th
What is a constructor function
Constructors build objects from dust. They turn a pile of arbitrary bits into a living object.
List x, List x () and list X (Bar ())
List x, declaring an object of type list named X, List X (), declaring a function named X, returning the list type; "LLVM hint" Empty parenthess interpreted as a function declartion "" List x (Bar ()), declares a function called X, contains a parameter of type Bar, return
and change the overall code to the following form # include Well, now we can locate the error. The most important problem is that the func function returns the result. According to the experiment above, when the function regards the object as the return value and assigns it to another external variable, the copy constructor of the class is called. In this case, the parameter of the copy constructor is "con
.
public static class Logger
{
private static int lognumber = 0;
static public void initializelogging ()
{
Console.WriteLine ("Log Initialization");
}
static public void Closelog ()
{
Console.WriteLine ("Log Off");
}
static public void Logmsg (String msg)
{
Console.WriteLine ("Log number is:" + Lognumber + ":" + msg);
}
On the client side, you cannot create an instance of logger, only by calling the method through the class name. static method name.
static void Main (string
The first type is that the class member has a member as a class object and the member's class contains the default constructor. Then, the C ++ compiler will help you generate a default constructor for this class, it is used to call the constructor of its member object to complete the initialization structure of the mem
Objective-C magic path [9-class constructor and member variable scope, and variables], objective-c9-
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
InC ++Medium,ConstructorIt is a special function called when the component object is used to initialize the object, so that the object can be in a reasonable State before it is used. However, the design of constructor functions is not perfect, and there are even some unreasonable features. For example, the condition that the constructor name and class name are the same is limited. These features are worth n
In this section, let's see when the compiler declares and defines a default constructor. It also explains why it does not seem to make sense to declare the meaning of a default constructor even if you do not give the definition.Q: When does the compiler implicitly define a default constructor.A: An implicitly declared default constructor implicitly defines a defa
1. member functions that cannot be automatically inherited
Constructor (including copy constructor)
Destructor= Operator
Ii. Inheritance and constructor
The constructor of the base class is not inherited. You must declare your own constructor In the derived class.When decla
C ++ provides the keyword explicit to prevent implicit conversions that should not be allowed by conversion constructors.
Constructors declared as explicit cannot be used in implicit conversions.
In C ++, the constructor of a parameter assumes two roles. 1 is a constructor 2 is a default and implicit type conversio
copy transformation function is as follows:
Classa: classa (const classa orig ):
Ia (orig. Ia), Pb (orig. Pb)
{}
// Write a classb with its own copy constructor, A classc. One of its member variables is the classb object, classc C; classc D (c); facts prove everything
See Pb (orig. PB). The pointer Pb is the pointer orig. A copy of Pb, But they point to the s
the constructor in the base class does not contain a parameterless constructor, the constructors in the derived class must all specify the base class constructor for the call, or else an errorFor example?
12345678910111213141516171819202122
public class MyBaseClass{public MyBaseClass(int i){Console.WriteLine("我是基类带一个参数的构造函数");}}public class MyDe
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.