Boolan* C++课程第一周笔记

来源:互联网 发布:巨杉数据库好用吗 编辑:程序博客网 时间:2024/06/01 09:31
第一周
20170407
类:
带指针
1、 不带指针
Object Based(基于对象):
单一对象
Object Oriented(面向对象):
多个有关联的对象

语言学习:
语言+标准库

头文件写法(防卫式声明):
#ifndef _COMPLEX_
#define _COMPLEX_
//前置声明

//类的声明

//类的定义


#endif 作用:无重复include 包含
Inline(内联)函数:
若函数在class内定义完成,便自动成为inline“候选人”。(最后不一定是,取决于编译器的处理)
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
//template <typename T>;
class complex
{
public:
void setre(double num);
void setim(double num);
double imag()const;
double real()const{ return re; }
private:
double re, im;

};
inline double complex::imag()const
{
return im;

}
inline void complex::setre(double num)
{
this->re = num;

}
inline void complex::setim(double num)
{
this->im = num;
}

#endif
带默认参数的默认构造函数
构造函数
尽量所有的参数、返回值都传递引用
传递引用加const表示该值不能被改变: complex & operator +=(const complex&);
函数后面加上const
double real () const { return re;}
不会改变数据内容
Const类对象调用的成员函数也必须是 const函数
带默认参数的默认构造函数

complex(double r = 0, double i = 0) :re(r), im(i){}//带默认参数的默认构造函数
不带指针的类多半不用析构函数
函数重载
double imag(double r){ re = r; }//重载
double imag()const;
但在编译后的实际名称不同。
易错:
以下构造函数不能重载:
complex(double r = 0, double i = 0) :re(r), im(i){}
complex() :re(0), im(0){}

友元函数
class complex
{
public:
complex (double r=0,double i=0):re(r),im(i){}
complex& operator += ( const complex&);
double real () const {return re;}
doube imag () const {return im;}
private:
double re,im;
friend complex& _doap1 (complex *,const complex&);
};

inline complex& _doap1 (complex* fir,const complex& r)
{//自由取得friend的private的成员
fir->re+=r.re;
fir->im+=r.im;
return *this;

}
相同class的各个objects互为friends(友元函数)
class complex
{
public:
complex (double r=0,double i=0):re(r),im(i){}

int func (const complex ¶m){return param.re+param.im;}
private:
double re,im;
};

{
complex c1(2,1);
complex c2;
c2.func(c1);
}
例:
complex.h

class complex
{
public:
friend class friend_complex;
complex(double r = 0, double i = 0) :re(r), im(i){}//带默认参数的默认构造函数
void setre(double num);
void setim(double num);
double imag(double r){ re = r; }//重载
double imag()const;
double real()const{ return re; }
private:
double re, im;

};
class friend_complex//友元函数
{
public:
void getvalue(const complex&s){ fri =s.re ;fim = s.im; }
double outfri()const{ return fri; }
double outfim()const{ return fim; }
private:
double fri, fim;
};


inline double complex::imag()const
{
return im;

}
inline void complex::setre(double num)
{
this->re = num;

}
inline void complex::setim(double num)
{
this->im = num;
}
#include<iostream>
#include"complex.h"
using namespace std;
int main()
{
const complex cm(1.5,2.55);
friend_complex fcm;
fcm.getvalue(cm);
cout << fcm.outfri()<< " " << fcm.outfim()<< endl;
system("pause");

}
20170409

操作符重载
返回值可以用于操作符的连串使用

