zoukankan      html  css  js  c++  java
  • 关于构造函数对象指针等问题

    /*关于构造函数对象指针及是否调用构造函数的问题*/

    #include<iostream>
    using namespace std;
    class Date {
      public:
       Date(){ ye=1; me=2; de=3;}
       Date(int y,int m,int d);
       void print();
       private:
        int ye;
        int me;
        int de;
    };
    void Date::print(){
      cout<<ye;
    }
     /*Date::Date(int y,int m,int d){
      this.y=y;
      this.m=m;
      this.d=d;
    }*/
    int main() {
     Date d;
     int i;
      typedef Date *  pdate;
    /*  pdate mydate[10];//正确,没有调用构造函数,因为定义了10个对象的指针
      mydate[1]=&d;
      mydate[1]->print();
      以上定义mydate的语句也可以写成下面的方式,用指针的形式来定义
      */
      pdate * mydate=new pdate[10];//也正确,在指针数组的每一个指针被重新赋值,以指向一个不同的Date对象。
      /*这时可以写如下语句*/
      /*
      for(i=0;i<10;i++)
      mydate[i]=new Date(y,m,d);
      */
      mydate[1]=&d;
      mydate[1]->print();
    return 0;
    }

    内联函数

    /*关于构造函数对象指针及是否调用构造函数的问题*/

    #include<iostream>
    using namespace std;
    class Date {
      public:
       Date(){ ye=1; me=2; de=3;}
       Date(int y,int m,int d);
       void Set(int m,int d,int y) {/*内联成员函数的隐式表示*/
       cout<<m<<" "<<d<<" "<<y<<endl;
       }
        void ok();
       void print();
       private:
        int ye;
        int me;
        int de;
    };
    inline void Date::ok(){/*内联函数的显式声明*/
       cout<<222<<endl;
    }
    /*内联函数定义在类的内部或者外部没有本质的区别*/
    void Date::print(){
      cout<<ye<<endl;
    }
     Date::Date(int y,int m,int d){
      this->ye=y;
      this->me=m;
      this->de=d;
    }
    int main() {
     Date d(2,3,4);
     int i;
      Date *pd=new Date;//定义指向对象的指针,并让它指向一个新建立的对象
      pd->print();
      pd->Set(11,11,11);
      pd->ok();
    return 0;
    }

     结果:1

             11 11 11

             222

    默认构造函数的调用:

    #include<iostream>
    #include<stdlib.h>
    #include<string.h>
    using namespace std;
    class Date{
       public:
      Date(string s);
      Date(int y=2012,int m=9,int d=12);//默认参数的构造函数
      void Print();
       private:
       int year;
       int month;
       int day;
    };
    Date::Date(int y,int m,int d) {
       year=y;
       month=m;
       day=d;
    }
    Date::Date(string s) {
       year=atoi(s.substr(0,4).c_str());
       month=atoi(s.substr(5,2).c_str());
       day=atoi(s.substr(7,2).c_str());
    }
    void Date::Print() {
      cout<<year<<"-"<<month<<"-"<<day<<endl;
    }
    int main(){
       Date d1;//调用默认参数的构造函数
       d1.Print();
       Date d2("2012-2-23");
       d2.Print();

    }

    结果:

    2012-9-12
    2012-2-23

    -----------------------------
    Process exited after 0.2264 s
    请按任意键继续. . .


     

  • 相关阅读:
    SQL JOIN
    string.Empty, "" 和 null 三者的区别
    java JDBC
    java 自定义注解
    Spring Bean自动检测
    Spring Aware接口
    IObservable 接口
    CloseHandle()函数的使用
    [置顶] 记一次讲座与前辈的对话
    让用户关上门说话:覆盖全美6000个社区的邻居私密社交网站Nextdoor是如何壮大的?
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/5092492.html
Copyright © 2011-2022 走看看