类与对象\对象的应用

简介: 类与对象\对象的应用

对象数组

  • 定义 CDate 类的对象数组 CDate dt[20]
  • 只能逐个引用数组元素,即每次只能引用一个对象
  • 如果有特定需要,记得要初始化
#include <iostream>
#include <cstring>
using namespace std;
 
class CDate
{
private:
    int m_year, m_month, m_day;
public:
    CDate(int year=1970, int month=1, int day=1);
    void Display();
};
 
CDate::CDate(int year, int month, int day)
{
    cout << "Constructor called." << endl;
    m_year = year;
    m_month = month;
    m_day = day;
}
 
void CDate::Display()
{
    cout << "Year: " << m_year << endl;
    cout << "Month: " << m_month << endl;
    cout << "Day: " << m_day << endl;
}
 
int main() 
{
    CDate array[3]=
    {
        CDate(2021, 1, 1),
        CDate(2021, 2, 2),
    };
    for (int i = 0; i < 3; i++)
    {
        array[i].Display();  
    }
    
    return 0;
}

对象指针

  • 存放同类对象地址的指针变量
int main() 
{
    CDate array[3]=
    {
        CDate(2021, 1, 1),
        CDate(2021, 2, 2),
    };
    CDate *p = array;
    for (; p < array + 3; p++)
    {
        p->Display();    //两种方式都能得到正确的结果
        (*p).Display();
    }
    return 0;
}

综合示例

#include <iostream>
using namespace std;
 
class CDate
{
private:
    int Date_Year, Date_Month, Date_Day;
public:
    CDate(int y=2019, int m=1, int d=1);
    CDate(const CDate& date);
    void Display();
    ~CDate();
};
 
CDate::CDate(int y, int m, int d): Date_Year(y), Date_Month(m), Date_Day(d)
{
    cout << "Constructor called" << endl;
}
 
CDate::CDate(const CDate& date)
{
    cout << "Copy constructor called" << endl;
    Date_Year = date.Date_Year;
    Date_Month = date.Date_Month;
    Date_Day = date.Date_Day+1;
}
 
void CDate::Display()
{
    cout << Date_Year << "-" << Date_Month << "-" << Date_Day << endl;
}
 
CDate fun(CDate date)
{
    CDate new_date(date);
    return new_date;
}
 
CDate::~CDate()
{
    cout << "Destructor called" << endl;
}
 
int main() 
{
    CDate *q = new CDate(2020, 1, 1);
    cout <<"动态对象>>";
    q->Display();
    delete q;
 
    q = new CDate[3];
    q[0] = CDate(2019, 4, 1);
    q[1] = CDate(2019, 5, 1);
    for (int i = 0; i < 3; i++)
    {
        q[i].Display();
    }
    delete[] q;     //一定要记得释放
                    //注意动态数组的释放格式
    return 0;
}

q[0] 和 q[1] 两处的赋值是通过生成无名对象来赋值

赋值之后有析构无名函数的步骤

对象引用

  • 不再另外占用内存
  • 与其代表的对象共享同一个单元
  • 对象引用必须在定义时初始化
CDate DateA(2019, 3, 13);
CDate DateB(2019, 5, 1);
CDate &pDate = DateA;
 
void f()
{
    DateA.Display();
    DateB.Display();
    pDate.Display();
}
 
int main() 
{
    cout<<"原始DataA、DataB、pDate的值:"<<endl;
    f();
 
    pDate = DateB;
    cout<<"修改后DataA、DataB、pDate的值:" << endl;
    f();
 
    pDate =CDate(2019, 7, 1);
    cout<<"修改后DataA、DataB、pDate的值:" << endl;
    f();
    
    return 0;
}

前两个 Constructor called 和后两个 Destructor called 都是由于第一、二两行的调用

对象参数

对象作为函数参数

  • 用实参对象初始化形参对象
  • 复制构造函数调用
  • 单向值传递
