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.F
C++基础 课程代码
最新推荐文章于 2024-02-20 03:00:00 发布