zoukankan      html  css  js  c++  java
  • 04737_C++程序设计_第10章_面向对象设计实例

    10.6.2

    使用包含的参考程序及运行结果。

    头文件cpp10.h

    源文件cpp10.cpp

    源文件Find10.cpp

    头文件cpp10.h

     1 #if ! defined(CPP10_H)
     2 #define CPP10_H
     3 #include<iostream>
     4 #include<cmath>
     5 using namespace std;
     6 class Point
     7 {
     8     double X, Y;
     9 public:
    10     Point(double = 0, double = 0);
    11     Point(Point&);
    12     void Display()
    13     {
    14         cout << X << "," << Y << endl;
    15     }
    16     double Distance(Point&);
    17     double Getx()
    18     {
    19         return X;
    20     }
    21     double Gety()
    22     {
    23         return Y;
    24     }
    25 };
    26 struct Cow
    27 {
    28     int Color;
    29     int Width;
    30 };
    31 struct Line
    32 {
    33     Point a, b;
    34     Cow cw;
    35 public:
    36     Line(Point&, Point&, Cow&);
    37     void Display();
    38     Line(Line&);
    39     double Distance();
    40     double Area();
    41 };
    42 #endif

    源文件cpp10.cpp

     1 #include"cpp10.h"
     2 Point::Point(double a, double b) :X(a), Y(b)
     3 {
     4 
     5 }
     6 Point::Point(Point&a)
     7 {
     8     X = a.X;
     9     Y = a.Y;
    10 }
    11 double Point::Distance(Point&a)
    12 {
    13     return sqrt((X - a.X)*(X - a.X) + (Y - a.Y)*(Y - a.Y));
    14 }
    15 Line::Line(Point&a1, Point&a2, Cow&a3) :a(a1), b(a2), cw(a3)
    16 {
    17 
    18 }
    19 Line::Line(Line&s) : a(s.a), b(s.b), cw(s.cw)
    20 {
    21 
    22 }
    23 void Line::Display()
    24 {
    25     a.Display();
    26     b.Display();
    27     cout << "Color=" << cw.Color << ',' << "Width=" << cw.Width << endl;
    28 }
    29 double Line::Distance()
    30 {
    31     double x = a.Getx() - b.Getx();
    32     double y = a.Gety() - b.Gety();
    33     return sqrt(x*x + y*y);
    34 }
    35 double Line::Area()
    36 {
    37     return cw.Width * Distance();
    38 }

    源文件Find10.cpp

     1 #include"cpp10.h"
     2 void main()
     3 {
     4     Point a;
     5     Point b(7.8, 9.8), c(34.5, 67.8);
     6     a = c;
     7 
     8     a.Display();
     9     b.Display();
    10     cout << "两点之距:" << a.Distance(b) << endl;
    11 
    12     Cow cw = { 3.5 };
    13     Line s(a, b, cw);
    14     Line s1(s);
    15 
    16     cout << "线段属性如下:" << endl;
    17     s1.Display();
    18     cout << "线段长度:" << s1.Distance() << endl;
    19     cout << "线段面积:" << s1.Area() << endl;
    20 
    21     system("pause");
    22 }

    10.6.4

    使用继承的参考程序和运行结果。

    头文件cpp101.h

    源文件cpp101.cpp

    源文件Find101.cpp

    头文件cpp101.h

     1 #if ! defined(CPP101_H)
     2 #define CPP10_H
     3 #include<iostream>
     4 #include<math.h>
     5 using namespace std;
     6 class Point
     7 {
     8 protected:
     9     double X, Y;
    10 public:
    11     Point(double = 0, double = 0);
    12     Point(Point&);
    13     virtual void Display()
    14     {
    15         cout << "X=" << X << ",Y=" << Y << endl;
    16     }
    17     double Distance(Point&);
    18     virtual double Area()
    19     {
    20         return 0;
    21     }
    22     double Getx()
    23     {
    24         return X;
    25     }
    26     double Gety()
    27     {
    28         return Y;
    29     }
    30 };
    31 struct Cow
    32 {
    33     int Color;
    34     int Width;
    35 };
    36 class Line :public Point
    37 {
    38     double X2, Y2;
    39     Cow cw;
    40 public:
    41     Line(double, double, double, double, Cow&);
    42     Line(Line&);
    43     void Display();
    44     double Distance();
    45     double Area();
    46     double Getx2()
    47     {
    48         return X2;
    49     }
    50     double Gety2()
    51     {
    52         return Y2;
    53     }
    54     double Getc()
    55     {
    56         return cw.Color;
    57     }
    58     double Getw()
    59     {
    60         return cw.Width;
    61     }
    62     friend void Disp(Line&t)
    63     {
    64         cout << t;
    65     }
    66     friend ostream &operator<<(ostream&, Line);
    67 };
    68 #endif

    源文件cpp101.cpp

     1 #include"cpp101.h"
     2 Point::Point(double a, double b) :X(a), Y(b)
     3 {
     4 
     5 }
     6 Point::Point(Point&a)
     7 {
     8     X = a.X;
     9     Y = a.Y;
    10 }
    11 double Point::Distance(Point&a)
    12 {
    13     return sqrt((X - a.X)*(X - a.X) + (Y - a.Y)*(Y - a.Y));
    14 }
    15 Line::Line(double a1, double a2, double a3, double a4, Cow&c) : Point(a1, a2), X2(a3), Y2(a4), cw(c)
    16 {
    17 
    18 }
    19 Line::Line(Line&s) : Point(s), X2(s.X2), Y2(s.Y2), cw(s.cw)
    20 {
    21 
    22 }
    23 double Line::Distance()
    24 {
    25     double x = X2 - X;
    26     double y = Y2 - Y;
    27     return sqrt(x*x + y*y);
    28 }
    29 void Line::Display()
    30 {
    31     cout << "X=" << X << ",Y=" << Y << ",XW=" << X2 << ",Y2=" << Y2
    32         << ",Color=" << cw.Color << ",Width=" << cw.Width << endl;
    33 }
    34 double Line::Area()
    35 {
    36     return cw.Width * Distance();
    37 }
    38 ostream &operator<<(ostream& stream, Line obj)
    39 {
    40     stream << "使重载<<输出线段属性如下:" << endl;
    41     stream << obj.Getx() << "," << obj.Gety() << ","
    42         << obj.Getx2() << "," << obj.Gety2() << ","
    43         << obj.Getc() << "," << obj.Getw() << endl;
    44     return stream;
    45 }

    源文件Find101.cpp

     1 #include"cpp101.h"
     2 void main()
     3 {
     4     Point a;
     5     Point b(7.8, 9.8), c(34.5, 67.8);
     6     a = c;
     7 
     8     a.Display();
     9     b.Display();
    10     cout << "两点之距:" << a.Distance(b) << endl;
    11 
    12     Cow cw = { 3,5 };
    13     Line s(7.8, 9.8, 34.5, 67.8, cw);
    14     Disp(s);
    15     Line s1(s);
    16 
    17     cout << "使用Display函数输出线段属性如下:" << endl;
    18     s1.Display();
    19     cout << "线段长度:" << s1.Distance() << endl;
    20     cout << "线段面积:" << s1.Area() << endl;
    21 
    22     cout << "派生类的对象赋给基类对象" << endl;
    23     a.Display();
    24     a = s;//派生类的对象可以赋给基类
    25     cout << "面积:" << a.Area() << endl;;
    26     
    27     cout << "派生类的对象赋给基类对象" << endl;
    28     Point *p = &s1;
    29     p->Display();
    30     cout << "面积:" << p->Area() << endl;
    31     
    32     cout << "基类对象引用派生类对象" << endl;
    33     Point &d = s1;
    34     d.Display();
    35     cout << "面积:" << d.Area() << endl;
    36     
    37     system("pause");
    38 }
  • 相关阅读:
    每日日报
    HTML相关
    60-Shiro功能扩展(记住我)
    60--Shiro安全框架
    服务器项目部署简单操作
    61--DB项目--修改密码模块设计
    59-SpringAOP --Cache操作(注解)
    58-Spring AOP 异步操作
    58-Spring-AOP事务管理
    Terminal 执行 java 命令
  • 原文地址:https://www.cnblogs.com/denggelin/p/5595807.html
Copyright © 2011-2022 走看看