拿出来大家共勉(其实是电脑内存放不下了),记得当时好像跟的是翁男神的课。
目录
oop(object operation programing)理念
概述
面向对象object-oriented定义
objects对象=attributes属性+services服务 ,不能跳过服务直接去操作内部数据 (面向过程:是从时间上去分析东西)
oop(object operation programing)理念
oop charasteristics
- everything is an object
- a program is a bunch of objects telling each other what(not how) to do by sending messages
- each objest has its own memory made up other objects
- every object has a type(对于oop,应该先有类型再有对象)
- all objects of a particular type can receive the same messages
Interface
an object has an interface.
function of the interface: 1.communication 2.protection
The hidden Implementation
Encapsulation(封装)
内容
解析符 resolver ::
- <class name >::<function name>
- ::<function name>
相当于是f的这个方法是归属在S下面
Definition of a class
compile follows
展开include,和宏的步骤叫预编译, 之后每一个源文件会编译成二进制待重定位文件。(windows上是.lib,linux上是.o) 然后连接器会连接每个上述文件为二进制可执行文件(windows上是.exe,linux是elf文件),并进行重定位。
.h
.cpp
the header files
Declarations & Definitions
Variables
fields成员变量
相对于类来说
运用时,成员变量不在类里,而是在类所含的对象里
parameters参数
local本地变量
相对于函数来说(函数是属于类,而不是对象,但是对象可以调用)
Call functions in a class
this:the hidden parameter
this 这个 类的 函数 所属的那个对象 的指针
函数属于类而不是对象,变量属于对象
Constructor&Destructor
Constructor构造函数
无返回类型,对象调用时使用构造
Class X{
int i;
public:
x();
}
Destructor析构函数
无参数
在对象消灭时使用
- the destructor is called 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 ibject.
destructor{
calss Y{
public :
~Y();
}
}
在用析构函数之前,人们也用init函数进行用之前的清除
init(int x,int y)
default constructor
对象初始化。无参数的构造函数就称为default constructor
Y Y1[]={Y(1),Y(2),Y(3)};
Y y2[2]={Y(1)};错误的,要两个却只给了一个
Dynamic memory allocation
New
- new int
分配空间
- new Stash
new一个类就是分配空间接着调用构造函数
- new int[10]
delete
- delete p;
- delete[] p;
先析构,再收回空间
tips
- don't use delete to free memory that new didn't allocate
- dont use delete to free the same blocj of memory twice in succession
- use delete[] if you used new[] to allocate an array
- use delete (no brackets) if you used new to allocate a single entity
- it's safe to apply delete to the null pointer (nothing happens)
setting limits
- public
- private
对于同一个类来说,并不是对象
- protected
子子孙孙类可以用
- friend
声明是朋友类后,就可以用其私有成员
class - struct
class 无特别声明private,就是private
struct 无特别声明private,就是public
Initialization vs. assignment
initialization before constructor
所有成员变量在initialization中进行初始化,不要放在构造函数里。
Student::Student(String s):name(s){}
assignment inside constructor
Student::Student(string s){name=s;}
Inheritance
it is to take the existing class,clone it, and then make additions and modifications to the clon
上面是父类,下面为子类.
Interface
一个类中所有public部分放在接口中,让别人通过接口访问