void CDate::ModifyDate(int y, int m, int d)
{
    Date_Year=y;
    Date_Month=m;
    Date_Day=d;   
}
/*----------------------------------------------*/
void f(CDate DateVar)
{
    DateVar.ModifyDate(2020, 2, 2);
    cout << "After modification: ";
    DateVar.Display();
}
 
int main() 
{
    CDate DateA;
    DateA.Display();
    f(DateA);       //f() 退出时,生命周期结束,调用析构函数
    cout << "After function call: ";
    DateA.Display();
    return 0;
}

对象指针作为函数参数

  • 仅将对象地址传给函数参数,提高效率,读写方便
  • 单向传地址
  • 不产生新对象,不调用复制构造函数
void f(CDate *pdate)
{
    pdate->ModifyDate(2020, 2, 2);
    pdate->Display();
}
 
int main() 
{
    CDate DateA(2019);
    DateA.Display();
    f(&DateA); 
    cout << "After function call: ";
    DateA.Display();
    return 0;
}

没有复制构造函数的调用

f() 函数调用结束之后也没有析构函数的调用

但 f() 函数的参数必须要带 &

使用指针不仅可以访问,还可以修改实参的值

对象引用作为函数参数

  • 可以使函数的调用作为左值使用
#include <iostream>
using namespace std;
 
class CDate
{
private:
    int Date_Year, Date_Month, Date_Day;
public:
    CDate(int y=2019, int m=1, int d=1);
    void Display();
    ~CDate();
    void ModifyDate(int y, int m, int d);
};
 
CDate::CDate(int y, int m, int d)
{
    cout << "Constructor called" << endl;
    Date_Year = y;
    Date_Month = m;
    Date_Day = d;
}
 
void CDate::Display()
{
    cout << Date_Year << "-" << Date_Month << "-" << Date_Day << endl;
}
 
CDate::~CDate()
{
    cout << "Destructor called" << endl;
}
 
void CDate::ModifyDate(int y, int m, int d)
{
    Date_Year=y;
    Date_Month=m;
    Date_Day=d;   
}
 
 
CDate &fun(CDate &pdate)
{
    pdate.ModifyDate(2020, 2, 2);
    cout << "引用pdata>>";
    pdate.Display();
    return pdate;
}
 
int main() 
{
    CDate DateA(2019),tdate;
    cout << "起始DateA>>";
    DateA.Display();
    cout << "起始tdate>>";
    tdate.Display();
  cout<<endl;
    
    tdate = fun(DateA);
    cout << "修改后DateA>>";
    DateA.Display();
    cout << "修改后tdate>>";
    tdate.Display();
  cout<<endl;
 
    fun(tdate) = CDate(2021, 2, 2);
    cout << "左修改后DateA>>";
    DateA.Display();
    cout << "左修改后tdate>>";
    tdate.Display();
  cout<<endl;
 
  system("pause");
    return 0;
}
目录
相关文章
|
1月前
|
C++
【C++】——类与对象(二)
【C++】——类与对象(二)
70 1
|
7月前
|
编译器 C语言 C++
类和对象(1):类,对象,this指针
类和对象(1):类,对象,this指针
|
8月前
|
Java
【JavaSE专栏63】多态,父类引用子类的对象,面向对象编程中的重要概念
【JavaSE专栏63】多态,父类引用子类的对象,面向对象编程中的重要概念
|
9天前
|
数据安全/隐私保护
类与对象\类的定义
类与对象\类的定义
6 0
|
1月前
面向对象编程:类、对象、继承与多态
面向对象编程:类、对象、继承与多态
18 0
|
1月前
|
存储 编译器 C语言
C++类与对象
C++类与对象
25 0
|
1月前
|
C++
c++类和对象一对象特楼一this指针的用途讲解
c++类和对象一对象特楼一this指针的用途讲解
10 1
|
1月前
|
安全 C++
c++类和对象一对象特性一构造函数和析构函数
c++类和对象一对象特性一构造函数和析构函数
12 0
|
1月前
|
存储 编译器 C++
C++类与对象【继承】
C++类与对象【继承】
|
1月前
|
数据安全/隐私保护 C++
C++类与对象【封装】
C++类与对象【封装】