上ly老师的OOPre课所做的一些笔记,用cpp为载体比较系统的讲解了面向对象的三大特征(封装、继承、多态),可以作为了解这方面知识的开始,但记录较为繁杂,我后续会持续做整理补充。
课堂笔记 Pre 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #include "iostream" using namespace std;struct Node { int data; struct Node *next; }; struct Test { int i; double d; int j; char ch:1 ; }; int main (int argc,char *argv[]) {}
0913
堆区栈区:堆区:malloc new的空间
static:
一次构造和全局变量一起析构;
只能本文件使用,链接器链接不到;
类里,用于共享内存,不能在构造函数初始化;
类中函数static修饰后,可以直接用类名调用
总结: static把对象的变量变成了类的变量
extern链接器指令
声明vs定义
引用,安全版指针,消除了两个问题,野指针问题和半路指岔 如何理解向上转型和多态,多态的触发条件是重写
0920
1 2 3 4 class Single { private : static }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 class Test { private : int i; int *p; public : Test (int ,int ); ~Test (); } Test t1 (1 ,2 ) ;Test t2 (t1) ; class Test { private : int i; int *p; public : Test (int ,int ); Test (const Test &t); ~Test (); } Test::Test (const Test &t){ this ->i = t.i; this -> p = new Test (*t.p); } Test t1 (1 ,2 ) ;Test t2 (t1) ;if (p!=NULL ){ free (p); p=NULL ; } int *fun () { int a=10 ; return &a; } int *fun () { int *p = new int (10 ); return p; } void fun () {} memcpy ()浅拷贝clone ()和putAll ()都是只对于built_in类型深克隆
C追求的是运行的高效,java追求的是编程的高效
继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class A {} class A2 extends A{} interface A { } class A2 implements A{}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 const double pi = 3.14 ;const int fun () { } cout << fun ()++ << endl; const Test &t const Test t;t.fun () class Test { public : void fun () const ; }
0927
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Account { int id; string name; int balance; public : Account (int id, int balance); Account& operator +(int money); Account& operator ++(); Account operator ++(int ); } Account& Account::operator +(int money){ this .balance += money; return *this ; } Account Account::operator ++(int ){ Account a (*this ) ; this -> balance++ return a; } Account& Account::operator ++(){ this -> balance ++; return *this ; } int main () { Account a (1 ,100 ) ; a = a + 1 ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 class Student { int cardId; char *name; public : void BorrowBook () ; } class Teacher { int cardId; char *name; ... }
1 2 3 super.fun1 (); Base::fun1 ();
1004
1 2 3 4 5 6 7 8 9 class Test {public : int i; int j; Test (int m); } Test::Test (int m): j (m),i (j){ }
多态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class Pet {public : int i; virtual void speak () ; virtual void sleep () ; } void Pet :: speak (){ cout << "Pet::speak" << endl; } class Cat : public Pet{public : void speak () ; } void Cat :: speak (){ cout<< "miaomiao" << endl; } void handle (Pet &pet) { pet.speak (); } int main () { Cat cat; handle (cat); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Test {public : Test (); virtual ~Test (); } class Derived : public Test{public : Derived (); ~Derived (); } void fun (Test *p) { delete p; } int main () { Derived *derived = new Derived; fun (derived); }
1 2 3 4 5 6 7 8 virtual void speak () {};virtual void speak () = 0 ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 class FlyObject {public : virtual void fly () = 0 ; } class Animal {} class Bird : public Animal . public FlyObject{ public : void fly () ; } class Machine {} class Airplane : public Machine . public FlyObject{ public : void fly () ; } void follow (Bird &bird) { bird.fly (); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 class Stack { int pool[100 ]; int top; public : Stack (); int pop () ; void push (int a) ; } Stack::Stack () :top (0 ){ } int Stack::pop () { return pool[--top]; } void Stack::push (int a) { pool[top++] = a; } int main () { Stack S; for (int i = 0 ; i < 10 ; i++) push (i*i); for (int i = 0 ; i < 10 ; i++) cout << pop () << endl; } template <class T >class Stack { T pool[100 ]; } int main () { Stack<int > intStack; Stack<double > doubleStack; } #include <vector> using namespace std;int main (int argc, char *argv[]) { vector<int > v; for (int i=0 ; i<100000 ; i++) v.push_back (i); cout << v[10034 ] << endl; }
1018
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <vector> #include <cstdio> using namespace std; namespace MyClass{ class Test { } } int main () { MyClass::Test test = new (); vector<int > v; for (i=0 ; i < 0 ; i++){ v.push_back (i); } vector<int >::iterator it = v.begin (); while (it != v.end ()){ cout << *it << endl; it++; } return 0 ; }
课程总结 OO based on cpp
封装
struct、class
data+function
access control(private, public, protest)
this
construct/destruct stack/heap
reference/copy constructor
keyWord: static const
继承
共性与特性
多重继承(x)
不要削弱父类接口
构造顺序
多态
upcasting,传参
runtime binding 前绑定,后绑定
vitual v-table v-ptr
高级抽象:抽象类,接口(行为的共性)
template
性能问题出现前,不需要考虑性能
真理导向,结果导向
GUI
console控制台程序
两类:基于Form, 基于Web
win32 application
API应用程序开发接口