第10章 类和对象(例子)例10.4 定义并测试长方形类CRect,长方形是由左上角坐标(left, top)和右下角坐标(right, bottom)组成。
#include
#include
class CRect //定义长方形类
{
private:
int left, top, right, bottom ;
public:
void setcoord(int, int, int, int);
void getcoord(int *L, int *T, int *R, int *B)
//注意:形参为指针变量
{
*L = l...
例10.4 定义并测试长方形类CRect,长方形是由左上角坐标(left, top)和右下角坐标(right, bottom)组成。
#include
#include
class CRect //定义长方形类
{
private:
int left, top, right, bottom ;
public:
void setcoord(int, int, int, int);
void getcoord(int *L, int *T, int *R, int *B)
//注意:形参为指针变量
{
*L = left; *T = top;
*R = right; *B = bottom;
}
void print(void)
{
cout<<"Area = ";
cout<ppt讲稿
例10.5 定义日期类,利用构造函数初始化数据成员。程序放在头文件date.h中,如下:
#include
class Date
{
int Year, Month, Day;
public:
Date( ) //重载构造函数 1
{
Year=2003; Month=1; Day=1;
}
Date(int y) //重载构造函数 2
{
Year=y; Month=1; Day=1;
}
Date(int y, int m) //重载构造函数 3
{
Year=y; Month=m; Day=1;
}
Date(int y, int m, int d) //重载构造函数 4
{
Year=y; Month=m; Day=d;
}
void ShowDate( )
{
cout <
class Date
{
int Year, Month, Day;
public:
Date(int y=2003, int m=1, int d=1)
//带参数缺省值的构造函数
{
Year=y; Month=m; Day=d;
}
void ShowDate( )
{
cout <
#include
class Student
{
char Num[10]; //学号,注意:用数组实现
char *Name; //姓名,注意:用指针实现
int Score; //成绩
public:
Student(char *nump, char *namep, int score)
{
if(nump) //在构造函数中,
{ //不需要动态申请Num成员的空间
strcpy(Num, nump);
}
else
strcpy(Num, "");
if(namep) //在构造函数中,
{ //需动态申请Name成员的空间
Name=new char[strlen(namep)+1];
strcpy(Name, namep);
}
else Name=0;
Score=score;
cout<<"Constructor Called!\n";
}
~Student( ) //在析构函数中,
{ //需释放Name成员的空间
if(Name) delete [ ] Name;
cout<<"Desturctor Called!\n";
}
void Show( )
{
cout << Num << endl;
cout << Name << endl;
cout << Score << endl;
}
};
void main( )
{
Student a("040120518", "George", 80);
a.Show( );
}
此程序运行结果是:
Constructor Called! //调用构造函数时的输出
040120518
George
80
Desturctor Called! //调用析构函数时的输出
返回ppt讲稿
例10.7 调用构造函数和析构函数的时机
#include
class Date
{
int Year, Month, Day;
public:
Date(int y=2002, int m=1, int d=1)
{
Year=y; Month=m; Day=d;
cout<<"Constructor: ";
ShowDate( );
}
void ShowDate( )
{
cout <
#include "point.h"
void main( )
{
Point p1(6, 8), p2(4, 7);
Point p3(p1); // A调用拷贝构造函数
Point p4=p2; // B调用拷贝构造函数
p1.Show( );
p3.Show( );
p2.Show( );
p4.Show( );
}
此程序运行结果是:
6, 8 Copy-initialization Constructor Called.
4, 7 Copy-initialization Constructor Called.
Point: 6, 8
Point: 6, 8
Point: 4, 7
Point: 4, 7
4, 7 Destructor Called. // 撤销 P4
6, 8 Destructor Called. // 撤销 P3
4, 7 Destructor Called. // 撤销 P2
6, 8 Destructor Called. // 撤销 P1
析构函数与构造函数的调用顺序相反
返回ppt讲稿
例10.10 不定义拷贝构造函数时,运行出错。
//文件 Li1010.cpp
#include
#include
class Student
{
char *Name; //姓名,注意:用指针实现
int Age; //年龄
public:
Student(char *namep, int age) //构造函数
{
Age=age;
if(namep) //在构造函数中,需动态申请空间
{
Name=new char[strlen(namep)+1];
strcpy(Name, namep);
}
else Name=NULL;
}
~Student( )//因在构造函数中动态申请了空间,
{ //则在析构函数中,需释放空间
if(Name) delete [ ] Name;
}
void Show( )
{
cout << Name << ',' << Age << endl;
}
};
void main( )
{
Student a("George", 20);
Student b=a; // A
}
此程序运行时出错,原因是: 没有定义类的拷贝构造函数。系统自动产生的拷贝构造函数如下:
Student::Student(const Student &s)
{
Name = s.Name; // 注意:地址值直接赋值
Age = s.Age;
}
正确的做法是,定义如下拷贝构造函数:
Student::Student(const Student &s)
{
Age=s.Age;
if(s.Name)
{
Name = new char[strlen(s.Name)+1]; //C
strcpy(Name, s.Name);
}
else Name=NULL;
}
教材图中地址有错 P193
返回ppt讲稿
例10.11 在本例中,使用例10.9中“平面坐标点”类的头文件point.h,测试用对象做函数参数及函数返回值时拷贝构造函数的使用。
#include "point.h" //普通函数,不是类的成员函数
Point move(Point p, int xoffset, int yoffset)
{
int x = p.Getx( )+xoffset, y = p.Gety( )+yoffset;
Point p=p1 //参数传递时
Point 内存临时对象=t。//返回对象时
Point t(x, y);
return t;
}
void main( )
{
Point p1(6, 8), p2;
p2=move(p1, 2, 4);
}
此程序运行结果是:
6, 8 Copy-initialization Constructor Called. //A
8, 12 Copy-initialization Constructor Called. // B
8, 12 Destructor Called. // 撤消对象t
6, 8 Destructor Called. // 撤消对象p
8, 12 Destructor Called. // 撤消内存临时对象
8, 12 Destructor Called. // 撤消对象p2
6, 8 Destructor Called. // 撤消对象p1
返回ppt讲稿
例10.13 利用构造函数完成类型转换
//文件 Li1013.cpp
#include
class Complex
{
double Real, Image;
public:
Complex(double x=0, double y=0)
{
Real=x; Image=y;
Show( );
cout << "调用了构造函数\n";
}
~Complex( )
{
Show( );
cout << "调用了析构函数\n";
}
void Show( )
{
cout<<'('<表
#include
class ListClass
{
int *ListPtr; // 指向线性表的指针
int nMax; // 线性表的最大长度
int nElem; // 线性表中当前元素的个数
public: //构造函数,初始化线性表,最大长度的缺省值为10
ListClass(int n=10)
{
nElem=0;
nMax=n;
if(n)
ListPtr = new int[n];
else
ListPtr=0;
}
~ListClass(void) //析构函数
{
delete [nMax] ListPtr;
}
int Elem(int); //重载函数①,在线性表尾增加
//一个元素,在类体外实现
int &Elem(unsigned n) //重载函数②
//返回线性表中第n个元素的引用
{ return ListPtr[n]; }
int Elem(void) //重载函数③
//返回线性表中当前元素的个数
{ return nElem; }
int Max(void) //返回线性表的长度
{ return nMax; }
int GetElem(int i)
{
if((i>=0) && (i
#include
class Point //定义“点”类
{
int x, y;
public:
Point(int a=0, int b=0)
{
x=a; y=b;
cout<
class Sample
{
int x, y;
public:
fun(int a, int b)
{
this-> x = a;
this-> y = b;
cout<
本文档为【第10章 类和对象(例子)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。