Question

In: Computer Science

Use the following class for the following problem. The only purpose of the class is to...

Use the following class for the following problem. The only purpose of the class is to display a message both when the constructor is invoked and when the destructor is executed.

class Trace

{

public:

Trace(string n);

~Trace();

private:

string name;

};

Trace::Trace(string n) : name(n)

{

cout << "Entering " << name << "\n";

}

Trace::~Trace()

{

cout << "Exiting " << name << "\n";

}

Requirement:

  1. Extend the class Trace with a copy constructor and an assignment operator, printing a short message in each. Use this class to demonstrate

    a. the difference between initialization

Trace t("abc");

Trace u = t;

and assignment.

Trace t("abc");

Trace u("xyz");

u = t;

b. the fact that all constructed objects are automatically destroyed.

c. the fact that the copy constructor is invoked if an object is passed by value to a function.

d. the fact that the copy constructor is not invoked when a parameter is passed by reference.

e. the fact that the copy constructor is used to copy a return value to the caller.

Programming language: C++

Requirement: please tightly follow up the rules by covering points from a-e.

Solutions

Expert Solution

Answer:

Source Code:

#include <iostream>

using namespace std;

class Trace
{
private: char *name;
public:
Trace(char *n);
~Trace();
Trace(const Trace &t);
void operator =(const Trace &t);
};
Trace:: Trace(char *n) // Parametrized constructor
{
name = n;
cout<<endl<<"Parametrized Constructor Executed."<<endl;
cout<<"Name:"<<name;
}
Trace:: ~Trace() // Destructor
{
cout<<endl<<"Destructor Executed"<<endl<<"Exsiting:"<<name;
}
Trace :: Trace(const Trace &t) // Definition of Copy constructor
{
cout<<endl<<"Copy Constructor Executed"<<endl<<"Copy the value of one object to another."<<endl;
name = t.name;
}
void Trace :: operator = (const Trace &t) // Overload assignment operator
{   
cout<<endl<<"Operator Overloading Done"<<"Using of = operator to copy two objects"<<endl;
name = t.name;
cout<<"Name of First Object:"<<name;
cout<<endl;
cout<<"Name of Second Object:"<<t.name<<endl;
}


int main()
{
Trace t1("Ram"); // Calls Parametrized Constructor
Trace t2("Sham"); // Calls Parametrized Constructor
Trace t3(t2) ; // Calls the copy Constructor Value is pass by referce only because c++ allow to pass only referece in copy constructor
t1 = t2; // Calls the Overloading Function
return 0;
}

OUTPUT:

Dear Student If you get any type of error or have any problem with source code when executing then explain in comment section. We will be there to solve your problems.

Thumbs Up if you are satisfy with answer.

Thankyou


Related Solutions

We have to solve only problem 2, I'm sharing Problem 1 only for reference purpose. Problem...
We have to solve only problem 2, I'm sharing Problem 1 only for reference purpose. Problem 1. (25 points) Alice is going to create a notebook including the profile of all her friends. For each of her friends, she would like to keep first-name, last-name, cell number, and birthdate (month and day). At the beginning, she doesn’t know how many spaces she should reserve for recording her friends, so she gradually inputs her friends’ information into it. • Please help...
Problem 1. The purpose of this problem is to practice the use of logic operations and...
Problem 1. The purpose of this problem is to practice the use of logic operations and quantifiers. For each Statement X below determine if each of the three statementsX1, X2, X3 that follow it satisfy the following: a) Xi implies X; b) X implies Xi; c) if Xi is true then X must be false; d) if X is true then Xi must be false. Statement A. In every house there is a mouse. A1. There is no house without...
Write the following questions as queries in SQL. Use only the operators discussed in class (no...
Write the following questions as queries in SQL. Use only the operators discussed in class (no outer joins) Consider the following database schema: INGREDIENT(ingredient-id,name,price-ounce) RECIPE(recipe-id,name,country,time) USES(rid,iid,quantity) where INGREDIENT lists ingredient information (id, name, and the price per ounce); RECIPE lists recipe information (id, name, country of origin, and time it takes to cook it); and USES tells us which ingredients (and how much of each) a recipe uses. The primary key of each table is underlined; rid is a foreign...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class...
Write the following questions as queries in RELATIONAL ALGEBRA. Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming). The following database schema is given: ATHLETE(name,age,height,weight,country) RACE(id,location,date,time-start,distance) COMPETES(aname,rid,time,position) where ATHLETE has information about runners (their name, age, height, weight, and nationality); RACE has information about races (id, location, date when it’s held, time it starts, and distance ran); and COMPETES keeps track of which runners run on with race, the time it...
9. The purpose of this problem is to use MATLAB to compute the magnitude, phase, and...
9. The purpose of this problem is to use MATLAB to compute the magnitude, phase, and total energy of a Fourier transform. a) Develop a MATLAB routine to plot the magnitude and phase of a given Fourier transform H(jω). The input part of your program will, of course, require that you specify the particular H(jω) of interest, but, once this is done, your program should not depend on the Fourier transform specified. You will need to select an appropriate range...
c++ The purpose of this project is to test your ability to use files, class design,...
c++ The purpose of this project is to test your ability to use files, class design, operator overloading, and Strings or strings effectively in program design Create a program which will read a phrase from the user and create a framed version of it for printing. For example, the phrase "hello world"would result in: ********* * hello * * world * ********* Whereas the phrase "the sky is falling"might be: *********** * the * * sky * * is *...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use dynamic memory allocation to create 10 objects of the class triangle Set default height and base of all created objects (triangle) to 5 and 10 respectively using a constructor Ask user to change only the height of the 5th triangle Print height and base of all (10) triangles to demonstrate that you did the task correctly Deallocate the 5th object Print height and base...
THIS PROBLEM NEEDS TO BE SOLVED ONLY USING EXCEL SOFTWARE! 1. Use the following data set...
THIS PROBLEM NEEDS TO BE SOLVED ONLY USING EXCEL SOFTWARE! 1. Use the following data set to answer the following: ew dbh e 23.5 e 43.5 e 6.6 e 11.5 e 17.2 e 38.7 e 2.3 e 31.5 e 10.5 e 23.7 e 13.8 e 5.2 e 31.5 e 22.1 e 6.7 e 2.6 e 6.3 e 51.1 e 5.4 e 9 e 43 e 8.7 e 22.8 e 2.9 e 22.3 e 43.8 e 48.1 e 46.5 e 39.8...
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write...
(Use the String Class to Solve This (and only the iostream, string and cctype libraries)) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT