Advertisement
//C++ No-argument constructor
#include<iostream>
using namespace std;
class A
{
public:
A() //no argument constructor.
{
cout<<"Constructor called" << "\n";
}
void message(void);
};
void A :: message()
{
cout<< "The message() function is called" << "\n";
}
int main()
{
// Calling a no-argument constructor to create the first object
A ob1;
// Another way to call a no-argument constructor, creating second object.
A ob2 = A();
ob1.message();
ob2.message();
}
Constructor is called
Constructor is called
The message() function is called
The message() function is called
//C++ Example of a default no-argment constructor
#include<iostream>
using namespace std;
class A
{
public:
void message()
{
cout<<"Hello from class A";
}
};
int main()
{
A ob; //comment1
ob.message();
}
A
In the code above, class A wasn't created with any constructor, hence it is given an invisible default no-argument constructor by the compiler.
At comment1, this no-argument constructor is called to create an object of class A.
Advertisement
//C++ An example of parameterized constructor
#include<iostream>
#include<string>
using namespace std;
class Names
{
private:
string firstName;
string secondName;
public:
Names(string firstN, string secondN)
{
firstName = firstN;
secondName = secondN;
}
void printName()
{
cout<< firstName << " " << secondName;
}
};
int main()
{
//Calling a constructor with two String values to create an object of Names class
Names ob = Names("Charlie","Chaplin");
//Calling the printName function
ob.printName();
}
Charlie Chaplin
//C++ An example of overloaded constructor
#include<iostream>
#include<string>
using namespace std;
class MathOP
{
public:
MathOP(int a, int b) //Constructor to add two integers
{
cout<< (a+b) << "\n";
}
MathOP(string a, string b) //Constructor to add two strings
{
cout << (a+b) << "\n";
}
MathOP(float a, float b) //Constructor to add two floating values
{
cout << float(a+b)<< "\n";
}
MathOP(char a, char b) //Constructor to add two characters
{
cout << (a+b) << "\n";
}
};
int main()
{
MathOP ob1 = MathOP(1,2);
MathOP ob2 = MathOP("The","Coldplay");
MathOP ob3 = MathOP(2.7f, 2.5f);
MathOP ob4 = MathOP('a', 'b');
}
3
TheColdplay
5.2
195
//C++ An example of copy constructor
#include<iostream>
using namespace std;
class A
{
private:
int a,b;
public:
A(int x, int y) //A simple constructor
{
a = x;
b = y;
}
A( A & ob) //A copy constructor
{
a = ob.a;
b = ob.b;
}
void show_ab()
{
cout<< "The value of a is : "<< a << "\n";
cout<< "The value of b is : "<< b << "\n";
}
};
int main()
{
//Calling the simple constructor
A ob1(2,4);
cout<< "The value of data member a and b of the first object:" <<"\n";
ob1.show_ab();
//Calling the copy constructor to copy the values of ob1 to ob2
A ob2(ob1);
cout<< "The value of data member a and b of the second object:" << "\n";
ob2.show_ab();
}
The value of data member a and b of the first object:
The value of a is : 2
The value of b is : 4
The value of data member a and b of the second object:
The value of a is : 2
The value of b is : 4
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement