【面向对象设计C++--翁恺】05-时钟例子+06-成员变量+07-构造和析构+08-对象初始化
05-时钟例子
面向对象的思想去思考
abstract 抽象,在某个层次看,屏蔽细节
- abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem
- modularization(模块化) is the process of dividing a whole into well-defined(容易辨认的) parts.which can be built and examined separately,and which interact(相互作用) in well-defined ways
modularizing the clock display
object & classes
class diagram
5个源程序文件 .h + .cpp
06-成员变量
local variable
- local variables are defined inside a method,have a scope limited to the method to which they belong
- a local variable of the same name as a field will prevent the filed being accessed form within a method
和c类似,重名时本地变量会屏蔽成员变量
fields,parameters,local variables
- all three kinds of variable are able to store a value that is appropriate to their defined type
- fields are defined outside constructors and methods
- fields are used to store data that persists(持续) throughout the life of an object.as such,they maintain the current state of object.they have a lifetime that lasts as long as their objects lasts.
- fields have class scope:their accessibility extends throughout the whole class,and so they can be used within any of the constructor methods of the class in which they are defined
field :成员变量
parameters:函数的参数
local variable:本地变量
// a.h
class A{
private:int i; // 成员变量, 声明而不是定义
public:void f();
};
// a.cpp
#include "a.h"void A::f(){int j = 10; // 本地变量,只在f中有效i=20; // 成员变量,所有类的成员函数中可以使用
}int main(){A a; // 成员变量i在a中a.f(); // 使用的是 a 的iA b; // 成员变量i在b中return 0;
}
成员变量不在类里面, 在类的每个对象里面。有了对象后才有了成员变量。
成员变量的秘密
parameters:函数的参数 和 local variable:本地变量 是相同的东西。都是本地存储,在函数内部有效。都会放在栈的地方,但有不同。
field :成员变量在哪里?声明只是说明有这个东西,但是不知道在哪里。成员变量写到类的声明里面,并不知道在哪里。成员变量在类的成员函数中就可以使用,不用考虑它们在哪里。它们在类的每个对象里面。目前一般来说,类是概念,没有变量。
函数是属于类的,不是属于对象的。所有对象用到的函数是同一个。但是成员变量是和对象绑定的。
(形同C语言中:要修改结构中的变量将结构的的地址作为函数的参数传入函数中—指针的方式—c++类中隐藏的参数=>this)
c++中所有的机制都是通过c语言实现的
call functions in a class
Point a;
a.print();
- there is a relationship with the function be called and the variable calls it
- the function itself knowns it is doing something with the variable
this: the hidden parameter
- this is a hidden parameter for all member functions,with the type of the class
void Point::pint()->(can be regarded as)
void Point::print(Point *p)
this 是一个关键字,是一个指针,类型是该函数所属的类的对象的指针。默认是可以隐藏不写的。
函数定义中对于 成员变量 i = 20 等价于 this -> i = 20
表示该成员变量是属于对象的。
07-构造和析构
point::init()
c++是一个注重效率的语言,不会自己做初始化,区别于java,自己搬进来自己打扫;
vs debug模式中未初始化的内存是十六进制的“0xcd,两个构成中文“烫”;
依赖于的程序员的自觉性,素养;
定义了一个对象,对象里面的数据没有初始化。
guaranteed initialization with the constructor
- if a class has a constructor,the compiler(编译器) automatically calls that constructor at the point an object is created,before client programmers can get their hands on the object
- the name of the constructor is the same as the name of class
how a constructor does?
class X {int i;
public:X();
};
- 构造函数X没有返回类型,和类名一模一样;
- 对象被创造的时候 X() 被自动调用;
- X() 也是类的成员函数,this指的是那个对象的地址;
X a;----->a.X()
constructors with arguments
- the constructor can have arguments to allow you to specify how an object is created,give it initialization values,and so on
Tree(int i){...} // 带参数构造函数
Tree t(12); // 12传给构造函数
the destructor
- in C++,cleanup is as important as initialization and is there guaranteed with destructor
- the destructor is named after the name of the class with a leading tilde (~).the destructor never has any arguments
class Y{
public:~Y(); // 波浪号
};
没有参数,没有返回类型;被析构之前,析构函数被调用,空间被收回。
when is a destructor called?
- the destructor is automatically by the compiler when the object goes out of scopethe destructor is automatically by the compiler when the object goes out of scope
- the only evidence for a destructor call is the closing brace of the scope that surrounds the object
08-对象初始化
编译器会在 { 开始的地方分配在大括号内所有变量的空间。构造器的调用要到对象定义的地方。
storage allocation
- the compiler allocates all the storage for a scope at the opening brace of scope
- the constructor call does’t happen until the sequence point where the object is defined
对象没有构造,如果进行析构会编译不通过。
aggregate initialization
struct Y{float f;float i;Y(int a);}; // Y(int a) 是构造函数Y y1[]={Y(1),Y(2),Y(3)}; //成员如果没有初始化将导致错误
Y y2[2]={Y(1))}; // 错误, 第二个元素意味着要找到一个默认的构造函数初始化,但是没有就报错了
the default constructor 默认构造函数
- a default constructor is one that can be called with no arguments
自己写的或者编译器给你的那个没有参数的构造函数—>default constructor