C++基础 课程代码

本文档是C++基础课程的一部分,涵盖了类与对象的使用,包括构造函数、析构函数、成员函数、指针操作以及静态成员和常量成员的处理。还涉及到参数传递、对象创建和释放、单例模式、运算符重载、函数对象、继承与派生等概念,通过多个示例展示了C++编程中的常见实践和技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2010

0412

a

// 0412a.cpp : Defines the entry point for the console application.
//类和对象的理解

#include "stdafx.h"

class CInfo
{
private:
 int data;

public :
 void Set(int i)
 {
  data = i;
 }
 int *Get()
 {
  return &data;
 }
 int &Get1()
 {
  return data;
 }
};

void Test()
{
 CInfo obj,*pObj2;
 int *pData;
 obj.Set(1);
 pData=obj.Get();//地址未释放
 *pData=2; //指针可以访问私有成员变量

 pObj2=new CInfo; //在堆上动态分配
 pObj2->Set(2);
 delete pObj2;

 int &Obj3=obj.Get1();//引用

}

int main(int argc, char* argv[])
{
 Test();

 return 0;
}

b

// 0412a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "info.h"

void Test()
{
 CInfo obj(0);
 CInfo obj2=2;
 CInfo obj3;
 CInfo *pObj4=new CInfo;
 delete pObj4;
}

int main(int argc, char* argv[])
{
 Test();
 return 0;
}

// Info.cpp: implementation of the CInfo class.
//
//

#include "stdafx.h"
#include "Info.h"

//
// Construction/Destruction
//

CInfo::CInfo(int i)
{
 m_iData=i;
 puts("CInfo ");
}

CInfo::~CInfo()
{
 puts("~CInfo");
}

const int * CInfo::Get()
{ 
 return &m_iData;
}

void CInfo::Set(int i)
{
 m_iData=i;
 
}
//Info.h

// Info.h: interface for the CInfo class.
//
//

#if !defined(AFX_INFO_H__983EF703_97E6_4EB0_8A15_7351BEE0482C__INCLUDED_)
#define AFX_INFO_H__983EF703_97E6_4EB0_8A15_7351BEE0482C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CInfo  
{
public:
 void Set(int i);
 const int * Get();
 CInfo(int i=0);//构造函数,在对象实例化出来以后自动执行,通常用来初始化成员变量
 virtual ~CInfo();//析构函数,在对象释放前自动执行的函数,通常用来清理对象在使用过程中占用的资源

private:
 int m_iData;
};

#endif // !defined(AFX_INFO_H__983EF703_97E6_4EB0_8A15_7351BEE0482C__INCLUDED_)

=================================================================================

0413

a

 

b

// 0413b.cpp : Defines the entry point for the console application.
//参数的传递

#include "stdafx.h"

class CA
{
public:
 // CA(int i){}
 CA(int i,int j){}
};

void Func(CA obj){}
CA Func2(){
 return CA(5,5);
}

int main(int argc, char* argv[])
{
/* CA obj(1);
 CA obj2=CA(2);
 CA obj3=3;//只有一个参数的情况下允许
 CA *pObj4=new CA(4);
*/

 CA obj(1,1);
 CA obj2=CA(2,2);
// CA obj3=3;//只有一个参数的情况下允许
 CA *pObj4=new CA(4,4);
 CA obj5[2]={CA(5,6),CA(7,8)};
// CA *pObj6=new CA[2]

 Func(obj);
 Func(obj2);
 Func(CA(3,3));//临时传递对象
 Func(obj);

 return 0;
}

 c

// 0413b.cpp : Defines the entry point for the console application.
//需要初始化成员变量的情况

#include "stdafx.h"

class CData
{
private:
 int m_j;
public:
 CData(int j)
 {
//  m_j=0;
  m_j=j;
 }
};

class CA
{
private:
 const int m_i;//常量必须初始化
 CData m_data;//类类型的成员必须初始化
public:
 static int m_j;静态变量必须初始化

