Scotty T
3
Q:

formats of constructor in c++

BigMommaClass {
    BigMommaClass(int, int);

private:
    ThingOne thingOne;
    ThingTwo thingTwo;
};

BigMommaClass::BigMommaClass(int numba1, int numba2): thingOne(numba1 + numba2), thingTwo(numba1, numba2) {
// Code here
}
3
// Cpp program to illustrate the 
// concept of Constructors 
#include <iostream> 
using namespace std; 
  
class construct { 
public: 
    int a, b; 
  
    // Default Constructor 
    construct() 
    { 
        a = 10; 
        b = 20; 
    } 
}; 
  
int main() 
{ 
    // Default constructor called automatically 
    // when the object is created 
    construct c; 
    cout << "a: " << c.a << endl 
         << "b: " << c.b; 
    return 1; 
} 
3
Line::Line( double len): length(len) {
   cout << "Object is being created, length = " << len << endl;
}
0

New to Communities?

Join the community