Cpp Chapter 10: Objects and Classes Part1
10.1 Procedural and object-oriented programming
) 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
10.2 Abstraction and classes
In computing, abstraction is the crucial step of representing information in terms of its interface with the user
10.2.1 What is a type?
Establishing a basic type, one need to consider:
- how much memory is needed for a data object
- how the bits in memory are interpreted
- what operations, or methods, can be performed on the data object
For built-in types like int and double, these are 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 that data into one package.
class specification
Generally, a class specification has two 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 are implemented
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:
- C++ keyword class identifies that you are defining the design of a class
- The class declaration enables you to declare objects, or instances of the Stock type
- A member function could be defined in the place(like set_tot()), or could also be represented by prototype temporarily
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:
- public: any program that uses the object of the class can access the public part directly
- private: a program could access private members of object only by public 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 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
10.2.3 Implementing class member functions
Member functions have two 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 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