zoukankan      html  css  js  c++  java
  • 04737_C++程序设计_第5章_特殊函数和成员

    例5.1

    分析下面程序中析构函数与构造函数的调用顺序。

     1 #include<iostream>
     2 
     3 using namespace std;
     4 
     5 class object
     6 {
     7 private:
     8     int val;
     9 public:
    10     object() :val(0)
    11     {
    12         cout << "Ddfault constructor for object" << endl;
    13     }
    14     object(int i) :val(i)
    15     {
    16         cout << "Constructor for object " << val << endl;
    17     }
    18     ~object()
    19     {
    20         cout << "Destructor for object" << val << endl;
    21     }
    22 };
    23 
    24 class container
    25 {
    26 private:
    27     object one;//初始化顺序与对象成员产生的顺序有关
    28     object two;//排在前面的先初始化
    29     int data;
    30 public:
    31     container() :data(0)
    32     {
    33         cout << "Ddfault constructor for object" << endl;
    34     }
    35     container(int i, int j, int k);
    36     ~container()
    37     {
    38         cout << "Destructor for object" << data << endl;
    39     }
    40 };
    41 
    42 container::container(int i, int j, int k) :two(i), one(j)//与初始化列表顺序无关
    43 {
    44     data = k;
    45     cout << "Constructor for container " << data << endl;
    46 }
    47 
    48 void main()
    49 {
    50     container obj, anObj(5, 6, 10);
    51 
    52     system("pause");
    53 }

     

    例5.2

    分析下面程序的输出结果。

     1 class Test
     2 {
     3     static int x;//静态数据成员
     4     int n;
     5 public:
     6     Test()
     7     {
     8 
     9     }
    10     Test(int a, int b)
    11     {
    12         x = a;
    13         n = b;
    14     }
    15     static int func()
    16     {
    17         return x;//静态成员函数
    18     };
    19     static void sfunc(Test&r, int a)
    20     {
    21         r.n = a;//静态成员函数
    22     }
    23     int Getn()
    24     {
    25         return n;//非静态成员函数
    26     }
    27 };
    28 
    29 int Test::x = 25;//初始化静态数据成员
    30 
    31 #include<iostream>
    32 
    33 using namespace std;
    34 
    35 void main()
    36 {
    37     cout << Test::func();//x在产生对象之前即存在,输出25
    38 
    39     Test b, c;
    40     b.sfunc(b, 58);//设置对象b的数据成员n
    41 
    42     cout << " " << b.Getn();
    43     cout << " " << b.func();//x属于所有对象,输出25
    44     cout << " " << c.func();//x属于所有对象,输出25
    45 
    46     Test a(24, 56);//将类的x值改为24
    47 
    48     cout << " " << a.func() << " " << b.func() << " " << b.func() << endl;
    49 
    50     system("pause");
    51 }

    5.3

    使用静态对象的例子

     1 #include<iostream>
     2 
     3 using namespace std;
     4 
     5 class test
     6 {
     7 private:
     8     int n;
     9 public:
    10     test(int i)
    11     {
    12         n = i;
    13         cout << "constructor:" << i << endl;
    14     }
    15     ~test()
    16     {
    17         cout << "destructor:" << n << endl;
    18     }
    19     int getn()
    20     {
    21         return n;
    22     }
    23     void inc()
    24     {
    25         ++n;
    26     }
    27 };
    28 
    29 void main()
    30 {
    31     cout << "loop start:" << endl;
    32 
    33     for (int i = 0; i < 3; i++)
    34     {
    35         static test a(3);
    36         test b(3);
    37         a.inc();
    38         b.inc();
    39         cout << "a.n=" << a.getn() << endl;
    40         cout << "b.n=" << b.getn() << endl;
    41     }
    42 
    43     cout << "loop end." << endl;
    44     cout << "Exit main()" << endl;
    45 
    46     system("pause");
    47 }

    例5.4

    使用友元函数计算两点距离的例子。

     1 #include <iostream>
     2 #include <cmath>//也可以使用头文件<math.h>
     3 using namespace std;
     4 class Point
     5 {
     6 private:
     7     double X, Y;
     8 public:
     9     Point(double xi, double yi)
    10     {
    11         X = xi, Y = yi;
    12     }
    13     double GetX()
    14     {
    15         return X;
    16     }
    17     double GetY()
    18     {
    19         return Y;
    20     }
    21     friend double distances(Point&, Point&);//声明友元函数
    22 };
    23 double distances(Point& a, Point& b)//像普通函数一样定义友元函数
    24 {
    25     double dx = a.X - b.X;//可以访问对象a的成员
    26     double dy = a.Y - b.Y;//可以访问对象b的成员
    27     return sqrt(dx*dx + dy*dy);
    28 }
    29 void main()
    30 {
    31     Point p1(3.5, 5.5), p2(4.5, 6.5);
    32     cout << "The distance is " << distances(p1, p2) << endl;
    33 
    34     system("pause");
    35 }

    例5.5

    类One的对象通过友元函数访问类Two的对象的私有数据。

     1 #include <iostream>
     2 using namespace std;
     3 class Two;//先声明类Two以便类One引用Two&
     4 class One
     5 {
     6 private:
     7     int x;
     8 public:
     9     One(int a)
    10     {
    11         x = a;
    12     }
    13     int Getx()
    14     {
    15         return x;
    16     }
    17     void func(Two&);//声明本类的成员函数,参数为Two类的引用
    18 };
    19 class Two
    20 {
    21 private:
    22     int y;
    23 public:
    24     Two(int b)
    25     {
    26         y = b;
    27     }
    28     int Gety()
    29     {
    30         return y;
    31     }
    32     friend void One::func(Two&);//声明类One的函数为友元函数
    33 };
    34 void One::func(Two& r)//定义类One的函数成员
    35 {
    36     r.y = x;//修改类Two的数据成员
    37 }
    38 void main()
    39 {
    40     One Obj1(5);
    41     Two Obj2(8);
    42     cout << Obj1.Getx() << " " << Obj2.Gety() << endl;
    43     Obj1.func(Obj2);
    44     cout << Obj1.Getx() << " " << Obj2.Gety() << endl;
    45 
    46     system("pause");
    47 }

    例5.6

    类One的对象可以通过任意一个成员函数访问类Two的对象的私有数据。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Two
     6 {
     7     int y;
     8 public:
     9     friend class One;
    10 };
    11 
    12 class One
    13 {
    14     int x;
    15 public:
    16     One(int a, Two&r, int b)
    17     {
    18         x = a;
    19         r.y = b;
    20     }
    21     void Display(Two&);
    22 };
    23 
    24 void One::Display(Two&r)
    25 {
    26     cout << x << " " << r.y << endl;
    27 }
    28 
    29 void main()
    30 {
    31     Two Obj2;
    32     One Obj1(23, Obj2, 55);
    33     Obj1.Display(Obj2);
    34 
    35     system("pause");
    36 }

    例5.7

    常数据成员初始化和常引用作为函数参数。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Base
     6 {
     7 private:
     8     int x;
     9     const int a;//常数据成员只能通过初始化列表来获得初值
    10     static const int b;//静态常数据成员
    11     const int& r;//常引用只能通过初始化列表来获得初值
    12 public:
    13     Base(int, int);
    14     void Show()
    15     {
    16         cout << x << "," << a << "," << b << "," << r << endl;
    17     }
    18     void Display(const double& r)
    19     {
    20         cout << r << endl;
    21     }
    22 };
    23 
    24 const int Base::b = 125;//静态常数据成员在类外初始化
    25 
    26 Base::Base(int i, int j) :x(i), a(j), r(x)//初始化列表
    27 {
    28 
    29 }
    30 
    31 void main()
    32 {
    33     Base a1(104, 118), a2(119, 140);
    34     a1.Show();
    35     a2.Show();
    36     a2.Display(3.14159);//常引用作为函数参数
    37 
    38     system("pause");
    39 }

    例5.8

    const对象调用const成员函数。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Base
     6 {
     7 private:
     8     double x, y;
     9     const double p;
    10 public:
    11     Base(double m, double n, double d) :p(d)//通过初始化列表来获得初值
    12     {
    13         x = m;
    14         y = n;
    15     }
    16     void Show();
    17     void Show() const;
    18 };
    19 
    20 void Base::Show()
    21 {
    22     cout << x << "," << y << " p=" << p << endl;
    23 }
    24 
    25 void Base::Show() const//定义时必须也使用const
    26 {
    27     cout << x << "," << y << " const p=" << p << endl;
    28 }
    29 
    30 void main()
    31 {
    32     Base a(8.9, 2.5, 3.1416);
    33     const Base b(2.5, 8.9, 3.14);
    34     b.Show();//调用void Show() const
    35     a.Show();//调用void Show()
    36 
    37     system("pause");
    38 }

    例5.9

    const函数返回的常量对象与其他常量对象一起使用。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class ConstFun
     6 {
     7 public:
     8     int f5() const
     9     {
    10         return 5;//返回常量对象
    11     }
    12     int Obj()
    13     {
    14         return 45;
    15     }
    16 };
    17 
    18 void main()
    19 {
    20     ConstFun s;//一般对象
    21     const int i = s.f5();//对象s使用f5()初始化常整数
    22     const int j = s.Obj();//对象s使用Obj()初始化常整数
    23     int x = s.Obj();//对象s使用Obj()作为整数
    24     int y = s.f5();//对象s使用f5()作为整数
    25 
    26     cout << i << " " << j << " "//输出5和45
    27         << x << " " << y;//输出45和5
    28 
    29     const ConstFun d;//const对象
    30     int k = d.f5();//常量对象d只能调用常成员函数
    31 
    32     cout << " k=" << k << endl;//输出5
    33 
    34     system("pause");
    35 }

    例5.10

    使用类对象数组和指针的例子。

     1 class Test
     2 {
     3     int num;
     4     double f1;
     5 public:
     6     Test(int n)//一个参数的构造函数
     7     {
     8         num = n;
     9     }
    10     Test(int n, double f)//两个参数的构造函数
    11     {
    12         num = n;
    13         f1 = f;
    14     }
    15     int GetNum()
    16     {
    17         return num;
    18     }
    19     double GetF()
    20     {
    21         return f1;
    22     }
    23 };
    24 
    25 #include <iostream>
    26 
    27 using namespace std;
    28 
    29 void main()
    30 {
    31     Test one[2] = { 2,4 }, *p;
    32     Test two[2] = { Test(1,3.2),Test(5,9.5) };
    33 
    34     for (int i = 0; i < 2; i++)//输出对象数组对象
    35     {
    36         cout << "one[" << i << "]=" << one[i].GetNum() << endl;
    37     }
    38 
    39     p = two;//使用指针输出对象数组元素
    40 
    41     for (int i = 0; i < 2; i++, p++)
    42     {
    43         cout << "tow[" << i << "]=(" << p->GetNum() << ","
    44             << p->GetF() << ")" << endl;
    45     }
    46 
    47     system("pause");
    48 }

    例5.11

    仍然使用类Test,但改用对象指针数组的演示主程序。

     1 class Test
     2 {
     3     int num;
     4     double f1;
     5 public:
     6     Test(int n)//一个参数的构造函数
     7     {
     8         num = n;
     9     }
    10     Test(int n, double f)//两个参数的构造函数
    11     {
    12         num = n;
    13         f1 = f;
    14     }
    15     int GetNum()
    16     {
    17         return num;
    18     }
    19     double GetF()
    20     {
    21         return f1;
    22     }
    23 };
    24 
    25 #include <iostream>
    26 
    27 using namespace std;
    28 
    29 void main()
    30 {
    31     Test *one[2] = { new Test(2),new Test(4) };
    32     Test *two[2] = { new Test(1,3.2),new Test(5,9.5) };
    33 
    34     for (int i = 0; i < 2; i++)//输出对象数组对象
    35     {
    36         cout << "one[" << i << "]=" << one[i]->GetNum() << endl;
    37     }
    38 
    39     for (int i = 0; i < 2; i++)
    40     {
    41         cout << "tow[" << i << "]=(" << two[i]->GetNum() << ","
    42             << two[i]->GetF() << ")" << endl;
    43     }
    44 
    45     system("pause");
    46 }

    求解一元二次方程

    头文件

    equation.h

    源文件

    equation.cpp

    源文件

    Find.cpp

    头文件

    equation.h

     1 #if ! defined(EQUATION_H)
     2 #define EQUATION_H
     3 #include<iostream>
     4 #include<cmath>
     5 using namespace std;
     6 //***************************
     7 //* 先声明FindRoot类        *
     8 //***************************
     9 class FindRoot
    10 {
    11 private:
    12     float a, b, c, d;
    13     double x1, x2;
    14 public:
    15     FindRoot(float x, float y, float z);
    16     void Find();
    17     void Display();
    18 };
    19 #endif

    源文件

    equation.cpp

     1 #include"equation.h"
     2 //***************************
     3 //* 实现FindRoot类            *
     4 //***************************
     5 FindRoot::FindRoot(float x, float y, float z)//构造函数
     6 {
     7     a = x;
     8     b = y;
     9     c = z;
    10     d = b*b - 4 * a*c;
    11 }
    12 
    13 void FindRoot::Find()//实现成员函数Find
    14 {
    15     if (d > 0)
    16     {
    17         x1 = (-b + sqrt(d)) / (2 * a);
    18         x2 = (-b - sqrt(d)) / (2 * a);
    19         return;
    20     }
    21     else if (d == 0)
    22     {
    23         x1 = x2 = (-b) / (2 * a);
    24         return;
    25     }
    26     else
    27     {
    28         x1 = (-b) / (2 * a);
    29         x2 = sqrt(-d) / (2 * a);
    30     }
    31 }
    32 
    33 void FindRoot::Display()//实现成员函数Display
    34 {
    35     if (d > 0)
    36     {
    37         cout << "X1=" << x1 << "
    X2" << x2 << endl;
    38         return;
    39     }
    40     else if (d == 0)
    41     {
    42         cout << "X1=X2=" << x1 << endl;
    43         return;
    44     }
    45     else
    46     {
    47         cout << "X1=" << x1 << "+" << x2 << "i" << endl;
    48         cout << "X2=" << x1 << "-" << x2 << "i" << endl;
    49     }
    50 }

    源文件

    Find.cpp

     1 #include"equation.h"
     2 
     3 void Read(float&, float&, float&);//参数使用对象引用方式
     4 
     5 void main()
     6 {
     7     float a, b, c;
     8 
     9     cout << "这是一个求方程ax2+bx+c=0的根的程序。" << endl;
    10 
    11     for (;;)//循环求解
    12     {
    13         Read(a, b, c);//准备系数
    14 
    15         if (a == 0)//根据输入系数a决定是否结束for循环
    16         {
    17             return;
    18         }
    19 
    20         FindRoot obj(a, b, c);//建立对象obj
    21 
    22         obj.Find();//求解
    23         obj.Display();//输出计算结果
    24     }//endfor
    25 }
    26 
    27 void Read(float& a, float& b, float& c)//准备系数
    28 {
    29     cout << "输入方程系数a:";
    30 
    31     cin >> a;
    32 
    33     if (a == 0)//系数a为零,则退出Read函数
    34     {
    35         getchar();//为了消除回车的影响
    36         return;//将a=0返给for循环并退出Read函数
    37     }
    38 
    39     cout << "输入方程系数b:";
    40     cin >> b;
    41 
    42     cout << "输入方程系数c:";
    43     cin >> c;
    44 }
  • 相关阅读:
    数组模拟链表
    高精度模板
    利用二分法求三次方根
    AcWing 789.数的范围
    二进制中1的个数
    AcWing 787.归并排序
    微信小程序form表单的bindsubmit提交没有效果
    本地项目如果上传到GitHub上
    微信小程序生成随机数
    CSS3 Filter的十种特效
  • 原文地址:https://www.cnblogs.com/denggelin/p/5586026.html
Copyright © 2011-2022 走看看