CPP Chapter 10:objects and Classes Part1

Source: Internet
Author: User

10.1 Procedural and object-oriented programming

) in procedural programming, your first concentrate on the procedures you'll follow
In OOP, you concentrate on the objects, thinking about the data and relevant operations

10.2 Abstraction and classes

In computing, abstraction are the crucial step of representing information in terms of their interface with the user

10.2.1 What is a type?

Establishing a basic type, one need to consider:

    1. How much memory was needed for a data object
    2. How the bits in memory is interpreted
    3. What operations, or methods, can is performed on the data object
      For built-in types like int and double, these is built to the compiler. For user-defined types in C + +, you need-to-figure out the sort of information.
10.2.2 Classes in C + +

A class is a vehicle for translating abstraction to a user-defined type. It combines data representation and methods for manipulating the data into one package.

Class specification

Generally, a class specification has both parts:

    1. A class declaration, which describes the data members and the member functions, also called method.
    2. A class method Definitions, which describes how certain class member functions is implemented

Interface

An interface are a shared framework for interactions between-B systems.

C + + programmers often place the form of a class definition in header file and place the class method Implem Entation in source code file
Here's an example of header file of class definition:

// stock00.h -- Stock class interface#ifndef STOCK00_H_INCLUDED#define STOCK00_H_INCLUDED#include <string>class Stock{private:    std::string company;    long shares;    double share_val;    double total_val;    void set_tot() {total_val = shares * share_val;}public:    void acquire(const std::string & co, long n, double pr);    void buy(long num, double price);    void sell(long num, double price);    void update(double price);    void show();};#endif // STOCK00_H_INCLUDED

Noteworthy:

    1. C + + keyword class identifies that is defining the design of a class
    2. The class declaration enables you to declare objects, or instances of the the Stock type
    3. A member function could is defined in the place (like Set_tot ()), or could also is represented by prototype temporarily

The most striking part of class is the binding of data and methods to a single unit

Access control

There is both access control keywords for class members up till now:

    1. Public: Uses the object of the class can access the public part directly
    2. Private: A program could access private members of object member functions

) data Hiding
The insulation of data from direct access by a program (just as keyword private)
) Encapsulation
To separate the details of implementation from the design of the interface (from abstraction)
Data hiding, placing class function definitions in a separate file is examples of encapsulation
Use of private member functions to handle implementation details of that don ' t form the public interface. In another word, public member functions constitutethe public interface, not all functions should is placed in T He public sector, functions like Set_tot () in this example are simple and not relevant to the interface, so it was placed I n the private sector
) keyword Private could also be omitted because default access to class members are private

10.2.3 Implementing class member functions

Member functions has a special characteristics:

    1. Use scope-resolution operator (::) to identify, the class to which the function belongs
    2. Class methods can access the private components of the class
void Buffoon::update();

This shows, which update () is a member function of the Buffoon class, so update () has class scope. Buffoon::update () is the qualified name of the function, update (), on the other hand, is the unqualified name, WH Ich could only being used in class scope
Example of class method implementation file:

Stock00.cpp-Implementing the Stock class//version 00#include <iostream> #include "stock00.h" void Stock::acqui    Re (const Std::string & Co, long N, double pr) {company = CO;  if (n < 0) {Std::cout << "number of shares can ' t is negative;" << company << "shares set        To 0.\n ";    Shares = 0;    } else shares = N;    Share_val = PR; Set_tot ();} void Stock::buy (Long num, double price) {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 ();    }}void Stock::sell (Long num, double price) {using Std::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 ();    }}void stock::update (double price) {share_val = Price; Set_tot ();} void Stock::show () {std::cout << ' Company: ' << company << ' Shares: ' << Shares << ' \ n '    ; Std::cout << "Shares Price: $" << share_val << "Total Worth: $" << total_val << "\ n";}

) any function with a definition of the class declaration becomes an inline function, just like Set_tot () Example. You can write the definition of Set_tot () outside the class declaration just to apply the keyword from inline to it:

class Stock{private:    void set_tot();public:    ...}inline void Stock::set_tot(){    ...}

According to the rewrite rule, doing are the same as writing the definition inside the class declaration.
When you call a member function, it uses the data members of the particular object used to invoke the member function, s Ometimes called sending a message

CPP Chapter 10:objects and Classes Part1

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.