C++定义理解

来源:互联网 发布:淘宝可以上闲鱼吗 编辑:程序博客网 时间:2024/06/04 20:13

编译指令:

命令  编译输出  输出文件     编译文件

g++   -o         outFileName   guc.cpp

g++ -o a test.cpp


查看代码

vi  test.cpp


& 引用, 属性别名   只有在声明引用时是引用操作符,其他时候都是地址操作符

建立引用是必须进行初始化并且绝不会再关联其他不同的变量


* 指针   是个变量,所以可以有指针变量的引用 ,可以把它再赋值成指向别处的地址


int  u =0

int &ru =u

* &u  =u = 0


//是储存局部变量,和函数内部定义的变量

//是储存静态变量,和全局变量

//当用 malloc/new来申请一块内存或者创建一个对象时,申请的内存在堆上分配,需要记录得到

//得到的地址,并且在不需要的时候释放这些内存

    

//全局对象在main之前被创建,main退出后销毁

//静态对象和全局对象类似,第一次进入作用域创建,但在程序开始时,内存已经分配好

//作用域是由{}定义,并不一定是整个函数


    /*

     拷贝构造函数

     */

    //Student(const Student&);  拷贝构造函数

    //Student joe

    //1, Student john = joe; //调用拷贝构造函数 用一个对象构造另一个对象

    //2, foo(joe) //拷贝构造函数拷贝一个临时对象

    

   //何时需要定义拷贝构造函数

    //类数据成员有指针

       //类数据成员管理资源(如打开一个文件)

   //如果一个类需要析构函数来释放资源,则它也需要拷贝析构函数

   //如果想禁止一个类的拷贝构造,需要将拷贝构造函数为private

    

    /*

      const 的值不能改变

      const *  是个常量  int const *p=3; error *p=4;

      * const  是指针自身是个常量  int *const p=0;  error p =3

      const修饰成员函数,不会对对象成员进行修改

     */


    /*

extern 继承

        protected  成员函数或成员,子类能访问父类的成员函数或成员 子类不能访问父类的私有成员函数或成员

     */

    

    /*

        构造是先构造父类,再构造子类,析构是先析构子类,再析构父类

     */



//mP3进行设值。 12都为 0

    memset(mP3, 0, 12);

    strcpy(mP3, mP2);

//两个char类型链接

    strcat(m_data, other.m_data);


.h

#ifndef IotekString_hpp

#define IotekString_hpp


#include <stdio.h>


//自定义命名空间

namespace iotek {

    

    class String

    {

        public:

            String(constchar* =NULL);

            ~String();

        

            String(constString&);

        

            //String a; a = b

            Stringoperator=(constString&);

            //String a; a = "hello"

            Stringoperator=(constchar*);

        

        

            Stringoperator+=(constString&);

            String operator+(constString&)const;

        

            Stringoperator+=(constchar *other);

            String operator+(constchar *other)const

        

        

            inlineconstchar *data()const{

            returnm_data;

            }

        

        

        private:

            char *m_data;

    };

    

}



#endif /* IotekString_hpp */



Cpp:


#include "IotekString.hpp"

#include <string.h> 

#include <iostream>

using namespace ::std;

using namespace ::iotek;//加入自定义命名空间



String::String(constchar *str)

{

    if(str==NULL)

    {

        m_data=newchar[1];

        *m_data='\0';

    }else{

        int length =strlen(str);

        m_data =newchar[length+1];

        strcpy(m_data, str);

    }

}



String::~String()

{

    delete[]m_data;

    m_data=NULL;

}




String::String(constString &other)

{

    int length =strlen(other.m_data);

    m_data=newchar[length+1];

    strcpy(m_data, other.m_data);

    

}




String& String::operator=(constString &other)

{

    if(this==&other)

    {

        return *this;

    }

    

    delete[]m_data;

    

    int length =strlen(other.m_data);

    m_data=newchar[length+1];

    strcpy(m_data, other.m_data);

    return *this;

}




