T1
#include<iostream>
using namespace std;
class Fraction { //数据成员,访问控制属性默认是私有
int m_numerator = 0; // 分子默认为0; C++11
int m_denominator = 1; //分母默认为1;
public://公有成员函数
Fraction(int above = 0, int below = 1) : m_numerator(above), m_denominator(below) {
cout << "Constructor called" << endl;
}
Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator), m_denominator(rhs.m_denominator) {
cout << "Copy constructor called" << endl;
}
~Fraction(){
cout << "Dead" << endl;
}
void divide(){
int m = gcd(m_numerator, m_denominator);
m_numerator /= m;
m_denominator /= m;
};
int getnumerator() const {
return m_numerator;
};
int getdenominator() const{
return m_denominator;
};
Fraction operator /(const Fraction&b){
Fraction f(m_numerator*b.getdenominator(),m_denominator*b.getnumerator());
f.divide();
return f;
}
ostream& operator << (ostream& os,const Fraction &a){
return os << a.m_numerator << " / " << a.m_denominator << endl;
}
private:
int gcd(int a,int b){
return b == 0?a:gcd(b,a%b);
}
};
Fraction divide1(const Fraction& divident, const Fraction& divisor) {
return Fraction(divident.getnumerator() * divisor.getdenominator(), divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
Fraction result(divident.getnumerator() * divisor.getdenominator(), divident.getdenominator() * divisor.getnumerator());
return result;
}
Fraction makeCommon(const Fraction&a,const Fraction&b){
Fraction f(a.getnumerator()*b.getdenominator() + a.getdenominator()*b.getnumerator(),a.getdenominator() * b.getdenominator());
f.divide();
return f;
}
int main(){
Fraction f1(2,4);
cout << f1;
f1.divide();
cout << f1;
Fraction f2(4,5);
Fraction f = makeCommon(f1, f2);
cout << f;
Fraction ff = f1 / f2;
cout << ff;
return 0;
}

Constructor called
Copy constructor called
Constructor called
Constructor called
Constructor called
Constructor called
Copy constructor called
Copy constructor called
Constructor called
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
T2
#include<iostream>
#include<algorithm>
using namespace std;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
int find_k_byorder(int a[],int len,int k){
for(int i = 0;i < len;++i){
if(a[i] == k) return i;
}
return -1;
}
int binary_search_k(int a[],int len,int k){
sort(a,a + len);
int l = 0,r = len;
while (l <= r) {
int mid = (l + r) >> 1;
if(a[mid] == k) return mid;
else if(a[mid] > k) r = mid-1;
else l = mid + 1;
}
return l;
}
int main(){
cout << find_k_byorder(a,5,17) << endl;
cout << find_k_byorder(b,5,17) << endl;
cout << binary_search_k(a,5,17) << endl;
cout << binary_search_k(b,5,17) << endl;
return 0;
}
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
void select_sort(vector<int>& t)
{
int len = t.size();
for (int i = 0;i < len;++i)
{
int k = t[i], p = i;
for (int j = i;j < len;++j)
{
if (t[j] < k)
{
k = t[j];
p = j;
}
}
swap(t[i], t[p]);
}
}
bool isPrime(int k) {
if (k == 2) return true;
if ((k & 1) == 0) return false;
for (int i = 2;i <= sqrt(k);++i) {
if (k % i == 0) return false;
}
return true;
}
int main() {
vector<int> t;
for (int i = 0;i < 5;++i) {
if (isPrime(a[i])) t.push_back(a[i]);
}
for (int i = 0;i < 5;++i) {
if (isPrime(b[i]) && b[i] != 17) t.push_back(b[i]);
}
select_sort(t);
cout << "Increase:";
for (vector<int>::iterator it = t.begin();it != t.end();++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
T3
#include<cmath>
#include<iostream>
using namespace std;
const double PI = 3.1415926;
class Circle;
class Point {
double m_x = 0, m_y = 0;
friend class Circle;
friend double dist(const Point&,const Point&);
public:
Point(double x=0, double y=0) : m_x(x), m_y(y) {
cout << "Constructor of Point" << endl;
}
Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
cout << "Copy constructor of Point" << endl;
}
~Point() {
cout << "Destructor of Point" << endl;
}
double getMX(){
return m_x;
}
double getMY(){
return m_y;
}
};
class Circle {
Point m_center;
double m_radius = 1.0;
public:
Circle(double r=1, const Point &p=Point()) :m_center(p), m_radius(r) { cout << "Constructor of Circle" << endl;
}
~Circle() {
cout << "Destructor of Circle" << endl;
}
double getX(){
return m_center.getMX();
}
double getY(){
return m_center.getMY();
}
double area(){
return PI*m_radius*m_radius;
}
double circumference(){
return 2*PI*m_radius;
}
};
double dist(const Point&a,const Point&b){
return sqrt((a.m_x - b.m_x)*(a.m_x - b.m_x) + (a.m_y - b.m_y)*(a.m_y - b.m_y));
}
int main() {
Circle a(2, Point(1, 3));
cout << "(" << a.getX() << "," << a.getY() << ")" << endl;
Point c(1,2);
Point b(3,5);
cout << dist(c, b) << endl;
cout << a.area() << " " << a.circumference() <<endl;
cout << "end" << endl;
return 0;
}
博客记录了构造函数和拷贝构造函数的多次调用情况,如多次出现“Constructor called”和“Copy constructor called”,随后记录了多次析构函数调用,出现多个“Destructor”。





