04737_C++程序设计_第10章_面向对象设计实例
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章_面向对象设计实例的更多相关文章
- 04747_Java语言程序设计(一)_第10章_网络与数据库编程基础
例10.1说明InetAddress类的用法的应用程序. public class Example10_1 { public static void main(String args[]) { try ...
- 全国计算机等级考试二级教程-C语言程序设计_第10章_字符串
字符型指针数组 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> //参数中,int a ...
- ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第七章_使用ArcGIS进行空间分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 使用ArcGIS进行空间分析 1.1 GIS分析基础 G ...
- ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第四章_入门案例分析 - ArcGIS知乎-新一代ArcGIS问答社区 1 入门案例分析 在第一章里,我们已经对ArcGIS系列软件的体系结构有了一 ...
- ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区
原文:ArcGIS for Desktop入门教程_第六章_用ArcMap制作地图 - ArcGIS知乎-新一代ArcGIS问答社区 1 用ArcMap制作地图 作为ArcGIS for Deskto ...
- 04747_Java语言程序设计(一)_第3章_面向对象编程基础
链式编程 每次调用方法后,返回的是一个对象 /* * 链式编程 * 每次调用方法后,返回的是一个对象 */ class Student { public void study() { System.o ...
- 《mysql必知必会》学习_第10章_20180731_欢
第10章,计算字段. P64 select concat (vend_name,'(',vend_country,')') from vendors order by vend_name; # 拼接, ...
- 04737_C++程序设计_第4章_类和对象
例4.1 描述点的Point类. 例4.2 根据上面对Point类的定义,演示使用Point类的对象. #define _SCL_SECURE_NO_WARNINGS #include <ios ...
- 04737_C++程序设计_第3章_函数和函数模板
例3.1 传对象不会改变原来对象数据成员值的例子. #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <str ...
随机推荐
- python排序(冒泡, 快速)
之前用java时学习的一些基础算法,今天在python上也研究下. 1. 冒泡排序 算法步骤: 50 30 70 90 10 1)50 跟 30 比不用交换. 2)步数+1, 30 跟70比 ...
- linux----LAMP之编译安装apache
第一步:解决依赖.安装apr.apr-util. apr-1.5.4.tar.gz下载地址:http://yunpan.cn/cFBzgsC3rDcyR 访问密码 4c82 apr-util-1.5 ...
- 【自学php】第一天-macbook上配置php
今天MacBook到手了,就正式开始学习php了.先搭个环境,由于MacBook自带了Apache和php所以只要修改下配置启动就可以了. 1.启用root用户(如果不启用root,下面的命令前都要加 ...
- Delphi 进阶基础技能说明
以下讨论均基于Delphi XE8,主要是利用DELPHI新版的功能,如:Unicode,泛型,匿名函数等[XE2 后应该都支持]. 用新特性的好处是少写代码,提高效率.本博客不再讨论Delphi旧版 ...
- rhel6.4 配置本地yum的源
1 创建rhel-debuginfo.repo,如果有则先备份再删除 cd /etc/yum.repos.d rm rhel-debuginfo.repo vi rhel ...
- POJ——放苹果
4:放苹果 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示) ...
- aix installp软件包管理工具
软件可以安装成两种状态:applied和committed.Applied状态保存了原来以前版本的软件,它把以前版本存储在/usr/lpp/PackageName目录下.这种方式可以回滚到以前的软件版 ...
- poj1007 qsort快排
这道题比较简单,但通过这个题我学会了使用c++内置的qsort函数用法,收获还是很大的! 首先简要介绍一下qsort函数. 1.它是快速排序,所以就是不稳定的.(不稳定意思就是张三.李四成绩都是90, ...
- 裸的单调队列-poj-2823-Sliding Window
题目链接: http://poj.org/problem?id=2823 题目意思: 给n个数,求连续区间长度为k的最大和最小值. 解题思路: 裸的单调队列不解释,用两个队列保存. 代码: #incl ...
- Sass介绍及入门教程
Sass是什么? Sass是"Syntactically Awesome StyleSheets"的简称.那么他是什么?其实没有必要太过于纠结,只要知道他是“CSS预处理器”中的一 ...