ex4-17.cpp - Example 4-17 Explicit Constructors

// File: ex4-17.cpp - explicit constructors

#include <iostream>
using namespace std;

class A
{
public:
	A(int);				// non-explicit ctor
};

class B
{
public:
	explicit B(int);	// explicit ctor
};

A::A(int) 
{ 
	cout << "A ctor called\n";
}

B::B(int) 
{ 
	cout << "B ctor called\n";
}

void funkA(A object)
{
	cout << "funkA called\n";
}

void funkB(B object)
{
	cout << "funkB called\n";
}

void funkAB(A obj) {
	cout << "funkAB(A) called\n";
}

void funkAB(B obj) {
	cout << "funkAB(B) called\n";
}



int main()
{
	A objA(2);		// instantiate an A object
	B objB(3);		// instantiate a B object
	funkA(objA);	// call funkA() with an exact match of the argument
	funkA(9);		// call funkA() with an non-exact match of the argument

	funkB(objB);	// call funkB() with an exact match of the argument

//	funkB(16);		// compile error - cannot convert an int to a B object

	funkAB(6);		// compile error if B(int) is not explicit

	return 0;
}




CIS27: Programming in C++    Instructor: Joe Bentley