zoukankan      html  css  js  c++  java
  • c++之类包含

    类的包含(称为has A)是程序设计中一种软件重用技术。即定义一个新的类时,通过编译器把另一个类 “抄”进来。
    当一个类中含有已经定义的类类型成员,带参数的构造函数对数据成员初始化,须使用初始化语法形式。

    构造函数 ( 变元表 ) : 对象成员1( 变元表 ) , … , 对象成员n ( 变元表 ) ;

     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 #include <string>
     5 
     6 //用类包含计算两点之间的距离
     7 class Point{
     8 public:
     9     Point(double x,double y){
    10         this->x = x;
    11         this->y = y;
    12     }
    13     Point(Point& p){
    14         cout <<"	copy_constract" <<endl;
    15     }
    16     double getX(){
    17         return x;
    18     }
    19     double getY(){
    20         return y;
    21     }
    22 private:
    23     double x,y;
    24 };
    25 class Distance{
    26 public:
    27     Distance(Point dp1,Point dp2):p1(dp1),p2(dp2){
    28         double x = dp1.getX()-dp2.getX();
    29         double y = dp1.getY()-dp2.getY();
    30         dist = sqrt(x*x + y*y);
    31     }
    32     double getDist(){
    33         return dist;
    34     }
    35 private:
    36     Point p1,p2;
    37     double dist;
    38 };
    39 
    40 void main(){
    41     Distance d(Point(3.1,5.7),Point(4.7,6.8));
    42     cout << d.getDist() << endl;
    43 }
     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 #include <string>
     5 
     6 //用类包含计算两点之间的距离
     7 class Point{
     8 public:
     9     Point(double x,double y){
    10         this->x = x;
    11         this->y = y;
    12     }
    13     Point(Point& p){
    14         cout <<"	copy_constract" <<endl;
    15     }
    16     double getX(){
    17         return x;
    18     }
    19     double getY(){
    20         return y;
    21     }
    22 private:
    23     double x,y;
    24 };
    25 class Distance{
    26 public:
    27     Distance(Point dp1,Point dp2):p1(dp1),p2(dp2){
    28         double x = dp1.getX()-dp2.getX();
    29         double y = dp1.getY()-dp2.getY();
    30         dist = sqrt(x*x + y*y);
    31     }
    32     double getDist(){
    33         return dist;
    34     }
    35 private:
    36     Point p1,p2;
    37     double dist;
    38 };
    39 
    40 void main(){
    41     Distance d(Point(3.1,5.7),Point(4.7,6.8));
    42     cout << d.getDist() << endl;
    43 }
  • 相关阅读:
    数据库事务隔离级别
    impala jdbc4的group by语句的bug,加上limit没错
    火狐不支持innerText属性,只支持innerHTML属性
    struts2.x + Tiles2.x读取多个xml 配置文件
    ids for this class must be manually assigned before calling save():Xxx
    整合ssh model $$_javassist_13 cannot be cast to javassist.util.proxy.Proxy
    火狐点击链接请求两次的问题
    C++——类和动态内存分配
    C++——使用类
    C++——对象和类
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4321267.html
Copyright © 2011-2022 走看看