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:
- How much memory was needed for a data object
- How the bits in memory is interpreted
- 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:
- A class declaration, which describes the data members and the member functions, also called method.
- 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:
- C + + keyword class identifies that is defining the design of a class
- The class declaration enables you to declare objects, or instances of the the Stock type
- 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:
- Public: Uses the object of the class can access the public part directly
- 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:
- Use scope-resolution operator (::) to identify, the class to which the function belongs
- 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