 CA(int i,int j):m_i(i),m_data(j)
 {
 }
};

int CA::m_j=100;

int main(int argc, char* argv[])
{
 CA obj(2,3);
 //obj.m_j=4;
 printf("m_j:%d\n",obj.m_j);

 CA obj2(2,3);
 return 0;
}

 d

// 0413d.cpp : Defines the entry point for the console application.
//常量成员变量、函数、对象,静态成员变量、成员函数

#include "stdafx.h"

class CA
{
private:
 const int m_i;//常量成员必须初始化
 int m_j; 
public:
 static int m_k;//静态成员必须在全局区域初始化
 CA(int i=0):m_i(i)
 {
 }
 void Func()const
 {
 // m_j=1; //常量成员函数不能修改成员变量,可以访问
 }
 static void Func2()
 {
 // m_j=6; //静态成员函数只能访问静态变量
 }
};
int CA::m_k=0;

int main(int argc, char* argv[])
{
 const CA obj;
 CA obj2;
 obj.Func();//常量对象访问常量成员函数或成员
 obj.m_k=1;
 obj2.m_k=2;
 CA::m_k=3;//静态成员可以加限制作用域访问
 CA::Func2();

 printf("%d",sizeof(obj));//大小为8, 类的静态成员不属于对象
 return 0;
}

e

// 0413e.cpp : Defines the entry point for the console appliCSingletontion.
//单例——只能实例化一个对象

#include "stdafx.h"

class CSingleton
{
public:
 static CSingleton *GetObj()
 {
  if(m_iRef == 0)
   m_pObj=new CSingleton;
  m_iRef++;
  return m_pObj;
 }

 static Release() //释放
 {
  if(--m_iRef == 0){
   delete m_pObj;
   m_pObj=NULL;
  }
 }
private:
 static CSingleton *m_pObj;
 static int m_iRef;
 CSingleton(){ }
};
CSingleton *CSingleton::m_pObj = NULL;
int CSingleton::m_iRef = 0;

int main(int argc, char* argv[])
{
 CSingleton *pObj=CSingleton::GetObj();
 CSingleton::Release();

 CSingleton *pObj2=CSingleton::GetObj();
 CSingleton::Release();

 return 0;
}

 f

// 0413f.cpp : Defines the entry point for the console application.
//this 指针

#include "stdafx.h"

class CA
{
public:
 int m_i;
 void Fun() //相当于 Fun(CA *this)
 {
  this->m_i=0;
 }
 CA &Func2()
 {
  return *this;//返回对象本身的引用
 }
};

int main(int argc, char* argv[])
{
 CA obj,obj2,*pObj;

 obj.Fun(); // 相当于Func(&obj);
 obj2.Fun();  // 相当于Func(&obj2);
 pObj=&obj;
 pObj->Fun(); // 相当于Func(pObj);

 CA &objRef = obj.Func2();  //引用

// obj.m_i=1;
 return 0;
}

 g

// 0413f.cpp : Defines the entry point for the console application.
//this 指针 理解

#include "stdafx.h"

class CA
{
public:
 int m_i;
 void Func()
 {
  delete this;
//  this->m_i=1;
  puts("Func()");
//  printf("%d",m_i);
 }
};

int main(int argc, char* argv[])
{
 CA *pObj=NULL;
 pObj->Func(); //相当于 Func(pObj);

 return 0;
}

 

x

// 0413X.cpp : Defines the entry point for the console application.
//队列的封装

#include "stdafx.h"
#include "MyQueue.h"

int main(int argc, char* argv[])
{
 int i;
 Person obj,objs[]={
  {1,"aaa","aaa111"},
  {2,"bbb","bbb222"},
  {3,"ccc","ccc333"},
  {4,"ddd","ddd444"},
 }; 
 CMyQueue queue;
 for(i=0; i<4; i++)
  queue.PushBack(objs[i]);

 for(i=0; i<2; i++){
  queue.PopFront(obj);
  printf("id:%d,name:%s,major:%s\n",
   obj.iId,obj.szName,obj.szMajor);
 }
 return 0;
}

 

