例4.1

描述点的Point类。

例4.2

根据上面对Point类的定义,演示使用Point类的对象。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point//类名Point
{
private://声明为私有访问权限
int x, y;//私有数据权限
public://声明为公有访问权限
void Setxy(int a, int b);//无返回值的公有成员函数
void Move(int a, int b);//无返回值的公有成员函数
void Display();//无返回值的公有成员函数
int Getx();//返回值为int的公有成员函数
int Gety();//返回值为int的公有成员函数
};//类声明以分号结束 void Point::Setxy(int a, int b)
{
x = a;
y = b;
} void Point::Move(int a, int b)
{
x = x + a;
y = y + b;
} void Point::Display()
{
cout << x << "," << y << endl;
} int Point::Getx()
{
return x;
} int Point::Gety()
{
return y;
} void print(Point a)//使用Point的对象a作为函数参数
{
a.Display();//显示对象a的数据成员的值
} void main()
{
Point A, B;//声明对象
A.Setxy(, );//为对象A赋初值
B = A;//B的数据成员取A的数据成员之值 A.Display();//显示A的数据成员
A.Move(-, );//移动A print(A);//等价于A.Display();
print(B);//等价于B.Display(); cout << A.Getx() << endl;//只能使用A.Getx(),不能使用A.x
}

例4.3

演示使用内联函数定义Point类及使用Point类指针和引用的完整例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>//包含头文件

 using namespace std;//声明命名空间

 class Point//使用内联函数定义类Point
{
private://声明为私有访问权限
int x, y;//私有数据权限
public://声明为公有访问权限
void Setxy(int a, int b)//无返回值的内联公有成员函数
{
x = a;
y = b;
}
void Move(int a, int b)//无返回值的内联公有成员函数
{
x = x + a;
y = y + b;
}
void Display()//无返回值的内联公有成员函数
{
cout << x << "," << y << endl;
}
int Getx()//返回值为int的内联公有成员函数
{
return x;
}
int Gety()//返回值为int的内联公有成员函数
{
return y;
}
};//类定义以分号结束 void print(Point *a)//类指针作为print函数的参数定义重载函数
{
a->Display();
} void print(Point&a)//类引用作为print函数的参数定义重载函数
{
a.Display();
} void main()//主函数
{
Point A, B, *p;//声明对象和指针
Point &RA = A;//声明对象RA引用对象A A.Setxy(, );//使用成员函数为对象A赋值
B = A;//使用赋值运算符为对象B赋值 p = &B;//类Point的指针指向对象B
p->Setxy(, );//使用指针调用函数Setxy重新设置B的值
print(p);//传递指针显示对象B的属性
p->Display();//再次显示对象B的属性 RA.Move(-, );//引用对象RA调用Move函数 print(A);//验证RA和A同步变化
print(&A);//直接传递A的地址作为指针参数
}

例4.4

构造函数的定义和执行过程实例程序。

例4.5

使用前面定义的Point类演示全局对象的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point//使用内联函数定义类Point
{
private:
int x, y;
public:
Point();//使用参数列表声明不带参数的构造函数
Point(int, int);//使用参数列表声明带2个参数的构造函数
}; Point global(, ); Point::Point() :x(), y()//定义不带参数的构造函数
{
cout << "Initializing default" << endl;
} Point::Point(int a, int b) : x(a), y(b)///定义带2个参数的构造函数
{
cout << "Initializing " << a << "," << b << endl;
} void main()
{
cout << "Entering main and exiting main" << endl; Point A;//使用不带参数的构造函数产生对象A
Point B(, );//使用带参数的构造函数产生对象B
Point C[];//使用不带参数的构造函数产生对象数组C
Point D[] = { Point(,),Point(,) };//使用带参数的构造函数产生对象数组D
}

例4.6

使用前面的Point类演示new运算符和构造函数的关系的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point//使用内联函数定义类Point
{
private:
int x, y;
public:
Point();//使用参数列表声明不带参数的构造函数
Point(int, int);//使用参数列表声明带2个参数的构造函数
}; Point::Point() :x(), y()//定义不带参数的构造函数
{
cout << "Initializing default" << endl;
} Point::Point(int a, int b) : x(a), y(b)///定义带2个参数的构造函数
{
cout << "Initializing " << a << "," << b << endl;
} void main()
{
Point *ptr1 = new Point;
Point *ptr2 = new Point(, ); delete ptr1;
delete ptr2;
}

例4.7

设计构造函数的默认参数。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point
{
private:
int x, y;
public:
Point(int = , int = );//声明两个参数均为默认参数
}; Point::Point(int a, int b) : x(a), y(b)//定义构造函数
{
cout << "Initializing " << a << "," << b << endl;
} void main()
{
Point A;//构造函数产生对象A
Point B(, );//构造函数产生对象B
Point C[];//构造函数产生对象数组C
}

例4.8

使用Point类演示建立和释放一个动态对象数组的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point
{
private:
int x, y;
public:
Point(int = , int = );//声明两个参数均为默认参数
~Point();//声明析构函数
}; Point::Point(int a, int b) : x(a), y(b)//定义构造函数
{
cout << "Initializing " << a << "," << b << endl;
} Point::~Point()//定义析构函数
{
cout << "Destrucor is active" << endl;
} void main()
{
Point *ptr = new Point[];
delete[]ptr;
}

例4.9

演示调用构造函数、复制构造函数及析构函数的综合例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point
{
private:
int X, Y;
public:
Point(int a = , int b = )//构造函数
{
X = a;
Y = b;
cout << "Initializing" << endl;
}
Point(const Point &p);//复制构造函数
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
void Show()
{
cout << "X=" << X << ",Y=" << Y << endl;
}
~Point()
{
cout << "delete..." << X << "," << Y << endl;
}
}; Point::Point(const Point &p)//定义复制构造函数
{
X = p.X;
Y = p.Y;
cout << "Copy Intializing " << endl;
} void display(Point p)//Point类的对象作为函数的形参
{
p.Show();
} void disp(Point&p)//Point类对象的引用作为函数的形参
{
p.Show();
} Point fun()//函数的返回值为Point类的对象
{
Point A(, );
return A;
} void main()
{
Point A(, );//对象A
//第1次调用复制构造函数
Point B(A);//(1)用A初始化B
Point C(, );//对象C cout << "called display(B)" << endl;
//第2次调用复制构造函数
display(B);//(2)对象B作为display的实参 cout << "Next..." << endl;
cout << "called disp(B)" << endl; disp(B); cout << "call C=fun(C)" << endl;
//第3次调用复制构造函数
C = fun();//(3)fun的返回值赋给对象C cout << "called disp(C)" << endl; disp(C); cout << "out..." << endl;
}

例4.10

构造一个求4个正整数中最大者的类Max,并用主程序验证它的功能。

 class Max//声明类
{
private://封装数据成员和成员函数
int a, b, c, d;//数据成员
int Maxi(int, int);//只允许类内部的成员函数调用
public://对外界的接口
void Set(int, int, int, int);//设置对象初值
int Maxi();//求最大值
}A[];//声明类的对象数组,定义结束
//类中成员函数的实现
int Max::Maxi(int x, int y)//求两个数的最大值
{
return (x > y) ? x : y;
} void Max::Set(int x1, int x2, int x3 = , int x4 = )//使用两个默认参数
{
a = x1;
b = x2;
c = x3;
d = x4;
} int Max::Maxi()//求自己类中四个数的最大值
{
int x = Maxi(a, b);//x和y为Maxi()函数的局部整数对象
int y = Maxi(c, d);
return Maxi(x, y);
} #define _SCL_SECURE_NO_WARNINGS #include <iostream> using namespace std;
//主程序
void main()
{
A[].Set(, , , );//为数组对象A[0]置初值
A[].Set(, , );//为数组对象A[1]置初值
A[].Set(, );//为数组对象A[2]置初值 for (int i = ; i < ; i++)//输出对象求值结果
{
cout << A[i].Maxi() << " ";
}
}

例4.11

使用对象成员的例子。

 #define _SCL_SECURE_NO_WARNINGS

 #include <iostream>

 using namespace std;

 class Point//定义点类
{
int x, y;
public:
void Set(int a, int b)
{
x = a;
y = b;
}
int Getx()
{
return x;
}
int Gety()
{
return y;
}
}; class Rectangle//在矩形类里使用Point类的成员
{
Point Loc;//定义一个Point类的对象作为顶点
int H, W;//H为高,W为宽
public:
void Set(int x, int y, int h, int w);
Point *GetLoc();//声明返回Point类指针的成员函数
int GetHeight()
{
return H;
}
int GetWidth()
{
return W;
}
}; void Rectangle::Set(int x, int y, int h, int w)
{
Loc.Set(x, y);//初始化坐标顶点
H = h;
W = w;
} Point *Rectangle::GetLoc()//返回类型Point *,作为Rectangle的成员函数
{
return &Loc;//返回对象Loc的地址
} void main()
{
Rectangle rect; rect.Set(, , , ); cout << rect.GetHeight() << "," << rect.GetWidth() << ","; Point *p = rect.GetLoc();//定义Point类的指针对象p并初始化 cout << p->Getx() << "," << p->Gety() << endl;
}

