zoukankan      html  css  js  c++  java
  • c++学习--面向对象一

                            类与对象一

    一 注意的点

    1 利用构造函数给数据成员初始化的两种办法:1 赋值语句,2 用成员初始化列表

       解释:1 成员初始化列表的一般形式为:

            数据成员名1(初始值1),数据成员名2(初始值2)

             2 在C++中某些类型的成员是不允许在构造函数中用赋值语句直接赋值的。例如, 对于用const修饰的数据成员,或是引用类型的数据成员,是不允许用赋值语句直接赋值的。因此,只能用成员初始化列表对其进行初始化。

     

    2 编译系统自动地调用析构函数情况

      1 主程序main()运行结束

      2 如果一个对象被定义在一个函数体内,则当这个函数结束时,改对象的析构函数被自动调用

      3 若一个对象是使用new运算符动态创建的,在使用delete运算符释放它的时候会自动调用析构函数

     

    3 拷贝构造函数

      1 该函数与类同名,并且不能指定返回值类型(因为它也是一种构造函数)

     

      2 该函数只有一个参数,并且是同类对象的引用。

      3  每个类必须有一个拷贝构造函数。

     

    #include<iostream>

    using namespace std;

    class Point       {

     public:

      Point(int a,int b)       //定义构造函数

      { x=a; y=b; cout<<"Using  normal constructor ";}

      Point(const Point &p)    //定义拷贝构造函数

      { x=2*p.x;y=2*p.y; cout<<"Using copy constructor ";}

      void print() { cout<<x<<"  "<<y<<endl; }

     private:

      int x,y; };

     void fun1(Point p){p.print();}//函数fun1的形参是类对象

    int main()

    { Point p1(30,40);//定义对象p1时,第1次调用普通的构造函数

      fun1(p1);       //在调用函数fun1,实参与形参结合时,

      return 0;      }     //第2次调用拷贝构造函数

     

     

    4 浅拷贝和深拷贝

       1 所谓浅拷贝,就是由默认的拷贝构造函数所实现的数据成员遂一赋值

          注:通常默认的拷贝构造函数是能够胜任工作的,但若类中含有指针类型的数据,这种按数据成员遂一赋值的方法将会产生错误。

      #include<iostream.h>//浅拷贝函数当遇到指针类型数据进行数据拷贝的时候会出现指针悬挂

    #include<string>

    class Student{

    public:Student(char *name1,float score1);

                          ~Student();

    private:char *name;

                       float score;

    };

    Student::Student(char *name1,float score1)

    {

             cout<<"Constructing..."<<name1<<endl;

             name=new char[strlen(name1)+1];

             if(name!=0)

             {

                       strcpy(name,name1);

                       score=score1;

             }

    }

    Student::~Student()

    {

             cout<<"Destructing..."<<name<<endl;

             name[0]='';

             delete []name;

    }

    int main()

    {

             Student stu1("liming",90);

             Student stu2=stu1;

             return 0;

    }

     

    为解决这个问题,我们需要显式地定义一个自己的拷贝构造函数,使之不但拷贝数据成员,而且为对象stu1和stu2分配各自的内存空间,这就是所谓的深拷贝。

     

    #include<iostream.h>//浅拷贝函数当遇到指针类型数据进行数据拷贝的时候会出现指针悬挂,用浅拷贝解决这个问题

    #include<string>

    class Student{

    public:Student(char *name1,float score1);

                Student(Student &stu);

                          ~Student();

    private:char *name;

                       float score;

    };

    Student::Student(char *name1,float score1)

    {

             cout<<"Constructing..."<<name1<<endl;

             name=new char[strlen(name1)+1];

             if(name!=0)

             {

                       strcpy(name,name1);

                       score=score1;

             }

    }

    Student::Student(Student &stu)//定义一个自己的拷贝构造函数,单独为stu1和stu2分配各自的内存空间

    {

             cout<<"Copy constructing..."<<stu.name<<endl;

             name=new char[strlen(stu.name)+1];

                       if(name!=0)

                       {

                                strcpy(name,stu.name);

                                score=stu.score;

                       }

    }

    Student::~Student()

    {

             cout<<"Destructing..."<<name<<endl;

             name[0]='';

             delete []name;

    }

    int main()

    {

             Student stu1("liming",90);

             Student stu2=stu1;

             return 0;

    }

     

     

     

  • 相关阅读:
    Spring基础知识
    Hibernate基础知识
    Struts2基础知识
    在eclipse里头用checkstyle检查项目出现 File contains tab characters (this is the first instance)原因
    java后台获取cookie里面值得方法
    ckplayer 中的style.swf 中的 style.xml 中的修改方法
    java hql case when 的用法
    Windows下Mongodb安装及配置
    Mongodb中经常出现的错误(汇总)child process failed, exited with error number
    Mac 安装mongodb
  • 原文地址:https://www.cnblogs.com/GoodPMj/p/5232326.html
Copyright © 2011-2022 走看看