Jump to content

Need help here

- - - - -

  • Please log in to reply
2 replies to this topic

#1
QJK

QJK

    Newbie

  • Members
  • Pip
  • 1 posts
Hello,

I have this code


class inner {

public: 

double data; 

string label; 

}; 


class middle {

public: 

inner set[5]; 

int cnt; 

}; 


class outer {

public: 

middle part1; 

inner part2; 


};


inner x;

middle y;

outer z;



And I need to know which of the following Statement is Valid and which one is Invalid


(a) x.label = “inside”;

(b) z.part1 = y;

(c) z.part2.inner = x;

(d) x.data = y.cnt;

(e) cout << x;

(f) z.part1.set[2] = x;

(g) outer.part1 = middle;

(h) outer.part1.set[0].data = 1.0;

(i) y.set.label = “from middle”;

(j) cin >> z.part1.cnt;



---------- Post added at 04:11 PM ---------- Previous post was at 04:10 PM ----------

I answered them, but I am not sure about it :p


(a) x.label = “inside”;  [B][COLOR=Red]Valid[/COLOR][/B]

(b) z.part1 = y;         [B][COLOR=Red]Valid[/COLOR][/B]

(c) z.part2.inner = x;  [B][COLOR=Red]Valid[/COLOR][/B]

(d) x.data = y.cnt;     [B][COLOR=Red]Valid[/COLOR][/B]

(e) cout << x;      [B][COLOR=Red]Invalid[/COLOR][/B]

(f) z.part1.set[2] = x;       [B][COLOR=Red]Invalid[/COLOR][/B]

(g) outer.part1 = middle;      [B][COLOR=Red]Invalid[/COLOR][/B]

(h) outer.part1.set[0].data = 1.0;    [B][COLOR=Red]Valid[/COLOR][/B]

(i) y.set.label = “from middle”;      [B][COLOR=Red]Valid[/COLOR][/B]

(j) cin >> z.part1.cnt;        [B][COLOR=Red]Valid[/COLOR][/B]



:P What do think

#2
psepheroth

psepheroth

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
For me, © is invalid; (f) is valid; (g) is valid; (i) is invalid--no bracket for set I guess.
These are just my opinion. Waiting for others to reply.

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Invalid ones are:
  • b) z.part1 refers to a object (of type middle) and no assignment operator was provided
  • c) z.part2 has no member named inner
  • e) no << operator was provided to output x
  • f) z.part1.set is an array of inner objects; no assignment operator was provided
  • g) outer is a name of class, no an object
  • h) see g)
  • i) y.set is an array, it would've been valid if an array index would've been provided: y.set[0].label = "from middle";

A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users