C程序设计教程与实验指导杨国兴类与对象.ppt
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C++语言程序设计
杨国兴 张东玲 彭涛
中国水利水电出版社
第4章 类与对象
4.1 类与对象 4.2 构造函数与析构函数 4.3 类的组合 4.4 友元 4.5 静态成员 4.6 对象数组与对象指针 4.7 this指针
第4章 类与对象
4.1 类与对象
4.1.1 类与对象的概念
对象(object):是现实世界中的客观事物。 类(class):是把具有相同属性的事物划分为一类,从而得出 的抽象概念。
定义CRect类的对象,定义对象的 格式:
类名 对象名1,对象名2,……
访问对象的公有成员,格式为: 对象名.公有成员函数名(参数表) 对象名.公有数据成员名
程序运行结果为:
矩形左上角坐标为(20,10) 矩形长和宽分别为100,200 矩形的颜色是Red 矩形左上角坐标为(50,50) 矩形长和宽分别为100,200 矩形的颜色是Blue
第4章 类与对象
4.1 类与对象
4.1.4 类的成员函数
1. 类成员函数的定义方式 ➢在类外部定义:如前面定义的长方形类的成员函数 一般格式为: 函数类型 类名::成员函数名(参数说明)
{ 函数体
} ➢在类中定义:如
class CRect { …… public:
void setcolor( char *c ){ strcpy( color , c ); } …… };
{ …… } 2. 析构函数的作用 在删除一个对象前被调用,释放该对象成员的内存空间,以 及其它一些清理工作。
第4章 类与对象
例4.3 设计一个简单的字符串类,类中有两个数据成员,分别
表示字符串的长度和字符串的内容,有一个构造函数和一个析
构函数,函数GetLength( )返回字符串长度,函数
GetContents( )获得字符串的内容,重载函数SetContents( )给
const double PI; int b; int &c; public: A(int x):PI(3.14),c(b) {
b=x; } void Output() {
cout << PI << "," << b << "," << c << endl; } };
void main() {
A x(10); x.Output(); }
第4章 类与对象
例 为CRect类添加构造函数
class CRect
{
private:
char color[10];
……
public:
CRect( );
CRect(char *c, int t, int left, int len, int wid);
void SetColor(char *c);
CRect r2(“red”, 10,10,100,100); //自动调用第二个构造函数
CRect r3("green", 200,200,50,50); //自动调用第二个构造函数
r1.Draw();
r2.Draw();
r3.Draw();
}
第4章 类与对象
例4.2 构造函数的初始化表
#include <iostream> using namespace std; class A { private:
int GetLength();
void GetContents(char *str);
void SetContents(int len, char *cont);
void SetContents(char *cont);
};
第4章 类与对象
例4.3 (续一)
CString::CString() {
在声明处就不能再给默认值了。
返回
第4章 类与对象
4.2 构造函数与析构函数
构造函数:对对象进行初始化。 析构函数:在对象销毁时进行内存释放等清理工作。
4.2.1 构造函数
1. 构造函数的特点 (1) 构造函数的函数名与类名相同。 (2) 不能定义构造函数的类型(即不能指明构造函数返回值的 类型)。 (3) 构造函数应声明为公有函数。 (4) 构造函数不能在程序中调用,在对象创建时,构造函数被 系统自动调用。 2. 构造函数的作用 构造函数的作用就是在对象被创建时利用特定的值构造对象, 将对象初始化为一个特定的状态,使此对象具有区别于其它对象 的特征。
程序运行结果: 3.14,10,10
第4章 类与对象
4.2 构造函数与析构函数
4.2.2 析构函数
1. 析构函数的特点 (1) 析构函数名字为符号“~”加类名。
(2) 析构函数没有参数,不能指定返回值类型。 (3) 一个类中只能定义一个析构函数,所以析构函数不能重载。 (4) 当一个对象作用域结束时,系统自动调用析构函数。 如CRect类的析构函数声明为:~CRect(); 定义为: CRect::~CRect()
length = 0; contents = NULL; cout << "字符串对象初始化" << endl; } CString::~CString() { cout << contents << "被销毁" << endl; if(contents != NULL)
delete contents; } int CString::GetLength() {
字符串设置值。 #include <iostream>
#include <string>
using namespace std;
class CString
{
private:
int length;
char *contents;
public: CString(); ~CString();
//构造函数 //析构函数
类名::函数名(参数表)
void CRect::Move(int t,int l) {
top = t;
left = l;
} void CRect::Draw()
{ cout << "矩形左上角坐标为(" << left << "," << top << ")" << endl; cout << "矩形长和宽分别为" << length << "," << width << endl; cout << "矩形的颜色是" << color << endl;
第4章 类与对象
4.1 类与对象
4.1.4 类的成员函数(续一)
2. 内联成员函数 ➢将成员函数的定义直接写在类中即成为内联成员函数 ➢在类外定义时用inline指出: 如: inline void CRect::SetColor(char *c) { strcpy(color, c); }
第4章 类与对象
struct CRect {
void SetColor(char *c); void SetSize(int l, int w); void Move(int t,int l); void Draw(); private: char color[10]; int left; int top; int length; int width; };
class CRect { private:
char color[10]; int left; int top; int length; int width; public: void SetColor(char *c); void SetSize(int l, int w); void Move(int t,int l); void Draw(); };
面向对象程序设计中的类,是具有相同属性和服务的一组对 象的集合,它为属于该类的全部对象提供了抽象的描述。
对象是类的实例,类是同种对象的抽象。
如:确定大小和颜色的矩形都是一个个具体的对象,而将所有矩形的共同特
点抽象出来olor ),左上角坐标 ( left, top ),长 ( length )和 宽 ( width ) 等;
第4章 类与对象
4.1 类与对象
4.1.3 成员的访问控制
private: 私有访问权限,只允许类中的成员函数访问,其他函 数不能访问。
protected: 保护访问权限,在第7章中介绍。 public: 公有访问权限,在任何函数中都可以访问。
例:若主函数中有以下语句,是否正确?
CRect r;
strcpy( r.color , “red”);
CRect::CRect(char *c, int t, int lef, int len, int wid )
{
strcpy(color, c);
top = t;
left = lef;
length = len;
width = wid;
}
void main()
{
CRect r1;
//自动调用第一个构造函数
r.top = 10; r.left = 20;
在主函数中不能访问类的私有成员
第4章 类与对象
4.1 类与对象
4.1.3 成员的访问控制(续)
若不指定类中的成员的访问权限,则默认为私有成员。
类也可以由struct关键字声明,strust与class的区别是:如果 不指定访问权限,前者缺省的访问权限是公有的,而后者是私有 的。用struct声明前面的矩形类:
对这些属性的处理包括改变矩形的颜色 ( SetColor ) 和大小 ( SetSize ) ,移 动矩形到新的位置 ( Move ),绘出矩形 ( Draw ) 等。将矩形的这些属性和方法作 为一个整体,封装在一起形成一个矩形类。
第4章 类与对象
4.1 类与对象
4.1.2 类的声明
class 类名 {
return length; } void CString::GetContents(char *str) {
strcpy(str, contents); }
第4章 类与对象
例4.3 (续二)
void CString::SetContents(int len, char *cont) {
length = len; if(contents != NULL)
……
二者是重载函数,在定义对象时,
};
如果不给出参数,就自动调用第一
CRect::CRect() {
个构造函数,如果给定5个参数就 自动调用第二个构造函数。
strcpy(color, "Black");
top = 0;
left = 0;
length = 0;
width = 0;
}
第4章 类与对象
例 为CRect类添加构造函数(续)
delete contents; contents = new char[len+1]; strcpy(contents,cont); cout << "两个参数的SetContents函数" << endl; } void CString::SetContents( char *cont) { length = strlen(cont); if(contents != NULL)
4.1 类与对象
4.1.4 类的成员函数(续二)
3. 带默认参数值的成员函数 注意:默认参数只能在声明或定义中的一处给出,即
如在类中的函数声明已经给出默认参数值:
void SetSize(int l=100, int w=100);
则在函数定义时就不能再给出默认值。
同样如果在定义时给出了默认值: void CRect::SetSize(int l=100, int w=100) { length=l; width = w; }
private: 私有数据成员和成员函数;
protected: 保护数据成员和成员函数;
public: 公有数据成员和成员函数;
};
第4章 类与对象
例4.1 定义一个长方形类CRect,其数据成员包括颜色,左上 角坐标,长和宽,其函数成员包括改变矩形的颜色(SetColor) 和大小(SetSize),移动矩形到新的位置(Move),绘出矩 形(Draw)。
}
第4章 类与对象
例4.1 (续二)
void main() {
CRect r; r.SetColor("Red"); r.Move(10,20); r.SetSize(100,200); r.Draw(); r.Move(50,50); r.SetColor("Blue"); r.Draw(); }
第4章 类与对象
例4.1 (续一)
void CRect::SetColor(char *c)
{ strcpy(color, c);
}
void CRect::SetSize(int l, int w)
{ length=l; width = w;
}
域运算符(::)用于指出该函数是 哪一个类的成员函数,用法:
杨国兴 张东玲 彭涛
中国水利水电出版社
第4章 类与对象
4.1 类与对象 4.2 构造函数与析构函数 4.3 类的组合 4.4 友元 4.5 静态成员 4.6 对象数组与对象指针 4.7 this指针
第4章 类与对象
4.1 类与对象
4.1.1 类与对象的概念
对象(object):是现实世界中的客观事物。 类(class):是把具有相同属性的事物划分为一类,从而得出 的抽象概念。
定义CRect类的对象,定义对象的 格式:
类名 对象名1,对象名2,……
访问对象的公有成员,格式为: 对象名.公有成员函数名(参数表) 对象名.公有数据成员名
程序运行结果为:
矩形左上角坐标为(20,10) 矩形长和宽分别为100,200 矩形的颜色是Red 矩形左上角坐标为(50,50) 矩形长和宽分别为100,200 矩形的颜色是Blue
第4章 类与对象
4.1 类与对象
4.1.4 类的成员函数
1. 类成员函数的定义方式 ➢在类外部定义:如前面定义的长方形类的成员函数 一般格式为: 函数类型 类名::成员函数名(参数说明)
{ 函数体
} ➢在类中定义:如
class CRect { …… public:
void setcolor( char *c ){ strcpy( color , c ); } …… };
{ …… } 2. 析构函数的作用 在删除一个对象前被调用,释放该对象成员的内存空间,以 及其它一些清理工作。
第4章 类与对象
例4.3 设计一个简单的字符串类,类中有两个数据成员,分别
表示字符串的长度和字符串的内容,有一个构造函数和一个析
构函数,函数GetLength( )返回字符串长度,函数
GetContents( )获得字符串的内容,重载函数SetContents( )给
const double PI; int b; int &c; public: A(int x):PI(3.14),c(b) {
b=x; } void Output() {
cout << PI << "," << b << "," << c << endl; } };
void main() {
A x(10); x.Output(); }
第4章 类与对象
例 为CRect类添加构造函数
class CRect
{
private:
char color[10];
……
public:
CRect( );
CRect(char *c, int t, int left, int len, int wid);
void SetColor(char *c);
CRect r2(“red”, 10,10,100,100); //自动调用第二个构造函数
CRect r3("green", 200,200,50,50); //自动调用第二个构造函数
r1.Draw();
r2.Draw();
r3.Draw();
}
第4章 类与对象
例4.2 构造函数的初始化表
#include <iostream> using namespace std; class A { private:
int GetLength();
void GetContents(char *str);
void SetContents(int len, char *cont);
void SetContents(char *cont);
};
第4章 类与对象
例4.3 (续一)
CString::CString() {
在声明处就不能再给默认值了。
返回
第4章 类与对象
4.2 构造函数与析构函数
构造函数:对对象进行初始化。 析构函数:在对象销毁时进行内存释放等清理工作。
4.2.1 构造函数
1. 构造函数的特点 (1) 构造函数的函数名与类名相同。 (2) 不能定义构造函数的类型(即不能指明构造函数返回值的 类型)。 (3) 构造函数应声明为公有函数。 (4) 构造函数不能在程序中调用,在对象创建时,构造函数被 系统自动调用。 2. 构造函数的作用 构造函数的作用就是在对象被创建时利用特定的值构造对象, 将对象初始化为一个特定的状态,使此对象具有区别于其它对象 的特征。
程序运行结果: 3.14,10,10
第4章 类与对象
4.2 构造函数与析构函数
4.2.2 析构函数
1. 析构函数的特点 (1) 析构函数名字为符号“~”加类名。
(2) 析构函数没有参数,不能指定返回值类型。 (3) 一个类中只能定义一个析构函数,所以析构函数不能重载。 (4) 当一个对象作用域结束时,系统自动调用析构函数。 如CRect类的析构函数声明为:~CRect(); 定义为: CRect::~CRect()
length = 0; contents = NULL; cout << "字符串对象初始化" << endl; } CString::~CString() { cout << contents << "被销毁" << endl; if(contents != NULL)
delete contents; } int CString::GetLength() {
字符串设置值。 #include <iostream>
#include <string>
using namespace std;
class CString
{
private:
int length;
char *contents;
public: CString(); ~CString();
//构造函数 //析构函数
类名::函数名(参数表)
void CRect::Move(int t,int l) {
top = t;
left = l;
} void CRect::Draw()
{ cout << "矩形左上角坐标为(" << left << "," << top << ")" << endl; cout << "矩形长和宽分别为" << length << "," << width << endl; cout << "矩形的颜色是" << color << endl;
第4章 类与对象
4.1 类与对象
4.1.4 类的成员函数(续一)
2. 内联成员函数 ➢将成员函数的定义直接写在类中即成为内联成员函数 ➢在类外定义时用inline指出: 如: inline void CRect::SetColor(char *c) { strcpy(color, c); }
第4章 类与对象
struct CRect {
void SetColor(char *c); void SetSize(int l, int w); void Move(int t,int l); void Draw(); private: char color[10]; int left; int top; int length; int width; };
class CRect { private:
char color[10]; int left; int top; int length; int width; public: void SetColor(char *c); void SetSize(int l, int w); void Move(int t,int l); void Draw(); };
面向对象程序设计中的类,是具有相同属性和服务的一组对 象的集合,它为属于该类的全部对象提供了抽象的描述。
对象是类的实例,类是同种对象的抽象。
如:确定大小和颜色的矩形都是一个个具体的对象,而将所有矩形的共同特
点抽象出来olor ),左上角坐标 ( left, top ),长 ( length )和 宽 ( width ) 等;
第4章 类与对象
4.1 类与对象
4.1.3 成员的访问控制
private: 私有访问权限,只允许类中的成员函数访问,其他函 数不能访问。
protected: 保护访问权限,在第7章中介绍。 public: 公有访问权限,在任何函数中都可以访问。
例:若主函数中有以下语句,是否正确?
CRect r;
strcpy( r.color , “red”);
CRect::CRect(char *c, int t, int lef, int len, int wid )
{
strcpy(color, c);
top = t;
left = lef;
length = len;
width = wid;
}
void main()
{
CRect r1;
//自动调用第一个构造函数
r.top = 10; r.left = 20;
在主函数中不能访问类的私有成员
第4章 类与对象
4.1 类与对象
4.1.3 成员的访问控制(续)
若不指定类中的成员的访问权限,则默认为私有成员。
类也可以由struct关键字声明,strust与class的区别是:如果 不指定访问权限,前者缺省的访问权限是公有的,而后者是私有 的。用struct声明前面的矩形类:
对这些属性的处理包括改变矩形的颜色 ( SetColor ) 和大小 ( SetSize ) ,移 动矩形到新的位置 ( Move ),绘出矩形 ( Draw ) 等。将矩形的这些属性和方法作 为一个整体,封装在一起形成一个矩形类。
第4章 类与对象
4.1 类与对象
4.1.2 类的声明
class 类名 {
return length; } void CString::GetContents(char *str) {
strcpy(str, contents); }
第4章 类与对象
例4.3 (续二)
void CString::SetContents(int len, char *cont) {
length = len; if(contents != NULL)
……
二者是重载函数,在定义对象时,
};
如果不给出参数,就自动调用第一
CRect::CRect() {
个构造函数,如果给定5个参数就 自动调用第二个构造函数。
strcpy(color, "Black");
top = 0;
left = 0;
length = 0;
width = 0;
}
第4章 类与对象
例 为CRect类添加构造函数(续)
delete contents; contents = new char[len+1]; strcpy(contents,cont); cout << "两个参数的SetContents函数" << endl; } void CString::SetContents( char *cont) { length = strlen(cont); if(contents != NULL)
4.1 类与对象
4.1.4 类的成员函数(续二)
3. 带默认参数值的成员函数 注意:默认参数只能在声明或定义中的一处给出,即
如在类中的函数声明已经给出默认参数值:
void SetSize(int l=100, int w=100);
则在函数定义时就不能再给出默认值。
同样如果在定义时给出了默认值: void CRect::SetSize(int l=100, int w=100) { length=l; width = w; }
private: 私有数据成员和成员函数;
protected: 保护数据成员和成员函数;
public: 公有数据成员和成员函数;
};
第4章 类与对象
例4.1 定义一个长方形类CRect,其数据成员包括颜色,左上 角坐标,长和宽,其函数成员包括改变矩形的颜色(SetColor) 和大小(SetSize),移动矩形到新的位置(Move),绘出矩 形(Draw)。
}
第4章 类与对象
例4.1 (续二)
void main() {
CRect r; r.SetColor("Red"); r.Move(10,20); r.SetSize(100,200); r.Draw(); r.Move(50,50); r.SetColor("Blue"); r.Draw(); }
第4章 类与对象
例4.1 (续一)
void CRect::SetColor(char *c)
{ strcpy(color, c);
}
void CRect::SetSize(int l, int w)
{ length=l; width = w;
}
域运算符(::)用于指出该函数是 哪一个类的成员函数,用法: