// CTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <math.h> using namespace std; class Cpoint{ private: double x,y; public: Cpoint(double x1,double y1){ x=x1;y=y1; } void print(){ cout<<"("<<x<<","<<y<<")"<<endl; } friend double dist(Cpoint a,Cpoint b); }; double dist(Cpoint a,Cpoint b){ double dx = b.x-a.x; double dy = b.y-a.y; return sqrt(dx*dx+dy*dy); } int _tmain(int argc, _TCHAR* argv[]) { Cpoint p1(3,4),p2(6,8);//实例化 p1.print(); p2.print(); double d = dist(p1,p2);//执行友元函数 cout<<"d="<<d<<endl; system("pause"); return 0; }
时间: 2024-10-29 19:06:48