c++ - Is it possible to take two values of one class to another class and use it? -
i beginner. and, tried one.
#include <iostream> using namespace std; class square { int number; public: square(int a): number(a) {} int getsquare() { return number*number; } }; class sumnumber { square a; int firnum; int secnum; public: sumnumber(int number, int x, int y): a(number),firnum(x), secnum(y) {} int getsumnumber() //output-er { return firnum + secnum + a.getsquare(); } }; int main() { sumnumber a(2,3,4); //sums squared number 2, , 3 , 4 cout << "sum of numbers\t" << a.getsumnumber() << endl; //=11 }
but, if wanted have 2 values of class square
class square { int number; int nextnumber; public: ... };
and on other class
class sumnumber //creating class sumnumber { square a; int firnum; //first number int secnum; //second number public: ... };
question is: possible take 2 values declared in square class sumnumber? if so, how?
i think looking having getter. method can return value of private member, since square class returning square of number can change follows:
class square { int number; int nextnumber; public: square(int a, int b): number(a), nextnumber(b) {} int getnumbersquared() { return number*number; } int getnextnumbersquared() { return nextnumber*nextnumber; } };
and in other class can have this:
class sumnumber { square a; int firnum; int secnum; public: sumnumber(int number, int nextnumber,int x, int y): a(number, nextnumber),firnum(x), secnum(y) {} int getsumnumber() //output-er { return firnum + secnum + a.getnumbersquared() + a.getnextnumbersquared(); } };
Comments
Post a Comment