C++语言基础 例程 继承与组合

贺老师的教学链接  本课讲解

点与直线

#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;
}
时间: 2024-09-20 10:47:00

C++语言基础 例程 继承与组合的相关文章

C++语言基础 例程 应用系统开发:银行储蓄系统

贺老师的教学链接  本课讲解 说明:(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统.(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出.建议建立多文件项目,将代码拷贝到IDE中看.(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接 Record.h #ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #

C++语言基础 例程 虚基类及应用

贺老师的教学链接  本课讲解 虚基类应用举例 #include <iostream> #include <cstring> using namespace std; class Person { public: Person(char *nam,char s,int a) //构造函数 { strcpy(name,nam); sex=s; age=a; } protected: //保护成员 char name[20]; char sex; int age; }; class Te

C++语言基础 例程 虚析构函数

贺老师的教学链接  本课讲解 问题的由来 #include <iostream> using namespace std; class Point { public: Point( ) { } ~Point() { cout<<"executing Point destructor"<<endl; } }; class Circle:public Point { public: Circle( ) { } ~Circle( ) { cout<&

C++语言基础 例程 C++中的输入和输出

贺老师的教学链接 程序将自行识别符号 #include <iostream> using namespace std; int main() { int a,b; char op; cin>>a>>op>>b cout<<"result: "<<'\n'; cout<<"a: "<<a<<'\n'; cout<<"b: "<

C++语言基础 例程 重载流插入运算符和流提取运算符

贺老师的教学链接  本课讲解 重载流插入运算符"<<" #include <iostream> using namespace std; class Complex { public: Complex( ) { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } Complex operator + (Complex &c2); //运算符"+"重载为成员函

C++语言基础 例程 文本文件的读写

贺老师的教学链接  本课讲解 示例:将数据写入ASCII文件 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10]; ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat" if(!outfile) //如果打开失败

C++语言基础 例程 标准输入流

贺老师的教学链接  本课讲解 例: 输入个数不确定的成绩 #include <iostream> using namespace std; int main( ) { float grade; cout<<"enter grade:"; while(cin>>grade)//能从cin流读取数据 { if(grade>=85) cout<<grade<<" GOOD!"<<endl; if

C++语言基础 例程 函数中的引用

贺老师的教学链接  本课讲解 引用作为形参 #include<iostream> using namespace std; class Sample { int x; public: Sample(int a): x(a) {cout<<"A";} Sample(Sample &a): x(a.x) {cout<<"B";} int getX(){return x;} }; void disp(Sample &s)

C++语言基础 例程 二进制文件应用案例

贺老师的教学链接  本课讲解 系统升级第一步:转换现有数据格式(附:数据文件点击打开链接) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; typedef struct { int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total; } Stude