- 3
- 0
- 约2.32千字
- 约 21页
- 2017-04-27 发布于四川
- 举报
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
您可能关注的文档
最近下载
- 2024年湖南汽车工程职业学院单招职业技能测试题库及答案(历年真题).docx VIP
- 广东省潮州市2024-2025学年高三上学期期末教学质量检测物理试卷.docx VIP
- 上海电力学院大一机械制图C习题本解答(造福学弟,不谢)-新版.pptx
- 广东省潮州市2024-2025学年高二上学期期末教学质量检测物理试卷(含答案).docx VIP
- 新生儿身份识别制度.pptx
- 康迪泰克空气弹簧.pdf VIP
- LY/T 2817-2017山桐子栽培技术规程.pdf
- 缓刑担保承诺书范文.docx VIP
- 记账实操-电梯保养公司会计账务处理分录.doc
- 脑机接口专利关键技术白皮书-复旦大学-2025.pdf VIP
原创力文档

文档评论(0)