c++ objects with constructors taking values into encapsulated objects -



c++ objects with constructors taking values into encapsulated objects -

i have been trying figure out how c++ handles multiple objects, constructors take in values, set within of each other.

#include <iostream> using namespace std; class inner { int myvar; public: inner(int input) { myvar = input; } int output() { homecoming myvar; } }; class outer { inner inner; public: outer(int input) { inner inner(input); } int value_out() { homecoming inner.output(); } }; int main() { int myvar = 0; outer outer(myvar); cout << outer.value_out() << endl; homecoming 0; }

my console output when go compile:

ubuntu@ubuntu:~$ cd desktop ubuntu@ubuntu:~/desktop$ touch test.cpp ubuntu@ubuntu:~/desktop$ g++ test.cpp -o test test.cpp: in constructor ‘outer::outer(int)’: test.cpp:19:20: error: no matching function phone call ‘inner::inner()’ outer(int input) { ^ test.cpp:19:20: note: candidates are: test.cpp:7:3: note: inner::inner(int) inner(int input) { ^ test.cpp:7:3: note: candidate expects 1 argument, 0 provided test.cpp:4:7: note: inner::inner(const inner&) class inner { ^ test.cpp:4:7: note: candidate expects 1 argument, 0 provided ubuntu@ubuntu:~/desktop$

i new c++ looking improve understanding on how falls place. coming scripting languages python , ruby lot of done automatically. give thanks help!

here trying working in ruby if helps improve create clear trying here....

class inner def initialize(input) @myvar = input end def output @myvar end end class outer def initialize(input) @inner = inner.new(input) end def value_out @inner.output end end myvar = 0 outer = outer.new(0) puts outer.value_out

so guess core of question how write object, has default constructor fellow member variable of object since c++ freaks out if assign fellow member variable. how 1 input within object requires input if fellow member variable of object? possible or trying incredibly stupid , bad?

i think little confused fellow member variable inner , inner used in constructor body

in constructor body,

outer(int input) { inner inner(input); }

the variable inner not class member. local variable in scope of function. initialize class member, need use:

outer(int input) : inner(input) {}

c++ object constructor encapsulation

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -