chdoca的个人博客分享 http://blog.sciencenet.cn/u/chdoca

博文

C++Primer学习笔记

已有 3056 次阅读 2012-5-2 20:10 |个人分类:学习心得|系统分类:科研笔记| 学习

第一章     快速入门(5.2

本章主要介绍了C++入门阶段的一些基础知识,包括编译环境、输入输出、控制结构、注释以及类的简要介绍。

知识点:

常用的编译器:GNU编译器和visual studio 编译器,调用GNU编译器的命令是:g++ prog.cc -0 prog,在UNIX下生成prog可执行文件,在window环境下生成prog.exe可执行文件。微软编译器采用的编译命令为:cl –GX prog.cppIDE为集成开发环境,本身包含编译器。编译之后默认产生aa.exe的可执行文件,运行此文件为a.exe(window)a.out(unix),在UNIX环境下运行常需要指出目录,./a.out表示可执行文件位于当前目录下。

标准输入输出:cin coutcinistream类的对象,coutostream类的对象,istreamostream都是iostream标准库中的类成员。<<>>分别为输出和输入流操作符,cin>>a表示将输入存入变量a中,运行之后返回cin类变量;cout<<a表示将a的值放入输出缓冲区,运行之后返回cout类变量。endl为操纵符,用于刷新缓冲区,以便及时显示输出对象。C++采用的是标准命名空间,C采用的全局命名空间,cincoutendl都是iostream空间中定义的符号,因此在使用时要加上std::,以表示使用的是iostream中的对象。

类是C++的一个重要特征,也是面向对象程序语言的一个体现。可以定义自己需要的类,定义完之后,可以对类进行各种操作,可以用此类类型定义变量。对类的各种操作是类的创建者定义的。可以定义函数执行类的加减、比较和输入输出。

 

程序分析:

Sales_item.h

 

#ifndef SALESITEM_H

#define SALESITEM_H

 

// Definition of Sales_item class and related functions goes here

 

 

#include <iostream>

#include <string>

 

class Sales_item {

friend bool operator==(const Sales_item&, const Sales_item&);

// other members as before

public:

    // added constructors to initialize from a string or an istream

    Sales_item(const std::string &book):

              isbn(book), units_sold(0), revenue(0.0) { }

    Sales_item(std::istream &is) { is >> *this; }

    friend std::istream& operator>>(std::istream&, Sales_item&);

    friend std::ostream& operator<<(std::ostream&, const Sales_item&);

public:

    // operations on Sales_item objects

    // member binary operator: left-hand operand bound to implicit this pointer

    Sales_item& operator+=(const Sales_item&);

    // other members as before

   

public:

    // operations on Sales_item objects

    double avg_price() const;

    bool same_isbn(const Sales_item &rhs) const

        { return isbn == rhs.isbn; }

    // default constructor needed to initialize members of built-in type

    Sales_item(): units_sold(0), revenue(0.0) { }

// private members as before

private:

    std::string isbn;

    unsigned units_sold;

    double revenue;

 

};

 

 

// nonmember binary operator: must declare a parameter for each operand

Sales_item operator+(const Sales_item&, const Sales_item&);

 

inline bool

operator==(const Sales_item &lhs, const Sales_item &rhs)

{

    // must be made a friend of Sales_item

    return lhs.units_sold == rhs.units_sold &&

           lhs.revenue == rhs.revenue &&

         lhs.same_isbn(rhs);

}

 

inline bool

operator!=(const Sales_item &lhs, const Sales_item &rhs)

{

    return !(lhs == rhs); // != defined in terms of operator==

}

 

using std::istream; using std::ostream;

 

// assumes that both objects refer to the same isbn

inline

Sales_item& Sales_item::operator+=(const Sales_item& rhs)

{

    units_sold += rhs.units_sold;

    revenue += rhs.revenue;

    return *this;

}

 

// assumes that both objects refer to the same isbn

inline

Sales_item

operator+(const Sales_item& lhs, const Sales_item& rhs)

{

    Sales_item ret(lhs);  // copy lhs into a local object that we'll return

    ret += rhs;           // add in the contents of rhs

    return ret;           // return ret by value

}

 

inline

istream&

operator>>(istream& in, Sales_item& s)

{

    double price;

    in >> s.isbn >> s.units_sold >> price;

    // check that the inputs succeeded

    if (in)

        s.revenue = s.units_sold * price;

    else

        s = Sales_item();  // input failed: reset object to default state

    return in;

}

 

inline

ostream&

operator<<(ostream& out, const Sales_item& s)

{

    out << s.isbn << "t" << s.units_sold << "t"

        << s.revenue << "t" <<  s.avg_price();

    return out;

}

 

inline

double Sales_item::avg_price() const

{

    if (units_sold)

        return revenue/units_sold;

    else

        return 0;

}

 

 

#endif


Sales_item.h
中定义了Sales_item类以及类对象的一系列运算。对类的操作主要是通过重载运算符实现的。程序中主要对六个运算符实现了重载,非别是:==<<>>++=!=,针对不同的运算符操作数构造相应的运算符重载函数。

其中==<<>>重载函数定义为friend类型,friend为友元类型,当函数用到类成员时常常需要定义为friend类型;+重载函数定义为Sales_item类的成员函数,因为+=运算符的左右操作数均为Sales_item&,编译时会将右操作数累加在左操作数上,并返回指向左操作数的隐含指针this!=+重载函数定义成类外部函数,因为他们没有用到类成员。

this是一个指向类对象的隐含指针,当调用类的函数并且此函数用到类的其它变量时,系统会自动把此类的地址赋给this指针,从而实现对类中变量的调用。这就好比你进入到一个房间,看到房间里的桌子,如果要你给出桌子的位置坐标,你必须首先得到房间的坐标才行。

&C++里表示“引用”,表示给对象里外一个名字,引用其实相当于C里面的指针,但又不同与指针,他与对象是相同的数据类型。如 int &a=b,相当于给变量b取另外一个名字aab同类型,改变ab都能改变相应的值。引用常用在函数的形参中。

 

inline

Sales_item& Sales_item::operator+=(const Sales_item& rhs)

{

    units_sold += rhs.units_sold;

    revenue += rhs.revenue;

    return *this;

}

该函数返回一个Sales_item引用类型变量,实现+=运算符的重载。+=有两个操作数,但左操作数用指向Sales_item类型的指针this来指定且是隐藏的,返回值为*this,即将this指向的类对象返回。



https://wap.sciencenet.cn/blog-691244-566188.html

上一篇:关于opengl坐标变换的一点体会
下一篇:C++Primer学习笔记
收藏 IP: 210.77.11.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-28 05:45

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部