// MyQueue.h: interface for the CMyQueue class.
//
//

#if !defined(AFX_MYQUEUE_H__5CDE9802_5E99_4FB2_9FEE_CF649E8762B9__INCLUDED_)
#define AFX_MYQUEUE_H__5CDE9802_5E99_4FB2_9FEE_CF649E8762B9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

 

struct Person
{
 int iId;
 char szName[16];
 char szMajor[16];
};

class CMyQueue  
{
private:
 struct Node
 {
  Node *pPrev;
  Person data;
  Node *pNext;
 };
public:
 bool PopFront(Person &iData);
 void PushBack(const Person &iData);
 CMyQueue();
 virtual ~CMyQueue();
private:
 Node *m_pHead;
 Node *m_pTail;
};

#endif // !defined(AFX_MYQUEUE_H__5CDE9802_5E99_4FB2_9FEE_CF649E8762B9__INCLUDED_)



// MyQueue.cpp: implementation of the CMyQueue class.
//
//

#include "stdafx.h"
#include "MyQueue.h"

CMyQueue::CMyQueue()
{
 m_pHead = NULL;
 m_pTail = NULL;
}

CMyQueue::~CMyQueue()
{
 Node *pNode;
 pNode=m_pHead;
 if(m_pHead)
 {
  m_pHead=m_pHead->pNext;
  delete pNode;
 }
}

void CMyQueue::PushBack(const Person &iData)
{
 Node *pNode = new Node;
 pNode->data=iData;
 
 if(m_pTail){
  pNode->pNext=NULL;
  pNode->pPrev=m_pTail;
  m_pTail->pNext=pNode;
  m_pTail=pNode;
 }
 else{ 
  pNode->pNext=NULL;
  pNode->pPrev=m_pTail;
  m_pHead=pNode; 
  m_pTail=pNode;
 }
}

bool CMyQueue::PopFront(Person &iData)
{ 
 Node *pNode=m_pHead;
 if(m_pHead==NULL)
  return false;
 iData=m_pHead->data;

 m_pHead=m_pHead->pNext;
 if(m_pHead ==NULL){
  m_pTail=NULL;
 }else{
  m_pHead->pPrev=NULL;
  }
 delete pNode;
 return true;
}

y


// 0413b.cpp : Defines the entry point for the console application.
//参数的传递和释放

#include "stdafx.h"
#include <string.h>

class CA  
{
private:
// char m_szBuf[16]; // ..1..

 char *pszBuf;
public:
 CA(char *szBuf="")
 {
//  strcpy(m_szBuf,szBuf); //..1..

//  pszBuf=(char*)malloc(strlen(szBuf)+1); strcpy(pszBuf,szBuf); //..2..
//  pszBuf=new char[strlen(szBuf)+1]; strcpy(pszBuf,szBuf);
  pszBuf=strdup(szBuf);
 }
 ~CA()
 {
//  delete []m_szBuf; //..2..

/*  if(*pszBuf){
   free(pszBuf);
   *pszBuf=NULL;
  }
*/  free(pszBuf);
 }
 CA(const CA &obj)//拷贝构造函数
 {
  pszBuf=strdup(obj.pszBuf);
 }
};

void Func()
{
 char buf[]="abcdefg";
 CA obj(buf);

 CA obj2=obj;
 CA obj3;
 //obj3=obj;
}

int main()
{
 Func();
 return 0;
}


 

// 0413b.cpp : Defines the entry point for the console application.
//参数的传递和释放

#include "stdafx.h"
#include <string.h>

class CA  
{
private:
// char m_szBuf[16]; // ..1..

