1. 程式人生 > >侯捷C++學習(一)

侯捷C++學習(一)

//c++學習
//標準庫非常重要
//要規範自己的程式碼
complex c1(2,1);
complex c2;
complex* pc = new complex(0,1);
string s1("Hello");
string s2("world");
string* ps

#include<iostream>
using namespace std;
int main() {
int i=7;
cout<<"i="<<i<<endl;
return 0;
}
#include<iostream>
#include"complex.h"//防衛式宣告
using namespace std;
int main() {
complex c1(2.1);
complex c2;
cout<<c1<<endl;
cout<<c2<<endl;
c2=c1+5;
c2=7+c1;
c2=c1+c2;
c2 +=3;
c2= -c1;
cout<<(c1=c2)<<endl;
cout<<(c1!=c2)<<endl;
cout<<conj(c1)<<endl;
return 0;
}
class complex {
public:
complex (double r=0,double i=0)
: re (r),im (i);//建構函式才有的初值列,
// { }
complex& operator +=(const complex&);
double real () const {
return re;
}
double imag () const {
return im;
}
private:
double re ,im;
friend complex&_doapl (complex*,const complex&);
}
{
complex c1(2,1);
complex c2;
complex* p = new complex(4);
//建構函式是沒有返回值的
}
//建構函式可以有很多個overloading過載同名可以存在過載
//常常發生在建構函式裡
class complex {
public:
complex (double r=0,double i=0)//
:re(r),im(i) {
}
complex () :re(0),im(0);
{}//不允許過載
complex& operator += (const complex&);
double real () const {
return re;
}
double imag () const {
return im;
}
private:
double re,im;
friend complex&_doapl (complex*,const complex&);
};
void real(double r) const {
re =r;
}
//建構函式在private區域
//表示這個函式是不可以被外界呼叫的
class complex {
public:
complex (double r = 0,double i = 0)
:re(r),im(i) {
}
complex& operator +=(const complex&);
double real () const {
return re;
}
double imag () const {
return im;
}
private:
double re,im;
friend complex&_doapl (complex*,const complex&);
};
{
complex c1(2,1);
complex c2;
...
}
//Singleton單份
//可以把建構函式寫在private裡面
//**在函式的後面加const class分類裡對於不改變資料的,在函式的後面加const
//比如把函式拿出來的
//對於上面的程式碼
{
complex c1(2,1);
cout<<c1.real();
cout<<c1.imag();
}

{
const complex c1(2,1);
cout<< c1.real();
cout<< c1.imag();
}
//要有const的話要都有const.周全的程式碼正規
//引數傳遞
class complex {
public:
complex (double r= 0, double i = 0)
:re (r) , im(i) {
}
complex operator += (const complex&);
double real () const {
return re;
}
double imag () const {
return im;
}
private:
double re,im;

friend complex&_dopal (complex*,const complex&);
};
ostrean&
operator<< (ostream& os, const complex& x) {
return os<< '(' <<real (x) <<','
<< imag (x) <<'(';
}
//儘量不要 pass by value 引用就是一個指標
//傳遞引用防止更改所以我們要pass by reference (to const)
//返回值的傳遞儘量by reference(to const)
calss complex {
public:
complex (double r = 0,double i = 0)
:re (r), im (i);
{ }
complex& operator += (const complex&);
double real () const {return re;}
double imag () const {return im;}
private:
double re,im;
friend complex&_dopal (complex*, const complex);
};
ostream&
operator << (ostream& os, const complex& x) {
return os << '(' <<real (x) << '.'
<< imag (x) << ')';
}
//儘量使用reference
class complex {
public:
complex (double r = 0,double i = 0)
:re(r),im(i);
{ }
complex& operator += (const complex&);
double real () const {
return re;
}
double imag () const {
return im;
}
private:
double re,im;
friend complex&_doapl (complex*, const complex);
};
inline complex&
_doapl (complex*,const complex& r) {
this->re += r.re;
this->im += r.im;
return *this;
}
//friend(友元)因為我們是朋友所以可以直接取得private裡面的re im
//private不讓外界取得re im
class complex {
public:
complex (double r = 0,double i = 0)
:re(r),im(i) {
}
int func(const complex& param) {
return param.re + param.im;
}
private:
double re,im;
};
{
complex c1(2,1);
complex c2;

c2.func(c1);
}
//相同的class的各個objects互為friends(友元)
//class body 外的各種定義
//do assignment plus
inline complex&
_doapl(complex* ths, const complex& r) {
ths->re += r.re;//第一引數將會被改動
ths->im += r.im;//第二引數將不會被改動
return *ths;
}
inline complex&
complex::operator += (const complex& r) {
return _doapl (this, s);
}
///什麼情況下不可以使用by reference
//c1+c2的時候+完的數值會被改變


//操作符過載—1,成員函式
//所有的成員函式都會帶有一個隱藏的引數 this
//不可以在引數列寫出來
inline complex&
_doapl(complex* ths,const complex& r) {
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex&
complex::operator +=(const complex& r) {
return _doapl (this,r);
}
{
complex c1(2,1);
complex c2(5);
c2 +=c1;
//??c3 += c2 += c1;
//要考慮到操作符過載,在設計函式的時候要考慮到問號裡連續使用的狀況
//連串使用的時候

}
//global函式
//class body之外的各種定義
inline double
imag(const complex& x) {
return x.imag ();
}
inline double
real(const complex& x) {
return x.real ();
}
{
complex c1(2,1);
cout << imag(c1);
cout << real(c1);
}
//operator overloading(操作符過載-2,非成員函式
inline complex
operator + (const complex& x, const complex& y) {
return complex (real (x) + real (y),
imag (x) + imag (y));
}
inline complex
operator + (const complex& x,double y) {
return complex (real (x) + y,imag (x));
}
inline complex
operator + (double x,const complex& y) {
return complex (x + real (y), imag (y));
}
--------->>>>> {
complex c1(2,1);
complex c2;
c2 = c1 + c2;
c2 = c1 + 5;
c2 = 7 + c1;
}
//上面的這些函式絕對不可以使用return by reference,
//因為,它們返回的必定是個local object
//也就是說加過的東西沒有放的地方,(x + real (y), imag (y))
//typename();
//生命短暫 temp object 臨時物件 return complex ();
//建構函式是有預設值的0
------------->>>>>>
inline complex
operator + (const complex& x)
{
return x;
}
inline complex
operator - (const complex& x)
{
return complex (-real (x), -imag (x););
}
{
complex c1 (2,1);
complex c2;
cout << -c1;
cout << +c1;
}
//正號和負號,只有一個引數的前面有+-號
// return complex (-real (x), -imag (x););
//臨時物件
//上面的那句是正號沒有變化,標準庫裡的,可以把它reference
--------->>>>>
inline bool
operator == (const complex& x,
const complex& y)
{
return real (x) == real (y)
&& imag (x) == imag (y);
}
inline bool
operator == ( const complex& x,double y)
{
return real (x) == y && imag (x) ==0
}
inline bool
operator == (double x, const complex& y)
{
return x == real (y) &&&imag (y) ==0;
}
{
complex c1(2,1);
complex c2;
cout << (c1 ==c2);
cout << (c1 == 2);
cout << (0 == c2);
}
----------->>
inline complex
conj (const complex& x)
{
return complex (real (x),-imag (x));
}
#include<iostream.h>
ostream&
operator << (ostream& os,const complex& x)
{
return os << '(' << real (x) << ','
<< imag (x) << ')';
}
{
complex c1(2,1);
cout << conj(c1);
cout << c1 << conj(c1);
}
//共軛複數