0

Still new to C++ and I'm trying to understand accessing private data, using constructors. How would I display the values of the data members of myClass? Any help would be great. Thanks

class NumberClass
{
public:
     void func();       // assigns numeric constants to nNum and fNum
     void print() const;    //displays the values of nNum and fNum
     NumberClass();
     NumberClass(int, float);
private:
     int nNum;
     float fNum;
};

int main()
{
    NumberClass myClass;
    //display values here

    return 0;
}
jacksonSD
  • 677
  • 2
  • 13
  • 27

3 Answers3

0

By the looks of your interface, your professor probably wants you to implement print() to print the members, and then call that method on your object.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

You could implement print(), and call it with a instance of NumberClass. Like

NumberClass obj;
...
obj.print()
YaleCheung
  • 630
  • 3
  • 9
0

You could write get methods for the class which is a good way to print it.

class NumberClass
{
public:
 void func();       // assigns numeric constants to nNum and fNum
 void print() const;    //displays the values of nNum and fNum
 NumberClass();
 NumberClass(int, float);
 private:
 int nNum;
 float fNum;

public int getNum(){
return nNum;
}
 public float getNum(){
return fNum;
}
};

int main()
{
NumberClass myClass;
//display values here
cout<<myClass.getNum();
return 0;
}

Could be the example but I didint checked so you can fixed the little issues.

jazz
  • 232
  • 1
  • 2
  • 10