is there a way to declare a data in member function? i am making a print function to print the data that i keyed in. but compiler error....
Here's my code.....
#include <iostream>
using namespace std;
class SpStudent
{
public:
SpStudent();
SpStudent(string, string, float);
~SpStudent();
string getName();
void setName(string);
string getID();
void setID(string);
float getGPA();
void setGPA(float);
void SpPrint(); //this is the function that i wan to create
private:
string name;
string ID;
float GPA;
};
SpStudent::~SpStudent()
{
name = ""; // null string, default
ID = "p00000"; // default value
GPA = 0.0; //default value
}
SpStudent::SpStudent(string Name, string Idnum, float Gpanum)
{
name = Name; //take on values specified in parameters
ID = Idnum;
GPA = Gpanum;
}
// above two function “construct” objects with some data //values. More to follow…
string SpStudent::getName()//getter
{
return name;
}
void SpStudent::setName(string Name) //setter
{
name = Name;
}
string SpStudent::getID() //getter
{
return ID;
}
void SpStudent::setID(string Idnum) //setter
{
ID = Idnum;
}
float SpStudent::getGPA()
{
return GPA;
}
void SpStudent::setGPA(float Gpanum)
{
GPA = Gpanum;
}
void SpStudent::SpPrint() // <-----the function that have error
{
SpStudent She;
cout<<She.getGPA();
}
//this ends the implementation file..
//call it “SpStudent.cpp” as it is made up //of C++ statements…
void SpPrint();
//She.setName("Betty Chan");
//She.setGPA(3.6);
//He.setGPA(3.2);
//print data fields to confirm the changes..
//……write your codes here…
return 0;
}
so...is there a way to print out SpStudent (She) in the SpPrint() function?
yes, because if the memory is being disallocated, the destructor will overload with the value. but is there a way to create a print() function that can print all the value out without going to my main to use the cout statement?
I tried create the print() function but the compiler return error.....in the code, I declare the print() function in the public class...is there any examples of codes that illustrate the print() function so that I can print the data out from the class.......