10.6.2

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

头文件cpp10.h

源文件cpp10.cpp

源文件Find10.cpp

头文件cpp10.h

 #if ! defined(CPP10_H)
#define CPP10_H
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
double X, Y;
public:
Point(double = , double = );
Point(Point&);
void Display()
{
cout << X << "," << Y << endl;
}
double Distance(Point&);
double Getx()
{
return X;
}
double Gety()
{
return Y;
}
};
struct Cow
{
int Color;
int Width;
};
struct Line
{
Point a, b;
Cow cw;
public:
Line(Point&, Point&, Cow&);
void Display();
Line(Line&);
double Distance();
double Area();
};
#endif

源文件cpp10.cpp

 #include"cpp10.h"
Point::Point(double a, double b) :X(a), Y(b)
{ }
Point::Point(Point&a)
{
X = a.X;
Y = a.Y;
}
double Point::Distance(Point&a)
{
return sqrt((X - a.X)*(X - a.X) + (Y - a.Y)*(Y - a.Y));
}
Line::Line(Point&a1, Point&a2, Cow&a3) :a(a1), b(a2), cw(a3)
{ }
Line::Line(Line&s) : a(s.a), b(s.b), cw(s.cw)
{ }
void Line::Display()
{
a.Display();
b.Display();
cout << "Color=" << cw.Color << ',' << "Width=" << cw.Width << endl;
}
double Line::Distance()
{
double x = a.Getx() - b.Getx();
double y = a.Gety() - b.Gety();
return sqrt(x*x + y*y);
}
double Line::Area()
{
return cw.Width * Distance();
}

源文件Find10.cpp

 #include"cpp10.h"
void main()
{
Point a;
Point b(7.8, 9.8), c(34.5, 67.8);
a = c; a.Display();
b.Display();
cout << "两点之距:" << a.Distance(b) << endl; Cow cw = { 3.5 };
Line s(a, b, cw);
Line s1(s); cout << "线段属性如下:" << endl;
s1.Display();
cout << "线段长度:" << s1.Distance() << endl;
cout << "线段面积:" << s1.Area() << endl; system("pause");
}

10.6.4

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

头文件cpp101.h

源文件cpp101.cpp

源文件Find101.cpp

头文件cpp101.h

 #if ! defined(CPP101_H)
#define CPP10_H
#include<iostream>
#include<math.h>
using namespace std;
class Point
{
protected:
double X, Y;
public:
Point(double = , double = );
Point(Point&);
virtual void Display()
{
cout << "X=" << X << ",Y=" << Y << endl;
}
double Distance(Point&);
virtual double Area()
{
return ;
}
double Getx()
{
return X;
}
double Gety()
{
return Y;
}
};
struct Cow
{
int Color;
int Width;
};
class Line :public Point
{
double X2, Y2;
Cow cw;
public:
Line(double, double, double, double, Cow&);
Line(Line&);
void Display();
double Distance();
double Area();
double Getx2()
{
return X2;
}
double Gety2()
{
return Y2;
}
double Getc()
{
return cw.Color;
}
double Getw()
{
return cw.Width;
}
friend void Disp(Line&t)
{
cout << t;
}
friend ostream &operator<<(ostream&, Line);
};
#endif

源文件cpp101.cpp

 #include"cpp101.h"
Point::Point(double a, double b) :X(a), Y(b)
{ }
Point::Point(Point&a)
{
X = a.X;
Y = a.Y;
}
double Point::Distance(Point&a)
{
return sqrt((X - a.X)*(X - a.X) + (Y - a.Y)*(Y - a.Y));
}
Line::Line(double a1, double a2, double a3, double a4, Cow&c) : Point(a1, a2), X2(a3), Y2(a4), cw(c)
{ }
Line::Line(Line&s) : Point(s), X2(s.X2), Y2(s.Y2), cw(s.cw)
{ }
double Line::Distance()
{
double x = X2 - X;
double y = Y2 - Y;
return sqrt(x*x + y*y);
}
void Line::Display()
{
cout << "X=" << X << ",Y=" << Y << ",XW=" << X2 << ",Y2=" << Y2
<< ",Color=" << cw.Color << ",Width=" << cw.Width << endl;
}
double Line::Area()
{
return cw.Width * Distance();
}
ostream &operator<<(ostream& stream, Line obj)
{
stream << "使重载<<输出线段属性如下:" << endl;
stream << obj.Getx() << "," << obj.Gety() << ","
<< obj.Getx2() << "," << obj.Gety2() << ","
<< obj.Getc() << "," << obj.Getw() << endl;
return stream;
}

