C++组合类
发布日期:2021-05-07 10:13:20 浏览次数:15 分类:原创文章

本文共 926 字,大约阅读时间需要 3 分钟。

#include<iostream>#include<cmath>using namespace std;class A	{
public: A(int xx=0,int yy=0){
x=xx;y=yy;cout<<"A_init1"<<endl;} A(A &a){
x=a.x;y=a.y;cout<<"A_init2"<<endl;} int getx(){
return x;} int gety(){
return y;} private: int x; int y; }; class B {
public: B(int x1,int x2,int x3,int x4,int length):p1(x1,x2),p2(x3,x4) {
len=length; cout<<"B_init1"<<endl; } B(A xp1,A xp2); B(B &l); double getlen(){
cout<<len<<endl;return len;} private: A p1; A p2; double len; }; B::B(A xp1,A xp2):p1(xp1),p2(xp2) {
double x=static_cast<double>(p1.getx()-p2.getx()); double y=static_cast<double>(p1.gety()-p2.gety()); len=sqrt(x*x+y*y); cout<<"B_init2"<<endl; } B::B(B &l):p1(l.p1),p2(l.p2){
len=l.len;cout<<"B_init3"<<endl; } int main() {
A m(0,0); A n(0,0); B p(m,n); cout<<endl; B q(p); cout<<endl; B c(2,3,4,5,10); return 0; }
上一篇:极大似然估计与逻辑回归的交叉熵
下一篇:C++析构函数

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月12日 18时39分57秒