Skip to content

Commit

Permalink
添加友元函数,显示隐式初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee committed May 3, 2018
1 parent 62182b9 commit 8e05396
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 23 deletions.
20 changes: 19 additions & 1 deletion chapter_10/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@
"array": "cpp",
"istream": "cpp",
"typeinfo": "cpp",
"string": "cpp"
"string": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"limits": "cpp",
"new": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"type_traits": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
55 changes: 45 additions & 10 deletions chapter_10/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

using namespace std;

template <typename T>
void show(T x)
{
cout << x;
}


Stock::Stock()
{
Expand All @@ -17,9 +13,26 @@ Stock::Stock()
_b = 0;
}

Stock::Stock(const string & co, long a1, double b1)
Stock::Stock(double n)
{
cout << "Stock::Stock(const string & co, long a1, double b1)\n";
cout << "Stock::Stock(double n)\n";
_company = "no name";
_a = 0;
_b = 0;
}

//只能显示转换
Stock::Stock(int n)
{
cout << "Stock::Stock(int n)\n";
_company = "no name";
_a = 0;
_b = 0;
}

Stock::Stock(const string & co, double a1, double b1)
{
cout << "Stock::Stock(const string & co, double a1, double b1)\n";
_company = co;
_a = a1;
_b = b1;
Expand All @@ -34,8 +47,9 @@ void Stock::show(void) const
{
cout << "void Stock::Tshow(void)\n";
cout << "_company: " << _company << '\n'
<< "_a: " << _a << '\n'
<< "_b: " << _b << endl;
<< "_a: " << _a << endl
<< "_b: " << _b << endl << endl;

}

const Stock & Stock::topval(const Stock & s) const
Expand All @@ -52,4 +66,25 @@ Stock Stock::operator+(const Stock & s) const
stock._a += s._a;
stock._b += s._b;
return stock;
}
}

Stock Stock::operator*(double m) const
{
cout << "Stock::operator*(double m) const\n";
return Stock(_company, this->_a * m, this->_b * m);
}

Stock operator*(double m, const Stock & n)
{
cout << "operator*(double m, const Stock & n)\n";
return n*m;
}

std::ostream & operator<<(std::ostream & os, const Stock & s)
{
cout << "Stock & operator<<(std::ostream & os, const Stock & s)\n";
cout << "_company: " << s._company << '\n'
<< "_a: " << s._a << endl
<< "_b: " << s._b << endl << endl;
return os;
}
15 changes: 12 additions & 3 deletions chapter_10/header/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@
#include <string>

template <typename T>
void show(T x);
void show(T x)
{
std::cout << x;
}

class Stock
{
private:
std::string _company;
long _a;
double _a;
double _b;

public:
Stock();
Stock(const std::string & co, long a1 = 1, double b1 = 0);
Stock(double n);
explicit Stock(int n);

Stock(const std::string & co, double a1 = 1, double b1 = 0);
~Stock();
void show(void) const; //当定义常量的类时,可以调用该方法
const Stock & topval(const Stock & s) const;
Stock operator+(const Stock & s) const; //重载加法
Stock operator*(double m) const; //重载乘法
friend Stock operator*(double m, const Stock & n); //重载超过一个参数,则只能写成友元函数
friend std::ostream & operator<<(std::ostream & os, const Stock & s);
};

#endif
69 changes: 60 additions & 9 deletions chapter_10/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ using namespace std;
void class_test(void);
void class_this_test(void);
void class_operator_test(void);

void friend_test(void);

int main(int agrs, char *argv[])
{
//class_test();
class_test();
// class_this_test();
class_operator_test();

//class_operator_test();
//friend_test();

cin.get();
cin.get();
return 0;
}

void mycout(void)
{
show("\n------------------------------------------------------\n");
}

//构造函数与析构函数,只要变量释放,就调用析构函数
void class_test(void)
{
Expand All @@ -52,18 +57,37 @@ void class_test(void)
cout << endl;
}

mycout();
{
Stock stock{"lee", 1, 2};
stock.show();
cout << endl;
}

mycout();
{
Stock stock1 = {"Bluce", 3, 4};
stock1.show();
cout << endl;
}

//可以隐式转换
mycout();
{
Stock mycat;
mycat = 1.0; //只有接受一个变量的构造函数才能这样使用,隐式转换
}

//只能显示转换
mycout();
{
Stock p1(5); //显示初始化

Stock p2;
p2 = 10; //这里调用的是 Stock(double n); 而不是Stock(int n);原因是Stock(int n)被声明成显示初始化,
//这里使用隐式转换,所以调用了Stock(double n)来代替
p2 = (int)10; //4.8版本这里仍使用Stock(double n)
}
}

//this指针指向调用类自身的地址
Expand All @@ -77,8 +101,35 @@ void class_this_test(void)
//重载运算符测试
void class_operator_test(void)
{
Stock stock1("Lee",1,1);
Stock stock2("hh", 2, 2);
stock1 = stock1 + stock2;
stock1.show();
}
{
Stock stock1("Lee",1,1);
Stock stock2("hh", 2, 2);
stock1 = stock1 + stock2;
stock1.show();
}

mycout();
{
Stock stock1("Lee",1,1);
cout << "stock1:\n" << stock1;
}
}

//friend友元函数测试,重载超过一个参数必须写成友元函数,而非成员函数
void friend_test(void)
{
{
Stock stock1("Lee", 1, 1);
stock1 = stock1 * 0.2;
stock1.show();
}

mycout();
{
Stock stock1("Lee", 1, 1);
stock1 = 0.3 * stock1;
stock1.show();
}
}


0 comments on commit 8e05396

Please sign in to comment.