源文件Find101.cpp

 #include"cpp101.h"
void main()
{
Point a;
Point b(7.8, 9.8), c(34.5, 67.8);
a = c; a.Display();
b.Display();
cout << "两点之距:" << a.Distance(b) << endl; Cow cw = { , };
Line s(7.8, 9.8, 34.5, 67.8, cw);
Disp(s);
Line s1(s); cout << "使用Display函数输出线段属性如下:" << endl;
s1.Display();
cout << "线段长度:" << s1.Distance() << endl;
cout << "线段面积:" << s1.Area() << endl; cout << "派生类的对象赋给基类对象" << endl;
a.Display();
a = s;//派生类的对象可以赋给基类
cout << "面积:" << a.Area() << endl;; cout << "派生类的对象赋给基类对象" << endl;
Point *p = &s1;
p->Display();
cout << "面积:" << p->Area() << endl; cout << "基类对象引用派生类对象" << endl;
Point &d = s1;
d.Display();
cout << "面积:" << d.Area() << endl; system("pause");
}

04737_C++程序设计_第10章_面向对象设计实例的更多相关文章

  1. 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础

    例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...

  2. 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串

    字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...

  3. ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...

  4. ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...

  5. ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区

    原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...

  6. 04747_Java语言程序设计(一)_第3章_面向对象编程基础

    链式编程 每次调用方法后,返回的是一个对象 /* * 链式编程 * 每次调用方法后,返回的是一个对象 */ class Student { public void study() { System.o ...

  7. 《mysql必知必会》学习_第10章_20180731_欢

    第10章,计算字段. P64 select concat (vend_name,'(',vend_country,')') from vendors order by vend_name; # 拼接, ...

  8. 04737_C++程序设计_第4章_类和对象

    例4.1 描述点的Point类. 例4.2 根据上面对Point类的定义,演示使用Point类的对象. #define _SCL_SECURE_NO_WARNINGS #include <ios ...

  9. 04737_C++程序设计_第3章_函数和函数模板

    例3.1 传对象不会改变原来对象数据成员值的例子. #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <str ...

随机推荐

  1. Flink Program Guide (2) -- 综述 (DataStream API编程指导 -- For Java)

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  2. python 通过ntlm验证下载文件

    最近使用python实现一个小工具,需要从网站下载文件,下载时服务端需要进行ntlm验证,否则返回401错误响应.经研究 requests库配合 requests-ntlm 可以解决这个问题. ntl ...

  3. MYSQL 部分事务

    MYSQL 中通过 savepoint 的方式来实现只提交事务的一部分. step 1 : savepoint savepoint_name;. 做标记 step 2 :rollbak to save ...

  4. Linux: service network/Network/NetworkManager

    Linux:service network/Network/NetworkManager start 这三种有什么不同? 1.network service的制御网络接口配置信息改动后,网络服务必须从 ...

  5. SysLog解析

    SysLog的标准是RFC3164描述的,但是很多厂家的日志都没有完全遵守这个标准,因此在SIEM和SEM中,日志解析就是第一个要面对的问题,因此,就我所看到的大部分SOC厂家(国内和国外)都需要针对 ...

  6. 杭电oj 2095 & 异或^符号在C/C++中的使用

    异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...

  7. ubuntu gnome vnc

    1,安装登陆管理器 --apt-get install gdm (还可以为kdm/xdm) lightdm,display manager

  8. warning C4133: “函数”: 从“char [5]”到“LPCWSTR”的类型不兼容

    弹窗打开一个网页,但是报错,代码如下: #include <stdio.h> #include <windows.h> void main() { ShellExecute(, ...

  9. 原生网络请求以及AFN网络请求/异步下载

    这里对网络请求方式做一个总结. 原生方式同步GET请求: NSString *urlStr = @"http://apis.juhe.cn/mobile/get?phone=13429667 ...

  10. for应用

    应用:迭代法,穷举法.一.迭代法:有一定规律. 每次循环都是从上次运算结果中获得数据,本次运算的结果都是要为下次运算做准备.例:1.100以内所有数的和.2.求阶乘3.求年龄.4.折纸.5.棋盘放粮食 ...