 char *pszBuf;
public:
 CA(char *szBuf="")
 {
//  strcpy(m_szBuf,szBuf); //..1..

//  pszBuf=(char*)malloc(strlen(szBuf)+1); strcpy(pszBuf,szBuf); //..2..
//  pszBuf=new char[strlen(szBuf)+1]; strcpy(pszBuf,szBuf);
  pszBuf=strdup(szBuf);
 }
 ~CA()
 {
//  delete []m_szBuf; //..2..

/*  if(*pszBuf){
   free(pszBuf);
   *pszBuf=NULL;
  }
*/  free(pszBuf);
 }
 CA(const CA &obj)//拷贝构造函数
 {
  pszBuf=strdup(obj.pszBuf);
 }
};

void Func()
{
 char buf[]="abcdefg";
 CA obj(buf);

 CA obj2=obj;
 CA obj3;
 //obj3=obj;
}

int main()
{
 Func();
 return 0;
}


xx

// 0413a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyQueue.h"

int main(int argc, char* argv[])
{
 int i;
 Person obj,objs[]={
  {1,"aaa","aaa111"},
  {2,"bbb","bbb222"},
  {3,"ccc","ccc333"},
  {4,"ddd","ddd444"},
 }; 
 CMyQueue queue;
 for(i=0; i<4; i++)
  queue.PushBack(objs[i]);

 for(i=0; i<2; i++){
  queue.PopFront(obj);
  printf("id:%d,name:%s,major:%s\n",
   obj.iId,obj.szName,obj.szMajor);
 }
 
 return 0;
}

 

// MyQueue.h: interface for the CMyQueue class.
//
//

#if !defined(AFX_MYQUEUE_H__35DF3F9B_5FE8_4081_8C81_F153515DF3AD__INCLUDED_)
#define AFX_MYQUEUE_H__35DF3F9B_5FE8_4081_8C81_F153515DF3AD__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct Person
{
 int iId;
 char szName[16];
 char szMajor[16];
};

class CMyQueue  
{
private:
 struct Node
 {
  Node *pPrev;
  Person data;
  Node *pNext;
 };

public:
 CMyQueue();
 virtual ~CMyQueue();
 void PushBack(const Person &data);
 bool PopFront(Person &data);

private:
 Node *m_pHead;
 Node *m_pTail;
};

#endif // !defined(AFX_MYQUEUE_H__35DF3F9B_5FE8_4081_8C81_F153515DF3AD__INCLUDED_)


// MyQueue.cpp: implementation of the CMyQueue class.
//
//

#include "stdafx.h"
#include "MyQueue.h"

//
// Construction/Destruction
//

CMyQueue::CMyQueue()
{
 m_pHead=NULL;
 m_pTail=NULL;
}

CMyQueue::~CMyQueue()
{
 Node *pNode;
 while(m_pHead){
  pNode=m_pHead;
  m_pHead=m_pHead->pNext;
  delete pNode;
 }
}

void CMyQueue::PushBack(const Person &data)
{
 Node *pNode=new Node;
 pNode->data=data;

 pNode->pNext=NULL;
 pNode->pPrev=m_pTail;
 
 if(m_pTail){
  m_pTail->pNext=pNode;
 }else{
  m_pHead=pNode;
 }
 m_pTail=pNode;
}

bool CMyQueue::PopFront(Person &data)
{
 Node *pNode=m_pHead;
 if(m_pHead == NULL)
  return false;
 data=m_pHead->data;
 m_pHead=m_pHead->pNext;
 if(m_pHead == NULL){
  m_pTail=NULL;
 }else{
  m_pHead->pPrev=NULL;
 }
 delete pNode;
 return true;
}


yy

// 0413b.cpp : Defines the entry point for the console appliCSingletontion.
//

#include "stdafx.h"

class CSingleton  //单例
{
private:
 static CSingleton *m_pObj;
 static int m_iRef;
 CSingleton(){ }
public:
 static CSingleton *GetObject()
 {
  if(m_iRef == 0)
   m_pObj=new CSingleton;
  m_iRef++;
  return m_pObj;
 }
 static Release()
 {
  if(--m_iRef == 0){
   delete m_pObj;
   m_pObj=NULL;
  }
 }
};

CSingleton *CSingleton::m_pObj=NULL;
int CSingleton::m_iRef=0;

