I am working on a program and need to make a friend, deconstructor, and copy constructor.....


I was given this formula for the destructor and completely confused...

cout << value1 << value2

I also do not seem to see a whole lot of information on copy constructors and friends so was wanting to know if someone can help me in exactly what I have to do for these.....

#include<iostream>
#include<string>

using namespace std;

class Arithmetic

{
public:
	int Add(int num1, int num2);
	int Subtract(int num1, int num2);
	int Multiply(int num1, int num2);

private:
	int addedTotal, subtractedTotal, multipliedTotal;

};

//Adding the two numbers together
int Add(int num1, int num2)
{
	int addedTotal;
	addedTotal = num1 + num2;
	cout << endl << "The two numbers added together are: " << addedTotal;
	return addedTotal;
}

//Subtracting the two numbers together
int Subtract(int num1, int num2)
{
	int subtractedTotal;
	subtractedTotal = num1 - num2;
	cout << endl << endl << "Subtracting the first and second number give a difference of: " << subtractedTotal << endl ;
	return subtractedTotal;
}

//Multiplying the two numbers together
int Multiply(int num1, int num2)
{
	int multipliedTotal;
	multipliedTotal = num1 * num2;
	cout << endl << "Numbers 1 and 2 multiplied together give a product of: " << multipliedTotal << endl << endl;
	return multipliedTotal;
}	
	 
int main()
{
	int num1, num2;
	cout << "Enter the first number: ";
	cin >> num1;
	cout << endl << "Enter the second number: ";
	cin >> num2;
	Add(num1, num2);
	Subtract(num1, num2);
	Multiply(num1, num2);
}

Recommended Answers

All 6 Replies

not sure if this is right but this is what i got for my destructor...

#include<iostream>
#include<string>

using namespace std;

class Arithmetic

{
public:
    int Add(int num1, int num2);
    int Subtract(int num1, int num2);
    int Multiply(int num1, int num2);

private:
    int addedTotal, subtractedTotal, multipliedTotal;

};

//Adding the two numbers together
int Add(int num1, int num2)
{
    int addedTotal;
    addedTotal = num1 + num2;
    cout << endl << "The two numbers added together are: " << addedTotal;
    return addedTotal;
}

//Subtracting the two numbers together
int Subtract(int num1, int num2)
{
    int subtractedTotal;
    subtractedTotal = num1 - num2;
    cout << endl << endl << "Subtracting the first and second number give a difference of: " << subtractedTotal << endl ;
    return subtractedTotal;
}

//Multiplying the two numbers together
int Multiply(int num1, int num2)
{
    int multipliedTotal;
    multipliedTotal = num1 * num2;
    cout << endl << "Numbers 1 and 2 multiplied together give a product of: " << multipliedTotal << endl << endl;
    return multipliedTotal;
}   

int main()
{
    int num1, num2;
    cout << "Enter the first number: ";
    cin >> num1;
    cout << endl << "Enter the second number: ";
    cin >> num2;
    Add(num1, num2);
    Subtract(num1, num2);
    Multiply(num1, num2);

    //Perform the destructors
    Arithmetic::Add(int num1, int num2)
    {
        delete [] addedTotal;
    }

    Arithmetic::Subtract(int num11, int num2)
    {
        delete [] subtractedTotal;
    }

    Arithmetic::Multiply(int num1, int num2)
    {
        delete [] multipliedTotal;
    }


}

Im not quite sure if I'm right, but you can't delete those integers because they are static.

C++ provides a default destructor. It will do just fine here.

If you had pointers or dynamic memory in your state variables, this is the place to free memory.

And one more thing, why are you calling delete[] on variables that are not dynamic arrays? It's an error to do this: does it compiles or does it prints a warning? By the way, automatic variables (not instantiated by new) don't need to be explicitly deleted: the default destructor provided by C++ ensures that the memory occupied by these variables will be freed by the garbage collector.

A destructor takes this form: ~A(). Note the ~ at the beginnig.

This is more information on delete: http://msdn.microsoft.com/en-us/library/h6227113(VS.80).aspx

Look on this page for C++ destructors: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/cplr380.htm

~ is just the notation for a destructor, you still have to write code GDI.

You can provide a destructor if you want, but in that case, it will do nothing useful. Just add this in the public: part:

~Aritmetic() {}

You can add an output message, just to be sure that the destructor code is done after crossing the last bracket (where the destructors are called).

Well, now start to study C++ classes from the beginning ;)...

You define (but not implement) absolutely useless class Arithmetic. It has three useless private data members (inaccessible and unused) and three declared but undefined member functions (you have basic +-* operators with this functionality in C++). After that you define three ordinar (not member) functions. You did not declare this class variables in the main body but you are trying to call member function - of what object?

No need in explicit constructors, destructors or friend functions of this class. You can't study the language with such primitive examples.

Better try to design class Line with a friend class Page as a list of Lines. Let Line objects contain arbitrary text (non-trivial copy contructor and destructor needed). Apropos, it's not so easy to construct a good example of class friends. As usually, no needs in friends with a well-designed object model...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.