Hello wonderful people, I have written this code as per the instructions and I understand what I am doing (the code for the use of the class will be provided by the teacher when the work is collected).However, I keep getting a compiler error for the part of the code that says to return the value of the string. And also warning messages that say there isn't return statements. Will someone please help me? I could always use other suggestion to improve me code and/or learn some new things! Thank you so much!!!
#include <iostream>
#include <cmath>
#include <cstring>
usingnamespace std;
/************************************************
Implement a class per the following specification.
Note: All the variable and function names are case sensitive.
Pay attention to the case.
Name of the Class: MyClass
Private Members:
1.Variable
Name: num
Type: int
2.Variable
Name: description
Type: string
public Members:
Member functions:
1. Default Constructor: Initialize num and descrription to 0 (Zero) and
empty string ("") respectively.
2. Constructor: Take two input parameters(int and string types) and
Initialize num and descrription using the parameters.
(Note: 1st parameter will be int type and
2nd parameter will be string type)
3. GetNum : No parameter, Will return the value of num member variable.
4. SetNum : 1 parameter of type int
Will set the value of num member variable equal to
the value of the parameter.
5. GetDesc : No parameter, Will return the value of description
member variable.
6. SetDesc : 1 parameter of type string
Will set the value of description member variable equal to
the value of the parameter.
7. Square : No parameter
Will return the square of the num member variable.
8. SquareRoot : No parameter
If num > 0, it Will return(integer portion only) the square
root of the num member variable, otherwise it will return 0.
9. Factorial : No parameter
If num > 0, it Will return the factorial of the num member
variable, otherwise it will return 1.
10. IsNegative : No parameter
Will return true(boolean) if the num member variable is < 0.
Will return false(boolean) if the num member variable is >= 0.
***********************************************/
// WRITE YOUR CODE HERE, ADD MORE LINES AS NEEDED
// ||
// ||
// \ || /
// \||/
// \/
class MyClass
{
private:
int num;
string description;
public:
MyClass();
MyClass(int n, string d);
int GetNum();
int SetNum(int x);
int GetDesc();
int SetDesc(string a);
int Square();
int SquareRoot();
int Factorial();
bool IsNegative();
};
MyClass::MyClass()
{
num = 0;
description = "";
}
MyClass::MyClass(int n, string d)
{
num = n;
description = d;
}
int MyClass::GetNum()
{
return 0;
}
int MyClass::SetNum(int x)
{
num = x;
}
int MyClass::GetDesc()
{
return description; //This is where the error is occurring
}
int MyClass::SetDesc(string a)
{
description = a;
}
int MyClass::Square()
{
return num*num;
}
int MyClass::SquareRoot()
{
if(num > 0)
{
return sqrt(num);
}
else
{
return 0;
}
}
int MyClass::Factorial()
{
int factorial;
if(num > 0)
{
for(int i = num; i > 0;i--)
{
factorial = factorial * i;
}
return factorial;
}
else
{
return 1;
}
}
bool MyClass::IsNegative()
{
if(num < 0)
{
returntrue;
}
else
{
returnfalse;
}
}
This is the compiler message:
myclass.cc: In member function ‘int MyClass::GetDesc()’:
myclass.cc:93:9: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘int’ in return
return description;
^
These are the warning messages:
myclass.cc: In member function ‘int MyClass::SetNum(int)’:
myclass.cc:90:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
myclass.cc: In member function ‘int MyClass::SetDesc(std::string)’:
myclass.cc:98:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
int MyClass::Factorial()
{
//you need to initialize otherwise a random rubbish value would be calculated.
int factorial = 1;
if(num > 0)
{
for(int i = num; i > 0;i--)
{
factorial *= i;
}
return factorial;
}
else
{
return 1;
}
}
Dude, you are a life saver!!! You helped me bring my F/D to a solid D almost C!!!!! THANK YOU SO MUCH!!!!!!!! I hope you have the best day ever, and all of your dreams come true!!!!!!!!!!!!! :D