侯捷C++课程PPT及代码,动手学起来
#include #include "complex.h"
using namespace std;
ostream& operator << (ostream& os, const complex& x) { return os << '(' << real (x) << ',' << imag (x) << ')'; }
int main() { complex c1(2, 1); complex c2(4, 0);
cout << c1 << endl; cout << c2 << endl;
cout << c1+c2 << endl; cout << c1-c2 << endl; cout << c1*c2 << endl; cout << c1 / 2 << endl;
cout << conj(c1) << endl; cout << norm(c1) << endl; cout << polar(10,4) << endl;
cout << (c1 += c2) << endl;
cout << (c1 == c2) << endl; cout << (c1 != c2) << endl; cout << +c2 << endl; cout << -c2 << endl;
cout << (c2 - 2) << endl; cout << (5 + c2) << endl;
return 0; }
=
m_datam_data
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&);
};
class String
{
public:
String(const char* cstr = 0);
String(const String& str);
String& operator=(const String& str);
~String();
char* get_c_str() const { return m_data; }
private:
char* m_data;
};
(VC)
complexreleasedebugcomplex4*8 + 4complex52VC1652166412padreleasecomplexstring
VC
complex8double3complex3String
delete[]
String.h
#ifndef __MYSTRING__
#define __MYSTRING__
class String
{
public:
String(const char* cstr=0);
String(const String& str);
String& operator=(const String& str);
~String();
char* get_c_str() const { return m_data; }
private:
char* m_data;
};
#include <cstring>
inline
String::String(const char* cstr)
{
if (cstr) {
m_data = new char[strlen(cstr)+1];
strcpy(m_data, cstr);
}
else {
m_data = new char[1];
*m_data = '\0';
}
}
inline
String::~String()
{
delete[] m_data;
}
inline
String& String::operator=(const String& str)
{
if (this == &str)
return *this;
delete[] m_data;
m_data = new char[ strlen(str.m_data) + 1 ];
strcpy(m_data, str.m_data);
return *this;
}
inline
String::String(const String& str)
{
m_data = new char[ strlen(str.m_data) + 1 ];
strcpy(m_data, str.m_data);
}
#include <iostream>
using namespace std;
ostream& operator<<(ostream& os, const String& str)
{
os << str.get_c_str();
return os;
}
#endif
string_test.cpp
#include "string.h"
#include <iostream>
using namespace std;
int main()
{
String s1("hello");
String s2("world");
String s3(s2);
cout << s3 << endl;
s3 = s1;
cout << s3 << endl;
cout << s2 << endl;
cout << s1 << endl;
}
C++ () 1 2
AAA = XXX XXXAAA AAA
explicit /
explicit
class Test1
{
public:
Test1(int n)
{
num=n;
}//
private:
int num;
};
class Test2
{
public:
explicit Test2(int n)
{
num=n;
}//explicit()
private:
int num;
};
int main()
{
Test1 t1=12;//,
Test2 t2=12;//,
Test2 t2(12);//
return 0;
}
Test1int23Test1Test2explicit24
explicit
Fractiondoubledouble()double d = 4 + f4ffdouble()fdouble()0.64double4.6
Fraction+f+44FractionFraction+
Fractiondouble()Fractiondoublef+4
14Fraction+fFrction
24Fraction+fdoubleFrction
3
Franctionexplictd2 = f + 4fdouble0.644.64.6Fraction
C++ stl
T* px*->px
*->
listlist::iterator ite;listFoolistclass Toperator*()(*node).datanode__link_type__link_type__list_node<T>*TFoonode__list_node<Foo>*,(*node).dataFoo&(operator*())FooFoo*
()
STL
T1U1T2U2 U1U2T1T2STL
C++11
C++11
reference
ABCBA,CBABCAam_data1m_data2AaAaA::vfunc1()A::vfunc2()bBBAvfunc1()BB::vfunc1()BAvfunc2()BAA::vfunc2()cCCvfunc1()CC::vfunc1()CAvfunc2()BA::vfunc2()C
thisthis->Serialize()(*(this->vptr)[n])(this)