C++ ------------------------------------------------------------- Consider the f
ID: 3795721 • Letter: C
Question
C++
-------------------------------------------------------------
Consider the following class definitions:
Base Class:
class temp
{
public:
void print() const;
void setDescription(string);
void setX(double);
string getDescription();
double getX(); temp();
temp(string, double);
private:
string description;
double x;
};
Derived Class:
class derivedFromTemp: public temp;
{
public:
void print(); //outputs the values of all the instance variables.
void setZ(double); //sets the value of z according to the parameter.
double getZ(); //returns the value of z.
double power() const; //returns x to the power of z.
derivedFromTemp(); //sets the values of instance variables to "", //0.0, and 0.0, respectively
derivedFromTemp(string, double, double); //sets the values of instance variables according to the parameters.
private:
double z;
}
Question:
a. Identify and correct errors, if any, in the definition of the class derivedFromTemp. Also give a correct definition of this class.
b. After correcting errors, if any, in the definition of the class derivedFromTemp, write the definition of the member functions of the class derivedFromTemp.
Explanation / Answer
class derivedFromTemp: public temp;
Semicolon is not required at the end, which is marked bold.
Correct form is:
class derivedFromTemp: public temp
{
//Body of the class
};
Semicolon is required at the end, which is marked bold.
Complete Program
#include<iostream>
#include<math.h>
using namespace std;
//Base class
class temp
{
public:
//Print function to display x value
void print() const
{
cout<<" X = "<<x;
cout<<" String = "<<description;
}
//To display description
void setDescription(string s)
{
description = s;
}
//Sets the value of x
void setX(double d)
{
x = d;
}
//Returns the description
string getDescription()
{
return description;
}
//Returns the x value
double getX()
{
return x;
}
//Default constructor
temp()
{
x = 0.0;
description = " ";
}
//Parameterized constructor
temp(string s, double d)
{
description = s;
x = d;
}
private:
//Data member
string description;
double x;
};//end of class
//Derived Class
class derivedFromTemp: public temp
{
public:
//Outputs the values of all the instance variables.
void print()
{
temp::print();
cout<<" Z = "<<z;
}
//Sets the value of z according to the parameter.
void setZ(double d)
{
z = d;
}
//Returns the value of z.
double getZ()
{
return z;
}
//Returns x to the power of z.
double power()
{
return pow(getX(), z);
}
//Sets the values of instance variables to "", 0.0, and 0.0, respectively
derivedFromTemp()
{
temp();
z = 0.0;
}
//Sets the values of instance variables according to the parameters.
derivedFromTemp(string s, double d, double dd): temp(s, d)
{
z = dd;
}
private:
//Data member
double z;
};//End of class
//Main function
int main()
{
double res;
derivedFromTemp de;
cout<<" Using Default Constructor ";
de.print();
derivedFromTemp pa ("Display x to the power z", 2.0, 3.0) ;
cout<<" Using Parameterized Constructor ";
pa.print();
//Calling the method power() to calculate x to the power z
res = pa.power();
cout<<" power( "<<pa.getX()<<" , "<<pa.getZ()<<" ) = "<<res;
}
Output:
Using Default Constructor
X = 0
String =
Z = 0
Using Parameterized Constructor
X = 2
String = Display x to the power z
Z = 3
power( 2 , 3 ) = 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.