04737_C++程序设计_第4章_类和对象的更多相关文章

  1. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  2. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  3. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  4. 《Pyhton语言程序设计》_第7章_对象和类

    #7.2.1_定义类 一个类的功能:数据域.定义方法.初始化程序 初始化程序总是被命名为:_ _init_ _ (两个连续的下划线) #7.2.4_self参数 #self参数是指向对象本身的参数,那 ...

  5. 04737_C++程序设计_第3章_函数和函数模板

    例3.1 传对象不会改变原来对象数据成员值的例子. #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <str ...

  6. 04737_C++程序设计_第10章_面向对象设计实例

    10.6.2 使用包含的参考程序及运行结果. 头文件cpp10.h 源文件cpp10.cpp 源文件Find10.cpp 头文件cpp10.h #if ! defined(CPP10_H) #defi ...

  7. 04747_Java语言程序设计(一)_第3章_面向对象编程基础

    链式编程 每次调用方法后,返回的是一个对象 /* * 链式编程 * 每次调用方法后,返回的是一个对象 */ class Student { public void study() { System.o ...

  8. Java精选笔记_面向对象(慨念、类和对象)

    面向对象概念 在程序中使用对象来映射现实中的事物,使用对象的关系来描述事物之间的联系,这种思想就是面向对象. 相对于面向过程而言.是一种思想,强调的是功能的对象. 面向对象的本质:以类的方式组织代码, ...

  9. 《Java从入门到失业》第四章:类和对象(4.1):初识类和对象

    4类和对象 在第一章曾经简单介绍过,在面向对象的世界里,一切事物皆对象,当解决一个问题的时候,我们先会考虑这个问题会涉及到哪些事物,然后把事物抽象成类,当时还画了一张图如下: 从本章开始,我们一点一点 ...

随机推荐

  1. Async 与 Await 关键字研究

    1        Aynsc 和 Await 关键字的研究 在 .NET 4.0 以后,基于 Task 的异步编程模式大行其道,因其大大简化了异步编程所带来的大量代码工作而深受编程人员的欢迎,如果你曾 ...

  2. JavaEE Tutorials (25) - 使用Java EE拦截器

    25.1拦截器概述380 25.1.1拦截器类381 25.1.2拦截器生命周期381 25.1.3拦截器和CDI38125.2使用拦截器381 25.2.1拦截方法调用382 25.2.2拦截生命周 ...

  3. c# 数据导出成excel 方法总结 见标红部分

    public void ServiceOrderExport(string data) { StringBuilder sb = new StringBuilder(); Type entityTyp ...

  4. java 解析 xml (DOM方法全)

    Java 处理 XML 的三种主流技术及介绍 http://www.ibm.com/developerworks/cn/xml/dm-1208gub/ 这篇文章讲的比较详细,下面我主要介绍 dom方法 ...

  5. linux中的strings命令简介

    摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道 ...

  6. Android的Activity切换动画特效库SwitchLayout,视图切换动画库,媲美IOS

    由于看了IOS上面很多开发者开发的APP的视图界面切换动画体验非常好,这些都是IOS自带的,但是Android的Activity等视图切换动画并没有提供原生的,所以特此写了一个可以媲美IOS视图切换动 ...

  7. 每日一小练——Eratosthenes 筛选法

    上得厅堂.下得厨房,写得代码.翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Eratosthenes筛选法 内容: 求质数是一个非常普遍的问题,通常不外乎用数去除.除到不尽时,给定的数就是质数.可是 ...

  8. Ubuntu12.04下jamvm1.5.4+classpath-0.98成功执行 helloworld.class

    经过两天的努力,总于在ubuntu以下编译好classpath-0.98与jamvm1.5.4,并能成功的运行类文件:jamvm hellowold,当屏幕上打印出"hello world! ...

  9. 20个热门jQuery的提示和技巧

    以下是一些非常有用的jQuery提示和所有jQuery的开发技巧. 1.优化性能复杂的选择 查询DOM中的一个子集,使用复杂的选择时,大幅提高了性能: var subset = $("&qu ...

  10. 不使用TNS直连数据库的三种方式

    1.在当前目录下新建tnsnames.ora文件 如windows环境下,在C:\Users\Administrator目录下新建tnsnames.ora文件,内容如下:test =(descript ...