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 threading 模块来实现多线程
以多线程的方式向标准输出打印日志 #!/usr/bin/python import time import threading class PrintThread(threading.Thread): ...
- 第一章 andriod studio 安装与环境搭建
原文 http://blog.csdn.net/zhanghefu/article/details/9286123 第一章 andriod studio 安装与环境搭建 一.Android Stu ...
- kernal linear regression
- java操作mysql的增删改查
prepareStatement(sql)是statement的子类,比statement好用. 如果数据库中定义的是int值,那么sql语句中要把int单独提出来.如".....value ...
- heap creation
There two methods to construct a heap from a unordered set of array. If a array has size n, it can b ...
- UVA 10003 Cutting Sticks 切木棍 dp
题意:把一根木棍按给定的n个点切下去,每次切的花费为切的那段木棍的长度,求最小花费. 这题出在dp入门这边,但是我看完题后有强烈的既是感,这不是以前做过的石子合并的题目变形吗? 题目其实就是把n+1根 ...
- sqlserver 编辑、修改字段说明(备注) sp_addextendedproperty
语法 sp_addextendedproperty [ @name = ] { 'property_name' } [ , [ @value = ] { 'value' } [ , [ @leve ...
- WinEdt7.0 初试
刚刚开始学的时候,安装就出了些问题(关于安装的问题,请看我之前的文章)不知道如何点击运行,编译.看了些博客论坛.终于成功了. 首先先写一个小代码: \documentclass[UTF8]{ctexa ...
- mysql 存储过程的应用
MySQL存储过程的创建 (1). 格式 MySQL存储过程创建的格式:CREATE PROCEDURE 过程名 ([过程參数[,...]]) [特性 ...] 过程体 这里先举个样例: mysq ...
- UTL_FILE 的用法
UTL_FILE 的用法 UTL_FILE 是用来进行文件IO处理的专用包,使用这外包的注意事项如下: 1. 生成的文件好象只能放置在DATABASE所在的服务器路径中. 2. 生成的文件如何DO ...