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 ...
随机推荐
- 这些年,我收集的JavaScript代码(一)
一.取URL中的参数 function getParameterByName(name) { var match = RegExp('[?&]' + name + '=([^&]*)' ...
- VitualBox中linux系统ping ip能通域名不通的解决办法
linux本机的DNS配置信息是在:/etc/resolv.conf vi打开,修改为本机的dns服务器地址
- android支付宝支付开发过程
原文:android支付宝支付开发过程 支付宝开发流程: 1.在支付宝申请一个账号并开通开发者功能和移动支付的功能. 支付宝地址:https://auth.alipay.com/login/index ...
- [11-3] Gradient Boosting regression
main idea:用adaboost类似的方法,选出g,然后选出步长 Gredient Boosting for regression: h控制方向,eta控制步长,需要对h的大小进行限制 对(x, ...
- Android UI ActionBar功能-Action Bar 左上角的向上或返回按钮
ActionBar在左上角还提供了一个向上或返回的按钮,默认情况下是隐藏的需要在代码中开启: 官方文档:http://wear.techbrood.com/training/basics/action ...
- javascript数组去重算法-----3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Lucene3.6.2入门系列】第10节_Tika
首先贴出来的是演示了借助Tika创建索引的HelloTikaIndex.java PS:关于Tika的介绍及用法,详见下方的HelloTika.java package com.jadyer.luce ...
- 一个tabBarController管理多个Storyboard
随着项目的业务逻辑越来越复杂,随着项目越来越大,那么我们Storybard中得控制器就越来越多, 就越来越难以维护.然而使用Storyborad又能更方便的帮助我们做屏幕适配(PS:尤其在6.6+出来 ...
- JavaScript 工作必知(九)function 说起 闭包问题
大纲 Function Caller 返回函数调用者 Callee 调用自身 作用域 闭包 function 函数格式 function getPrototyNames(o,/*optional*/ ...
- hdu 4681 string
字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...