Class-3C ++ Primer Study Notes

Source: Internet
Author: User

[Cpp]
Class Sale_item
{
Public:
// Use the default string constructor to initialize isbn.
Sale_item (): price (0.0 ){}
 
Sale_item (const string & book): isbn (book), price (0.0 ){}
 
// We recommend that you use the default real parameters to merge the above two constructors. As follows:
// Sale_item (const string & book = ""): isbn (book), price (0.0 ){}
Private:
String isbn;
Double price;
};

Default constructor for merging
If no constructor is defined for a class, the compiler automatically generates a default constructor.
However, if only one constructor has been defined, the compiler will not generate any more default constructor.
Reason: because a class needs to control the initialization of an object under certain circumstances, the class is likely to need control in all circumstances.

The initialization rules of the merged default constructor are the same as those of the variable.
Class type, which is initialized using their Default constructors
Built-in and composite types, such as pointers and arrays, are initialized only for objects defined in the global scope,
If the local scope is defined, the built-in and composite types are not initialized and are in the undefined state.

Normally, you should define a default constructor.

Suppose: NoDefault is a class, which has a constructor that accepts a string argument,
In this case, the compiler does not generate the default constructor.
So,
1. When I define A Class A with A member of the NoDefault type,
All constructors of A must pass an initial string to initialize members of the NoDefault type.

2. When I define A Class A with A member of the NoDefault type,
The compiler will not generate the default constructor of. Only display definitions by yourself

3. The NoDefault type cannot be used as the element type for dynamically allocating arrays.
[Cpp]
Int * iArr = new int [10];
 
// The preceding method is feasible. The following method reports an error.
NoDefault * arr = new NoDefault [10];

4. The NoDefault Type Static allocation array must also provide display initialization for each element.
5. If there is a container that saves the NoDefault object, such as: vector,
Then, the constructor of the container not only provides the container size, but also provides the element initialization type.


[Cpp]
// This is a function declaration. The return type of the function is Sales_item.
Sales_item myObj ();
 
//
Sales_item myObj2;
 
// Create a Sales_item object and use the default constructor to initialize it.
Sales_item myObj3 = new Sales_item ();


Implicit type conversion
[Cpp]
Class Sales_item
{
Public:
 
Sales_item (const string & book = ""): isbn (book), units_sold (0), revenue (0.0 ){}
// Explicit can only be used. The constructor declaration inside the class does not need to be repeated outside the class definition body.
Explicit Sales_item (istream & is );
Sales_item ();
 
Bool same_isbn (Sales_item item)
{
Return item. isbn = isbn;
}
Private:
String isbn;
Int units_sold;
Double revenue;
};
// Error: explicit can only be declared in the constructor of the class.
Explicit Sales_item: Sales_item (istream & is)
{
/*...*/
}
 
/*...*/
String null_book = "9-999-9999-9 ";
Sales_item item_a = Sales_item ("1-111-1111-1 ");
 
// The following will call the constructor implicitly to generate a Sales_item object for comparison.
// But this implicit conversion is not necessarily what we really want.
// To prevent this implicit conversion, you can use the explicit keyword before the constructor.
Item_a.same_isbn (null_book );
 
// We recommend that you use the following method to avoid errors.
Item_a.same_isbn (Sales_item (null_book ));


[Cpp]
// No constructor is defined, and all data members are public classes,
// You can use the same method as the array element to initialize members.
// However, we recommend that you use constructors.
Struct Data
{
Int ival;
Char * ptr;
};
Data val1 = {0, 0 };


Youyuan allows a class to access and authorize its own non-public members to the specified class or function.
It can only appear within the class definition. Generally, all the membership declarations are put into groups at the beginning or end of the class definition.

[Cpp]
Class Screen
{
Public:
Typedef string: size_type index;
Private:
Int height;
Int width;
 
// Youyuan is not a Screen member,
// It can appear anywhere in the Screen class definition body.
// And is not affected by Access Control (private or public)
Friend class Window_Mgr;
 
// Set the member functions of other classes to youyuan
Friend Window_Mgr & Window_Mgr: relocate (Screen: index, Screen: index, Screen &);
};
Class Window_Mgr
{
Public:
Window_Mgr & relocate (Screen: index r, Screen: index c, Screen & s)
{
S. height + = r;
S. width + = c;
Return * this;
}
Private:
 
};


To set the member function of a class as a friend, this class must be defined first.
To set a class or a non-member function as a friend, you do not need to declare it in advance.
(P398, the example seems to be incorrect)

Global Objects will damage encapsulation, while static members defined in the class can maintain good encapsulation.
Static data member, associated with the class, rather than the object of the class.
The static member function does not have this parameter.
[Cpp]
Class Account
{
Public:
Void applyint () {amount + = amount * interestRate ;}
Static double rate () {return interestRate ;}
Static void rate (double );

Private:
String owner;
Double amount;
Static double interestRate;
Static double initRate ();
 
// Exception. The static data member of the const type can be initialized in the class definition body.
// However, even so, it must be defined externally.
Static const int period = 30;
};
// External definition, but Initialization is not required.
Const int Account: period;
 
// The internal statement is static.
// You do not need to specify static
// Static cannot be declared as const or virtual function
// Declared as const is the object that promises not to modify the function,
// However, the static function does not belong to any object. It is only associated with the class.
Void Account: rate (double newRate)
{
InterestRate = newRate;
}
 
// Static data member, which must be defined externally in the class definition body (exactly once)
// Once the Member name appears, the definition of the static member is in the class scope.
// Therefore, you can directly use the private member function initRate
Double Account: interestRate = initRate ();
Account ac1;
Account * ac2 = & ac1;
Double rate;
Rate = ac1.rate ();
Rate = ac2-> rate ();
Rate = Account: rate ();

[Cpp]
Class Bar
{
Public:
Private:
// The type of the static data member, which can be the class type of the member.
Static Bar mem1;
Bar * mem2;
 
// Error
Bar mem3;
}; Www.2cto.com
Class Screen
{
Public:
Screen & clear (char = bkground );
Private:
// Static data member, which can be used as the default real parameter.
Static const char bkground = '#';
};


 

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.