[已咕咕]面向对象程序设计笔记

面向对象(object-oriented)程序设计

1

  • IDE的功能

2

  • 关注的是对象,不是流程
  • 对象可以是任何东西
  • 面向对象既适用于设计,也适用于实现
  • 对象里封装着方法,通过接口调用,即对象等于数据和包装数据的操作
  • 对象之间交互的形式是消息Objects send message
  • Messages are:1.Composed by the sender 2.Interpreted by the receiver 3.Implemented by methods
  • Messages may cause receiver to change state
  • Messages may return results
  • Class defines Object -------Object is a Class -------- cat and cat class
  • OOP Characteristics
  • public or private
  • Q:Are Messages Objects? Are Outputs Objects?

3

  • 成员函数声明,定义和实体
  • <Class Name>::<function name>
  • ::<function name>
  • definition of a class:声明在.h,定义在.cpp
  • include header(=interface)
  • declarations:.h,definitions:#include .cpp ,after pre-compiler
  • g++ x.cpp --save-temps 保存中间临时文件 -Wall 输出可能的所有Warning -c no link -D(XXXXX)带着XXXXX条件
  • -m32输出32位程序
  • 声明:extern 变量 function 函数 class/struct 类/结构
  • ".h"当前目录 <.h>/<>系统目录
  • 条件编译#ifndef HEADER_FLAG #define HEADER_FLAG // Type declaration here... #endif //HEADER_FLAG解决.h重复声明相同class问题
  • 一个头文件只放一个类的声明
  • 成员变量的作用域

4

  • 构造函数在对象创建时自动被调用
  • 构造函数和类名称相同
  • 构造函数不是整型
  • class X{public:X();};
  • 定义时加括号向构造函数传参
  • 析构函数在对象被结束之前被调用;
  • 析构函数没有参数~X();

5

  • new:new int;new Stash;new int[10];
  • delete:delete p;delete[] p;
  • delete:一个对象,一次析构
  • delete[]:多个对象,多次析构
  • c++ OOP权限仅在编译时生效被检查
  • Friend声明:A说B是它的Friends,B就能访问A的private
  • 前向声明struct X;'struct X{};'
  • class default to private;struct default to public
  • 初始化列表:X():p(int)对X对象构造前赋值p
  • string must have a default constructor

6

  • 对象组成(用对象组成对象):class xxx{...};class xx{xxx x;};
  • 对象继承(superset):(Base Class,Super,Parent)<-(Derived Class,Sub,Child)
  • 对象继承(语法):class X{};class XX:public X{};``protected让子类独享的权限

7

  • 函数缺省值:default int XXX(int a,int b,int c=1,int d=2){} XXX(6,7);||XXX(6,7,7,9);默认从左向右按顺序填充
  • 内联函数inline,直接把代码贴过去,加快函数运行(汇编代码例子,最终的可执行代码无该函数),.h和.cpp要同时加inline,内联函数要有内容
  • 递归的函数不要inline

8

  • 派生类有基类所有的成员函数和成员变量,不论是privatepublic还是protected
  • 派生类成员函数不能访问继承自基类的private成员
  • 派生类可以对基类进行修改和扩充

OOP Characteristics

  • Everything is an object
  • A program is a bunch of objects telling each other what to so by sending messages
  • Each object has its own memory made up of other objects
  • Every object has a type
  • All objects of a particular type can receive the same messages
原文地址:https://www.cnblogs.com/passguan/p/8447723.html