"C + + Primer Plus" 10.2 Abstract and class learning notes

Source: Internet
Author: User

What is the 10.2.1 type?
The basic type completes the following three tasks:
* Determine the amount of memory required by the data object;
* Decide how to interpret bits in memory (long and float occupy the same number of bits in memory, but convert them to numbers differently);
* Determines the actions or methods that can be performed using a data object.
In the case of built-in types, information about the operation is built into the compiler. However, when you define a user-defined type in C + +, you must provide this information yourself. Pay for these old stayput. The power and flexibility to tailor new data types to the actual needs.
10.2.2 classes in C + +
In general, the class specification consists of two parts:
* Class declaration: Describes the data part as a data member and describes the public interface in a way that member functions (called methods) are described.
* Class method definition: Describes how to implement a class member function.
Simply put: The class declaration provides a blueprint for the class, and the method definition provides the details.
class declaration Example:
Program Listing 10.1 stock00.h

//Stock00.h--Stock class interface//version xx#ifndef Stock00_h_#defineStock00_h_#include<string>classStock//class declaration{Private: std::stringCompany ; Longshares; DoubleShare_val; DoubleTotal_val; voidSet_tot () {total_val = shares *Share_val;} Public:    voidAcquire (ConstSTD::string& Co,LongNDoublePR); voidBuyLongNumDoublePrice ); voidSellLongNumDoublePrice ); voidUpdateDoublePrice ); voidshow ();}; //Note semicolon at the end#endif

The stock is the type name of this new class. This declaration allows us to declare a variable of the stock type--called an object or an instance. Each object represents a stock. For example, the following declaration creates two stock objects, named Sally and Solly, respectively:
Stock Sally;
Stock Solly;
Here, the Sally object can represent a company stock that Sally holds.
1. Access control
The keywords private and public are also new, and they describe access control for class members.
Programs that use class objects have direct access to the public part, and only the private members of the object can be accessed through a common member function (or friend function). "Friend function--11 chapter"
A common member function is a bridge between the private members of a program and an object, providing an interface between the object and the program.
preventing programs from accessing data directly is called data hiding.
C + + also provides a third access to the vacant keyword protected. "Protected keyword--13 chapter"
Class design separates the common interfaces from the implementation details as much as possible. A common interface represents an abstract component of a design.
Putting the implementation details together and separating them from the abstraction is called encapsulation. Data should be hidden (placing the data in the private part of the Class) is an encapsulation.
2. Controlling access to Members: public or private
10.2.3 Implementing class member functions
The definition of member functions is very similar to regular function definitions, they have function headers and function bodies, and can have return types and parameters. But they also have two special features:
* When defining a source function, use the parse operator (::) to identify the class to which the function belongs;
* Class methods can access the private components of a class.
For example, the function header of the update member function is as follows:
void Stock::update (double price)
Example: implementation of a class
Program Listing 10.2 Stock00.cpp

//Stock00.cpp--Implementing the Stock class//version xx#include <iostream>#include"stock00.h"voidStock::acquire (ConstSTD::string& Co,LongNDoublePR) { Company=Co; if(N <0) {Std::cout<<"Number of shares can ' t be negative,"<< company <<"shares set to 0.\n"; Shares=0; }    Elseshares=N; Share_val=PR; Set_tot ();}voidStock::buy (LongNumDoublePrice ) {    if(Num <0) {Std::cout<<"Number of shares purchased can ' t be negative."<<"Transaction is aborted.\n"; }    Else{shares+=num; Share_val=Price ;    Set_tot (); }}voidStock::sell (LongNumDoublePrice ) {    usingstd::cout; if(Num <0) {cout<<"Number of shares sold can ' t be negative."<<"Transaction is aborted.\n"; }    Else if(Num >shares) {cout<<"You can ' t sell more than you have!"<<"Transaction is aborted.\n"; }    Else{shares-=num; Share_val=Price ;    Set_tot (); }}voidStock::update (DoublePrice ) {Share_val=Price ; Set_tot ();}voidstock::show () {std::cout<<"Company :"<< Company<<"Shares:"<< shares <<"\ n"<<"Share Price: $"<<Share_val<<"Total Wroth: $"<< Total_val <<"\ n";}

10.2.4 using classes
......
10.2.5 Modify the implementation
......
10.2.6 Summary
The typical class declaration format is as follows:
Class ClassName
{
Private
data member declarations
Public
member function prototypes
}

"C + + Primer Plus" 10.2 Abstract and class learning notes

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.