c++基础 类的设计

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值