int main()
{
 CSingleton *pObj=CSingleton::GetObject();
 pObj->Release();

 CSingleton *pObj2=CSingleton::GetObject();
 
 pObj2->Release();
 return 0;
}


=================================================================================

0414

a

// 0314a.cpp : Defines the entry point for the console application.
//友元函数、友元类

#include "stdafx.h"

class CA
{
private:
 void Func() { }
friend void Test();//声明为友元函数
friend  class CB;//声明为友元类
};

class CB
{
private:
 void Func2()
 {
  CA *pObj=NULL;
  pObj->Func();
 }
};

void Test()
{
 CA obj;
 obj.Func();
}

int main(int argc, char* argv[])
{
 Test();
 return 0;
}

 b

// 0314a.cpp : Defines the entry point for the console application.
//类成员函数声明为友元

#include "stdafx.h"

class CB
{
public:
 void Funb();
};

class CA
{
private:
 void Funa() { }
 friend void CB::Funb();
};

void CB::Funb()
{
 CA *pObj=NULL;
 pObj->Funa();
}
int main(int argc, char* argv[])
{
 CB b;
 b.Funb();
 return 0;
}

 c

// 0414c.cpp : Defines the entry point for the console application.
//运算符重载

#include "stdafx.h"

class CA
{
private:
 int m_i;
public:
 CA(int i=0)
 {
  m_i=i;
 }
 friend CA operator+(const CA&a,int i);
};

CA operator+(const CA&a,int i)
{
 CA obj;
 obj.m_i=obj.m_i+i;
 return obj;
}

int main(int argc, char* argv[])
{
 int i=0;
 int *pi=NULL;
 i++;//单目运算符重载
 pi++;

 CA a,b;
 b=a+2;
 operator+(a,2);

 return 0;
}

 d

// 0414c.cpp : Defines the entry point for the console application.
//运算符重载为成员函数

#include "stdafx.h"

class CA
{
private:
 int m_i;
public:
 CA(int i=0)
 {
  m_i=i;
 }

CA operator+(int i)
 {
  CA obj;
  obj.m_i=m_i+i;
  return obj;
 }
};

int main(int argc, char* argv[])
{
 CA a,b;
 b=a+2; //b=a.operator+(2);

 return 0;
}

 e

// 0414c.cpp : Defines the entry point for the console application.
//单目运算符重载为成员函数

#include "stdafx.h"

class CA
{
private:
 int m_i;
public:
CA operator++()
 {
/*  CA a;
  a.m_i=++m_i;
  return a;
*/
  ++m_i;
  return *this;
 }
CA operator++(int)
 {
 CA ret(*this);
  ++m_i;
  return ret;
 }
};

int main(int argc, char* argv[])
{
 CA a,b;
 ++a; //a.operator ++();

 b++;

 return 0;
}

f

// 0414f.cpp : Defines the entry point for the console application.
//赋值运算符重载

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

class CA
{
private:
 char *pszBuf;
public:
 CA(const char *szBuf="")
 {
  pszBuf=strdup(szBuf);
 }
 ~CA()
 {
  free(pszBuf);
 }
 const CA &operator=(CA &objSrc)
 {
  free(pszBuf);
  pszBuf=strdup(objSrc.pszBuf);
  return *this;
 }
};

int main(int argc, char* argv[])
{
 CA a="abcdefg";
 CA b;
 b=a; //b=a.operator =();

 return 0;
}

  g

// 0414f.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <stdlib.h>

class CA
{
private:
 char *pszBuf;
 int m_i;
public:
 CA(int i):m_i(i) { }

 CA(const char *szBuf="")
 {
  pszBuf=strdup(szBuf);
 }

 char &operator[](int iIndex) //数组运算符重载
 {
  return pszBuf[iIndex];
 }
 operator int() //类型转换重载函数 int
 {
  return m_i;
 }

 void *operator new[](size_t iSize)  //new运算符重载  *operator new(size_t iSize)
 {
  void *p=malloc(iSize); //分配空间
  return p;
 }