String& String::operator=(constchar *other)

{

    delete[]m_data;

    if(other==NULL)

    {

        m_data=newchar[1];

        *m_data='\0';

    }else{

        int length =strlen(other);

        m_data =newchar[length+1];

        strcpy(m_data, other);

    }

    return *this;

}


String& String::operator+=(constString& other)

{

    char *tmp =m_data;

    int length =strlen(m_data)+strlen(other.m_data);

    m_data=newchar[length+1];

    strcpy(m_data, tmp);

    //两个char类型链接

    strcat(m_data, other.m_data);

    delete[] tmp;

    tmp=NULL;

    

    return *this;

}


String String::operator+(constString& other) const

{

    String result;

    result +=*this;

    result +=other;

    

    return result;

}


String& String::operator+=(constchar* other)

{

    String tmp(other);

    *this +=tmp;

    

    return *this;

}


String String::operator+(constchar* other)const

{

    String result=*this;

    result +=other;

    

    return result;

}


/*

多重继承

*/

class A:public B,public C{

}

如果class B C都相同的函数,就要定义第三方class D, BC通过 virtual继承

class B:virtual public D{

}

class C:virtual public D{

}



/*

多态

*/ 


//虚函数和抽象类

class Animal{

  virtual ~Animal()  //基类必须是纯虚析构函数,如果不是纯虚析构函数将会用到指针调用的时候,它只会调用基类析构函数,不会调用子类的析构函数,会导致内存泄漏

  virtual void makeSound();


//纯虚函数和接口类  接口类不能实例化,不能生产对象实例

class Animal{

  virtual ~Animal()=0;

  virtual void makeSound()=0;

}

class Dog:public Animal{

   void makeSound();


}

class Cat:public Animal{

  void makeSound();

}

int main(string[] args)

{

  Dog dog;

  Cat cat;

  fun(dog);//输出的结果 是dog的makeSound

  fun(cat);//输出的结果 是cat的makeSound


  bar(&dog);//输出的结果 是dog的makeSound

  bar(&cat);//输出的结果 是cat的makeSound




}

//使用引用   是多态输出

void func(const Animal &animal)

{

    animal.makeSound()

}

//是用指针 是多态输出

void bar(Animal *animal)

{

    animal.makeSound()

}  


//是用值 不是多态输出

void foo(Animal animal)

{

    animal.makeSound()

  fun(dog);//输出的结果 是dog的makeSound

  fun(cat);//输出的结果 是cat的makeSound

  fun(dog);//输出的结果 是dog的makeSound

  fun(cat);//输出的结果 是cat的makeSound


类模板定义:

template<模板参数表>

class 类名

{

类成员声明:

}

template<class numtype>//声明一个模板,虚拟类型名为numtyp

class Compare//类模板名为Compare

{
public :
   Compare(numtype a,numtype b)
   {
      x=a;y=b;
   }
   numtype max()
   {
      return(x>y)?x:y;
   }
   numtype min()
   {
      return(x<y)?x:y;
   }
private :
   numtype x,y;
};




/*

算法,向量,迭代器

*/ 


#include <algorithm>

#include <vector>


bool compare(int a,int b)

{

    return a>b;//升序排列,如果改为return a>b,则为降序

}


vector<int> ab;

ab.push_back(1);

ab.push_back(32);

ab.push_back(22);

ab.push_back(12);

    

sort(ab.begin(),ab.end(),compare);//默认为升序

    

vector<int>::iterator iter;

    

for(iter=ab.begin();iter!=ab.end();++iter)

{

     cout<<*iter<<endl;

}

printf("\n");


#include<list>

 /*

列表 list

   */

list<int> abList;

abList.push_back(1);

abList.push_back(32);

list<int>::const_iterator iterlist;

    

for(iterlist=abList.begin();iterlist!=abList.end();++iterlist)

{

     cout<<*iterlist<<endl;

}

printf("\n");