/** @file list3201.cpp */ /** Listing 32-1. Mystery Program */ #include <iostream> #include <ostream> struct point { point() : x_(0.0), y_(0.0) { std::cout << "default constructor\n"; } point(double x, double y) : x_(x), y_(y) { std::cout << "constructor(" << x << ", " << y << ")\n"; } double x_; double y_; }; int main() { point pt();// 此处存在二义性,好像是定义了一函数,函数返回类型是point,所以如果是要定义一个point对象pt,pt后面不能加括号(); point pt; // 正确调用默认的构造函数 std::cout<<pt.x_<<std::endl; // 调用point()默认构造函数 }
/** @file list3203.cpp */ /** Listing 32-3. Problem with istream Iterators */ #include <algorithm> #include <iostream> #include <istream> #include <iterator> #include <ostream> #include <vector> int main() { using namespace std; /// 如果是 vector<int> orig ( istream_iterator<int>(cin)), (istream_iterator<int>() ); /// 编译器会把 orig 当成是一个函数,有二义性,在编译时会处理成 vector<int> orig istream_iterator<int>(cin), istream_iterator<int>() /// 为避免歧义,在参数外面加上 括号 ,如下 vector<int> orig( ( istream_iterator<int>(cin) ), ( istream_iterator<int>() ) ); vector<int>::iterator zero(find(orig.begin(), orig.end(), 0)); vector<int> from_zero(zero, orig.end()); copy(from_zero.begin(), from_zero.end(), ostream_iterator<int>(cout, "\n")); }
/** @file list3206.cpp */ /** Listing 32-6. const Member Functions for Class point */ #include <cmath> // for sqrt and atan #include <cmath> #include <iostream> #include <ostream> struct point { /// Distance to the origin. double distance() const { std::cout<<"x="<<x<<std::endl; return std::sqrt(x*x + y*y); } /// Angle relative to x-axis. double angle() const { return std::atan2(y, x); } /// Add an offset to x and y. void offset(double off) { offset(off, off); } /// Add an offset to x and an offset to y void offset(double xoff, double yoff) { x = x + xoff; y = y + yoff; } /// Scale x and y. void scale(double mult) { this->scale(mult, mult); } /// Scale x and y. void scale(double xmult, double ymult) { this->x = this->x * xmult; this->y = this->y * ymult; } double x; double y; }; // void print_polar(point pt) //此处既可以调用非const方法,也可以调用const方法 void print_polar(point const& pt) { // point 对象的const引用 pt 只可以调用 const 方法, std::cout << "{ r=" << pt.distance() << ", angle=" << pt.angle() << " }\n"; } void print_cartesian(point const& pt) { std::cout << "{ x=" << pt.x << ", y=" << pt.y << " }\n"; } int main() { point p1, p2; double const pi = 3.141592653589792; p1.x = std::cos(pi / 3); p1.y = std::sin(pi / 3); print_polar(p1); print_cartesian(p1); p2 = p1; p2.scale(4.0); print_polar(p2); print_cartesian(p2); p2.offset(0.0, -2.0); print_polar(p2); print_cartesian(p2); }
#include<iostream> using namespace std; class Counter { int value; public: Counter(int v = 0) { value = v; } Counter(Counter& c) { value = c.value; cout << "拷贝构造函数被调用" << endl; } Counter& operator=(Counter& c) // 重载赋值运算符 { value = c.value; cout << "赋值运算符重载函数被调用" << endl; return *this; } void display() { cout << value << endl; } }; void main() { Counter countOne(5); countOne.display(); Counter countTwo = countOne; //创建新对象,拷贝构造函数被调用 countTwo.display(); countTwo = countOne; //对象已存在,赋值运算符重载函数被调用 countTwo.display(); }
#include<iostream> using namespace std; class Complex //复数类声明 { public: //外部接口 Complex(double r = 0, double i = 0) //构造函数 { real = r; imag = i; } void display() //输出复数 { cout << real; if (imag >= 0) cout << "+" << imag; else cout << imag; cout << "i" << endl; } Complex operator + (Complex &c2); //运算符+重载成员函数 Complex operator + (double r); //运算符+重载成员函数 private: //私有数据成员 double real; //复数实部 double imag; //复数虚部 }; Complex Complex::operator +(Complex &c2) //重载运算符+函数实现 { cout << "address : " << &c2 << std::endl; Complex temp; temp.real = real + c2.real; temp.imag = imag + c2.imag; return temp; } Complex Complex::operator + (double r) { Complex temp; temp.real = real + r; temp.imag = imag; return temp; } int main() //主函数 { Complex c1(1, 2), c2(3, 4), c3, c4, c5; //定义复数类的对象 cout << "c1 address " << &c1 << std::endl; cout << "c2 address " << &c2 << std::endl; cout << "c3 address " << &c3 << std::endl; cout << "c4 address " << &c4 << std::endl; cout << "c5 address " << &c5 << std::endl; cout << "c1 = "; c1.display(); cout << "c2 = "; c2.display(); c3 = c1 + c2; //使用重载运算符完成复数加法 cout << "c3 = c1+c2="; c3.display(); c4 = c1 + c2 + c3; //使用重载运算符完成复数加法, 顺序相当于(c1 + c2)+ c3 cout << "c4 = c1 + c2 + c3= "; c4.display(); c5 = c1 + 10; cout << "c5 = c5 + 10 = "; c5.display(); return 0; }