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);


0 0
原创粉丝点击