1 #pragma once 2 3 #include <string> 4 5 // 类T: 声明 6 class T { 7 // 对象属性、方法 8 public: 9 T(int x = 0, int y = 0); // 普通构造函数 10 T(const T &t); // 复制构造函数 11 T(T &&t); // 移动构造函数 12 ~T(); // 析构函数 13 14 void adjust(int ratio); // 按系数成倍调整数据 15 void display() const; // 以(m1, m2)形式显示T类对象信息 16 17 private: 18 int m1, m2; 19 20 // 类属性、方法 21 public: 22 static int get_cnt(); // 显示当前T类对象总数 23 24 public: 25 static const std::string doc; // 类T的描述信息 26 static const int max_cnt; // 类T对象上限 27 28 private: 29 static int cnt; // 当前T类对象数目 30 31 // 类T友元函数声明 32 friend void func(); 33 }; 34 35 // 普通函数声明 36 void func(); 37 38 39 // 类T: 实现 40 // 普通函数实现 41 42 #include "t.h" 43 #include <iostream> 44 #include <string> 45 46 using std::cout; 47 using std::endl; 48 using std::string; 49 50 // static成员数据类外初始化 51 const std::string T::doc{"a simple class sample"}; 52 const int T::max_cnt = 999; 53 int T::cnt = 0; 54 55 56 // 对象方法 57 T::T(int x, int y): m1{x}, m2{y} { 58 ++cnt; 59 cout << "T constructor called.\n"; 60 } 61 62 T::T(const T &t): m1{t.m1}, m2{t.m2} { 63 ++cnt; 64 cout << "T copy constructor called.\n"; 65 } 66 67 T::T(T &&t): m1{t.m1}, m2{t.m2} { 68 ++cnt; 69 cout << "T move constructor called.\n"; 70 } 71 72 T::~T() { 73 --cnt; 74 cout << "T destructor called.\n"; 75 } 76 77 void T::adjust(int ratio) { 78 m1 *= ratio; 79 m2 *= ratio; 80 } 81 82 void T::display() const { 83 cout << "(" << m1 << ", " << m2 << ")" ; 84 } 85 86 // 类方法 87 int T::get_cnt() { 88 return cnt; 89 } 90 91 // 友元 92 void func() { 93 T t5(42); 94 t5.m2 = 2049; 95 cout << "t5 = "; t5.display(); cout << endl; 96 } 97 98 99 #include "t.h" 100 #include <iostream> 101 102 using std::cout; 103 using std::endl; 104 105 void test(); 106 107 int main() { 108 test(); 109 cout << "\nmain: \n"; 110 cout << "T objects'current count: " << T::get_cnt() << endl; 111 } 112 113 void test() { 114 cout << "test class T: \n"; 115 cout << "T info: " << T::doc << endl; 116 cout << "T objects'max count: " << T::max_cnt << endl; 117 cout << "T objects'current count: " << T::get_cnt() << endl << endl; 118 119 120 T t1; 121 cout << "t1 = "; t1.display(); cout << endl; 122 123 T t2(3, 4); 124 cout << "t2 = "; t2.display(); cout << endl; 125 126 T t3(t2); 127 t3.adjust(2); 128 cout << "t3 = "; t3.display(); cout << endl; 129 130 T t4(std::move(t2)); 131 cout << "t3 = "; t4.display(); cout << endl; 132 133 cout << "T objects'current count: " << T::get_cnt() << endl; 134 135 func(); 136 }
问题1:不能运行。
函数定义时没有指明所属类。
问题2:(1)普通构造函数:初始化对象的成员变量,在创建对象时为对象设置初始状态。(2)复制构造函数:用一个已有对象初始化新对象,在对象被复制时调用。(3)移动构造函数:将一个临时对象的资源移动到新对象中,在创建对象时调用。(4)析构函数:在对象生命周期结束时被调用,清理无用资源。
问题3:不能。
1 #ifndef COMPLEX_H 2 #define COMPLEX_H 3 4 #include <iostream> 5 #include <cmath> 6 7 class Complex { 8 private: 9 double real; 10 double imag; 11 12 public: 13 14 Complex(double r = 0.0, double i = 0.0); 15 16 double get_real() const; 17 double get_imag() const; 18 void set_real(double r); 19 void set_imag(double i); 20 21 void add(const Complex& other); 22 23 24 double abs() const; 25 26 27 bool is_equal(const Complex& other) const; 28 bool is_not_equal(const Complex& other) const; 29 30 31 void output() const; 32 }; 33 34 #endif 35 36 37 38 #include "Complex.h" 39 Complex::Complex(double r, double i) : real(r), imag(i) {} 40 41 double Complex::get_real() const { 42 return real; 43 } 44 45 double Complex::get_imag() const { 46 return imag; 47 } 48 49 void Complex::set_real(double r) { 50 real = r; 51 } 52 53 void Complex::set_imag(double i) { 54 imag = i; 55 } 56 57 void Complex::add(const Complex& other) { 58 real += other.real; 59 imag += other.imag; 60 } 61 62 double Complex::abs() const { 63 return std::sqrt(real * real + imag * imag); 64 } 65 66 bool Complex::is_equal(const Complex& other) const { 67 return (real == other.real) && (imag == other.imag); 68 } 69 70 bool Complex::is_not_equal(const Complex& other) const { 71 return !is_equal(other); 72 } 73 74 void Complex::output() const { 75 if (imag >= 0) 76 std::cout << real << " + " << imag << "i"; 77 else 78 std::cout << real << " - " << -imag << "i"; 79 } 80 81 82 double Complex::abs() const { 83 return std::sqrt(real * real + imag * imag); 84 } 85 86 bool Complex::is_equal(const Complex& other) const { 87 return (real == other.real) && (imag == other.imag); 88 } 89 90 void Complex::output() const { 91 if (imag >= 0) 92 std::cout << real << " + " << imag << "i"; 93 else 94 std::cout << real << " - " << -imag << "i"; 95 } 96 97 98 99 #include"Complex.h" 100 #include <iostream> 101 using std::cout; 102 using std::endl; 103 using std::boolalpha; 104 void test() { 105 cout << "类成员测试: " << endl; 106 cout << Complex::doc << endl; 107 cout << endl; 108 cout << "Complex对象测试: " << endl; 109 Complex c1; 110 Complex c2(3, -4); 111 const Complex c3(3.5); 112 Complex c4(c3); 113 114 cout << "c1 = "; output(c1); cout << endl; 115 cout << "c2 = "; output(c2); cout << endl; 116 cout << "c3 = "; output(c3); cout << endl; 117 cout << "c4 = "; output(c4); cout << endl; 118 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() 119 << endl; 120 cout << endl; 121 cout << "复数运算测试: " << endl; 122 cout << "abs(c2) = " << abs(c2) << endl; 123 c1.add(c2); 124 cout << "c1 += c2, c1 = "; output(c1); cout << endl; 125 cout << boolalpha; 126 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 127 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 128 c4 = add(c2, c3); 129 cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl; 130 } 131 int main() { 132 test(); 133 }
1 #include <iostream> 2 #include <complex> 3 using std::cout; 4 using std::endl; 5 using std::boolalpha; 6 using std::complex; 7 void test() { 8 cout << "标准库模板类comple测试: " << endl; 9 complex<double> c1; 10 complex<double> c2(3, -4); 11 const complex<double> c3(3.5); 12 complex<double> c4(c3); 13 cout << "c1 = " << c1 << endl; 14 cout << "c2 = " << c2 << endl; 15 cout << "c3 = " << c3 << endl; 16 cout << "c4 = " << c4 << endl; 17 cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << 18 endl; 19 cout << endl; 20 cout << "复数运算测试: " << endl; 21 cout << "abs(c2) = " << abs(c2) << endl; 22 c1 += c2; 23 cout << "c1 += c2, c1 = " << c1 << endl; 24 cout << boolalpha; 25 cout << "c1 == c2 : " << (c1 == c2) << endl; 26 cout << "c1 != c3 : " << (c1 != c3) << endl; 27 c4 = c2 + c3; 28 cout << "c4 = c2 + c3, c4 = " << c4 << endl; 29 } 30 int main() { 31 test(); 32 }
1 #include<iostream> 2 using namespace std; 3 class Fraction { 4 public: 5 Fraction(int fz=0, int fm = 1); 6 Fraction(const Fraction& f); 7 int get_up()const; 8 int get_down()const; 9 Fraction negative() const; 10 friend void output(const Fraction &f); 11 friend Fraction add(const Fraction& f1, const Fraction& f2); 12 friend Fraction sub(const Fraction& f1, const Fraction& f2); 13 friend Fraction mul(const Fraction& f1, const Fraction& f2); 14 friend Fraction div(const Fraction& f1,const Fraction& f2); 15 static string doc(); 16 private: 17 int up; 18 int down; 19 }; 20 void output(const Fraction& f); 21 Fraction add(const Fraction& f1,const Fraction& f2); 22 Fraction sub(const Fraction& f1,const Fraction& f2); 23 Fraction mul(const Fraction& f1,const Fraction& f2); 24 Fraction div(const Fraction& f1,const Fraction& f2); 25 26 27 28 29 30 #include"Fraction.h" 31 int gcd(int x, int y) { 32 if (x == 0 || y == 0) { 33 return x ? y == 0 : y; 34 } 35 if (x%y == 0) 36 return y; 37 else 38 return gcd(y, x % y); 39 } 40 Fraction::Fraction(int fz,int fm):up(fz),down(fm){ 41 int flag = 0; 42 if (up * down < 0) { 43 flag = 1; 44 } 45 fz = abs(up); 46 fm = abs(down); 47 int t = gcd(fz, fm); 48 fz /= t; 49 fm /= t; 50 if (flag == 1) { 51 up = fz * -1; 52 down = fm; 53 } 54 else { 55 up = fz; 56 down = fm; 57 } 58 59 60 61 #include "Fraction.h" 62 void test1() { 63 cout << "Fraction类测试: " << endl; 64 cout << Fraction::doc() << endl << endl; 65 Fraction f1(5); 66 Fraction f2(3, -4), f3(-18, 12); 67 Fraction f4(f3); 68 cout << "f1 = "; output(f1); cout << endl; 69 cout << "f2 = "; output(f2); cout << endl; 70 cout << "f3 = "; output(f3); cout << endl; 71 cout << "f4 = "; output(f4); cout << endl; 72 Fraction f5(f4.negative()); 73 cout << "f5 = "; output(f5); cout << endl; 74 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << 75 f5.get_down() << endl; 76 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 77 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 78 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 79 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 80 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 81 82 } 83 void test2() { 84 Fraction f6(42, 55), f7(0, 3); 85 cout << "f6 = "; output(f6); cout << endl; 86 cout << "f7 = "; output(f7); cout << endl; 87 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 88 } 89 int main() { 90 cout << "测试1: Fraction类基础功能测试\n"; 91 test1(); 92 cout << "\n测试2: 分母为0测试: \n"; 93 test2(); 94 }