我是新手,不仅是想学习,而且是一定要学习。非常非常欢迎各位批评指导。
构造函数是c++中类的特殊成员函数,它首先是类的成员,其次属于类成员中的成员函数,(类的成员分数据成员和成员函数),再次它是特殊的成员函数(它不能指定返回类型)。它的存在的价值在于,创建类类型的对象时初始化对象的每一个数据成员。只要创建类类型的新对象,都要执行构造函数。
构造函数的调用,在源程序中表现在定义一个类类型的对象。假设有三个构造函数,构造函数的原型(也即函数接口)如下:Sales_item(),
Sales_item(const std::string &book), Sales_item(std::istream &is).
以下是我编的源程序代码,以试验调用这三种构造函数。
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item objectvar1; // 由于创建的对象objectvar1后面没有形参(也就没有括号),使用默认实参,调用Sales_item类的默认构造函数。
std::string bookisbn("123"); //使用显式实参"123"调用构造函数Sales_item(const std::tring& book),123是字符串格式,一定要打双引号。
Sales_item objectvar2("1234");
Sales_item objectvar3(bookisbn);
Sales_item objectvar4(std::cin); //从键盘输入参数,调用构造函数Sales_item(std::istream &is)来初始化对象objectvar4
std::cout << "objectvar1 is :\n" << objectvar1 << std::endl;
std::cout << "objectvar2 is :\n" << objectvar2 << std::endl;
std::cout << "objectvar3 is :\n" <<objectvar3 << std::endl;
std::cout << "objectvar4 is :\n" <<objectvar4 << std::endl;
return 0 ;
}
下面是类Sales_item的定义,假设放在头文件Sales_item.h中。此类基本摘自c++primer 第四版,稍做修改。
// Definition of Sales_item class and related functions goes here
// 类名Sale_item.类的数据成员:书号isbn,销售数量units_sold,此笔交易总收入revenue。类所支持的操作(接口):重载操作符==,重载的输入输出操作符>>、<<,重载的复合赋值操作符+=,
#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; // 声明一个const成员函数,表明隐含的this指针是指向const对象的。
bool same_isbn(const Sales_item &rhs) const // 定义成员函数same_isbn.
{ return isbn == rhs.isbn; }
// default constructor needed to initialize members of built-in type
Sales_item():isbn("No number"), units_sold(0), revenue(0.0) { } //No number 一定要用双引号。
// private members as before,定义3个数据成员。
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,根据ret后的形参lhs来初始化ret对象。
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