标签:inf describe sim version abstract .cpp mit psu ++
) In procedural programming, you first concentrate on the procedures you will follow
) In OOP, you concentrate on the objects, thinking about the data and relevant operations
In computing, abstraction is the crucial step of representing information in terms of its interface with the user
Establishing a basic type, one need to consider:
) A class is a vehicle for translating abstraction to a user-defined type. It combines data representation and methods for manipulating that data into one package.
class specification
Generally, a class specification has two parts:
interface
An interface is a shared framework for interactions between two systems.
) C++ programmers often place the form of a class definition in header file and place the class method implementation 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:
The most striking part of class is the binding of data and methods into a single unit
access control
There are two access control keywords for class members up till now:
) 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 are examples of encapsulation
) Use private member functions to handle implementation details that don‘t form the public interface. In another word, public member functions constitute the public interface, not all functions should be placed in the public sector, functions like set_tot() in this example is simple and not relevant to the interface, so it was placed in the private sector
) keyword private could also be omitted because default access to class members is private
Member functions have two special characteristics:
void Buffoon::update();
This shows that 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, which could only be 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::acquire(const std::string & co, long n, double pr)
{
company = co;
if (n < 0)
{
std::cout << "Number of shares can‘t be 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 in the class declaration becomes an inline function, just like set_tot() in this example. You can write the definition of set_tot() outside the class declaration just to apply the keyword inline to it:
class Stock
{
private:
void set_tot();
public:
...
}
inline void Stock::set_tot()
{
...
}
According to the rewrite rule, doing so is 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, sometimes called sending a message
Cpp Chapter 10: Objects and Classes Part1
标签:inf describe sim version abstract .cpp mit psu ++
原文地址:https://www.cnblogs.com/fsbblogs/p/9749708.html