Study Notes for week 3 (3-2)

Source: Internet
Author: User

Day 17th:

Synthesis is used when a class contains other classes.
When a class is an extension of another class, it contains some attributes of another class, and adds some property behavior inheritance.

An object synthesized by other objects cannot be initialized before all its member objects are initialized.
Before constructing the composite class, you should first use the constructor to initialize the table to construct the member class.
Provides all the parameters required to construct the entire synthetic class, including the required parameters for each member class in the constructor class.
You do not need to call the destructor of the member class.
Private Data of the member class cannot be accessed. To access them, you must call the Public member functions.

When one class and another class only have individual member functions or data members, the inherited method is not used at the same time. Use merging when a class contains another or more classes. However, synthetic classes are not just extended versions of other classes. An inherited constructor initializes a table and plays an important role when creating an object. When constructing a composite object, you must ensure that all member components have been constructed.
If a composite class or a member element class contains pointer members, you must compile the overload assignment function.



Day 18th:

Static concatenation is used in most programs without virtual functions. Unless a function pointer or virtual function is used, the C/VC ++ compiler uses static concatenation.
Static concatenation is used to determine the function call during compilation rather than runtime. This method is efficient.

Dynamic concatenation means that function calls can be determined only at runtime. In OOP, a pointer pointing to all classes in the class family is used. As long as the Pointer Points to the base class, it can point to any derived class. The compiler cannot determine which class the Pointer Points to until it is run. The Virtual keyword is added to instruct the compiler to determine which function is called at runtime. The base class is the function of the derived class.

When dynamic concatenation is required using Virtual, VC ++ inserts an indirect pointer in the function call, instead of pointing to the first address of the function, instead, it points to the Virtual function call Address Table (pointing to the function's first address is a direct pointer). This Table is called VTABLE (Virtual Table ). At runtime, the object pointer triggers the offset of the member function in the virtual table to call the correct function.
A pure virtual function does not contain any code. You can assign a value of 0 to the pure virtual function, so that the compiler does not allow users to try to instantiate a base class containing pure virtual functions.

Any base class that contains one or more pure virtual functions is called an abstract base class. It cannot be instantiated. The member function is empty, but it can be used as the hierarchical mode of its derived class. Such a base class provides a framework of classes in all class families. Abstract base classes can be used to list all public data members and member functions, so that derived classes can inherit.

File: // Filename:1206. CPP
# Include
# Include
# Include
# Include
Int dispMenu (void );
Void addParts (void );
Void prParts (void );
Class inventoryItem {
Char partCode [5];
Char descrip [20];
Int num;
Float price;
Public:
Void addToInv (char P [], char D [], int N, float PR)
{
Num = N;
Price = PR;
Strcpy (partCode, P );
Strcpy (descrip, D );
This-> toDisk ();
}
Void toDisk (void)
{
Ofstream invOut;
InvOut. open ("INV. DAT", ios: app );
InvOut. write (char *) this, sizeof (* this ));
}
Void getData (void );
};
Void inventoryItem: getData (void)
{
Ifstream invIn ("INV. DAT ");
While (invIn)
{
InvIn. read (char *) this, sizeof (* this ));
If (invIn. good ())
{
Cout <"part code:" <cout <"description:" <cout <"Quantitly:" <cout <"Price:" <}
}
}
Main ()
{
Int menu;
Do
{
Menu = dispMenu ();
Switch (menu)
{
Case 1: addParts ();
Break;
Case 2: prParts ();
Break;
Case 3: exit (1 );
Default:
Cerr <"*** Enter 1, 2or3 ***";
}
} While (menu! = 3 );
Return 0;
}
Int dispMenu (void)
{
Int ans;
Cout <"function menu :";
Cout <"1. Add record ";
Cout <"2. Show records ";
Cout <"3. Quit ";
Cin> ans;
Return ans;
}
Void addParts (void)
{
Char pc [5];
Char de [20];
Int q;
Float pr;
InventoryItem part;
Cin. ignore ();
Cout <"name :";
Cin. getline (pc, 5 );
Cout <"Note :";
Cin. getline (de, 20 );
Cout <"Quantity :";
Cin> q;
Cout <"Price :";
Cin> pr;
Part. addToInv (pc, de, q, pr );
}
Void prParts (void)
{
InventoryItem part;
Part. getData ();
}
 
The "21 days proficient in C ++" self-study note ends now. It is not complete because it was something more than a year ago, at that time, I only remember that although the title was 21 days, it took me half a year to get started? It seems that I understand it, but I cannot do anything. What is the problem? Raytheon's @ # % $ @! @@#~ ¥ #. However, since I started, I will not give up and continue to learn. The next book I read is "VC technology insider 』. I will make up my notes if I have time.

Related Article

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.