c ++ object-oriented programming two notes

Classes and class distinction no pointers # with a pointer.

 

1. Add the header file guard defense declared, to prevent the introduction of duplicate headers     

2. The layout of header files

Including pre-declaration, the class declaration, the class definition

 

chestnut:

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&);
};
template <typename T>
class complex
{
public:
	complex(T r = 0, T i = 0)
		:re(r), im(i)
	{

	}
	complex& operator += (const complex&);
	T real() const { return re; }
	T imag() const { return im; }
private:
	T re, im;

	friend complex& _doapl(complex*, const complex&);
};

Note:

1. If the function is completed within a defined class body, inline become candidate. inline classes written before the outer body to achieve.

2. access level access level: public private, etc.

3.ctor constructors: a default argument initial value column assignment (not initialization data initialization in the constructor in the initial column (the former is initialized, the latter assignment, although the effect is the same, but with different timing and efficiency))

4. constructor can have a number of heavy-duty (compiler generates a unique name) be careful not to let the call is ambiguous (same function overloading)

5.ctor placed into the private area (e.g. singleton)


class A
{
public:
	static A& getInstance();
	setup(){...}
private:
	A();
	A(const A& rhs);
};

A& A::getInstance()
{
	static A a;
	return a;
}

 

6. Constant [] const member functions this modification does not change the content of this [fee] if not a constant member functions the user can not use the constant object to call.

7. parameter passing: -pass by value- vs. -pass by reference (to const) - 

Try to pass by reference.

8. The return value is passed: return by value vs. return by reference (to const)

The return value also try to pass by reference (note the case of temporary objects)

9.friend (friend)

inline complex& _doapl(complex* ths, const complex& r)
{
	ths->re = r.re;
	ths->im = r.im;
	return *ths;
}

 Various objects of the same class mutual friends friend

10. The operator overloading

Member functions and non-member function overloading, etc.

11. transmitters do not need to know the recipient is received in the form refrence

inline complex&
_doapl(complex* ths, const complex& r)
{

    return *ths;
}

inline complex&
complex::operator +=(const complex& r)
{
    return _doapl(this, r);
}

{


 c3 += c2+=c1;
}

12.local object can not return a pointer or reference

13.typename (); temporary object anonymous object

14. overloaded <<

ostream&
operator << (ostream& os, const complex& x)
{
    return os << "(" << real(x) << "," << imag(x) << ")";
}

 

Published 44 original articles · won praise 5 · Views 1397

Guess you like

Origin blog.csdn.net/qq_33776188/article/details/104621234