📜  C ++中对象的动态初始化

📅  最后修改于: 2021-05-31 18:59:14             🧑  作者: Mango

在本文中,我们将讨论使用动态构造函数对对象进行动态初始化。

  • 对象的动态初始化是指在运行时初始化对象,即在运行时提供对象的初始值。
  • 这可以通过使用构造函数并将参数传递给构造函数来实现。
  • 当同一个类的多个构造函数具有不同的输入时,这非常方便。

动态构造函数:

  • 用于在运行时分配内存的构造函数称为动态构造函数
  • 使用new运算符在运行时分配内存,类似地,使用delete运算符在运行时释放内存。

动态分配

方法:

  1. 在下面的示例中, new用于在默认构造函数中动态初始化变量,并在堆上分配内存。
  2. 怪胎类的对象调用该函数,并显示动态分配的变量(即ptr)的值。

下面是使用new运算符动态初始化对象的程序:

C++
// C++ program for dynamic allocation
#include 
using namespace std;
  
class geeks {
    int* ptr;
  
public:
    // Default constructor
    geeks()
    {
        // Dynamically initializing ptr
        // using new
        ptr = new int;
        *ptr = 10;
    }
  
    // Function to display the value
    // of ptr
    void display()
    {
        cout << *ptr << endl;
    }
};
  
// Driver Code
int main()
{
    geeks obj1;
  
    // Function Call
    obj1.display();
  
    return 0;
}


C++
// C++ program to dynamically
// deallocating the memory
#include 
using namespace std;
  
class geeks {
    int* ptr;
  
public:
    // Default constructor
    geeks()
    {
        ptr = new int;
        *ptr = 10;
    }
  
    // Function to display the value
    void display()
    {
        cout << "Value: " << *ptr
             << endl;
    }
};
  
// Driver Code
int main()
{
    // Dynamically allocating memory
    // using new operator
    geeks* obj1 = new geeks();
    geeks* obj2 = new geeks();
  
    // Assigning obj1 to obj2
    obj2 = obj1;
  
    // Function Call
    obj1->display();
    obj2->display();
  
    // Dynamically deleting the memory
    // allocated to obj1
    delete obj1;
  
    return 0;
}


C++
// C++ program to illustrate the dynamic
// initialisation as memory is allocated
// to the object
#include 
using namespace std;
  
class bank {
    int principal;
    int years;
    float interest;
    float returnvalue;
  
public:
    // Default constructor
    bank() {}
  
    // Parameterised constructor to
    // calculate interest(float)
    bank(int p, int y, float i)
    {
        principal = p;
        years = y;
        interest = i;
        returnvalue = principal;
        cout << "\nDeposited amount (float):";
  
        // Finding the interest amount
        for (int i = 0; i < years; i++) {
            returnvalue = returnvalue
                          * (1 + interest);
        }
    }
  
    // Parameterised constructor to
    // calculate interest(integer)
    bank(int p, int y, int i)
    {
        principal = p;
        years = y;
        interest = i;
        returnvalue = principal;
        cout << "\nDeposited amount"
             << " (integer):";
  
        // Find the interest amount
        for (int i = 0; i < years; i++) {
            returnvalue = returnvalue
                          * (1 + interest);
        }
    }
  
    // Display function
    void display(void)
    {
        cout << returnvalue
             << endl;
    }
};
  
// Driver Code
int main()
{
    // Variable initialisation
    int p = 200;
    int y = 2;
    int I = 5;
    float i = 2.25;
  
    // Object is created with
    // float parameters
    bank b1(p, y, i);
  
    // Function Call with object
    // of class
    b1.display();
  
    // Object is created with
    // integer parameters
    bank b2(p, y, I);
  
    // Function Call with object
    // of class
    b2.display();
  
    return 0;
}


输出:
10

动态解除分配

方法:

  1. 在下面的代码中, delete用于动态释放内存。
  2. 使用赋值运算符将obj1的内容覆盖在对象obj2中,然后使用delete运算符将obj1释放。

下面是使用delete运算符动态释放内存的代码。

C++

// C++ program to dynamically
// deallocating the memory
#include 
using namespace std;
  
class geeks {
    int* ptr;
  
public:
    // Default constructor
    geeks()
    {
        ptr = new int;
        *ptr = 10;
    }
  
    // Function to display the value
    void display()
    {
        cout << "Value: " << *ptr
             << endl;
    }
};
  
// Driver Code
int main()
{
    // Dynamically allocating memory
    // using new operator
    geeks* obj1 = new geeks();
    geeks* obj2 = new geeks();
  
    // Assigning obj1 to obj2
    obj2 = obj1;
  
    // Function Call
    obj1->display();
    obj2->display();
  
    // Dynamically deleting the memory
    // allocated to obj1
    delete obj1;
  
    return 0;
}
输出:
Value: 10
Value: 10

下面的C++程序演示了对象的动态初始化和银行存款的计算:

C++

// C++ program to illustrate the dynamic
// initialisation as memory is allocated
// to the object
#include 
using namespace std;
  
class bank {
    int principal;
    int years;
    float interest;
    float returnvalue;
  
public:
    // Default constructor
    bank() {}
  
    // Parameterised constructor to
    // calculate interest(float)
    bank(int p, int y, float i)
    {
        principal = p;
        years = y;
        interest = i;
        returnvalue = principal;
        cout << "\nDeposited amount (float):";
  
        // Finding the interest amount
        for (int i = 0; i < years; i++) {
            returnvalue = returnvalue
                          * (1 + interest);
        }
    }
  
    // Parameterised constructor to
    // calculate interest(integer)
    bank(int p, int y, int i)
    {
        principal = p;
        years = y;
        interest = i;
        returnvalue = principal;
        cout << "\nDeposited amount"
             << " (integer):";
  
        // Find the interest amount
        for (int i = 0; i < years; i++) {
            returnvalue = returnvalue
                          * (1 + interest);
        }
    }
  
    // Display function
    void display(void)
    {
        cout << returnvalue
             << endl;
    }
};
  
// Driver Code
int main()
{
    // Variable initialisation
    int p = 200;
    int y = 2;
    int I = 5;
    float i = 2.25;
  
    // Object is created with
    // float parameters
    bank b1(p, y, i);
  
    // Function Call with object
    // of class
    b1.display();
  
    // Object is created with
    // integer parameters
    bank b2(p, y, I);
  
    // Function Call with object
    // of class
    b2.display();
  
    return 0;
}
输出:
Deposited amount (float):2112.5

Deposited amount (integer):7200
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”