Help understanding classes
Apr 26, 2012 at 7:20pm UTC
So I just started learning classes today and here is the code I am working on. I am supposed to implement a class holding name/age pairs. Then have an input operation for names and an input operation for ages. The program runs and compiles but it doesn't allow for input, the programs just runs to the end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
class Name_pairs{
string nameinput;
double ageinput;
public :
vector<string> name;
vector<double > age;
void read_names();
void read_ages();
};
void Name_pairs::read_names(){
while (cin>>nameinput){
if (nameinput=="|" ){
break ;}
name.push_back(nameinput);
}}
void Name_pairs::read_ages(){
for (int i=0; i<name.size(); i=i+1){
cout<<"Enter the age of " <<name[i]<<":" <<endl;
cin>>ageinput;
age.push_back(ageinput);
}
}
int main(){
cout<<"Please enter a list of names followed by '|'" <<endl;
Name_pairs read_names();
Name_pairs read_ages();
keep_window_open();
return 0;
}
Last edited on Apr 26, 2012 at 7:31pm UTC
Apr 26, 2012 at 9:11pm UTC
Why do you have variables used only locally as instance variables of your class? Why are the instance variables you do have public?
Oh, and this only compiles because those:
1 2
Name_pairs read_names();
Name_pairs read_ages();
Are interpreted as forward declarations of functions. As those functions aren't actually used anywhere, you don't get linker errors.
It should look a little bit more like
1 2 3
Name_pairs pairs;
pairs.read_names();
pairs.read_ages();
Please review your course material as to why.
Apr 27, 2012 at 4:47pm UTC
My book doesn't really have a lot of good examples on classes, especially on implementing them in the code.
If I understand instance variables correctly, that they are variables which will have different values for each object, then I did that because in my text some examples were like that. Upon reading some other books, I have changed it to:
1 2 3 4 5 6 7
class Name_pairs{
vector<string> name;
vector<double > age;
public :
void read_names();
void read_ages();
void print(vector<string>& x, vector<double >& y);
So instead of :
1 2 3 4 5 6
void Name_pairs::read_names(){
while (cin>>nameinput){
if (nameinput=="|" ){
break ;}
name.push_back(nameinput);
}}
I should write
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void Name_pairs::read_names(string x){
while (cin>>x){
if (x=="|" ){
break ;}
name.push_back(x);
}}
int main(){
string nameinput;
Name_pairs pairs;
pairs.red_names(nameinput);
keep_window_open();
return 0;
}
Apr 27, 2012 at 5:12pm UTC
Actually, that's still wrong, but at least somewhat closer.
1 2 3 4 5 6 7
void Name_pairs::read_names(){
string x;
while (cin>>x){
if (x=="|" ){
break ;}
name.push_back(x);
}}
Passing parameters is only for... well, passing parameters.
Apr 27, 2012 at 6:07pm UTC
Writing the code like that, I would need somewhere to tell the program what x is, such as the set_values used in the classes example on this site?
1 2 3 4 5 6 7 8
class CRectangle {
int x, y;
public :
void set_values (int ,int );
.....
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
I guess what I am also confused about, is if I write
void Name_pairs::read_names()
How do I call my arguments in int main()?
Sorry if these are really elementary questions. I am starting to understand all the different pieces, I am just struggling to connect it all.
May 3, 2012 at 2:12am UTC
Here is my final code which works, though I am not sure how "correct" it is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include "std_lib_facilities.h"
class Name_pairs{
private :
vector<string> name;
vector<double > age;
vector<string> namecopy;
vector<double > agecopy;
public :
void read_names();
void read_ages();
void sorting();
int match(const vector<string>& x,const string& y);
friend ostream& operator <<(ostream& out, const Name_pairs& np);
vector<string> getname(){
return name;}
};
void Name_pairs::read_names(){
string x;
cout<<"Please enter a list of names, ending with a '|'" <<endl;
cout<<"You will be asked for ages after entering in all names" <<endl;
while (cin>>x){
if (x=="|" ){
break ;}
name.push_back(x);
}}
void Name_pairs::read_ages(){
double x;
for (int i=0; i<name.size(); i=i+1){
cout<<"Enter the age of " <<name[i]<<":" <<endl;
cin>>x;
age.push_back(x);
}}
int Name_pairs::match(const vector<string>& x,const string& y){
for (int i=0; i<name.size(); i=i+1){
if (y==x[i]){
return i;
}}}
void Name_pairs::sorting(){
namecopy=name;
agecopy=age;
sort(name.begin(), name.end());
for (int i=0; i<name.size(); i=i+1){
age[i]=agecopy[Name_pairs::match(namecopy,name[i])];
}}
ostream& operator <<(ostream& out, const Name_pairs& np){ //replaced print with <<
for (int i=0; i<np.name.size(); i=i+1){
out<<np.name[i]<<" " <<np.age[i]<<endl;}
return out;}
bool operator ==(Name_pairs lhs,Name_pairs rhs){ //== defined for Name_pairs
if (lhs.getname() == rhs.getname()){
return true ;}
else {
return false ;}}
bool operator !=(Name_pairs lhs,Name_pairs rhs){ //!= defined for Name_pairs
if (lhs.getname() != rhs.getname()){
return true ;}
else {
return false ;}}
int main(){
Name_pairs pairs;
pairs.read_names();
pairs.read_ages();
cout<<"Unsorted name/age pairs \n" <<pairs<<endl; //implementation of <<
pairs.sorting();
cout<<endl<<"Sorted name/age pairs \n" <<pairs<<endl;
keep_window_open();
return 0;
}
Topic archived. No new replies allowed.