1.5 自有数据类型

1.5.1 类cururncy

在学习数据结构时,除了C++自身提供的int ,float ,double等数据类型以外,更多的数据结构是我们自己需要定义的。而需要自己定义的数据类型就是自有数据类型。

在C++中定义自有数据类型的最灵活的方法就是使用C++的类(class)结构。

现需要你设定一个处理货币类型的currency的对象(也称为实例)。

这种对象有三个成员:符号,美元,美分。例如$2.35(2 美元,35美分,符号是+)。

对这种对象我们想要执行的操作如下:

·给成员赋值

·确定成员值

·两个对象相加

·增加成员的值

·输出


程序1-13 currency类声明


class currency  //声明了类的名称currency,其类的成员在其后的一对“{}”中。
{
    public:
        enum signType{
            plus;
            minus;
        }
        //构造函数
        currency(signType theSign = plus,
                 unsigned long theDollar = 0,
                 unsigned int theCents = 0);
        //构析函数
        ~currency() {}
        void setValue(signType, unsigned long, unsigned int);
        void setValue(double);
        signType getSign() const {return sign;};
        unsigned long getDollars() const {return dollars;}; 
        unsigned int getCents() const {return cnets;};
        currency add(const currency&) const;
        currency& increment(const currency&);
        void output() const;
    private:
        signType sign;          //对象的符号
        unsigned long dollars;  //对象的数量
        unsigned int cents;     //美分的数量
};

类的成员声明了两个部分:

1.公有(public)

用来声明操作类对象的成员函数,他们对用户是可见的,是用户与类对象交互的唯一手段

2.私有(private)

私有部分一般放简单的变量,可赋值的结构。他们对用户是不可见的。通查与实现细节有关。

“一个优秀的软件设计者会在私有部分声明数据成员”

程序分析:

1.公有部分:

        第一个成员函数:

        currency(signType theSign = plus,
                 unsigned long theDollar = 0,
                 unsigned int theCents = 0);

        与类名相同,这种函数名与类名相同的函数称作构造函数

 在本例中,构造函数有三个参数,其缺省值(默认选项)分别是plus,0和0。

创建一个currency类对象时,构造函数就会被自动调用。

创建currency类对象的方法有如下两种:

currency f,g(plus3,45),h(minus,10);

currurcy *m = new currency(puls,8,12)

 第一行声明了三个currency对象f,g,h。其中f用缺省值(plus,0,0)初始化,结果为$0.00。

第二个成员函数:

~currency() {}

        第二个成员函数是~currency函数,比类名多了一个~。这种成员函数成为析构函数。

每当一个currency类的函数超出作用域时,就会自动调用析构函数来删除这个对象。与构造函数一样,析构函数也没有返回值。 

 函数:

        void setValue(signType, unsigned long, unsigned int);
        void setValue(double);

这两个函数的名称相同,但是他们的签名是不一样的,所以导致他们的调用方法不一样。

g.setValue(minus,33,0);

h.setValue(20.52);

 成员函数(1):

        signType getSign() const {return sign};
        unsigned long getDollars() const {return dollars}; 
        unsigned int getCents() const {return cents};

        getSign、getDollars和getCents返回对象的相应数据成员,关键字const 指明这些对象时不会改变值的。这些函数被称为常量函数

成员函数(2):

        currency add(const currency&) const;
        currency& increment(const currency&);

add()把调用对象的货币值与参数对象的货币值相加,返回相加结果。

increment()将符号加到调用对象上。

add()返回的是对象的值。

incerment()返回的对象的引用。(可改变对象的值)


程序1-13没有指定复制构造函数,C++将使用缺省复制构造函数,仅仅复制数据成员。


作用域说明符 “::”用于指明该函数currency类的成员函数。因此currency::currency表示currency的构造函数(因为与类名相同)。而currency::output表示currency类的有output成员函数。

程序1-14实现了currency的构造函数。仅仅调用了具有三个成员函数的setValue来给数据对象成员初始化。

程序1-14 currency的构造函数

currency::currency(signType theSign,unsigned long theDollars,unsigned int theCents)
{//创建一个currency类的对象
    setValue(theSign,theDollars,theCents);
}

程序1-15 给私有数据成员赋值

//给私有成员赋值
void currency::currency(signType theSign,unsigned long theDollars,unsigned int theCents)
{//给调用对象的数据成员赋值
    if(theCents > 99) // 美分太多
        throw illegalParameterValue("Cents should be < 100");
    
    sign = theSign;
    dollars = theDollar;
    cents = theCents
}

void currency::setValue(double theAmount)
{//给调用的对象的数据成员赋值
    if(theAmount < 0) {sign = minus;theAmount = - theAmount;}
    else sign = plus;
    dollars = (unsigned long)theAmount;                         //取整数部分
    cents = (unsigned int)((theAmount + 0.001 - dollars) * 100);//提取两位小数
}

程序1-17 函数increment 和 output 

currency& currency::increment(const currency& x)
{//增加x
    *this = add(x);
    return *this;
}

void currency::output() const
{//输出调用对象的值
    if(sign == minus) cout << '-';
    cout << '$' << dollars << '.';
    if(cents < 10) cout << '0';
}

类currency的数据成员以及被设为私有,用户不能直接访问。因此,用户通过下面的语句直接改变私有数据是允许的。

h.cents = 20;
h.dollars =100;
h.sign = plus;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值