特殊的操作符用全局函数
“<<”操作符应定义为非成员函数
ostream&
operator<< (ostream& os,const complex& x)//参数次序决定cout的位置
{
return os <<’(‘<<real(x)<<’,’<<imag(x)<<’)’;
}
complex c1;
complex c2;
cout<<c1<<c2;
过称谓c1先进行操作符处理,再c2进行操作符处理
*另类
#include<iostream>
#include"operator.h"
using namespace std;
ostream&
operator << (complex&x, ostream&os)
{
return os << '(' << x.real() << ',' << x.imag() << ')';
}
int main()
{
complex cm(1.5,2.55);
cm<<cout;
system("pause");

}
成员函数的写法(有this指针)
//操作符重载
complex& _doapl(complex*ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
complex& operator +=(const complex&r){ return _doapl(this, r); }
#include<iostream>
#include"complex.h"
using namespace std;
int main()
{
complex cm(1.5,2.55);
complex cm2(2.5, 5.4);
friend_complex fcm;
fcm.getvalue(cm);//
cm += cm2+=cm2;
cout << cm.real()<< " " << cm.imag()<< endl;
system("pause");

}
非成员函数的写法
inline complex//非引用,因为是局部变量
operator + (const complex& x,const complex&y)
{
return complex(real(x) + real(y),imga (x) + imag (y));
}
inline complex//非引用
operator+(const complex&x,double y)
{
return complex (real(x) + y,imag (x);
}
typename();//构造临时对象,进行到下一行结束生命
return 不用reference 语法分析
传递者无需知道接受者是以reference形式接受;
不含static成员函数的对象调用成员函数的方式
complex c1,c2,c3;(real()不是static类型)
cout<<c1.real();

cout<<complex::real(&c1);//相当于传入this指针
static
静态成员函数 无this指针,只能处理静态成员
两种调用方式:
1、 通过object调用
2、 通过class name 调用
#ifndef _LYFSTATIC_
#define _LYFSTATIC_
class Account{
public:
static double m_rate;//脱离对象的,属于类的
static double set_rate(const double&x){ m_rate = x; return x; }
};

#endif
#include<iostream>
using namespace std;

#include"head.h"
double Account::m_rate = 8.0;//初值化
int main()
{

Account a;
a.set_rate(7.0);
cout << a.m_rate;
Account::set_rate(5.0);
cout << Account::set_rate(5.0);
cout << a.m_rate;
system("pause");
return 0;

}

结论:可以看出,直接通过class name调用时,可以直接改变之前object的static公有成员。私有成员不能通过classname调用
把构造函数放在private内(与static有关):
//只有一个对象的类

Class A{
public:
static A& getInstance();
setup(){…}
private:
A();
A(const A&rhs);

};
A& A::getInstance()
{
static A a;//只对这个函数有用
return a;
}
调用函数:
A::getInstance().setup();//使用class name调用
例:
#ifndef _LYFSTATIC_
#define _LYFSTATIC_
class Account{
public:
Account(){}
static Account& set_rate(const double&x);
static double m_rate;
static Account&creatrAccount();
private:

Account(const double&snu){ m_rate = snu; }

};
Account& Account::set_rate(const double&x)
{
static Account sob(x);
return sob;

}
#endif
#include<iostream>
using namespace std;

#include"head.h"
double Account::m_rate = 8.0;
int main()
{

Account::set_rate(2.5);
cout << Account::m_rate;
Account a;
cout << a.m_rate;

system("pause");
return 0;
}
class template
#ifndef _LYFSTATIC_
#define _LYFSTATIC_
template <typename T>

class Account{
public:
Account(){}
Account(const T &snu):m_rate(snu){ }

private:
T m_rate;
};

#endif
#include<iostream>
using namespace std;

#include"head.h"
int main()
{
Account<double> a(2.5);
system("pause");
return 0;

}
优秀模板例:
#ifndef _LYFSTATIC_
#define _LYFSTATIC_


class stone{
public:
stone(int w, int h, int we) :_w(w), _h(h), _weight(we){}
bool operator<(const stone&s)const
{
return _weight < s._weight;
}
private:
int _w, _h, _weight;
};
template <class T>//函数模板,不必明确指出类型
inline
const T & min(const T &a, const T &b)
{
return b < a ? b : a;
}
#endif
#include<iostream>
using namespace std;

#include"head.h"
int main()
{
stone r1(2, 3,3),r2(2,2,5);
min(r1, r2);
system("pause");
return 0;
}
0 0
原创粉丝点击