Menu

[Solved] C Value Semantics Specifies Different Ways Copying Value Objects Class Objects Class Using Q37271765

C++

Value semantics specifies different ways of copying the value ofobjects of a class to other objects of the same class. Usingexamples write two ways in which this can be done for the Quadraticclass.

#include <iostream>

using namespace std;

// ax^2 + b*x+ c

class Quadratic{

public:

       Quadratic(double aa =0,double bb =0,

               double cc =0);//default constructor

       Quadratic(constQuadratic& source);

       ~Quadratic(); //destructor

       double evaluate(doublex);

private:

       // If the member variablesare const

       // use an initializationlist in the construtor

       const double a;

       const double b;

       const double c;

};

Quadratic::Quadratic(double aa, double bb ,

double cc):a(aa), b(bb), c(cc) {

// Example of constructor that uses an initialization list

// a(aa) is read as a = aa

//default constructor

}

Quadratic::Quadratic(const Quadratic& source):

     a(source.a), b(source.b),c(source.c){

       // Using an initializationlist again

    // because a, b, c are const

       /*

       If the memeber variableswhere not const,

       then we could initializethem as shown below:

       a = source.a;

       b = source.b;

       c = source.c;

       */

}

Quadratic::~Quadratic(){

}

double Quadratic::evaluate(double x){

       return a*x*x + b*x + c;

}

int main(){

       Quadratic q;

       Quadratic* p = newQuadratic;

       Quadratic* z = newQuadratic(1, 5, 10);

       Quadratic x(*z); // Copyconstructor

       Quadratic y = *z; // Copyconstructor

       return 0;

}

Expert Answer


Answer to C++ Value semantics specifies different ways of copying the value of objects of a class to other objects of the same cla… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *