C++, member function.

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…

// more to follow…


int main()
{


SpStudent She("betty","p0826888",3.45);
SpStudent He("betty","p0826888",3.45);
She.setName("Betty Chan");
She.setGPA(3.6);
He.setGPA(3.2);
cout<<"Name: "<<She.getName();

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?

any help is appreciated....Thank You ^.^
Last edited on
1
2
3
4
5
6
SpStudent::~SpStudent()
{
name = ""; // null string, default
ID = "p00000"; // default value
GPA = 0.0; //default value
}

You are overloading the destructor as a constructor
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?

Thank You
1.) ????
2.) Yes, either overload operator << or just create a print() function.
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.......

Thank you
And the error was?
undefined reference to `SpStudent::SpStudent()'|
||=== Build finished: 1 errors, 0 warnings ===|
You declared your own default constructor but you didn't implement it
is there a sample code as a example to implement it?....stuck for very long...

Thank You
Remove the tilde from your destructor definition and you'll have it.
Then remove the destructor declaration in the class body as you don't need it
Topic archived. No new replies allowed.