c++基础 类的设计
来源:互联网 发布:规律转移矩阵 编辑:程序博客网 时间:2024/06/05 23:51
Designing Classes
When you work with structures or classes in C++, it is important to keep in mid that the definition introduces a new types and doesn't in itself declare any variables.
Once you have the definition, you can then use the type name to declare variables.
定义一个点point
Point.h
#ifndef POINT_H#define POINT_H#include <string>class Point {public: Point(); Point(int xc, int yc); int getX(); int getY(); std::string toString();private: int x; int y;};#endif // POINT_H
Point.cpp
#include "point.h"Point::Point(){ x = 0; y = 0;}Point::Point(int xc, int yc){ x = xc; y = yc;}int Point::getX(){ return x;}int Point::getY(){ return y;}std::string Point::toString(){ return "(" + integerToString(x) + ", " + integerToString(y) + ")";}
operator overloading
Operator overloading makes it possible to simplify this process even further.
C++ already overloads the stream insertion operator << so that it can display strings along with the primitive types.
In almost all cases, the function name consists of the keyword operator followed by the operator symbol.
The hardest part of coding is the operator<< function is writing its prototype.
Putting all these observation together suggests that the prototype for the overloaded version of operator<< must look like this:
ostream & operator<<(ostream &os, Point pt);
ostream & operator<<(ostream &os, Point pt) { os << pt.toString(); return os;}
These references are legal in C++ because the definition in the private section of a class are private to the class and not to the object. The code for the methods of a class can refer to the instance variables of any object of that class.
一种是内部成员定义,一种是外部函数定义。
外部函数定义时,需要函数是这个类的friend.
friend bool operator==(Point p1, Point p2);
If two classes want access to the private variables of the other, each class must explicitly declare the other class as a friend.
Thus, to overload the prefix form of the ++ operator for the Direction type, you would define the function
Direction operator++(Direction & dir) { dir = Direction(dir + 1); return dir;}
To overload the suffix form, you would instead define the function
Direction operator++(Direction &dir, int) { Direction old = dir; dir = Direction(dir + 1); return old;}
To use rational numbers(分数)in C++, you have to define a new class to represent them.
Experience and practice are the best teacher, but following a general design framework can help you get you start along this path.
1. Think generally about how clients are likely to use the class.
2. Determine what information belongs in the private state of each object.
3. Define a set of constructors to create new objects.
4. Enumerate the operation that will become the public method of the class.
5. Code and test the implementation.
The names for these variables are shorted versions of the mathematical terms numerator and denominator, which refer to the upper and lower parts of a fraction.
int num;int den;The situation is much better in C++. In C++, you implement rational arithmetic by overloading the operator +, -, *, and / to work with Rational objects.
Rational operator+(Rational r1, Rational r2);
- c++基础 类的设计
- <C/C++基础> 设计一个复数类(拷贝函数的使用)
- Objective - C基础: 第一天 - 10.如何合理的设计一个类
- C++---基础篇(结构--设计自己的数据类型)
- C语言编程基础,手机购物程序的设计
- 郝斌的C语言基础 115 合理设计函数
- 应用的基础设计
- 小白零基础学习C++(1)基础类的实现
- objective-c的常用基础类
- objective C 基础 类的组成
- Objective-C 类的基础程序
- Objective-C基础-类方法的学习
- C基础的复习
- ******************** C语言的基础 ********************
- C语言的基础
- c的基础问题
- 图的基础(C++)
- 也谈单件模式类的设计(C++)
- ubuntu与win7在局域网下共享文件
- Cenos7_Install_Mysql5.7
- CSDN日报20170411
- 全息电话会议开源项目LiveScan3D-Hololens
- PAT乙级(Basic Level)真题 数素数(素数筛法)
- c++基础 类的设计
- javascript中模拟实现java语言中的map
- hadoop的hive sql语句总结
- JS深入之你知道点号(.)是怎么玩的吗?(二)
- tomcat8出现Could not initialize class sun.awt.X11GraphicsEnvironment问题
- C语言实现单链表
- Activemq管理和基本介绍
- iOS网络编程之系统架构
- Linux(二) 之 入门vi