Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Parameterized Constructor in C++

Parameterized Constructor:
It may be necessary to initialize the various data elements of different objects with different values when they are created.
This is achieved by passing arguments to the constructor function when the objects are created.
The constructors that can take arguments are called parameterized constructors.

#include <iostream> 
using namespace std; 
 
class ForgetCode { 
public: 
  int x; 
 
  ForgetCode(int i);  // constructor 
  ~ForgetCode();      // destructor 
};   
 
// Implement a parameterized constructor. 
ForgetCode::ForgetCode(int i) { 
    x = i; 
}   
 
// Implement MyClass destructor. 
ForgetCode::~ForgetCode() { 
  cout << "Destructing object whose x value is " << x  <<" \n"; 
} 
   
int main() {   
  ForgetCode t1(5); 
 ForgetCode t2(19); 
 
  cout << t1.x << " " << t2.x << "\n"; 
 
  return 0; 
}