C++语言基础 例程 继承与组合
2015-05-09 22:47
453 查看
贺老师的教学链接 本课讲解
点与直线
点与直线
#include<iostream> #include<Cmath> using namespace std; class Dot { public: float x,y; Dot(float a=0,float b=0) { x=a; y=b; } void Show(void) { cout<<"x="<<x<<'\t'<<"y="<<y<<endl; } }; class Line:public Dot { Dot d1,d2; public: Line(Dot dot1,Dot dot2):d1(dot1),d2(dot2) { x=(d1.x+d2.x)/2; y=(d1.x+d2.y)/2; } void Showl(void) { cout<<"Dot1: "; d1.Show(); cout<<"Dot2: "; d2.Show(); cout<<"Length="<<sqrt((d1.x-d2.x)*(d1.x-d2.x)+(d1.y-d2.y)*(d1.y-d2.y))<<endl; cout<<"Center: "<<"x="<<x<<'\t'<<"y="<<y<<endl; } }; int main() { float a,b; cout<<"Input Dot1: \n"; cin>>a>>b; Dot dot1(a,b);//调用Dot的构造函数 cout<<"Input Dot2: \n"; cin>>a>>b; Dot dot2(a,b); Line line(dot1,dot2); line.Showl(); return 0; }
相关文章推荐
- C++语言基础 例程 多重继承
- C++语言基础 例程 应用系统开发:银行储蓄系统
- C++语言基础 例程 范型编程简介
- C++语言基础 例程 C++的输入输出与流对象
- C++语言基础 例程 函数中的引用
- C++语言基础 例程 运算符重载的方法
- C++语言基础(9)-继承
- C++语言基础(10)-虚继承
- C++语言基础 例程 标准输出流
- C++语言基础 例程 STL简介
- C++语言基础 例程 重载双目运算符
- 【c++语言基础】浅析C++继承与派生
- C++语言基础 例程 标准输入流
- C++语言基础 例程 重载单目运算符
- C++语言基础 例程 文本文件的读写
- C++语言基础 例程 构造函数
- C++语言基础 例程 二进制文件及其顺序读写
- C++语言基础 例程 默认构造函数
- C++语言基础 例程 重载流插入运算符和流提取运算符
- C++语言基础 例程 派生类的声明与构成