C++讲义 第6章 类与对象的高级操作

《C++程序设计》教学讲义6章


第6章 类与对象的高级操作
§6.1 拷贝构造函数
作用:用已经存在的对象初始化同类新对象。
例子:
#include <iostream>
using namespace std;

class CA
{
public:
CA(int x);
CA(CA &a);
private:
int m_a;
};

CA::CA(int x)
{
m_a = x;
cout<<"Constructor,m_a:"<<m_a<<endl;
}

CA::CA(CA &a)
{
m_a = a.m_a;
cout<<"Copy constructor,m_a"<<m_a<<endl;
}

int main()
{
CA a(16);
CA b(a);
return 0;
}

§6.2 对象指针
引例:
#include <iostream>
using namespace std;

class CA
{
public:
CA(int x);
private:
int m_a;
};

CA::CA(int x)
{
m_a = x;
cout<<"Constructor,m_a:"<<m_a<<endl;
}

int main()
{
CA* p = new CA(5);
delete p;
p = NULL;
return 0;
}

§6.3 对象引用
引例:
#include <iostream>
using namespace std;

class CA
{
public:
CA(int x);
void Show();
private:
int m_a;
};

CA::CA(int x)
{
m_a = x;
cout<<"Constructor"<<endl;
}

void CA::Show()
{
cout<<"m_a = "<<m_a<<endl;
}

int main()
{
CA a(6);
CA &b = a;
b.Show();
return 0;
}

§6.4 对象数组
引例:
#include <iostream>
using namespace std;

class CA
{
public:
CA();
CA(int x);
void Show();
private:
int m_a;
};

CA::CA()
{
m_a = 0;
}

CA::CA(int x)
{
m_a = x;
}

void CA::Show()
{
cout<<"m_a = "<<m_a<<endl;
}

int main()
{
CA a[3] = {CA(0), CA(1), CA(2)};

for (int i=0; i<3; ++i)
{
   a[i].Show();
}

CA b[3];
b[0] = CA(2);
b[1] = CA(1);
b[2] = CA(0);

for (int i=0; i<3; ++i)
{
   b[i].Show();
}
return 0;
}

§6.5 类数据成员指针
引例:
#include <iostream>
using namespace std;

class CA
{
public:
CA();
~CA();
private:
char* p;
};

CA::CA()
{
p = new char[64];
cout<<"Constructor"<<endl;
}

CA::~CA()
{
delete[] p;
p = NULL;
cout<<"Deconstructor"<<endl;
}

int main()
{
CA a;
return 0;
}

§6.6 this指针
概述:
编译器隐含定义,成员函数可以显示使用。
必须使用this指针的情况:
成员函数中对象需要整体引用而不是引用对象的一个成员时。
例子:
#include <iostream>
using namespace std;

class CPoint
{
public:
CPoint& Set(int x, int y);
void Show();
private:
int m_x;
int m_y;
};

CPoint& CPoint::Set(int x, int y)
{
m_x = x;
m_y = y;

return *this;
}

void CPoint::Show()
{
cout<<"("<<m_x<<","<<m_y<<")"<<endl;
}

int main()
{
CPoint a;
a.Set(5, 9);
a.Show();
return 0;
}

§6.7 常量const
引例:
#include <iostream>
using namespace std;

class CA
{
public:
CA(int x);
void Show() const;
private:
const int m_a;
};

CA::CA(int x):m_a(x)//常量数据成员初始化方式:初始化列表
{
}

void CA::Show() const
{
cout<<"m_a = "<<m_a<<endl;
}

int main()
{
CA a(6);
a.Show();
return 0;
}


§6.8 C++文件的组织形式
类头文件:类名.h,存放类的声明
类文件:类名.cpp,存放类成员函数的实现
主文件:main.cpp,存放main函数

例子:
//Clock.h
#include <iostream>
using namespace std;
//类的定义
class Clock
{
public:
    void ShowTime();
    void SetTime(int NewH=0, int NewM=0, int NewS=0);
private:
    int m_Hour;
    int m_Minute;
    int m_Second;
};

//Clock.cpp
#include "Clock.h"

//类成员函数实现
void Clock::ShowTime()
{
cout<<m_Hour<<":"<<m_Minute<<":"<<m_Second<<endl;
}
//类成员函数实现
void Clock::SetTime(int NewH, int NewM, int NewS)
{
m_Hour = NewH; m_Minute = NewM; m_Second = NewS;
}

//main.cpp
#include "Clock.h"

int main()
{
Clock a;//类对象声明
a.SetTime(12, 30, 0);
a.ShowTime();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值