where is my wrong !
Apr 1, 2018 at 11:21pm UTC
i want to set the name of shape and print it out, but it only show me
It's name 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
#include <iostream>
#include <string>
using namespace std;
class shape {
protected :
string name;
public :
shape (string y =" " )
:name(y){}
void getname(string namy) {
name = namy;
}
string getn() {
return name;
}
};
class circle : public shape {
private :
int R;
public :
circle(int l=0) {
R = l;
}
void set_the_R (int a) {
R = a;
}
int getr() {
return R;
}
};
int main() {
circle T;
string p = "afsfasf" ;
T.getname (p);
T.set_the_R(2);
cout << " It's name is " ; T.getn();
cout << " \n It's Radius is : " ; T.getr();
system("pause" );
return 0;
}
Last edited on Apr 1, 2018 at 11:29pm UTC
Apr 1, 2018 at 11:50pm UTC
T.getn(); is returning an a string, but you aren't printing it.
change to:
1 2 3
cout << " Its name is " << T.getn();
cout << " \n Its Radius is : " << T.getr();
Apr 2, 2018 at 12:40am UTC
Thanks Gando but if i say
1 2 3 4 5
void getn() {
cout name;
}
then i can say onle
is that right ?
Apr 2, 2018 at 1:20pm UTC
If you mean
cout << name;
, yes, you *can* do that. But that then becomes a really misleading function name -- "getX" usually implies a return value of X.
Likewise,
1 2 3 4
void getname(string namy) {
name = namy;
}
Is a misleading function as well. You are not getting anything. You are setting the name.
Topic archived. No new replies allowed.