OOP(C)第二章C初探-创建和使用对象(最终版).pptVIP

  • 3
  • 0
  • 约2.32千字
  • 约 21页
  • 2017-04-27 发布于四川
  • 举报

OOP(C)第二章C初探-创建和使用对象(最终版).ppt

OOP(C)第二章C初探-创建和使用对象(最终版)

第二章 C++初探:创建和使用对象; This chapter explains key differences between C and C++, and takes you through three essential C++ features: Type safety (类型安全性) Classes (类:抽象、封装、多态性等) Templates (模板) Also covered are IOStreams and the free store operators new and delete.;2.1 概述(cont.);2.2 C++的历史 ;2.3 对C子集的某些改进 ;Abstract (抽象) Encapsulation (封装) Access Control(Public,Private,Protected) Friends(友元):允许友元破坏封装性 Inheritance (继承) Virtual Function (多态性) Later Binding (晚绑定) Overloading (重载) -- 允许函数名和运算符重载 Template (模板);2.5 C++的输入输出:IOStreams初探; cout是一个预定义的对象; cout“Hello,world” 等价于: cout.operator(“Hello,world”) C++的输入输出是类型安全的输入输出. (取代printf);2.6 new and delete 运算符;2.7 对象(Object)概述;2.7.1 例:Class Stack ;// intstack.cpp #include intstack.h StackOfInt::StackOfInt(int stk_size) { data = new int[length = stk_size]; ptr = 0; }; void StackOfInt::push(int x) { if (ptr length) data[ptr++] = x; else throw overflow; } int StackOfInt::pop() { if (ptr 0) return data[--ptr]; else throw underflow; };// (intstack.cpp continued) int StackOfInt::top() const { if (ptr 0) return data[ptr-1]; else throw underflow; } int StackOfInt::size() const { return ptr; } StackOfInt::~StackOfInt() { delete [] data; } ;输出:4 3 2 1 0;2.8 Templates(模板:参数化的类) ;例:通用栈模板;templateclass T StackT::Stack(int stk_size) { data = new T[length = stk_size]; ptr = 0; }; … templateclass T void StackT::push(T x) { if (ptr length) data[ptr++] = x; else throw overflow; } templateclass T T StackT::pop() { if (ptr 0) return data[--ptr]; else throw underflow; } ;templateclass T T StackT::top() const { if (ptr 0) return data[ptr-1]; else throw underflow; } … // tstack9b.cpp: Tests the Stack template #include stack9b.h #include iostream usin

文档评论(0)

1亿VIP精品文档

相关文档