 void operator delete[](void *p)   //delete 运算符重载  operator delete(void *p)
 {

 }
};

int main(int argc, char* argv[])
{
 CA a="abcdefg";
 a[2]='k'; //a.operator [](2);

 CA b(3);
 int i=(int)b; 

 CA *pObj=new CA[2]; 
 delete []pObj;

 return 0;
}

 x

// 0414c.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyString.h"

int main()
{
 char szSrc[]="Hello!";
 //调用无参数的构造函数 
 CMyString s1;  
 //调用类型转换函数,将s1转换成const char *,即将s1.m_pchData暴露给外部;
 printf("s1 is:%s\n",(const char *)s1);
 //调用参数类型为const char *的构造函数
 //char *pszRealParam=szSrc;
 CMyString s2;
 //调用拷贝构造函数
 CMyString s3(s2);
 //重载运算符 =
 s1="My name is s1";
 //重载运算符 =
 s3=s1;
 s3="My name";
 //s3=s3+" is"+" s3";
 s2=s3+"abc";
 //重载运算符 +=
 s3+=" ,and call +=";
 //重载运算符[]
 char c=s3[1];  //应该返回'y'
 //最后注意不同的对象分别释放自己的m_pchData所指向的内存
 printf("s3:%s\n",(const char *)s3);
 char szBuf[32];
 strcpy(szBuf,s3);
 return 0;
}


y

// 0414c.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyString.h"

int main()
{
 char szSrc[]="Hello!";
 //调用无参数的构造函数 
 CMyString s1;  
 //调用类型转换函数,将s1转换成const char *,即将s1.m_pchData暴露给外部;
 printf("s1 is:%s\n",(const char *)s1);
 //调用参数类型为const char *的构造函数
 //char *pszRealParam=szSrc;
 CMyString s2;
 //调用拷贝构造函数
 CMyString s3(s2);
 //重载运算符 =
 s1="My name is s1";
 //重载运算符 =
 s3=s1;
 s3="My name";
 //s3=s3+" is"+" s3";
 s2=s3+"abc";
 //重载运算符 +=
 s3+=" ,and call +=";
 //重载运算符[]
 char c=s3[1];  //应该返回'y'
 //最后注意不同的对象分别释放自己的m_pchData所指向的内存
 printf("s3:%s\n",(const char *)s3);
 char szBuf[32];
 strcpy(szBuf,s3);
 return 0;
}


// MyString.h: interface for the CMyString class.
//
//

#if !defined(AFX_MYSTRING_H__8816140B_3BF2_4307_9F2C_A7AFD585AE66__INCLUDED_)
#define AFX_MYSTRING_H__8816140B_3BF2_4307_9F2C_A7AFD585AE66__INCLUDED_

typedef char *   LPTSTR;
typedef const char * LPCTSTR;
//包装一个字符串类
class CMyString  
{
private:
 //m_pszData指向实际存放字符串的缓冲区
 //该缓冲区应该在CMyString的构造函数中,
 //采用堆分配方式分配出来
 LPTSTR m_pszData;
public:
 //应该根据pszData分配合适大小的缓冲区,
 //以容纳pszData字符串的拷贝
 CMyString(LPCTSTR pszData="");

 //应该根据sSrc分配合适大小的缓冲区,
 //以容纳sSrc字符串的拷贝
 CMyString(const CMyString &sSrc);

 //应该释放m_pszData指向的堆内存
 ~CMyString();

 //将sSrc表示的字符串复制给当前对象
 const CMyString &operator=(const CMyString &sSrc);

 //将pszSrc表示的字符串复制给当前对象
 const CMyString &operator=(LPCTSTR pszSrc);

 //用当前字符串对象和pszSrc合成一个新字符串对象
 CMyString operator+(LPCTSTR pszSrc);

 //在当前对象的字符串末尾追加字符串pszSrc
 //注意应该首先扩充当前对象的原有缓冲区大小
 const CMyString &operator+=(LPCTSTR pszSrc);

