例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. NFC介绍

    简介 本文介绍Nokia设备所支持的近场通信技术(NFC)及相关的功能.旨在为使用 Qt/Symbian/Java™ API为Nokia手机开发应用的开发者 刚开始接触NFC开发时提供有用的信息. 什 ...

  2. QT5中如何使用QFtp类(这个类虽然没有被收录,但一直在更新)

    由于QT5对QML的支持有很大的改进,所以打算将原来基于QT4的程序移植到QT5上,在移植用QFtp类写的程序时傻眼了! Qt5 移除了 QFtp API,原因是其实现质量.QNetworkAcces ...

  3. 在QTableView中使用各种自定义委托

    QT的MVC(View/Delegate)模型十分强大,可以利用各种控件来对表格的输入进行限制,不过我以前一直没有过,这几天研究了一下,写个小例子,希望大家喜欢. 如果看不懂这个例子,请先看QT的自带 ...

  4. LoadRunner 技巧之THML 与 URL两种录制模式分析

    Loadrunner的Virtual User Generator 提供人脚本的录制功能,对于初学者来说,这大大的降低了编写脚本的门槛,loadrunner提供两种录制脚本的方式:Html_based ...

  5. Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏

    Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...

  6. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  7. iOS7 UIKit动力学-碰撞特性UICollisionBehavior 下

    上文讲到了为window加一个边界.实现碰撞的效果,接下来我们将提到一个托付方法: - (void)collisionBehavior:(UICollisionBehavior *)behavior ...

  8. iOS开发-使用Storyboard进行界面跳转及传值

    前言:苹果官方是推荐我们将所有的UI都使用Storyboard去搭建,Storyboard也是一个很成熟的工具了.使用Storyboard 去搭建所有界面,我们可以很迅捷地搭建出复杂的界面,也就是说能 ...

  9. Android 查看通讯录Contacts是否发生变化

    目的:确定通讯录是否发生变化 根据:參见ContactsContract.RawContacts类中的VERSION常量,该值是仅仅读的,当通讯录发生变化时,都会使该值变化 方法:version值是相 ...

  10. JAVASCRIPT——文字出现效果练习

    写一句诗.诗的最初状态是隐藏的,效果是让诗缓慢出现,直到显示完全 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...