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. aspose.words 处理word转PDF

    处理如下: import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.platform.cust ...

  2. java+android学习路线图

    java.android学习路线图  看图之前先按住Ctrl键同时滑动鼠标滚轮

  3. Java书籍推荐

    Java书籍推荐 转自:http://www.cnblogs.com/exclm/archive/2009/01/03/1367597.html 一.入门 <Java 2从入门到精通>- ...

  4. Codeforces 734F Anton and School(位运算)

    [题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi ...

  5. user Collaborative Filtering

    ---恢复内容开始--- 算法步骤: 1.计算用户相似度 2.对于特定用户,选出k个最相似的用户,将这些用户评价过的前k好的物品推荐给该用户   用户相似度 度量: 其中|N(u)|表示用户u评价过的 ...

  6. C# 连接 Mysql 中文乱码问题

    网上有很多解决乱码的方法,什么 set names utf8   .什么在插入数据前进行编码,亲自试了都没有效果,在网上寻觅了很久,终于找到一种方法,并亲试成功: 首先要保证你的数据库是UTF8字符集 ...

  7. WinForm 窗体与窗体相互嵌套

    只要将要被潜逃的的窗体的TopLeve设置为Flase即可像普通的控件一样,被添加到另外一个窗体中,TopLeve:是否为顶级窗口,下面来看代码: public partial class TTFor ...

  8. iOS学习,需要懂的一些基础

    1.  KVC 与 KVO 全称是Key-value coding,翻译成键值编码.顾名思义,在某种程度上跟map的关系匪浅.它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制. 全称 ...

  9. Linux下实现视频读取(二)---camera參数设定

    Camera的可设置项极多,V4L2 支持了不少.但Sam之前对这些设置的使用方法和涵义都是在看videodev2.h中边看边理解.感觉很生涩. 直到写这篇blog时,才发现v4l2有专门的SPEC来 ...

  10. Android常见布局问题

    原文链接:http://www.cnblogs.com/Birdmafly/p/3809802.html 好久没写博了,因为最近在忙着做一个app,实在是没有时间.现在快完工了.想着还是把这个布局问题 ...