 //返回字符串的第iIndex个字符
 char operator[](int iIndex);

 //将对象转换成LPCTSTR类型,实际上就是将
 //m_pszData指针返回出去
 operator LPCTSTR()
 {
  return m_pszData;
 }
};

#endif // !defined(AFX_MYSTRING_H__8816140B_3BF2_4307_9F2C_A7AFD585AE66__INCLUDED_)


#include "stdafx.h"
#include "MyString.h"

CMyString::CMyString(const char *pszData)
{
 m_pszData=new char[strlen(pszData)+1];
 strcpy(m_pszData,pszData);
}

CMyString::CMyString(const CMyString &sSrc)
{
 m_pszData=new char[strlen(sSrc.m_pszData)+1];
 strcpy(m_pszData,sSrc.m_pszData);
}

CMyString::~CMyString()
{
 delete []m_pszData;
}

const CMyString &CMyString::operator=(const char *pszSrc)
{
 delete []m_pszData;
 m_pszData=new char[strlen(pszSrc)+1];
 strcpy(m_pszData,pszSrc);
 return *this;
}

const CMyString &CMyString::operator=(const CMyString &sSrc)
{
 if(this == &sSrc)
  return *this;
 return operator=(sSrc.m_pszData);
}

CMyString CMyString::operator+(const char *pszSrc)
{
 char *pszData=new char[strlen(m_pszData)+strlen(pszSrc)+1];
 strcpy(pszData,m_pszData);
 strcat(pszData,pszSrc);
 return CMyString(pszData);
}

const CMyString &CMyString::operator+=(const char *pszSrc)
{
 char *pszNewData=new char[strlen(m_pszData)+strlen(pszSrc)+1];
 strcpy(pszNewData,m_pszData);
 strcat(pszNewData,pszSrc);
 delete []m_pszData;
 m_pszData=pszNewData;
 return *this;
}

char CMyString::operator[](int iIndex)
{
 //此处可以考虑不判断
 if(iIndex<0 || iIndex>int(strlen(m_pszData))-1)
  return 0;
 return m_pszData[iIndex];
}

 =================================================================================

0415

a

// 0415a.cpp : Defines the entry point for the console application.
// ->、*运算符重载 ,包装类的应用(单例)

#include "stdafx.h"

class CMyCom
{
public:
 void Func()
 {
  puts("CMyCom::Func()");
 }
};

class CSmartPtr //包装类(CMyCom)方便使用  应用单例
{
private:
 CMyCom *m_pCom;

public:
 CSmartPtr()
 {
  m_pCom=NULL;
 }
 ~CSmartPtr()
 {
  delete(m_pCom);
 }
 void CreateInstance()
 {
  if(m_pCom==NULL)
   m_pCom = new CMyCom;
 }

 CMyCom *operator->() //->运算符重载
 {
  return m_pCom;
 }

 CMyCom &operator*() //*运算符重载 返回引用
 {
  return *m_pCom;
 }

 CMyCom *GetMyCom() //函数的形式
 {
  return m_pCom;
 }
};

void Func()
{
 CSmartPtr obj; //智能指针
 obj.CreateInstance();

 obj->Func(); //m_pCom->Func();
// obj.operator ->()->Func();
 (*obj).Func(); //m_pCom.Func();
// obj.operator *().Func();

 obj.GetMyCom()->Func();
 
}

int main(int argc, char* argv[])
{
 Func();
 return 0;
}

 b

// 0415b.cpp : Defines the entry point for the console application.
//函数对象

#include "stdafx.h"

class CFuctionObject
{
public:
// int m_iCount;
 void operator ()(int i,int j)
 {
//  m_iCount++;
  printf("i:%d,j:%d",i,j);
 }

};

int main(int argc, char* argv[])
{
 CFuctionObject myfunc; //函数对象,仿函数 STL比较常见
 myfunc(1,2);//对象模仿函数行为

 return 0;
}

c

// 0415c.cpp : Defines the entry point for the console application.
//继承与派生

#include "stdafx.h"

class CB
{
public:
 int m_i;
 void Func()
 {
  m_i=0;
 }
};

class CD:protected CB
{
public:
 int m_j;
 void Func()//不会覆盖
 {
  m_i=0;
  Func();//递归调用自己
  CB::Func();//调用基类
 }
};

int main(int argc, char* argv[])
{
 CD d;
 d.Func(); //默认就近原则 
// d.CB::Func();
 return 0;
}

  d

// 0415c.cpp : Defines the entry point for the console application.
//基类与派生类的赋值

#include "stdafx.h"

class CB
{
public:
 int m_i;
 void Func()
 {
 
 }
};

class CD:public CB
{
public:
 int m_j;
 void Func(int i)
 {
  
 }
};

int main(int argc, char* argv[])
{
 CD d;
// d.Func();
 CB *pb=&d;//基类可指向派生类,派生类不能指向基类
 CB &bRef=d;

 CB b;
 b=d;//只能从派生类赋给基类(存储空间上理解)

 return 0;
}

 e

// 0415c.cpp : Defines the entry point for the console application.
//继承的构造与析构顺序,基类带参构造函数的初始化

#include "stdafx.h"

class CB
{
public:
 int m_i;
 CB(int i)
 {
  m_i=i;
  puts("CB()");
 }
 ~CB()
 {
  puts("~CB()");
 }
};

class CD:public CB
{
public:
 int m_j;
 CD(int i,int j):CB(i)//基类参数的传递
 {
  m_j=j;
  puts("CD()");
 }
 ~CD()
 {
  puts("~CD()");
 }
};
void Func()
{
 CD d(1,2);
}

int main(int argc, char* argv[])
{
 Func();

 return 0;
}

 f

// 0415f.cpp : Defines the entry point for the console application.
//多重继承,虚拟继承 virtual

#include "stdafx.h"
#include <string.h>

class CBase
{
public:
 int m_i;
};

class CDerived1:virtual public CBase
{
public :
 int m_j;
};

class CDerived2:virtual public CBase
{
public:
 int m_k;
};

class CDerivedEX:public CDerived1,
    public CDerived2
{

};

int main(int argc, char* argv[])
{
 CDerivedEX d;
 CDerived1 *p1=&d;
 CDerived2 *p2=&d;

// CBase *pb1=&(CDerived1)d;//产生临时对象,不能这样转换

// CBase *pb2=(CDerived2*)&d;
 CBase *pb=&d;
 int i=sizeof(d);//大小20,有个维护虚继承的指针

 return 0;
}


 x

// 0415a.cpp : Defines the entry point for the console application.
//继承的应用—网络

#include "stdafx.h"

class CTcpSocket
{
public:
 void Connect(const char *pszSvr);
 void Close();
 int Send(const char *pchData,int iSize);
 int Recv(char *pchData,int iSize);
};

class CSmtpSocket:public CTcpSocket 
{
public:
 bool Login(const char *pszName,const char *pszPassword);
 int SendMail(const char *pszMail,const char *pszAddr);
 void Logout();
};

class CFtpSocket:public CTcpSocket
{
public:
 bool Login(const char *pszName,const char *pszPassword);
 int SendFile(const char *pszFile);
};

class CHttpSocket:public CTcpSocket
{
public:
 int Download(const char *pszURL);
};

void Func()
{
 CSmtpSocket smtp;
 CFtpSocket ftp;
 smtp.Connect("smtp.163.com");
 smtp.Login("gao","111111");
 smtp.SendMail("Hello","liu@gmail.com");
 smtp.Logout();

 ftp.Connect("192.168.1.254");
 ftp.Login("aaa","bbb");
 ftp.SendFile("c:\\a.txt");
}

int main()
{
 return 0;
}

 y

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
 int ary[5];
  int (*p)[5]=&ary;  //指向整个数组
 int *pi=ary;  //指向首地址
 ++p;
 ++pi;

 return 0;
}


=================================================================================

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值